如何在Java MIDI程序中更改乐器

时间:2019-04-15 14:03:29

标签: java midi javax.sound.midi

如何在Midi中更换乐器? 在显示“ Channel out of range”(通道超出范围)后,我可以将channel的值最多更改为9,这全是钢琴。如何将其更改为例如吉他

  public noteplay()
  throws InvalidMidiDataException,MidiUnavailableException
  {
    bpm =80;
    beatLength = 1;
    theTimeMelody = 0;
    theTimeBass =0;
    seq = new Sequence(Sequence.PPQ,3);
    trkBass = seq.createTrack();
    trkMelody = seq.createTrack();
  }
  public void melody(int volume)
    throws InvalidMidiDataException,MidiUnavailableException

  {

   int list1[] = { 56,59,60,63,56,59,60,63, 61,64,65,68,61,64,65,68,    56,59,60,63,56,59,60,63,    63,66,67,70,73,66,67,70,};

    for (int pitch : list1)
      {
    ShortMessage onMsg = new ShortMessage();
    onMsg.setMessage(ShortMessage.NOTE_ON, 0, pitch, 80);

    ShortMessage offMsg = new ShortMessage();
    offMsg.setMessage(ShortMessage.NOTE_OFF, 0, pitch, 90);

    trkBass.add(new MidiEvent(onMsg,theTimeBass));
    trkBass.add(new MidiEvent(offMsg,theTimeBass+beatLength));
    theTimeBass += beatLength;

      }
  } 
}

1 个答案:

答案 0 :(得分:0)

通用MIDI标准为鼓保留了第十个通道。

您可以将其他15个通道用于旋律乐器。