本文整理汇总了Java中javax.sound.midi.MidiEvent.getMessage方法的典型用法代码示例。如果您正苦于以下问题:Java MidiEvent.getMessage方法的具体用法?Java MidiEvent.getMessage怎么用?Java MidiEvent.getMessage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.sound.midi.MidiEvent
的用法示例。
在下文中一共展示了MidiEvent.getMessage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMicrosecondsPerQuarterNote
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
/**
* Gets the number of microseconds per quarter note for a sequence, used to
* determine its BPM.
*
* @param sequence
* @return
*/
private static int getMicrosecondsPerQuarterNote(Sequence sequence) {
// Check all MIDI tracks for MIDI_SET_TEMPO message
for (Track track : sequence.getTracks()) {
for (int i = 0; i < track.size(); i++) {
MidiEvent event = track.get(i);
MidiMessage message = event.getMessage();
if (message instanceof MetaMessage) {
MetaMessage m = (MetaMessage) message;
byte[] data = m.getData();
int type = m.getType();
if (type == MIDI_SET_TEMPO) {
return ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
}
}
}
}
return 0;
}
示例2: findFirstTempo
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
/**
* Find the first tempo meta message and return the tempo value
* @return
*/
public static float findFirstTempo(Sequence sequence) throws Exception
{
for(Track track : sequence.getTracks())
{
for(int n=0;n<track.size();n++)
{
MidiEvent event = track.get(n);
if(event.getMessage() instanceof MetaMessage
&& ((MetaMessage)event.getMessage()).getType()==0x51)
{
float tempo = new TempoMessage((MetaMessage)event.getMessage()).getBpm();
System.out.println("Found tempomessage "+tempo+" bpm");
return tempo;
}
}
}
throw new Exception("No tempo message found");
}
示例3: calcLastNoteTick
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
public long calcLastNoteTick()
{
long lastNoteTick = 0;
for (Track t : sequence.getTracks())
{
for (int j = t.size() - 1; j >= 0; j--)
{
MidiEvent evt = t.get(j);
MidiMessage msg = evt.getMessage();
if (msg instanceof ShortMessage)
{
ShortMessage m = (ShortMessage) msg;
if (m.getCommand() == ShortMessage.NOTE_OFF)
{
if (evt.getTick() > lastNoteTick)
{
lastNoteTick = evt.getTick();
}
break;
}
}
}
}
return lastNoteTick;
}
示例4: main
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
public static void main(String argv[]) throws Exception {
Sequence seq = new Sequence(Sequence.PPQ, 240);
Track t = seq.createTrack();
log("add 10 events in random order");
t.add(createEvent(10, 5));
t.add(createEvent(0, 0));
t.add(createEvent(10, 6));
t.add(createEvent(11, 8));
t.add(createEvent(10, 7));
t.add(createEvent(0, 1));
t.add(createEvent(0, 2));
t.add(createEvent(15, 9));
t.add(createEvent(0, 3));
t.add(createEvent(1, 4));
// now compare the events.
// The note param will tell us the
// the expected position
long lastTick = 0;
for (int i = 0; i < t.size(); i++) {
MidiEvent ev = t.get(i);
if (ev.getMessage() instanceof ShortMessage) {
ShortMessage msg = (ShortMessage) ev.getMessage();
log(""+i+": ShortMessage at tick "+ev.getTick()
+" with expected position "+msg.getData1());
if (ev.getTick() < lastTick) {
log(" FAILED: last tick is larger than this event's tick!");
failed = true;
}
if (i != msg.getData1()) {
log(" FAILED: Track did not order correctly.");
failed = true;
}
}
}
if (failed) throw new Exception("Test FAILED!");
log("Test passed.");
}
示例5: printEvent
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
public static void printEvent(MidiEvent event)
{
MidiMessage message = event.getMessage();
long tick = event.getTick();
byte[] data = message.getMessage();
StringBuffer sb = new StringBuffer((data.length * 3) - 1);
for (int i = 0; i < data.length; i++)
{
sb.append(toHexByteString(data[i]));
if (i < data.length - 1) sb.append(' ');
}
System.out.printf("%5d: %s%n", tick, sb);
}
示例6: write
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
public static void write(OutputStream out, List<MidiEvent> events, MidiToAudioSettings settings) throws Throwable {
MidiToAudioSynth.instance().openSynth();
MidiToAudioSynth.instance().loadSoundbank(getPatchs(events), settings.getSoundbankPath());
int usqTempo = 60000000 / 120;
long previousTick = 0;
long timePosition = 0;
MidiToAudioWriter.sort(events);
Receiver receiver = MidiToAudioSynth.instance().getReceiver();
AudioInputStream stream = MidiToAudioSynth.instance().getStream();
Iterator<MidiEvent> it = events.iterator();
while(it.hasNext()){
MidiEvent event = (MidiEvent)it.next();
MidiMessage msg = event.getMessage();
timePosition += ( (event.getTick() - previousTick) * usqTempo) / TGDuration.QUARTER_TIME;
if (msg instanceof MetaMessage) {
if (((MetaMessage) msg).getType() == 0x51) {
byte[] data = ((MetaMessage) msg).getData();
usqTempo = ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
}
} else {
receiver.send(msg, timePosition);
}
previousTick = event.getTick();
}
long duration = (long) (stream.getFormat().getFrameRate() * ( (timePosition / 1000000.0) ));
AudioInputStream srcStream = new AudioInputStream(stream, stream.getFormat(), duration );
AudioInputStream dstStream = AudioSystem.getAudioInputStream(settings.getFormat(), srcStream );
AudioSystem.write(new AudioInputStream(dstStream, dstStream.getFormat(), duration ), settings.getType(), out);
dstStream.close();
srcStream.close();
MidiToAudioSynth.instance().closeSynth();
}
示例7: writeTrack
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
/**
* Write a track to an output stream.
*
* @param track the track to write
* @param dos a MidiDataOutputStream to write to
* @return the number of bytes written
*/
private int writeTrack(Track track, MidiDataOutputStream dos)
throws IOException
{
int i = 0, elength = track.size(), trackLength;
MidiEvent pme = null;
dos.writeInt(0x4d54726b); // "MTrk"
trackLength = computeTrackLength(track, dos);
dos.writeInt(trackLength);
while (i < elength)
{
MidiEvent me = track.get(i);
int dtime = 0;
if (pme != null)
dtime = (int) (me.getTick() - pme.getTick());
dos.writeVariableLengthInt(dtime);
// FIXME: use running status byte
byte msg[] = me.getMessage().getMessage();
dos.write(msg);
pme = me;
i++;
}
// We're done if the last event was an End of Track meta message.
if (pme != null && (pme.getMessage() instanceof MetaMessage))
{
MetaMessage mm = (MetaMessage) pme.getMessage();
if (mm.getType() == 0x2f) // End of Track message
return trackLength + 8;
}
// Write End of Track meta message
dos.writeVariableLengthInt(0); // Delta time of 0
dos.writeByte(0xff); // Meta Message
dos.writeByte(0x2f); // End of Track message
dos.writeVariableLengthInt(0); // Length of 0
return trackLength + 8 + 4;
}
示例8: writeTrack
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
/**
* Write a track to an output stream.
*
* @param track the track to write
* @param dos a MidiDataOutputStream to write to
* @return the number of bytes written
*/
private int writeTrack(Track track, MidiDataOutputStream dos)
throws IOException
{
int i = 0, elength = track.size(), trackLength;
MidiEvent pme = null;
dos.writeInt(0x4d54726b); // "MTrk"
trackLength = computeTrackLength(track, dos);
dos.writeInt(trackLength);
while (i < elength)
{
MidiEvent me = track.get(i);
int dtime = 0;
if (pme != null)
dtime = (int) (me.getTick() - pme.getTick());
dos.writeVariableLengthInt(dtime);
// FIXME: use running status byte
byte msg[] = me.getMessage().getMessage();
dos.write(msg);
pme = me;
i++;
}
// We're done if the last event was an End of Track meta message.
if (pme != null && (pme.getMessage() instanceof MetaMessage))
{
MetaMessage mm = (MetaMessage) pme.getMessage();
if (mm.getType() == 0x2f) // End of Track message
return trackLength + 8;
}
// Write End of Track meta message
dos.writeVariableLengthInt(0); // Delta time of 0
dos.writeByte(0xff); // Meta Message
dos.writeByte(0x2f); // End of Track message
dos.writeVariableLengthInt(0); // Length of 0
return trackLength + 8 + 4;
}
示例9: ChangeResolution
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
/**
* Change resolution (TPQN) and retiming events.
* @param seq Sequence to be processed.
* @param resolution Ticks per quarter note of new sequence.
* @return New sequence with new resolution.
* @throws InvalidMidiDataException throw if MIDI data is invalid.
*/
public static Sequence ChangeResolution(Sequence sourceSeq, int resolution) throws InvalidMidiDataException
{
// sequence must be tick-based
if (sourceSeq.getDivisionType() != Sequence.PPQ)
{
throw new UnsupportedOperationException("SMPTE is not supported.");
}
Sequence seq = new Sequence(sourceSeq.getDivisionType(), resolution);
// process all input tracks
for (int trackIndex = 0; trackIndex < sourceSeq.getTracks().length; trackIndex++)
{
Track sourceTrack = sourceSeq.getTracks()[trackIndex];
Track track = seq.createTrack();
// process all events
double timingRate = (double) resolution / sourceSeq.getResolution();
for (int eventIndex = 0; eventIndex < sourceTrack.size(); eventIndex++)
{
MidiEvent sourceEvent = sourceTrack.get(eventIndex);
MidiEvent event = new MidiEvent(sourceEvent.getMessage(), Math.round(sourceEvent.getTick() * timingRate));
track.add(event);
}
}
// if the target resolution is shorter than source resolution,
// events at different timing might be located at the same timing.
// As a result, there might be zero-length note and/or
// same control changes at the same timing.
//
// Probably, they should be removed for better conversion.
// I do not remove them anyway at the moment,
// because it does not cause any major problems.
return seq;
}
示例10: NoteEvent
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
/**
* The process generating the NoteEvent should first supply it start event,
* and when ready the end event - to form a complete Note event
* @param startEvent
*
*/
NoteEvent(MidiPart part, MidiEvent startEvent)
{
super(part,startEvent.getTick());
this.startEvent = startEvent;
ShortMessage shm = (ShortMessage)startEvent.getMessage();
note = shm.getData1();
velocity = shm.getData2();
channel = shm.getChannel();
startTick = startEvent.getTick();
}
示例11: findNearest
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
private long findNearest(long tick, int[] v) {
if (notesCount == 0) return tick;
Track track = getTrack();
int size = track.size();
int shift = 4 * sequence.getResolution(); // pattern starts at 4 * ppq
long t = tick % length; // tick inside pattern, to be (de-)quantized
t = tick + shift;
int i = 0;
long nearest = -1;
v[0] = 100;
long tt = t;
MidiEvent ev;
boolean isNote;
do {
ev = track.get(i++);
tt = ev.getTick();
MidiMessage msg = ev.getMessage();
isNote = (msg instanceof ShortMessage) && (((ShortMessage)msg).getCommand() == ShortMessage.NOTE_ON);
if ((isNote) && ( ( tt < t ) || ( (tt-t) < (t-nearest) ) ) ) {
nearest = tt;
v[0] = ((ShortMessage)msg).getData2();
}
} while ((i < size) && ((!isNote) || (tt < t))); // stops after first note imediately following after (tt<t) no longer is true
return nearest - shift;
}
示例12: findFurthest
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
private long findFurthest(long tick, int q, int[] v) {
if (notesCount == 0) return tick;
Track track = getTrack();
int size = track.size();
int shift = 4 * sequence.getResolution(); // pattern starts at 4 * ppq
long t = tick % length; // tick inside pattern, to be (de-)quantized
t = tick + shift;
int i = 0;
long furthest = t;
v[0] = 100;
int q2 = q/2;
long tt;
MidiEvent ev;
while ((i < size) && ( (tt = (ev = track.get(i++)).getTick()) < t+q2)) {
MidiMessage msg = ev.getMessage();
if ((msg instanceof ShortMessage) && (((ShortMessage)msg).getCommand() == ShortMessage.NOTE_ON)) {
long d = t - tt;
if (d < 0) d = -d;
if (d <= q2) {
if (abs(furthest-t) < d) { // found something in allowed range, and even further away
furthest = tt;
v[0] = ((ShortMessage)msg).getData2();
}
}
}
}
return furthest - shift;
}
示例13: convertToType1
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
/**
* Separates the MIDI file to have one track per channel (Type 1).
*/
public static void convertToType1(Sequence song)
{
if (song.getTracks().length == 1)
{
Track track0 = song.getTracks()[0];
Track[] tracks = new Track[CHANNEL_COUNT];
int trackNumber = 1;
int i = 0;
while (i < track0.size())
{
MidiEvent evt = track0.get(i);
if (evt.getMessage() instanceof ShortMessage)
{
int chan = ((ShortMessage) evt.getMessage()).getChannel();
if (tracks[chan] == null)
{
tracks[chan] = song.createTrack();
String trackName = (chan == DRUM_CHANNEL) ? "Drums" : ("Track " + trackNumber);
trackNumber++;
tracks[chan].add(MidiFactory.createTrackNameEvent(trackName));
}
tracks[chan].add(evt);
if (track0.remove(evt))
continue;
}
i++;
}
}
}
示例14: calcFirstNoteTick
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
public long calcFirstNoteTick()
{
long firstNoteTick = Long.MAX_VALUE;
for (Track t : sequence.getTracks())
{
for (int j = 0; j < t.size(); j++)
{
MidiEvent evt = t.get(j);
MidiMessage msg = evt.getMessage();
if (msg instanceof ShortMessage)
{
ShortMessage m = (ShortMessage) msg;
if (m.getCommand() == ShortMessage.NOTE_ON)
{
if (evt.getTick() < firstNoteTick)
{
firstNoteTick = evt.getTick();
}
break;
}
}
}
}
if (firstNoteTick == Long.MAX_VALUE)
return 0;
return firstNoteTick;
}
示例15: sortAndDeliverMidiMessages
import javax.sound.midi.MidiEvent; //导入方法依赖的package包/类
/**
* Takes all of the MIDI events in the given Sequence, sorts them according to
* when they are to be played, and sends the events to the MidiMessageRecipient
* when the each event is ready to be played.
*
* @param sequence The Sequence with messages to sort and deliver
* @param recipient the handler of the delivered message
*/
public static final void sortAndDeliverMidiMessages(Sequence sequence, MidiMessageRecipient recipient)
{
double timeFactor = 1.0;
Map<Long, List<MidiEvent>> timeMap = new HashMap<Long, List<MidiEvent>>();
long longestTime = TimeEventManager.sortSequenceByTimestamp(sequence, timeMap);
long lastTime = 0;
for (long time=0; time < longestTime; time++)
{
List<MidiEvent> midiEventList = (List<MidiEvent>)timeMap.get(time);
if (midiEventList != null)
{
for (MidiEvent event : midiEventList)
{
MidiMessage message = event.getMessage();
if ((message.getMessage().length >= 2) && (message.getMessage()[1] == 0x51) && (message instanceof MetaMessage))
{
int bpm = parseMicrosecondsPerBeat((MetaMessage)message, time);
timeFactor = TimeFactor.getTimeFactor(sequence, bpm);
System.out.println("TimeFactor is "+timeFactor);
}
recipient.messageReady(message, time);
}
try
{
long sleepTime = (int)(((time - lastTime) * (TimeFactor.QUARTER_DURATIONS_IN_WHOLE+0.20)));
Thread.sleep(sleepTime); // (int) (1 * timeFactor));
lastTime = time;
} catch (Exception ex)
{
throw new JFugueException(JFugueException.ERROR_SLEEP);
}
}
}
}