flutter_midi_pro 1.0.3 copy "flutter_midi_pro: ^1.0.3" to clipboard
flutter_midi_pro: ^1.0.3 copied to clipboard

The `flutter_midi_pro` plugin provides functions for loading SoundFont (.sf2) files, as well as playing and stopping MIDI notes.

flutter_midi_pro #

pub packageGitHub stars GitHub issues

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 FlutterMidiPro class to access the plugin's functions.

import 'package:flutter_midi_pro/flutter_midi_pro.dart';

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);
    _flutterMidi.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}) {
    _flutterMidi.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) {
    _flutterMidi.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 _flutterMidi = FlutterMidiPro();
  final String _value = 'assets/tight_piano.sf2';
  Map<int, NoteModel> pointerAndNote = {};

  Future<void> load(String asset) async {
    ByteData byte = await rootBundle.load(asset);
    _flutterMidi.loadSoundfont(
        sf2Data: byte, name: _value.replaceAll('assets/', ''));
  }

  void play(int midi, {int velocity = 127}) {
    _flutterMidi.playMidiNote(midi: midi, velocity: velocity);
  }

  void stop(int midi) {
    _flutterMidi.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.

Melih Hakan Pektas

Thank you for contributing to flutter_piano_pro!

License #

This project is licensed under the MIT License. See the LICENSE file for details.

22
likes
0
pub points
82%
popularity

Publisher

verified publishermelihhakanpektas.com

The `flutter_midi_pro` plugin provides functions for loading SoundFont (.sf2) files, as well as playing and stopping MIDI notes.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, path_provider, plugin_platform_interface

More

Packages that depend on flutter_midi_pro