Java Code Examples for javax.sound.midi.Sequence#getResolution()

The following examples show how to use javax.sound.midi.Sequence#getResolution() . 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: RealTimeSequencer.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
synchronized void setSequence(Sequence seq) {
    if (seq == null) {
        init();
        return;
    }
    tracks = seq.getTracks();
    muteSoloChanged();
    resolution = seq.getResolution();
    divisionType = seq.getDivisionType();
    trackReadPos = new int[tracks.length];
    // trigger re-initialization
    checkPointMillis = 0;
    needReindex = true;
}
 
Example 2
Source File: RealTimeSequencer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
synchronized void setSequence(Sequence seq) {
    if (seq == null) {
        init();
        return;
    }
    tracks = seq.getTracks();
    muteSoloChanged();
    resolution = seq.getResolution();
    divisionType = seq.getDivisionType();
    trackReadPos = new int[tracks.length];
    // trigger re-initialization
    checkPointMillis = 0;
    needReindex = true;
}
 
Example 3
Source File: MidiUtils.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Given a tick, convert to microsecond
 * @param cache tempo info and current tempo
 */
public static long tick2microsecond(Sequence seq, long tick, TempoCache cache) {
    if (seq.getDivisionType() != Sequence.PPQ ) {
        double seconds = ((double)tick / (double)(seq.getDivisionType() * seq.getResolution()));
        return (long) (1000000 * seconds);
    }

    if (cache == null) {
        cache = new TempoCache(seq);
    }

    int resolution = seq.getResolution();

    long[] ticks = cache.ticks;
    int[] tempos = cache.tempos; // in MPQ
    int cacheCount = tempos.length;

    // optimization to not always go through entire list of tempo events
    int snapshotIndex = cache.snapshotIndex;
    int snapshotMicro = cache.snapshotMicro;

    // walk through all tempo changes and add time for the respective blocks
    long us = 0; // microsecond

    if (snapshotIndex <= 0
        || snapshotIndex >= cacheCount
        || ticks[snapshotIndex] > tick) {
        snapshotMicro = 0;
        snapshotIndex = 0;
    }
    if (cacheCount > 0) {
        // this implementation needs a tempo event at tick 0!
        int i = snapshotIndex + 1;
        while (i < cacheCount && ticks[i] <= tick) {
            snapshotMicro += ticks2microsec(ticks[i] - ticks[i - 1], tempos[i - 1], resolution);
            snapshotIndex = i;
            i++;
        }
        us = snapshotMicro
            + ticks2microsec(tick - ticks[snapshotIndex],
                             tempos[snapshotIndex],
                             resolution);
    }
    cache.snapshotIndex = snapshotIndex;
    cache.snapshotMicro = snapshotMicro;
    return us;
}
 
Example 4
Source File: MidiUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Given a tick, convert to microsecond
 * @param cache tempo info and current tempo
 */
public static long tick2microsecond(Sequence seq, long tick, TempoCache cache) {
    if (seq.getDivisionType() != Sequence.PPQ ) {
        double seconds = ((double)tick / (double)(seq.getDivisionType() * seq.getResolution()));
        return (long) (1000000 * seconds);
    }

    if (cache == null) {
        cache = new TempoCache(seq);
    }

    int resolution = seq.getResolution();

    long[] ticks = cache.ticks;
    int[] tempos = cache.tempos; // in MPQ
    int cacheCount = tempos.length;

    // optimization to not always go through entire list of tempo events
    int snapshotIndex = cache.snapshotIndex;
    int snapshotMicro = cache.snapshotMicro;

    // walk through all tempo changes and add time for the respective blocks
    long us = 0; // microsecond

    if (snapshotIndex <= 0
        || snapshotIndex >= cacheCount
        || ticks[snapshotIndex] > tick) {
        snapshotMicro = 0;
        snapshotIndex = 0;
    }
    if (cacheCount > 0) {
        // this implementation needs a tempo event at tick 0!
        int i = snapshotIndex + 1;
        while (i < cacheCount && ticks[i] <= tick) {
            snapshotMicro += ticks2microsec(ticks[i] - ticks[i - 1], tempos[i - 1], resolution);
            snapshotIndex = i;
            i++;
        }
        us = snapshotMicro
            + ticks2microsec(tick - ticks[snapshotIndex],
                             tempos[snapshotIndex],
                             resolution);
    }
    cache.snapshotIndex = snapshotIndex;
    cache.snapshotMicro = snapshotMicro;
    return us;
}
 
Example 5
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;
}