在 android-midi-lib 中发送消息
Sending messages in android-midi-lib
我正在使用 android-midi-lib https://code.google.com/p/android-midi-lib/ 来生成音乐。我正在尝试切换乐器,但所有通道听起来都一样(钢琴),这是应该的。仔细研究后,我发现我应该向频道发送 ProgramChange
消息,以便他们使用不同的工具,但我不知道该怎么做。那么,如何向频道发送消息或发送任何消息呢?
这是我使用的代码
// 2. Add events to the tracks
// Track 0 is the tempo map
TimeSignature ts = new TimeSignature();
ts.setTimeSignature(4, 4, TimeSignature.DEFAULT_METER, TimeSignature.DEFAULT_DIVISION);
Tempo tempo = new Tempo();
tempo.setBpm(128);
tempoTrack.insertEvent(ts);
tempoTrack.insertEvent(tempo);
// Track 1 will have some notes in it
final int NOTE_COUNT = 80;
Random rand = new Random();
int beat1 = rand.nextInt((81 - 35) + 1) + 35;
int beat2 = rand.nextInt((81 - 35) + 1) + 35;
int beat3 = rand.nextInt((81 - 35) + 1) + 35;
for(int i = 0; i < NOTE_COUNT; i++)
{
int pitch = rand.nextInt((80 - 30) + 1) + 30;
int channel = alkuChannel;
//int channelTest = 1;
//int pitch = alkuPitch + i;
int velocity = 100;
long tick = i * alkuTick;
long duration = 120;
noteTrack.insertNote(channel, pitch, velocity, tick, duration);
noteTestTrack.insertNote(9, beat1, 100, tick, duration*2);
if (i%2==0) {
noteTestTrack.insertNote(9, beat2, 100, tick, duration*2);
}
}
// 3. Create a MidiFile with the tracks we created
ArrayList<MidiTrack> tracks = new ArrayList<MidiTrack>();
tracks.add(tempoTrack);
tracks.add(noteTrack);
tracks.add(noteTestTrack);
MidiFile midi = new MidiFile(MidiFile.DEFAULT_RESOLUTION, tracks);
看看 implementation of insertNote()
:
public void insertNote(int channel, int pitch, int velocity, long tick, long duration)
{
insertEvent(new NoteOn(tick, channel, pitch, velocity));
insertEvent(new NoteOn(tick + duration, channel, pitch, 0));
}
对 ProgramChange
事件做同样的事情:
track.insertEvent(new ProgramChange(tick, channel, program));
我正在使用 android-midi-lib https://code.google.com/p/android-midi-lib/ 来生成音乐。我正在尝试切换乐器,但所有通道听起来都一样(钢琴),这是应该的。仔细研究后,我发现我应该向频道发送 ProgramChange
消息,以便他们使用不同的工具,但我不知道该怎么做。那么,如何向频道发送消息或发送任何消息呢?
这是我使用的代码
// 2. Add events to the tracks
// Track 0 is the tempo map
TimeSignature ts = new TimeSignature();
ts.setTimeSignature(4, 4, TimeSignature.DEFAULT_METER, TimeSignature.DEFAULT_DIVISION);
Tempo tempo = new Tempo();
tempo.setBpm(128);
tempoTrack.insertEvent(ts);
tempoTrack.insertEvent(tempo);
// Track 1 will have some notes in it
final int NOTE_COUNT = 80;
Random rand = new Random();
int beat1 = rand.nextInt((81 - 35) + 1) + 35;
int beat2 = rand.nextInt((81 - 35) + 1) + 35;
int beat3 = rand.nextInt((81 - 35) + 1) + 35;
for(int i = 0; i < NOTE_COUNT; i++)
{
int pitch = rand.nextInt((80 - 30) + 1) + 30;
int channel = alkuChannel;
//int channelTest = 1;
//int pitch = alkuPitch + i;
int velocity = 100;
long tick = i * alkuTick;
long duration = 120;
noteTrack.insertNote(channel, pitch, velocity, tick, duration);
noteTestTrack.insertNote(9, beat1, 100, tick, duration*2);
if (i%2==0) {
noteTestTrack.insertNote(9, beat2, 100, tick, duration*2);
}
}
// 3. Create a MidiFile with the tracks we created
ArrayList<MidiTrack> tracks = new ArrayList<MidiTrack>();
tracks.add(tempoTrack);
tracks.add(noteTrack);
tracks.add(noteTestTrack);
MidiFile midi = new MidiFile(MidiFile.DEFAULT_RESOLUTION, tracks);
看看 implementation of insertNote()
:
public void insertNote(int channel, int pitch, int velocity, long tick, long duration)
{
insertEvent(new NoteOn(tick, channel, pitch, velocity));
insertEvent(new NoteOn(tick + duration, channel, pitch, 0));
}
对 ProgramChange
事件做同样的事情:
track.insertEvent(new ProgramChange(tick, channel, program));