flutter_midi_pro
The flutter_midi_pro
plugin provides functions for loading SoundFont (.sf2) files, as well as playing and stopping MIDI notes.
Installation
To use this plugin, add flutter_midi_pro
as a dependency in your pubspec.yaml file. For example:
dependencies:
flutter_midi_pro: ^1.0.0
Usage
Import flutter_midi_pro.dart
and use the MidiPro
class to access the plugin's functions.
import 'package:flutter_midi_pro/flutter_midi_pro.dart';
Create a MidiPro Variable
final _midiPro = MidiPro();
Load SoundFont File
Use the loadSoundfont
function to load a SoundFont file with optional ByteData
and a specified name.
final String _path = 'assets/tight_piano.sf2';
Future loadSoundfont(String asset) async {
ByteData byte = await rootBundle.load(asset);
_midiPro.loadSoundfont(
sf2Data: byte, name: _path.replaceAll('assets/', ''));
}
Play MIDI Note
Use the playMidiNote
function to play a MIDI note with a given MIDI value and velocity.
play(int midi, {int velocity = 127}) {
_midiPro.playMidiNote(midi: midi, velocity: velocity);
}
Stop MIDI Note
Use the stopMidiNote
function to stop a MIDI note with a given MIDI number and velocity.
void stop(int midi) {
_midiPro.stopMidiNote(midi: midi);
}
Example
Here's an example of how you could use the flutter_midi_pro
plugin to play a piano using a SoundFont file and using the flutter_piano_pro
:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_midi_pro/flutter_midi_pro.dart';
import 'package:flutter_piano_pro/flutter_piano_pro.dart';
import 'package:flutter_piano_pro/note_model.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _midiPro = MidiPro();
final String _value = 'assets/tight_piano.sf2';
Map<int, NoteModel> pointerAndNote = {};
Future<void> load(String asset) async {
ByteData byte = await rootBundle.load(asset);
_midiPro.loadSoundfont(
sf2Data: byte, name: _value.replaceAll('assets/', ''));
}
void play(int midi, {int velocity = 127}) {
_midiPro.playMidiNote(midi: midi, velocity: velocity);
}
void stop(int midi) {
_midiPro.stopMidiNote(midi: midi);
}
@override
void initState() {
load(_value);
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Midi Pro Example'),
),
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
PianoPro(
noteCount: 15,
onTapDown: (NoteModel? note, int tapId) {
if (note == null) return;
play(note.midiNoteNumber);
setState(() => pointerAndNote[tapId] = note);
},
onTapUpdate: (NoteModel? note, int tapId) {
if (note == null) return;
if (pointerAndNote[tapId] == note) return;
stop(pointerAndNote[tapId]!.midiNoteNumber);
play(note.midiNoteNumber);
pointerAndNote[tapId] = note;
},
onTapUp: (int tapId) {
stop(pointerAndNote[tapId]!.midiNoteNumber);
pointerAndNote.remove(tapId);
},
)
],
)),
),
);
}
}
Contributions
Contributions are welcome! Please feel free to submit a PR or open an issue.
Contact
If you have any questions or suggestions, feel free to contact the package maintainer, Melih Hakan Pektas, via email or through GitHub.
Thank you for contributing to flutter_piano_pro!
License
This project is licensed under the MIT License. See the LICENSE file for details.