Java Code Examples for javax.sound.midi.Track#get()
The following examples show how to use
javax.sound.midi.Track#get() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MidiWaveformSynthesizer.java From chart-fx with Apache License 2.0 | 6 votes |
public static final void addNotesToTrack(final Track track, final Track trk) throws InvalidMidiDataException { for (int ii = 0; ii < track.size(); ii++) { final MidiEvent me = track.get(ii); final MidiMessage mm = me.getMessage(); if (mm instanceof ShortMessage) { final ShortMessage sm = (ShortMessage) mm; final int command = sm.getCommand(); int com = -1; if (command == ShortMessage.NOTE_ON) { com = LOCAL_NOTE_ON; } else if (command == ShortMessage.NOTE_OFF) { com = LOCAL_NOTE_OFF; } if (com > 0) { final byte[] b = sm.getMessage(); final int l = (b == null ? 0 : b.length); final MetaMessage metaMessage = new MetaMessage(com, b, l); final MidiEvent me2 = new MidiEvent(metaMessage, me.getTick()); trk.add(me2); } } } }
Example 2
Source File: MidiWaveformSynthesizer.java From chart-fx with Apache License 2.0 | 5 votes |
public final Track mergeShortMessageEvent(final Track[] tracks) { final Track trk = sequence.createTrack(); for (final Track track : tracks) { for (int i = 0; i < track.size(); i++) { final MidiEvent evt = track.get(i); final MidiMessage mm = evt.getMessage(); if (mm instanceof ShortMessage) { trk.add(evt); } } } return trk; }
Example 3
Source File: TrackAddSameTick.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
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."); }
Example 4
Source File: MidiWaveformSynthesizer.java From chart-fx with Apache License 2.0 | 4 votes |
public void decode(final float[] data, final int frameSize, final int updatePeriod, final int samplingRate, final int nBits) { if (frameSize <= 0) { throw new IllegalArgumentException("Frame size must be greater than zero"); } final Track track = mergeShortMessageEvent(sequence.getTracks()); final float length = 1e-6f * sequence.getMicrosecondLength(); final float ts = 1.0f / samplingRate; final long trackTicks = track.ticks(); final float tickLength = trackTicks <= 0 ? 0 : length / trackTicks; final int frameCount = data.length / frameSize; final float scale = 2 << Math.max(1, nBits + 1); final int fftSize = 2 * frameSize; final FloatFFT_1D fft = new FloatFFT_1D(fftSize); final float[] apodization = new float[fftSize]; for (int i = 0; i < fftSize; i++) { apodization[i] = (float) Apodization.Hann.getIndex(i, apodization.length); } int frameCounter = 0; int tickIndex = 0; final float[] waveForm = new float[2 * frameSize]; final int nUpdateDistance = (int) (updatePeriod / 1000.0 * samplingRate); for (int i = 0; frameCounter < frameCount; i++) { final float t = i * ts; final MidiEvent tickEvt = track.get(tickIndex); final float tickTimeStamp = tickEvt.getTick() * tickLength; // update waveform by one sample update(samplingRate, nBits); if ((t > tickTimeStamp) && (tickIndex < track.size() - 1)) { if ((tickEvt.getMessage() instanceof ShortMessage)) { final ShortMessage sm = (ShortMessage) tickEvt.getMessage(); final int note = sm.getData1() & 0xFF; final int velocity = sm.getData2() & 0xFF; final int command = sm.getCommand(); if ((command == ShortMessage.NOTE_ON) || (command == LOCAL_NOTE_ON)) { noteAmplitude[note] = velocity; } else if ((command == ShortMessage.NOTE_OFF) || (command == LOCAL_NOTE_OFF)) { noteAmplitude[note] = 0.0f; } } tickIndex++; } if (i > 0 && (i % nUpdateDistance == 0)) { for (int j = 0; j < waveForm.length; j++) { final float noise = (1e-3f * System.nanoTime() % 2); // adds some noise waveForm[j] = apodization[j] * getSample(j) + noise / scale; } decodeFrame(fft, waveForm, data, (frameCounter * frameSize) % data.length); frameCounter++; } } // return synthesizer to its original state for (int note = 0; note < N_NOTES; note++) { noteAmplitude[note] = 0.0f; synthesizerChannel.noteOff(note, 0); } }
Example 5
Source File: RealTimeSequencer.java From Bytecoder with Apache License 2.0 | 4 votes |
/** * chase all events from beginning of Track * and send note off for those events that are active * in noteOnCache array. * It is possible, of course, to catch notes from other tracks, * but better than more complicated logic to detect * which notes are really from this track */ private void sendNoteOffIfOn(Track track, long endTick) { int size = track.size(); int done = 0; try { for (int i = 0; i < size; i++) { MidiEvent event = track.get(i); if (event.getTick() > endTick) break; MidiMessage msg = event.getMessage(); int status = msg.getStatus(); int len = msg.getLength(); if (len == 3 && ((status & 0xF0) == ShortMessage.NOTE_ON)) { int note = -1; if (msg instanceof ShortMessage) { ShortMessage smsg = (ShortMessage) msg; if (smsg.getData2() > 0) { // only consider Note On with velocity > 0 note = smsg.getData1(); } } else { byte[] data = msg.getMessage(); if ((data[2] & 0x7F) > 0) { // only consider Note On with velocity > 0 note = data[1] & 0x7F; } } if (note >= 0) { int bit = 1<<(status & 0x0F); if ((noteOnCache[note] & bit) != 0) { // the bit is set. Send Note Off getTransmitterList().sendMessage(status | (note<<8), -1); // clear the bit noteOnCache[note] &= (0xFFFF ^ bit); done++; } } } } } catch (ArrayIndexOutOfBoundsException aioobe) { // this happens when messages are removed // from the track while this method executes } }
Example 6
Source File: RealTimeSequencer.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * chase all events from beginning of Track * and send note off for those events that are active * in noteOnCache array. * It is possible, of course, to catch notes from other tracks, * but better than more complicated logic to detect * which notes are really from this track */ private void sendNoteOffIfOn(Track track, long endTick) { int size = track.size(); int done = 0; try { for (int i = 0; i < size; i++) { MidiEvent event = track.get(i); if (event.getTick() > endTick) break; MidiMessage msg = event.getMessage(); int status = msg.getStatus(); int len = msg.getLength(); if (len == 3 && ((status & 0xF0) == ShortMessage.NOTE_ON)) { int note = -1; if (msg instanceof ShortMessage) { ShortMessage smsg = (ShortMessage) msg; if (smsg.getData2() > 0) { // only consider Note On with velocity > 0 note = smsg.getData1(); } } else { byte[] data = msg.getMessage(); if ((data[2] & 0x7F) > 0) { // only consider Note On with velocity > 0 note = data[1] & 0x7F; } } if (note >= 0) { int bit = 1<<(status & 0x0F); if ((noteOnCache[note] & bit) != 0) { // the bit is set. Send Note Off getTransmitterList().sendMessage(status | (note<<8), -1); // clear the bit noteOnCache[note] &= (0xFFFF ^ bit); done++; } } } } } catch (ArrayIndexOutOfBoundsException aioobe) { // this happens when messages are removed // from the track while this method executes } if (DEBUG_PUMP) Printer.println(" sendNoteOffIfOn: sent "+done+" messages."); }
Example 7
Source File: Midi2WavRenderer.java From computoser with GNU Affero General Public License v3.0 | 4 votes |
public static double send(Sequence seq, Receiver recv) { float divtype = seq.getDivisionType(); assert (seq.getDivisionType() == Sequence.PPQ); Track[] tracks = seq.getTracks(); int[] trackspos = new int[tracks.length]; int mpq = 500000; int seqres = seq.getResolution(); long lasttick = 0; long curtime = 0; while (true) { MidiEvent selevent = null; int seltrack = -1; for (int i = 0; i < tracks.length; i++) { int trackpos = trackspos[i]; Track track = tracks[i]; if (trackpos < track.size()) { MidiEvent event = track.get(trackpos); if (selevent == null || event.getTick() < selevent.getTick()) { selevent = event; seltrack = i; } } } if (seltrack == -1) break; trackspos[seltrack]++; long tick = selevent.getTick(); if (divtype == Sequence.PPQ) curtime += ((tick - lasttick) * mpq) / seqres; else curtime = (long) ((tick * 1000000.0 * divtype) / seqres); lasttick = tick; MidiMessage msg = selevent.getMessage(); if (msg instanceof MetaMessage) { if (divtype == Sequence.PPQ) if (((MetaMessage) msg).getType() == 0x51) { byte[] data = ((MetaMessage) msg).getData(); mpq = ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff); } } else { if (recv != null) recv.send(msg, curtime); } } return curtime / 1000000.0; }