adding packages

This commit is contained in:
2026-01-15 14:38:46 -08:00
parent ef86e3ab6a
commit 6869cf47e5
5253 changed files with 726695 additions and 34 deletions
@@ -0,0 +1,83 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'src/cache.dart';
import 'src/platform_interface.dart';
class FlutterMidi extends FlutterMidiPlatform {
static const MethodChannel _channel = MethodChannel('flutter_midi');
/// Needed so that the sound font is loaded
/// On iOS make sure to include the sound_font.SF2 in the Runner folder.
/// This does not work in the simulator.
@override
Future<String?> prepare({
required ByteData? sf2,
String name = 'instrument.sf2',
}) async {
if (sf2 == null) return Future.value(null);
if (kIsWeb) return _channel.invokeMethod('prepare_midi');
File? _file = await writeToFile(sf2, name: name);
if (_file == null) return null;
return _channel.invokeMethod('prepare_midi', {'path': _file.path});
}
/// Needed so that the sound font is loaded
/// On iOS make sure to include the sound_font.SF2 in the Runner folder.
/// This does not work in the simulator.
@override
Future<String?> changeSound({
required ByteData? sf2,
String name = 'instrument.sf2',
}) async {
if (sf2 == null) return Future.value(null);
File? _file = await writeToFile(sf2, name: name);
if (_file == null) return null;
final Map<dynamic, dynamic> mapData = <dynamic, dynamic>{};
mapData['path'] = _file.path;
debugPrint('Path => ${_file.path}');
final String result = await _channel.invokeMethod('change_sound', mapData);
debugPrint('Result: $result');
return result;
}
/// Unmute the device temporarily even if the mute switch is on or toggled in settings.
@override
Future<String> unmute() async {
final String result = await _channel.invokeMethod('unmute');
return result;
}
/// Use this when stopping the sound onTouchUp or to cancel a long file.
/// Not needed if playing midi onTap.
/// Stop with velocity in the range between 0-127
@override
Future<String?> stopMidiNote({
required int midi,
int velocity = 64,
}) async {
return _channel.invokeMethod('stop_midi_note', {
'note': midi,
'velocity': velocity,
});
}
/// Play a midi note from the sound_font.SF2 library bundled with the application.
/// Play a midi note in the range between 0-127
/// Play with velocity in the range between 0-127
/// Multiple notes can be played at once as separate calls.
@override
Future<String?> playMidiNote({
required int midi,
int velocity = 64,
}) async {
return _channel.invokeMethod('play_midi_note', {
'note': midi,
'velocity': velocity,
});
}
}
@@ -0,0 +1,41 @@
import 'dart:async';
import 'dart:js' as js;
import 'package:flutter/widgets.dart';
import 'package:tonic/tonic.dart' as tonic;
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:flutter/services.dart';
import 'src/platform_interface.dart';
class FlutterMidiPlugin extends FlutterMidiPlatform {
static void registerWith(Registrar registrar) {
WidgetsFlutterBinding.ensureInitialized();
final instance = FlutterMidiPlugin();
final MethodChannel channel = MethodChannel(
'flutter_midi',
const StandardMethodCodec(),
registrar.messenger,
);
channel.setMethodCallHandler(instance.handleMethodCall);
}
Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) {
case 'play_midi_note':
final int midi = call.arguments['note'];
String _note = tonic.Pitch.fromMidiNumber(midi).toString();
_note = _note.replaceAll('', 'b').replaceAll('', '#');
js.context.callMethod('playNote', [_note]);
return 'Result: $_note';
case 'stop_midi_note':
final int midi = call.arguments['note'];
String _note = tonic.Pitch.fromMidiNumber(midi).toString();
_note = _note.replaceAll('', 'b').replaceAll('', '#');
// print('Midi -> $midi/$_note');
js.context.callMethod('stopNote');
return 'Result: $_note';
default:
}
}
}
@@ -0,0 +1,16 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
Future<File?> writeToFile(ByteData data,
{String name = "instrument.sf2"}) async {
if (kIsWeb) return null;
final buffer = data.buffer;
final directory = await getApplicationDocumentsDirectory();
final path = "${directory.path}/$name";
return File(path)
.writeAsBytes(buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
@@ -0,0 +1,70 @@
import 'package:flutter/services.dart';
import 'platform_interface.dart';
const MethodChannel _channel = MethodChannel('flutter_midi');
class MethodChannelUrlLauncher extends FlutterMidiPlatform {
/// Needed so that the sound font is loaded
/// On iOS make sure to include the sound_font.SF2 in the Runner folder.
/// This does not work in the simulator.
@override
Future<String?> prepare({
required ByteData? sf2,
String name = 'instrument.sf2',
}) async {
return _channel.invokeMethod<String>('prepare_midi', {
'name': name,
'data': sf2,
});
}
/// Needed so that the sound font is loaded
/// On iOS make sure to include the sound_font.SF2 in the Runner folder.
/// This does not work in the simulator.
@override
Future<String?> changeSound({
required ByteData? sf2,
String name = 'instrument.sf2',
}) async {
return _channel.invokeMethod<String>('change_sound', {
'name': name,
'data': sf2,
});
}
/// Unmute the device temporarily even if the mute switch is on or toggled in settings.
@override
Future<String?> unmute() {
return _channel.invokeMethod<String>('unmute');
}
/// Use this when stopping the sound onTouchUp or to cancel a long file.
/// Not needed if playing midi onTap.
/// Stop with velocity in the range between 0-127
@override
Future<String?> stopMidiNote({
required int midi,
int velocity = 64,
}) {
return _channel.invokeMethod<String>('stop_midi_note', {
'note': midi,
'velocity': velocity,
});
}
/// Play a midi note from the sound_font.SF2 library bundled with the application.
/// Play a midi note in the range between 0-127
/// Play with velocity in the range between 0-127
/// Multiple notes can be played at once as separate calls.
@override
Future<String?> playMidiNote({
required int midi,
int velocity = 64,
}) {
return _channel.invokeMethod<String>('play_midi_note', {
'note': midi,
'velocity': velocity,
});
}
}
@@ -0,0 +1,63 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'method_channel.dart';
class FlutterMidiPlatform extends PlatformInterface {
FlutterMidiPlatform() : super(token: _token);
static final Object _token = Object();
static FlutterMidiPlatform _instance = MethodChannelUrlLauncher();
static FlutterMidiPlatform get instance => _instance;
static set instance(FlutterMidiPlatform instance) {
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}
/// Needed so that the sound font is loaded
/// On iOS make sure to include the sound_font.SF2 in the Runner folder.
/// This does not work in the simulator.
Future<String?> prepare({
required ByteData? sf2,
String name = 'instrument.sf2',
}) async {
debugPrint('Setup Midi..');
throw UnimplementedError('prepare() has not been implemented.');
}
/// Needed so that the sound font is loaded
/// On iOS make sure to include the sound_font.SF2 in the Runner folder.
/// This does not work in the simulator.
Future<String?> changeSound({
required ByteData? sf2,
String name = 'instrument.sf2',
}) async {
throw UnimplementedError('changeSound() has not been implemented.');
}
/// Unmute the device temporarily even if the mute switch is on or toggled in settings.
Future<String?> unmute() async {
throw UnimplementedError('canLaunch() has not been implemented.');
}
/// Use this when stopping the sound onTouchUp or to cancel a long file.
/// Not needed if playing midi onTap.
/// Stop with velocity in the range between 0-127
Future<String?> stopMidiNote({
required int midi,
int velocity = 64,
}) async {
throw UnimplementedError('stopMidiNote() has not been implemented.');
}
/// Play a midi note from the sound_font.SF2 library bundled with the application.
/// Play a midi note in the range between 0-127
/// Play with velocity in the range between 0-127
/// Multiple notes can be played at once as separate calls.
Future<String?> playMidiNote({
required int midi,
int velocity = 64,
}) async {
throw UnimplementedError('playMidiNote() has not been implemented.');
}
}