Android-使用MIDI文件播放SoundFont [英] Android - Play SoundFont with MIDI file
问题描述
我有一个midi文件,并且我已使用以下代码在Android中使用MediaPlayer播放了该midi文件:
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()
它默认使用一种乐器(例如钢琴)演奏,现在我想添加soundfont(sf2/sf3)文件,以使用不同的乐器和混响效果演奏中音音符.
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.
推荐答案
有两个库将用于使用SoundFont播放midi文件.
There are two libraries that will be used to play a midi file using SoundFont.
只是用于在Android上播放MIDI音符的合成器.您可以将其与USB/蓝牙MIDI库一起使用来创建MIDI应用程序.
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文件.
SoundFont2 file is supported.
此库提供了读取,操作和写入MIDI文件的接口.支持回放"作为实时事件分发系统.该库不包括实际的音频播放或设备接口.
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.
初始化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();
播放midi文件中的Midi音符
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();
可以定义SF频道
private int channel = 0;
这篇关于Android-使用MIDI文件播放SoundFont的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!