在android-midi-lib中发送消息

时间:2015-03-06 14:13:27

标签: android midi

我使用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);

1 个答案:

答案 0 :(得分:0)

查看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));