Skip to content

Commit

Permalink
Add Stop note function
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhbao0408 committed Mar 20, 2023
1 parent 15c7105 commit 1300960
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
3 changes: 1 addition & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.teplyakov.midiplayer">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="me.teplyakov.midiplayer">
</manifest>
4 changes: 4 additions & 0 deletions android/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ extern "C" JNIEXPORT void JNICALL Java_me_teplyakov_midiplayer_MidiPlayerPlugin_
fluid_synth_noteon(synthSingleton, 0, note, velocity);
}

extern "C" JNIEXPORT void JNICALL Java_me_teplyakov_midiplayer_MidiPlayerPlugin_fluidsynthStopNote(JNIEnv* env, jobject, jint note, jint velocity) {
fluid_synth_noteoff(synthSingleton, 0, note);
}

extern "C" JNIEXPORT void JNICALL Java_me_teplyakov_midiplayer_MidiPlayerPlugin_fluidsynthUnload(JNIEnv* env, jobject) {
// Clean up
delete_fluid_audio_driver(adriverSingleton);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class MidiPlayerPlugin: FlutterPlugin, MethodCallHandler {

@JvmStatic
private external fun fluidsynthPlayNote(note: Int, velocity: Int)

@JvmStatic
private external fun fluidsynthStopNote(note: Int)

@JvmStatic
private external fun fluidsynthUnload()
Expand Down Expand Up @@ -49,6 +52,12 @@ class MidiPlayerPlugin: FlutterPlugin, MethodCallHandler {
if (note != null && velocity != null) {
fluidsynthPlayNote(note, velocity)
}
} else if (call.method == "stop_note") {
val args = call.arguments() as? Map<String, Any?>
val note = args?.get("note") as? Int
if (note != null) {
fluidsynthStopNote(note)
}
} else if (call.method == "dispose") {
fluidsynthUnload()
} else {
Expand Down
7 changes: 7 additions & 0 deletions ios/Classes/SwiftMidiPlayerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public class SwiftMidiPlayerPlugin: NSObject, FlutterPlugin {
let message = "Playing: \(String(describing: note))"
result(message)
}
case "stop_note":
if let args = call.arguments as? Dictionary<String, Any>,
let note = args["note"] as? Int {
au.stopPitch(midi: note)
let message = "Stopped: \(String(describing: note))"
result(message)
}
case "dispose":
result("done")
default:
Expand Down
9 changes: 9 additions & 0 deletions lib/midi_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ class MidiPlayer {
);
}

Future<void> stopNote({required int note}) {
return _channel.invokeMethod(
'stop_note',
{
'note': note,
},
);
}

/// Dispose engine
Future<void> dispose() {
return _channel.invokeMethod('dispose');
Expand Down

0 comments on commit 1300960

Please sign in to comment.