我试图使用java创建一个MIDI文件,我努力使代码添加一个三十二分音符(在将它添加到序列并生成MIDI文件后,它是空的) 。谢谢你的帮助!
编辑:这里是代码(这个代码生成带有十六分音符的MIDI文件)EDIT2:问题出在我的midi玩家身上。感谢@Maxim指出
Sequence s = null;
try {
s = new Sequence(Sequence.PPQ, 480);
} catch (InvalidMidiDataException e) {
e.printStackTrace();
}
Track t = s.createTrack();
ShortMessage sm = new ShortMessage();
//instru
sm.setMessage(ShortMessage.PROGRAM_CHANGE, 0, 25, 0); //9 ==> is the channel 10.
t.add(new MidiEvent(sm, 0));
t.add(createNoteOnEvent(75, 0));
t.add(createNoteOffEvent(75, 120));
File outputFile = new File("C:\\test.mid");
MidiSystem.write(s, 0, outputFile);
private static MidiEvent createNoteOnEvent(int nKey, long lTick)
{
return createNoteEvent(ShortMessage.NOTE_ON,
nKey,
64,
lTick);
}
private static MidiEvent createNoteOffEvent(int nKey, long lTick)
{
return createNoteEvent(ShortMessage.NOTE_OFF,
nKey,
0,
lTick);
}
private static MidiEvent createNoteEvent(int nCommand,
int nKey,
int nVelocity,
long lTick)
{
ShortMessage message = new ShortMessage();
try
{
message.setMessage(nCommand,
0, // always on channel 1
nKey,
nVelocity);
}
catch (InvalidMidiDataException e)
{
e.printStackTrace();
System.exit(1);
}
MidiEvent event = new MidiEvent(message,
lTick);
return event;
}