flutter_midi_engine 0.3.0
flutter_midi_engine: ^0.3.0 copied to clipboard
MIDI synthesizer plugin for Flutter. Play MIDI notes with built-in General MIDI sounds or SF2 soundfonts on Android, iOS, macOS and the web.
Flutter MIDI Engine #
A MIDI synthesizer plugin for Flutter. Play notes with built-in General MIDI sounds, or load your own SF2 soundfont on iOS, macOS and the web.
Features #
- Built-in sounds:
loadSoundfont('')works with no soundfont file - SF2 soundfonts: load your own
.sf2on iOS, macOS and the web - Multi-Channel MIDI: Full 16-channel MIDI support for complex arrangements
- Program Changes: Switch between 128 different instruments per soundfont
- Audio Effects: Built-in reverb and chorus effects
- Complete MIDI Control:
- Note on/off with velocity
- Volume and pan control per channel
- Pitch bend messages
- Control change messages
- All notes off / reset controllers
- Automatic Audio Routing: Audio follows the active output device — plug in or unplug a wired/Bluetooth headset mid-playback and sound switches over automatically (no manual re-initialization required)
- Cross-Platform: Android, iOS, macOS and the web
Platform Support #
| Platform | Status | Technology |
|---|---|---|
| Android | ✅ Supported (built-in sounds only) | MidiDriver (Sonivox EAS) |
| iOS | ✅ Supported | AudioToolbox AUSampler |
| macOS | ✅ Supported | AudioToolbox AUSampler |
| Web | ✅ Supported | SpessaSynth (Web Audio AudioWorklet) |
| Windows | 🚧 Planned | - |
| Linux | 🚧 Planned | - |
Requirements #
| Tool | Minimum version |
|---|---|
| Flutter | 3.50.0 |
| Dart | 3.14 |
| Android | minSdk 24 (Android 7.0) |
| iOS | 13.0 |
| macOS | 10.15 |
| Web | A browser with AudioWorklet support (current Chrome, Edge, Firefox, Safari) |
Tested with Flutter 3.50. The Android module uses built-in Kotlin, so apps build with
android.builtInKotlinset to eithertrueorfalse.On iOS and macOS the plugin works with both Swift Package Manager and CocoaPods.
Call
loadSoundfont('')to use built-in sounds with no soundfont file: Sonivox General MIDI on Android, Apple'sgs_instruments.dlson macOS, and a small bundled soundfont on iOS and the web (9 basic instruments: piano, electric piano, organ, guitars, violin, trumpet, flute, synth lead). For better sound on iOS and the web, load your own.sf2file.Android can't load soundfont files. It always plays the built-in Sonivox sounds, even if you pass a path.
Installation #
Add this to your package's pubspec.yaml file:
dependencies:
flutter_midi_engine: ^0.3.0
Getting Started #
1. Add a Soundfont (optional) #
Skip this to use the built-in sounds with loadSoundfont(''). To use your own
SF2 file on iOS, macOS and the web, add it to your assets:
flutter:
assets:
- assets/soundfonts/piano.sf2
2. Basic Usage #
import 'package:flutter_midi_engine/flutter_midi_engine.dart';
final midiEngine = FlutterMidiEngine();
// Built-in sounds
await midiEngine.loadSoundfont('');
// Or your own soundfont from assets (iOS, macOS and the web)
// await midiEngine.loadSoundfontFromAsset('assets/soundfonts/piano.sf2');
// Play middle C
await midiEngine.playNote(note: 60, velocity: 100);
// Wait a bit
await Future.delayed(Duration(seconds: 1));
// Stop the note
await midiEngine.stopNote(note: 60);
3. Advanced Features #
Change Instruments
// Change to Church Organ (program 19)
await midiEngine.changeProgram(program: 19);
// Play on different channels with different instruments
await midiEngine.changeProgram(program: 0, channel: 0); // Piano on channel 0
await midiEngine.changeProgram(program: 40, channel: 1); // Violin on channel 1
await midiEngine.playNote(note: 60, channel: 0);
await midiEngine.playNote(note: 67, channel: 1);
Add Audio Effects
Only level is used for now. The other parameters are accepted but ignored.
// Set reverb (hall effect)
await midiEngine.setReverb(
roomSize: 0.8,
damping: 0.3,
width: 1.0,
level: 0.5,
);
// Set chorus
await midiEngine.setChorus(
voices: 5,
level: 0.7,
speed: 0.5,
depth: 1.0,
);
Volume and Pan Control
// Set volume for channel 0
await midiEngine.setVolume(volume: 100, channel: 0);
// Pan channel 1 to the right
await midiEngine.setPan(pan: 127, channel: 1);
Pitch Bend
// Bend pitch up
await midiEngine.sendPitchBend(value: 4096, channel: 0);
// Reset to center
await midiEngine.sendPitchBend(value: 0, channel: 0);
Control Changes
// Enable sustain pedal
await midiEngine.sendControlChange(controller: 64, value: 127);
// Disable sustain pedal
await midiEngine.sendControlChange(controller: 64, value: 0);
iOS Setup #
For iOS, you need to ensure the app can play audio even when the device is muted:
// Call this before loading the soundfont
await midiEngine.unmute();
await midiEngine.loadSoundfontFromAsset('assets/soundfonts/piano.sf2');
Web Setup #
No setup is needed. The synthesizer and the built-in soundfont ship with the
plugin as web-only assets (about 1.5 MB, loaded the first time you call
loadSoundfont). They are not added to Android, iOS or macOS apps.
- Browsers block audio until the user interacts with the page. The plugin
resumes audio on the first tap, click or key press, so the first note you play
from a tap is heard. You can also call
unmute()from a tap handler. - On the web,
loadSoundfont(path)takes a URL (for example'https://example.com/piano.sf2'or a path relative to your app).loadSoundfontFromAssetworks too. SF2, SF3 and DLS files are supported. - Unlike iOS and macOS, each of the 16 channels keeps its own instrument, and channel 9 (the 10th) plays drums.
Audio Routing (Headset / Bluetooth) #
The engine follows the active audio output device automatically. When a wired or Bluetooth headset is connected or disconnected during playback, audio is re-bound to the new route instead of cutting out — in both directions (speaker → headset and headset → speaker). You don't need to re-initialize the engine yourself.
Under the hood:
- Android listens for output-device changes (
AudioDeviceCallback) and restarts the MIDI driver so its audio track re-binds to the current device. - iOS configures the
AVAudioSessionfor playback (including Bluetooth A2DP and AirPlay), observes route-change and interruption notifications, and restarts the audio graph so the sampler follows the new device. It also recovers automatically after interruptions such as phone calls. - macOS follows the default output device set in System Settings.
- Web plays through the browser, which follows the system output device.
Example App #
Check out the example directory for a complete piano app demonstrating all features.
MIDI Note Reference #
MIDI notes range from 0 to 127, where:
- Middle C (C4) = 60
- A4 (440 Hz) = 69
Formula: MIDI Note = 12 * octave + semitone + 12
General MIDI Program Numbers #
Some common instrument program numbers:
| Program | Instrument |
|---|---|
| 0 | Acoustic Grand Piano |
| 4 | Electric Piano 1 |
| 19 | Church Organ |
| 24 | Acoustic Guitar (nylon) |
| 40 | Violin |
| 56 | Trumpet |
| 73 | Flute |
See the full General MIDI specification for all 128 instruments.
Soundfont Resources #
Free soundfonts you can use:
API Reference #
Core Methods #
loadSoundfont(String path)- Load soundfont from file pathloadSoundfontFromAsset(String assetPath)- Load soundfont from assetsunloadSoundfont()- Unload current soundfontplayNote({required int note, int velocity, int channel})- Play MIDI notestopNote({required int note, int velocity, int channel})- Stop MIDI note
Instrument Control #
changeProgram({required int program, int channel})- Change instrument
Effects & Mix #
setVolume({required int volume, int channel})- Set channel volume (0-127)setPan({required int pan, int channel})- Set stereo pan (0=left, 64=center, 127=right)setReverb({double roomSize, double damping, double width, double level})- Configure reverbsetChorus({int voices, double level, double speed, double depth})- Configure chorus
MIDI Control #
sendControlChange({required int controller, required int value, int channel})- Send CC messagesendPitchBend({required int value, int channel})- Send pitch bend (-8192 to 8191)stopAllNotes()- Stop all playing notesresetAllControllers()- Reset all MIDI controllers
iOS-Specific #
unmute()- Allow audio playback even when device is muted (no-op on other platforms)
Troubleshooting #
Android #
Issue: No sound on Android
- Make sure
loadSoundfont('')returnedtruebefore playing notes - Verify device volume is turned up
Issue: My soundfont sounds the same as the default
- Android always uses the built-in Sonivox sounds. Custom soundfonts work on iOS, macOS and the web.
iOS #
Issue: No sound on iOS
- Call
unmute()before loading soundfont - Test on a real device (simulator has limited MIDI support)
- Use an SF2 file. SF3 (compressed) files may not load
- A custom soundfont needs a preset at bank 0, program 0
Issue: Soundfont not found
- Verify the file path is correct
- When using assets, make sure the file is listed in
pubspec.yaml
Web #
Issue: No sound on the web
- Click or tap the page first. Browsers block audio until the user interacts.
- Make sure
loadSoundfont('')returnedtrue - A custom soundfont URL must be reachable from the browser (same origin, or served with CORS headers)
Audio routing #
Issue: Sound stops when connecting/disconnecting a headset
- Handled automatically since v0.1.5. Upgrade if you're on an older version.
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Credits #
Built with:
- Android: MidiDriver (Sonivox EAS)
- iOS and macOS: AudioToolbox AUSampler
- Web: SpessaSynth by Spessasus
(Apache-2.0, see
assets/web/SPESSASYNTH_LICENSE) - Inspired by: flutter_midi