jdk/src/share/classes/com/sun/media/sound/MidiUtils.java
author amenkov
Wed, 06 Apr 2011 15:12:33 +0400
changeset 9215 cab45ca6ab44
parent 5506 202f599c92aa
child 18215 b2afd66ce6db
permissions -rw-r--r--
6992523: FindBugs scan - Malicious code vulnerability Warnings in com.sun.media.sound.* Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.sound.midi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
// TODO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
// - define and use a global symbolic constant for 60000000 (see convertTempo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Some utilities for MIDI (some stuff is used from javax.sound.midi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class MidiUtils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public final static int DEFAULT_TEMPO_MPQ = 500000; // 120bpm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public final static int META_END_OF_TRACK_TYPE = 0x2F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public final static int META_TEMPO_TYPE = 0x51;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /** return true if the passed message is Meta End Of Track */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static boolean isMetaEndOfTrack(MidiMessage midiMsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        // first check if it is a META message at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (midiMsg.getLength() != 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            || midiMsg.getStatus() != MetaMessage.META) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // now get message and check for end of track
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        byte[] msg = midiMsg.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return ((msg[1] & 0xFF) == META_END_OF_TRACK_TYPE) && (msg[2] == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** return if the given message is a meta tempo message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static boolean isMetaTempo(MidiMessage midiMsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // first check if it is a META message at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (midiMsg.getLength() != 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            || midiMsg.getStatus() != MetaMessage.META) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // now get message and check for tempo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        byte[] msg = midiMsg.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // meta type must be 0x51, and data length must be 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return ((msg[1] & 0xFF) == META_TEMPO_TYPE) && (msg[2] == 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /** parses this message for a META tempo message and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * the tempo in MPQ, or -1 if this isn't a tempo message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static int getTempoMPQ(MidiMessage midiMsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // first check if it is a META message at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (midiMsg.getLength() != 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            || midiMsg.getStatus() != MetaMessage.META) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        byte[] msg = midiMsg.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (((msg[1] & 0xFF) != META_TEMPO_TYPE) || (msg[2] != 3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int tempo =    (msg[5] & 0xFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    | ((msg[4] & 0xFF) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    | ((msg[3] & 0xFF) << 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return tempo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * converts<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * 1 - MPQ-Tempo to BPM tempo<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * 2 - BPM tempo to MPQ tempo<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static double convertTempo(double tempo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (tempo <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            tempo = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return ((double) 60000000l) / tempo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * convert tick to microsecond with given tempo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Does not take tempo changes into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Does not work for SMPTE timing!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static long ticks2microsec(long tick, double tempoMPQ, int resolution) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return (long) (((double) tick) * tempoMPQ / resolution);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * convert tempo to microsecond with given tempo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Does not take tempo changes into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Does not work for SMPTE timing!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static long microsec2ticks(long us, double tempoMPQ, int resolution) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // do not round to nearest tick
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        //return (long) Math.round((((double)us) * resolution) / tempoMPQ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return (long) ((((double)us) * resolution) / tempoMPQ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Given a tick, convert to microsecond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param cache tempo info and current tempo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static long tick2microsecond(Sequence seq, long tick, TempoCache cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (seq.getDivisionType() != Sequence.PPQ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            double seconds = ((double)tick / (double)(seq.getDivisionType() * seq.getResolution()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return (long) (1000000 * seconds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            cache = new TempoCache(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int resolution = seq.getResolution();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        long[] ticks = cache.ticks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int[] tempos = cache.tempos; // in MPQ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int cacheCount = tempos.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // optimization to not always go through entire list of tempo events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int snapshotIndex = cache.snapshotIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        int snapshotMicro = cache.snapshotMicro;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // walk through all tempo changes and add time for the respective blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        long us = 0; // microsecond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (snapshotIndex <= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            || snapshotIndex >= cacheCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            || ticks[snapshotIndex] > tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            snapshotMicro = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            snapshotIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (cacheCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // this implementation needs a tempo event at tick 0!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            int i = snapshotIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            while (i < cacheCount && ticks[i] <= tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                snapshotMicro += ticks2microsec(ticks[i] - ticks[i - 1], tempos[i - 1], resolution);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                snapshotIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            us = snapshotMicro
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                + ticks2microsec(tick - ticks[snapshotIndex],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                 tempos[snapshotIndex],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                 resolution);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        cache.snapshotIndex = snapshotIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        cache.snapshotMicro = snapshotMicro;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return us;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Given a microsecond time, convert to tick.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * returns tempo at the given time in cache.getCurrTempoMPQ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static long microsecond2tick(Sequence seq, long micros, TempoCache cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (seq.getDivisionType() != Sequence.PPQ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            double dTick = ( ((double) micros)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                           * ((double) seq.getDivisionType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                           * ((double) seq.getResolution()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                           / ((double) 1000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            long tick = (long) dTick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (cache != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                cache.currTempo = (int) cache.getTempoMPQAt(tick);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return tick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            cache = new TempoCache(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        long[] ticks = cache.ticks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int[] tempos = cache.tempos; // in MPQ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int cacheCount = tempos.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int resolution = seq.getResolution();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        long us = 0; long tick = 0; int newReadPos = 0; int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // walk through all tempo changes and add time for the respective blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // to find the right tick
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (micros > 0 && cacheCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // this loop requires that the first tempo Event is at time 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            while (i < cacheCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                long nextTime = us + ticks2microsec(ticks[i] - ticks[i - 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                                    tempos[i - 1], resolution);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (nextTime > micros) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                us = nextTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            tick = ticks[i - 1] + microsec2ticks(micros - us, tempos[i - 1], resolution);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (Printer.debug) Printer.debug("microsecond2tick(" + (micros / 1000)+") = "+tick+" ticks.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            //if (Printer.debug) Printer.debug("   -> convert back = " + (tick2microsecond(seq, tick, null) / 1000)+" microseconds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        cache.currTempo = tempos[i - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return tick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Binary search for the event indexes of the track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param tick - tick number of index to be found in array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return index in track which is on or after "tick".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *   if no entries are found that follow after tick, track.size() is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static int tick2index(Track track, long tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (tick > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            int high = track.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            while (low < high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                // take the middle event as estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                ret = (low + high) >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                // tick of estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                long t = track.get(ret).getTick();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                if (t == tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                } else if (t < tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    // estimate too low
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    if (low == high - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        // "or after tick"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        ret++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    low = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                } else { // if (t>tick)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    // estimate too high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    high = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public static class TempoCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        long[] ticks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        int[] tempos; // in MPQ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // index in ticks/tempos at the snapshot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int snapshotIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // microsecond at the snapshot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int snapshotMicro = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int currTempo; // MPQ, used as return value for microsecond2tick
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        private boolean firstTempoIsFake = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        public TempoCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // just some defaults, to prevents weird stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            ticks = new long[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            tempos = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            tempos[0] = DEFAULT_TEMPO_MPQ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            snapshotIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            snapshotMicro = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        public TempoCache(Sequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            refresh(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        public synchronized void refresh(Sequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            ArrayList list = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            Track[] tracks = seq.getTracks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (tracks.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                // tempo events only occur in track 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                Track track = tracks[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                int c = track.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                for (int i = 0; i < c; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    MidiEvent ev = track.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    MidiMessage msg = ev.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    if (isMetaTempo(msg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        // found a tempo event. Add it to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        list.add(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            int size = list.size() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            firstTempoIsFake = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if ((size > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                && (((MidiEvent) list.get(0)).getTick() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                // do not need to add an initial tempo event at the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                firstTempoIsFake = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            ticks  = new long[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            tempos = new int[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            int e = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (firstTempoIsFake) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                // add tempo 120 at beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                ticks[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                tempos[0] = DEFAULT_TEMPO_MPQ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                e++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            for (int i = 0; i < list.size(); i++, e++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                MidiEvent evt = (MidiEvent) list.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                ticks[e] = evt.getTick();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                tempos[e] = getTempoMPQ(evt.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            snapshotIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            snapshotMicro = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        public int getCurrTempoMPQ() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return currTempo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        float getTempoMPQAt(long tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return getTempoMPQAt(tick, -1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        synchronized float getTempoMPQAt(long tick, float startTempoMPQ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            for (int i = 0; i < ticks.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                if (ticks[i] > tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    if (i > 0) i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    if (startTempoMPQ > 0 && i == 0 && firstTempoIsFake) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        return startTempoMPQ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    return (float) tempos[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return tempos[tempos.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}