javax.sound.Midi,如何包含元事件类型的示例?
javax.sound.Midi, Example of how include Meta Event Type?
我正在检查这个问题Terminate Java Midi output
基于此文档https://www.csie.ntu.edu.tw/~r92092/ref/midi/
我想在受控模式中加入一些额外的事件...
这里是 Midi 代码:
import javax.sound.midi.*;
class TestMidiSound {
public static MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
ShortMessage a = new ShortMessage();
try {
a.setMessage(comd, chan, one, two);
} catch (InvalidMidiDataException e) {
e.printStackTrace();
}
return new MidiEvent(a, tick);
}
public static void main(String[] args) {
try {
final Sequencer seq = MidiSystem.getSequencer();
seq.open();
int[] events = {125};
seq.addControllerEventListener(new ControllerEventListener() {
@Override
public void controlChange(ShortMessage event) {
System.out.println(
"event.getCommand():" + event.getCommand()
+ "\tevent.getChannel():" + event.getChannel()
+ "\tevent.getData1():" + event.getData1()
+ "\tevent.getData2():" + event.getData2()
+ "\tevent.toString():" + event.toString());
}
}, events);
Sequence s = new Sequence(Sequence.PPQ, 4);
Track t = s.createTrack();
for (int i = 5; i < 60; i += 4) {
t.add(MySound.makeEvent(/*144*/ShortMessage.NOTE_ON, 1 /*channel*/, i/*pitch*/, 100 /*velocity*/, i /*tick*/));
// No will be controlled
t.add(MySound.makeEvent(/*176*/ShortMessage.CONTROL_CHANGE, 1/*channel*/, 127 /*[Channel Mode Message] Poly Mode On (+ mono off, +all notes off)*/, 0, i/*tick*/));
t.add(MySound.makeEvent(/*176*/ShortMessage.CONTROL_CHANGE, 1/*channel*/, 125 /*[Channel Mode Message] Omni Mode On (+ all notes off)*/, 0, i + 2/*tick*/));
t.add(MySound.makeEvent(/*128*/ShortMessage.NOTE_OFF, 1/*channel*/, i/*pitch*/, 100/*velocity*/, i + 2/*tick*/));
}
seq.setSequence(s);
seq.addMetaEventListener(new MetaEventListener() {
@Override
public void meta(MetaMessage metaMsg) {
if (metaMsg.getType() == 0x2F) {
seq.close();
}
}
});
seq.setTempoInBPM(220);
seq.start();
} catch (InvalidMidiDataException e) {
e.printStackTrace();
} catch (MidiUnavailableException e) {
e.printStackTrace();
}
}
}
这是我的活动
- 拍号
- 调号
- 设置速度
- Midi 端口前缀
- Midi 通道前缀
- SMPTE 偏移
如何包含元事件类型?.
private static MidiEvent createMetaMessageMidiEvent(int type, byte[] data, int length, long instant) {
MetaMessage metaMessage = new MetaMessage();
try {
metaMessage.setMessage(type, data, length);
} catch (InvalidMidiDataException e) {
}
return new MidiEvent(metaMessage, instant);
}
现在您可以插入事件了...
long instant = 0L;
byte[] bytesSetTempo = {0x0F, 0x42, 0x40};//1'000.000
byte[] bytesTimeSignature = {0x05, 0x03, 0x18, 0x16};//'Time Signature/numerator: '05' = 5, denominator pow: '03' -> Pow(2, 3) = 8, MIDI Clocks :'18' = 24, number 1/32 notes per 24 MIDI clocks:'16' = 16'
String trackNamePiano = new String("Track Name:Piano");
byte[] bytesKeySignaturePiano = {0x02, 0x00};//'02' = 2 sharps, key: '00' = major key'
trackPiano.add(createMetaMessageMidiEvent(0x03, trackNamePiano.getBytes(), trackNamePiano.length(), instant));
trackPiano.add(createMetaMessageMidiEvent(0x51, bytesSetTempo, bytesSetTempo.length, instant));
trackPiano.add(createMetaMessageMidiEvent(0x58, bytesTimeSignature, bytesTimeSignature.length, instant));
trackPiano.add(createMetaMessageMidiEvent(0x59, bytesKeySignaturePiano, bytesKeySignaturePiano.length, instant));
String trackNameViolin = new String("Track Name:Violin");
byte[] bytesKeySignatureViolin = {(byte) 0xFE, 0x01};//'FE' = 2 flats, key: '01' = minor key'
trackViolin.add(createMetaMessageMidiEvent(0x03, trackNameViolin.getBytes(), trackNameViolin.length(), instant));
trackViolin.add(createMetaMessageMidiEvent(0x58, bytesTimeSignature, bytesTimeSignature.length, instant));
trackViolin.add(createMetaMessageMidiEvent(0x51, bytesSetTempo, bytesSetTempo.length, instant));
trackViolin.add(createMetaMessageMidiEvent(0x59, bytesKeySignatureViolin, bytesKeySignatureViolin.length, instant));
我正在检查这个问题Terminate Java Midi output
基于此文档https://www.csie.ntu.edu.tw/~r92092/ref/midi/
我想在受控模式中加入一些额外的事件...
这里是 Midi 代码:
import javax.sound.midi.*;
class TestMidiSound {
public static MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
ShortMessage a = new ShortMessage();
try {
a.setMessage(comd, chan, one, two);
} catch (InvalidMidiDataException e) {
e.printStackTrace();
}
return new MidiEvent(a, tick);
}
public static void main(String[] args) {
try {
final Sequencer seq = MidiSystem.getSequencer();
seq.open();
int[] events = {125};
seq.addControllerEventListener(new ControllerEventListener() {
@Override
public void controlChange(ShortMessage event) {
System.out.println(
"event.getCommand():" + event.getCommand()
+ "\tevent.getChannel():" + event.getChannel()
+ "\tevent.getData1():" + event.getData1()
+ "\tevent.getData2():" + event.getData2()
+ "\tevent.toString():" + event.toString());
}
}, events);
Sequence s = new Sequence(Sequence.PPQ, 4);
Track t = s.createTrack();
for (int i = 5; i < 60; i += 4) {
t.add(MySound.makeEvent(/*144*/ShortMessage.NOTE_ON, 1 /*channel*/, i/*pitch*/, 100 /*velocity*/, i /*tick*/));
// No will be controlled
t.add(MySound.makeEvent(/*176*/ShortMessage.CONTROL_CHANGE, 1/*channel*/, 127 /*[Channel Mode Message] Poly Mode On (+ mono off, +all notes off)*/, 0, i/*tick*/));
t.add(MySound.makeEvent(/*176*/ShortMessage.CONTROL_CHANGE, 1/*channel*/, 125 /*[Channel Mode Message] Omni Mode On (+ all notes off)*/, 0, i + 2/*tick*/));
t.add(MySound.makeEvent(/*128*/ShortMessage.NOTE_OFF, 1/*channel*/, i/*pitch*/, 100/*velocity*/, i + 2/*tick*/));
}
seq.setSequence(s);
seq.addMetaEventListener(new MetaEventListener() {
@Override
public void meta(MetaMessage metaMsg) {
if (metaMsg.getType() == 0x2F) {
seq.close();
}
}
});
seq.setTempoInBPM(220);
seq.start();
} catch (InvalidMidiDataException e) {
e.printStackTrace();
} catch (MidiUnavailableException e) {
e.printStackTrace();
}
}
}
这是我的活动
- 拍号
- 调号
- 设置速度
- Midi 端口前缀
- Midi 通道前缀
- SMPTE 偏移
如何包含元事件类型?.
private static MidiEvent createMetaMessageMidiEvent(int type, byte[] data, int length, long instant) {
MetaMessage metaMessage = new MetaMessage();
try {
metaMessage.setMessage(type, data, length);
} catch (InvalidMidiDataException e) {
}
return new MidiEvent(metaMessage, instant);
}
现在您可以插入事件了...
long instant = 0L;
byte[] bytesSetTempo = {0x0F, 0x42, 0x40};//1'000.000
byte[] bytesTimeSignature = {0x05, 0x03, 0x18, 0x16};//'Time Signature/numerator: '05' = 5, denominator pow: '03' -> Pow(2, 3) = 8, MIDI Clocks :'18' = 24, number 1/32 notes per 24 MIDI clocks:'16' = 16'
String trackNamePiano = new String("Track Name:Piano");
byte[] bytesKeySignaturePiano = {0x02, 0x00};//'02' = 2 sharps, key: '00' = major key'
trackPiano.add(createMetaMessageMidiEvent(0x03, trackNamePiano.getBytes(), trackNamePiano.length(), instant));
trackPiano.add(createMetaMessageMidiEvent(0x51, bytesSetTempo, bytesSetTempo.length, instant));
trackPiano.add(createMetaMessageMidiEvent(0x58, bytesTimeSignature, bytesTimeSignature.length, instant));
trackPiano.add(createMetaMessageMidiEvent(0x59, bytesKeySignaturePiano, bytesKeySignaturePiano.length, instant));
String trackNameViolin = new String("Track Name:Violin");
byte[] bytesKeySignatureViolin = {(byte) 0xFE, 0x01};//'FE' = 2 flats, key: '01' = minor key'
trackViolin.add(createMetaMessageMidiEvent(0x03, trackNameViolin.getBytes(), trackNameViolin.length(), instant));
trackViolin.add(createMetaMessageMidiEvent(0x58, bytesTimeSignature, bytesTimeSignature.length, instant));
trackViolin.add(createMetaMessageMidiEvent(0x51, bytesSetTempo, bytesSetTempo.length, instant));
trackViolin.add(createMetaMessageMidiEvent(0x59, bytesKeySignatureViolin, bytesKeySignatureViolin.length, instant));