Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Play SoundFont with MIDI file

I have one midi file and I have played that midi file using MediaPlayer in android using the following code:

val mMediaPlayer = MediaPlayer.create(context, R.raw.test_ring_1)

mMediaPlayer?.start()

It default play with one instrument like piano, now I want to add soundfont (sf2/sf3) file to play the midi notes with different instrument and with reverberation effects.

Please guide a way to achieve expected result.

like image 922
jigar kanjariya Avatar asked Jun 11 '19 10:06

jigar kanjariya


People also ask

How do I play MIDI files with Soundfonts?

You can also import your own SoundFont (sf2) files and test them with MIDI files. Just open MIDI, SoundFont files from your Google Drive and your computer, and Play it in your browser. It's very easy to use. You can control your playback like Play, Stop, Seek, Volume and all common controls in any audio player.

What is a General MIDI SoundFont?

SoundFont is a brand name that collectively refers to a file format and associated technology that uses sample-based synthesis to play MIDI files. It was first used on the Sound Blaster AWE32 sound card for its General MIDI support.


1 Answers

There are two libraries that will be used to play a midi file using SoundFont.

Midi Driver

Just a synthesizer for playing MIDI note on Android. You can use it with USB/Bluetooth-MIDI library together to create your MIDI application.

SoundFont2 file is supported.

Android MIDI Library

This library provides an interface to read, manipulate, and write MIDI files. "Playback" is supported as a real-time event dispatch system. This library does NOT include actual audio playback or device interfacing.

To initialize SF2-SoundBank

SF2Soundbank sf = new SF2Soundbank(getAssets().open("test.sf2"));
        synth = new SoftSynthesizer();
        synth.open();
        synth.loadAllInstruments(sf);
        synth.getChannels()[0].programChange(0);
        synth.getChannels()[1].programChange(1);
        recv = synth.getReceiver();

To Play the Midi notes from midi file

MidiFile midiFile = new MidiFile(getAssets().open("test.mid"));

// Create a new MidiProcessor:
MidiProcessor processor = new MidiProcessor(midiFile);

// listen for all midi events:
processor.registerEventListener(new MidiEventListener() {
    @Override
    public void onStart(boolean fromBeginning) {

    }

    @Override
    public void onEvent(MidiEvent event, long ms) {

        if (event.getClass() == NoteOn.class) {

                NoteOn noteOn = ((NoteOn) event);

                try {
                    ShortMessage msg = new ShortMessage();
                    msg.setMessage(ShortMessage.NOTE_ON, channel, noteOn.getNoteValue(), noteOn.getVelocity());
                    recv.send(msg, ms);
                } catch (InvalidMidiDataException e) {
                    e.printStackTrace();
                }

            } else if (event.getClass() == NoteOff.class) {

                NoteOff noteOff = ((NoteOff) event);

                try {
                    ShortMessage msg = new ShortMessage();
                    msg.setMessage(ShortMessage.NOTE_ON, channel, noteOff.getNoteValue(), noteOff.getVelocity());
                    recv.send(msg, ms);
                } catch (InvalidMidiDataException e) {
                    e.printStackTrace();
                }

            }
    }

    @Override
    public void onStop(boolean finished) {

    }
}, MidiEvent.class);

// Start the processor:
processor.start();

Variable to define SF channel

private int channel = 0;
like image 162
Abhishek Chaniyara Avatar answered Sep 22 '22 17:09

Abhishek Chaniyara