jdk/src/java.desktop/share/classes/com/sun/media/sound/MidiUtils.java
author serb
Thu, 18 Feb 2016 22:11:29 +0300
changeset 36454 d2853d1fc614
parent 35667 ed476aba94de
child 40444 afabcfc2f3ef
permissions -rw-r--r--
8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV Reviewed-by: prr, amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
27061
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2014, 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 java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
27061
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    30
import javax.sound.midi.MetaMessage;
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    31
import javax.sound.midi.MidiDevice;
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    32
import javax.sound.midi.MidiEvent;
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    33
import javax.sound.midi.MidiMessage;
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    34
import javax.sound.midi.Sequence;
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    35
import javax.sound.midi.Track;
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
// TODO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
// - define and use a global symbolic constant for 60000000 (see convertTempo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Some utilities for MIDI (some stuff is used from javax.sound.midi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    45
public final class MidiUtils {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 27061
diff changeset
    47
    public static final int DEFAULT_TEMPO_MPQ = 500000; // 120bpm
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 27061
diff changeset
    48
    public static final int META_END_OF_TRACK_TYPE = 0x2F;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 27061
diff changeset
    49
    public static final int META_TEMPO_TYPE = 0x51;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    51
    /**
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    52
     * Suppresses default constructor, ensuring non-instantiability.
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    53
     */
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    54
    private MidiUtils() {
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    55
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
27061
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    57
    /**
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    58
     * Returns an exception which should be thrown if MidiDevice is unsupported.
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    59
     *
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    60
     * @param  info an info object that describes the desired device
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    61
     * @return an exception instance
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    62
     */
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    63
    static RuntimeException unsupportedDevice(final MidiDevice.Info info) {
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    64
        return new IllegalArgumentException(String.format(
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    65
                "MidiDevice %s not supported by this provider", info));
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    66
    }
3afa6296f688 8058115: Some of MidiDeviceProviders do not follow the specification
serb
parents: 25859
diff changeset
    67
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /** return true if the passed message is Meta End Of Track */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static boolean isMetaEndOfTrack(MidiMessage midiMsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // first check if it is a META message at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (midiMsg.getLength() != 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            || midiMsg.getStatus() != MetaMessage.META) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // now get message and check for end of track
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        byte[] msg = midiMsg.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return ((msg[1] & 0xFF) == META_END_OF_TRACK_TYPE) && (msg[2] == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /** return if the given message is a meta tempo message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static boolean isMetaTempo(MidiMessage midiMsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // first check if it is a META message at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (midiMsg.getLength() != 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            || midiMsg.getStatus() != MetaMessage.META) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // now get message and check for tempo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        byte[] msg = midiMsg.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // meta type must be 0x51, and data length must be 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return ((msg[1] & 0xFF) == META_TEMPO_TYPE) && (msg[2] == 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /** parses this message for a META tempo message and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * the tempo in MPQ, or -1 if this isn't a tempo message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static int getTempoMPQ(MidiMessage midiMsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // first check if it is a META message at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (midiMsg.getLength() != 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            || midiMsg.getStatus() != MetaMessage.META) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        byte[] msg = midiMsg.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (((msg[1] & 0xFF) != META_TEMPO_TYPE) || (msg[2] != 3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int tempo =    (msg[5] & 0xFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    | ((msg[4] & 0xFF) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    | ((msg[3] & 0xFF) << 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return tempo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
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
     * converts<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * 1 - MPQ-Tempo to BPM tempo<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * 2 - BPM tempo to MPQ tempo<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static double convertTempo(double tempo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (tempo <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            tempo = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return ((double) 60000000l) / tempo;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * convert tick to microsecond with given tempo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Does not take tempo changes into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Does not work for SMPTE timing!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static long ticks2microsec(long tick, double tempoMPQ, int resolution) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return (long) (((double) tick) * tempoMPQ / resolution);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * convert tempo to microsecond with given tempo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Does not take tempo changes into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Does not work for SMPTE timing!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public static long microsec2ticks(long us, double tempoMPQ, int resolution) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // do not round to nearest tick
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        //return (long) Math.round((((double)us) * resolution) / tempoMPQ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return (long) ((((double)us) * resolution) / tempoMPQ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Given a tick, convert to microsecond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param cache tempo info and current tempo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static long tick2microsecond(Sequence seq, long tick, TempoCache cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (seq.getDivisionType() != Sequence.PPQ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            double seconds = ((double)tick / (double)(seq.getDivisionType() * seq.getResolution()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return (long) (1000000 * seconds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            cache = new TempoCache(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int resolution = seq.getResolution();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        long[] ticks = cache.ticks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int[] tempos = cache.tempos; // in MPQ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        int cacheCount = tempos.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // optimization to not always go through entire list of tempo events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int snapshotIndex = cache.snapshotIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int snapshotMicro = cache.snapshotMicro;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // walk through all tempo changes and add time for the respective blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        long us = 0; // microsecond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (snapshotIndex <= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            || snapshotIndex >= cacheCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            || ticks[snapshotIndex] > tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            snapshotMicro = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            snapshotIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (cacheCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // this implementation needs a tempo event at tick 0!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            int i = snapshotIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            while (i < cacheCount && ticks[i] <= tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                snapshotMicro += ticks2microsec(ticks[i] - ticks[i - 1], tempos[i - 1], resolution);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                snapshotIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            us = snapshotMicro
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                + ticks2microsec(tick - ticks[snapshotIndex],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                 tempos[snapshotIndex],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                 resolution);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        cache.snapshotIndex = snapshotIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        cache.snapshotMicro = snapshotMicro;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return us;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Given a microsecond time, convert to tick.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * returns tempo at the given time in cache.getCurrTempoMPQ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public static long microsecond2tick(Sequence seq, long micros, TempoCache cache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (seq.getDivisionType() != Sequence.PPQ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            double dTick = ( ((double) micros)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                           * ((double) seq.getDivisionType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                           * ((double) seq.getResolution()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                           / ((double) 1000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            long tick = (long) dTick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (cache != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                cache.currTempo = (int) cache.getTempoMPQAt(tick);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return tick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            cache = new TempoCache(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        long[] ticks = cache.ticks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        int[] tempos = cache.tempos; // in MPQ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int cacheCount = tempos.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int resolution = seq.getResolution();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        long us = 0; long tick = 0; int newReadPos = 0; int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // walk through all tempo changes and add time for the respective blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // to find the right tick
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (micros > 0 && cacheCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // this loop requires that the first tempo Event is at time 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            while (i < cacheCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                long nextTime = us + ticks2microsec(ticks[i] - ticks[i - 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                                    tempos[i - 1], resolution);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                if (nextTime > micros) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                us = nextTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            tick = ticks[i - 1] + microsec2ticks(micros - us, tempos[i - 1], resolution);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (Printer.debug) Printer.debug("microsecond2tick(" + (micros / 1000)+") = "+tick+" ticks.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            //if (Printer.debug) Printer.debug("   -> convert back = " + (tick2microsecond(seq, tick, null) / 1000)+" microseconds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        cache.currTempo = tempos[i - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return tick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Binary search for the event indexes of the track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32865
diff changeset
   253
     * @param tick  tick number of index to be found in array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return index in track which is on or after "tick".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *   if no entries are found that follow after tick, track.size() is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public static int tick2index(Track track, long tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (tick > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            int low = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            int high = track.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            while (low < high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                // take the middle event as estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                ret = (low + high) >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                // tick of estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                long t = track.get(ret).getTick();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                if (t == tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                } else if (t < tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    // estimate too low
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    if (low == high - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        // "or after tick"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        ret++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    low = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                } else { // if (t>tick)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    // estimate too high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    high = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   287
    public static final class TempoCache {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        long[] ticks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        int[] tempos; // in MPQ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // index in ticks/tempos at the snapshot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        int snapshotIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // microsecond at the snapshot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int snapshotMicro = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int currTempo; // MPQ, used as return value for microsecond2tick
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        private boolean firstTempoIsFake = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        public TempoCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // just some defaults, to prevents weird stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            ticks = new long[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            tempos = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            tempos[0] = DEFAULT_TEMPO_MPQ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            snapshotIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            snapshotMicro = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        public TempoCache(Sequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            refresh(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        public synchronized void refresh(Sequence seq) {
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 18215
diff changeset
   315
            ArrayList<MidiEvent> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            Track[] tracks = seq.getTracks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (tracks.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                // tempo events only occur in track 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                Track track = tracks[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                int c = track.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                for (int i = 0; i < c; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    MidiEvent ev = track.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    MidiMessage msg = ev.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    if (isMetaTempo(msg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        // found a tempo event. Add it to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        list.add(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            int size = list.size() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            firstTempoIsFake = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if ((size > 1)
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 18215
diff changeset
   333
                && (list.get(0).getTick() == 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                // do not need to add an initial tempo event at the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                firstTempoIsFake = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            ticks  = new long[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            tempos = new int[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            int e = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (firstTempoIsFake) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                // add tempo 120 at beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                ticks[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                tempos[0] = DEFAULT_TEMPO_MPQ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                e++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            for (int i = 0; i < list.size(); i++, e++) {
24548
9c007a986347 8042256: Fix raw and unchecked lint warnings in com.sun.media.sound
darcy
parents: 18215
diff changeset
   348
                MidiEvent evt = list.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                ticks[e] = evt.getTick();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                tempos[e] = getTempoMPQ(evt.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            snapshotIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            snapshotMicro = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        public int getCurrTempoMPQ() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return currTempo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        float getTempoMPQAt(long tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return getTempoMPQAt(tick, -1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        synchronized float getTempoMPQAt(long tick, float startTempoMPQ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            for (int i = 0; i < ticks.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                if (ticks[i] > tick) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    if (i > 0) i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    if (startTempoMPQ > 0 && i == 0 && firstTempoIsFake) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        return startTempoMPQ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    return (float) tempos[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return tempos[tempos.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
}