我已经下载了https://code.google.com/p/android-midi-lib/以尝试长时间播放纯弦声音。我想了解如何使用默认三角钢琴以外的 MIDI 通道。渠道应该是答案。更改它们似乎没有效果。所有 16 个通道听起来都像 Android 中的三角钢琴吗?还是我错过了什么?!?
我已经改编了引用项目中的示例文件写入例程。我尝试了 5 个 MIDI 通道。它们听起来都像频道 0。我在 Droid 2 和 S3 上得到了一致的结果。
MidiTrack tempoTrack = new MidiTrack();
MidiTrack noteTrack = new MidiTrack();
// 2. Add events to the tracks
// 2a. Track 0 is typically the tempo map
TimeSignature ts = new TimeSignature();
ts.setTimeSignature(4, 4, TimeSignature.DEFAULT_METER, TimeSignature.DEFAULT_DIVISION);
Tempo t = new Tempo();
t.setBpm(bpm);
tempoTrack.insertEvent(ts);
tempoTrack.insertEvent(t);
for(int i = 21; i < 108; i += 5)
{
int channel = 8, pitch = 1 + i, velocity = 100;
noteTrack.insertNote(channel, pitch + 2, velocity, noteTrack.getLengthInTicks(), 120);
}
// 3. Create a MidiFile with the tracks we created
ArrayList<MidiTrack> tracks = new ArrayList<MidiTrack>();
tracks.add(tempoTrack);
tracks.add(noteTrack);
MidiFile midi = new MidiFile(MidiFile.DEFAULT_RESOLUTION, tracks);
// 4. Write the MIDI data to a file
//omitted for clarity
try {
FileInputStream is = new FileInputStream(output);
mediaPlayer.reset();
mediaPlayer.setDataSource(is.getFD()); //there are issues if you pass in the file name
mediaPlayer.prepare();
} catch (Exception e) {
System.err.println(e);
}
我没有发现我的 MIDI 文件格式。但是,似乎认为频道正在写入稍后由 MediaPlayer 读取的文件。我已验证将通道信息写入文件时是否正确。我看到了两个嫌疑人:MediaPlayer 和系统 MIDI 引擎。我不知道该把重点放在哪里...
这是应该将通道信息 (ChannelEvent) 写入 MIDI 文件的代码:
@Override
public void writeToFile(OutputStream out, boolean writeType) throws IOException
{
super.writeToFile(out, writeType);
if(writeType)
{
int typeChannel = (mType << 4) + mChannel;
out.write(typeChannel);
}
out.write(mValue1);
if(mType != PROGRAM_CHANGE && mType != CHANNEL_AFTERTOUCH)
{
out.write(mValue2);
}
}
有什么想法吗?