Java Code Examples for javax.sound.midi.Track#size()

The following examples show how to use javax.sound.midi.Track#size() . 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 vote down vote up
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 vote down vote up
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: MidiUtils.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Binary search for the event indexes of the track
 *
 * @param tick  tick number of index to be found in array
 * @return index in track which is on or after "tick".
 *   if no entries are found that follow after tick, track.size() is returned
 */
public static int tick2index(Track track, long tick) {
    int ret = 0;
    if (tick > 0) {
        int low = 0;
        int high = track.size() - 1;
        while (low < high) {
            // take the middle event as estimate
            ret = (low + high) >> 1;
            // tick of estimate
            long t = track.get(ret).getTick();
            if (t == tick) {
                break;
            } else if (t < tick) {
                // estimate too low
                if (low == high - 1) {
                    // "or after tick"
                    ret++;
                    break;
                }
                low = ret;
            } else { // if (t>tick)
                // estimate too high
                high = ret;
            }
        }
    }
    return ret;
}
 
Example 4
Source File: MidiUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Binary search for the event indexes of the track
 *
 * @param tick  tick number of index to be found in array
 * @return index in track which is on or after "tick".
 *   if no entries are found that follow after tick, track.size() is returned
 */
public static int tick2index(Track track, long tick) {
    int ret = 0;
    if (tick > 0) {
        int low = 0;
        int high = track.size() - 1;
        while (low < high) {
            // take the middle event as estimate
            ret = (low + high) >> 1;
            // tick of estimate
            long t = track.get(ret).getTick();
            if (t == tick) {
                break;
            } else if (t < tick) {
                // estimate too low
                if (low == high - 1) {
                    // "or after tick"
                    ret++;
                    break;
                }
                low = ret;
            } else { // if (t>tick)
                // estimate too high
                high = ret;
            }
        }
    }
    return ret;
}
 
Example 5
Source File: TrackAddSameTick.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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 6
Source File: MidiWaveformSynthesizer.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
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 7
Source File: RealTimeSequencer.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * 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 8
Source File: RealTimeSequencer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 9
Source File: SeqRecordDoesNotCopy.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String argv[]) {
    Sequencer s = null;
    try {
        s = MidiSystem.getSequencer();
        s.open();
    } catch (final MidiUnavailableException ignored) {
        // the test is not applicable
        return;
    }
    try {
        Sequence seq = new Sequence(Sequence.PPQ, 384, 2);
        s.setSequence(seq);
        Track t = seq.getTracks()[0];
        ShortMessage msg = new ShortMessage();
        msg.setMessage(0x90, 0x40, 0x7F);
        t.add(new MidiEvent(msg, 11000));
        msg.setMessage(0x90, 0x40, 0x00);
        t.add(new MidiEvent(msg, 12000));
        t = seq.getTracks()[1];
        s.recordEnable(t, -1);
        System.out.println("Started recording...");
        s.startRecording();
        Receiver r = s.getReceiver();
        Thread.sleep(100);
        // send a normal message
        System.out.println("Recording a normal NOTE ON message...");
        msg.setMessage(0x90, 0x40, 0x6F);
        r.send(msg, -1);
        Thread.sleep(100);
        // send a normal message
        System.out.println("Recording a normal NOTE OFF message...");
        msg.setMessage(0x90, 0x40, 0x00);
        r.send(msg, -1);
        Thread.sleep(100);
        s.stop();
        // now see if the messages were recorded
        System.out.println("Recorded messages:");
        int sameMessage = 0;
        for (int i = 0; i < t.size(); i++) {
            System.out.print(" "+(i+1)+". ");
            printEvent(t.get(i));
            if (t.get(i).getMessage() == msg) {
                System.out.println("## Failed: Same Message reference!");
                sameMessage++;
            }
        }
        if (sameMessage > 0) {
            System.out.println("## Failed: The same instance was recorded!");
            throw new Exception("Test FAILED!");
        }
        System.out.println("Did not detect any duplicate messages.");
        System.out.println("Test passed.");
    } catch (Exception e) {
        System.out.println("Unexpected Exception: "+e);
        //e.printStackTrace();
        throw new RuntimeException("Test FAILED!");
    } finally {
        s.close();
    }
}
 
Example 10
Source File: SlicerTest.java    From mpcmaid with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testMarkers() {
	assertEquals("Slicer: 3.8424037s (169450 samples), 9 markers", slicer.toString());
	System.out.println(markers);

	assertEquals(9, markers.size());

	assertFalse(markers.isUnset());

	Marker marker = markers.getSelectedMarker();
	assertEquals(0, marker.getLocation());
	System.out.println(marker);

	markers.selectMarker(4);
	assertEquals(4, markers.getSelectedMarkerIndex());
	assertEquals(79872, markers.getSelectedMarkerLocation());
	assertEquals(79872, markers.getSelectedMarker().getLocation());

	assertEquals(0, markers.getRangeFrom(0).getFrom());

	final LocationRange range3 = markers.getRangeFrom(3);
	System.out.println(range3);
	final LocationRange range4 = markers.getRangeFrom(4);
	System.out.println(range4);

	assertEquals(3.8424037, markers.getDuration(), 0.000001);
	assertEquals(124.92, markers.getTempo(), 0.01);

	// remove
	markers.deleteSelectedMarker();
	assertEquals(8, markers.size());
	assertEquals(3, markers.getSelectedMarkerIndex());

	final LocationRange range3bis = markers.getRangeFrom(3);
	assertEquals(range3.getFrom(), range3bis.getFrom());
	assertEquals(range4.getTo(), range3bis.getTo());

	// last range extends up to the Frame Length
	final LocationRange range7 = markers.getRangeFrom(7);
	assertEquals(169450, range7.getTo());
	System.out.println(range7);

	final int[] midiTicks = { 0, 32, 97, 129, 190, 222, 288, 320, 381, 413, 478, 510, 575, 607, 673, 705, 705 };
	try {
		final Sequence midiSequence = markers.exportMidiSequence(null, MIDI_PPQ);
		final Track track = midiSequence.getTracks()[0];
		assertEquals(17, track.size());
		for (int i = 0; i < track.size(); i++) {
			// System.out.println(track.get(i).getTick());
			assertEquals(midiTicks[i], track.get(i).getTick());
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
	assertEquals(3, markers.getSelectedMarkerIndex());
	markers.insertMarker();
	assertEquals(4, markers.getSelectedMarkerIndex());
	assertEquals(73727, markers.getLocation(4));
}
 
Example 11
Source File: Midi2WavRenderer.java    From computoser with GNU Affero General Public License v3.0 4 votes vote down vote up
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;
}