0N/A/*
6321N/A * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.media.sound;
0N/A
0N/Aimport javax.sound.midi.*;
0N/Aimport java.util.ArrayList;
0N/A
0N/A// TODO:
0N/A// - define and use a global symbolic constant for 60000000 (see convertTempo)
0N/A
0N/A/**
0N/A * Some utilities for MIDI (some stuff is used from javax.sound.midi)
0N/A *
0N/A * @author Florian Bomers
0N/A */
6321N/Apublic final class MidiUtils {
0N/A
0N/A public final static int DEFAULT_TEMPO_MPQ = 500000; // 120bpm
0N/A public final static int META_END_OF_TRACK_TYPE = 0x2F;
0N/A public final static int META_TEMPO_TYPE = 0x51;
0N/A
6321N/A /**
6321N/A * Suppresses default constructor, ensuring non-instantiability.
6321N/A */
6321N/A private MidiUtils() {
6321N/A }
0N/A
0N/A /** return true if the passed message is Meta End Of Track */
0N/A public static boolean isMetaEndOfTrack(MidiMessage midiMsg) {
0N/A // first check if it is a META message at all
0N/A if (midiMsg.getLength() != 3
0N/A || midiMsg.getStatus() != MetaMessage.META) {
0N/A return false;
0N/A }
0N/A // now get message and check for end of track
0N/A byte[] msg = midiMsg.getMessage();
0N/A return ((msg[1] & 0xFF) == META_END_OF_TRACK_TYPE) && (msg[2] == 0);
0N/A }
0N/A
0N/A
0N/A /** return if the given message is a meta tempo message */
0N/A public static boolean isMetaTempo(MidiMessage midiMsg) {
0N/A // first check if it is a META message at all
0N/A if (midiMsg.getLength() != 6
0N/A || midiMsg.getStatus() != MetaMessage.META) {
0N/A return false;
0N/A }
0N/A // now get message and check for tempo
0N/A byte[] msg = midiMsg.getMessage();
0N/A // meta type must be 0x51, and data length must be 3
0N/A return ((msg[1] & 0xFF) == META_TEMPO_TYPE) && (msg[2] == 3);
0N/A }
0N/A
0N/A
0N/A /** parses this message for a META tempo message and returns
0N/A * the tempo in MPQ, or -1 if this isn't a tempo message
0N/A */
0N/A public static int getTempoMPQ(MidiMessage midiMsg) {
0N/A // first check if it is a META message at all
0N/A if (midiMsg.getLength() != 6
0N/A || midiMsg.getStatus() != MetaMessage.META) {
0N/A return -1;
0N/A }
0N/A byte[] msg = midiMsg.getMessage();
0N/A if (((msg[1] & 0xFF) != META_TEMPO_TYPE) || (msg[2] != 3)) {
0N/A return -1;
0N/A }
0N/A int tempo = (msg[5] & 0xFF)
0N/A | ((msg[4] & 0xFF) << 8)
0N/A | ((msg[3] & 0xFF) << 16);
0N/A return tempo;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * converts<br>
0N/A * 1 - MPQ-Tempo to BPM tempo<br>
0N/A * 2 - BPM tempo to MPQ tempo<br>
0N/A */
0N/A public static double convertTempo(double tempo) {
0N/A if (tempo <= 0) {
0N/A tempo = 1;
0N/A }
0N/A return ((double) 60000000l) / tempo;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * convert tick to microsecond with given tempo.
0N/A * Does not take tempo changes into account.
0N/A * Does not work for SMPTE timing!
0N/A */
0N/A public static long ticks2microsec(long tick, double tempoMPQ, int resolution) {
0N/A return (long) (((double) tick) * tempoMPQ / resolution);
0N/A }
0N/A
0N/A /**
0N/A * convert tempo to microsecond with given tempo
0N/A * Does not take tempo changes into account.
0N/A * Does not work for SMPTE timing!
0N/A */
0N/A public static long microsec2ticks(long us, double tempoMPQ, int resolution) {
0N/A // do not round to nearest tick
0N/A //return (long) Math.round((((double)us) * resolution) / tempoMPQ);
0N/A return (long) ((((double)us) * resolution) / tempoMPQ);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Given a tick, convert to microsecond
0N/A * @param cache tempo info and current tempo
0N/A */
0N/A public static long tick2microsecond(Sequence seq, long tick, TempoCache cache) {
0N/A if (seq.getDivisionType() != Sequence.PPQ ) {
0N/A double seconds = ((double)tick / (double)(seq.getDivisionType() * seq.getResolution()));
0N/A return (long) (1000000 * seconds);
0N/A }
0N/A
0N/A if (cache == null) {
0N/A cache = new TempoCache(seq);
0N/A }
0N/A
0N/A int resolution = seq.getResolution();
0N/A
0N/A long[] ticks = cache.ticks;
0N/A int[] tempos = cache.tempos; // in MPQ
0N/A int cacheCount = tempos.length;
0N/A
0N/A // optimization to not always go through entire list of tempo events
0N/A int snapshotIndex = cache.snapshotIndex;
0N/A int snapshotMicro = cache.snapshotMicro;
0N/A
0N/A // walk through all tempo changes and add time for the respective blocks
0N/A long us = 0; // microsecond
0N/A
0N/A if (snapshotIndex <= 0
0N/A || snapshotIndex >= cacheCount
0N/A || ticks[snapshotIndex] > tick) {
0N/A snapshotMicro = 0;
0N/A snapshotIndex = 0;
0N/A }
0N/A if (cacheCount > 0) {
0N/A // this implementation needs a tempo event at tick 0!
0N/A int i = snapshotIndex + 1;
0N/A while (i < cacheCount && ticks[i] <= tick) {
0N/A snapshotMicro += ticks2microsec(ticks[i] - ticks[i - 1], tempos[i - 1], resolution);
0N/A snapshotIndex = i;
0N/A i++;
0N/A }
0N/A us = snapshotMicro
0N/A + ticks2microsec(tick - ticks[snapshotIndex],
0N/A tempos[snapshotIndex],
0N/A resolution);
0N/A }
0N/A cache.snapshotIndex = snapshotIndex;
0N/A cache.snapshotMicro = snapshotMicro;
0N/A return us;
0N/A }
0N/A
0N/A /**
0N/A * Given a microsecond time, convert to tick.
0N/A * returns tempo at the given time in cache.getCurrTempoMPQ
0N/A */
0N/A public static long microsecond2tick(Sequence seq, long micros, TempoCache cache) {
0N/A if (seq.getDivisionType() != Sequence.PPQ ) {
0N/A double dTick = ( ((double) micros)
0N/A * ((double) seq.getDivisionType())
0N/A * ((double) seq.getResolution()))
0N/A / ((double) 1000000);
0N/A long tick = (long) dTick;
0N/A if (cache != null) {
0N/A cache.currTempo = (int) cache.getTempoMPQAt(tick);
0N/A }
0N/A return tick;
0N/A }
0N/A
0N/A if (cache == null) {
0N/A cache = new TempoCache(seq);
0N/A }
0N/A long[] ticks = cache.ticks;
0N/A int[] tempos = cache.tempos; // in MPQ
0N/A int cacheCount = tempos.length;
0N/A
0N/A int resolution = seq.getResolution();
0N/A
0N/A long us = 0; long tick = 0; int newReadPos = 0; int i = 1;
0N/A
0N/A // walk through all tempo changes and add time for the respective blocks
0N/A // to find the right tick
0N/A if (micros > 0 && cacheCount > 0) {
0N/A // this loop requires that the first tempo Event is at time 0
0N/A while (i < cacheCount) {
0N/A long nextTime = us + ticks2microsec(ticks[i] - ticks[i - 1],
0N/A tempos[i - 1], resolution);
0N/A if (nextTime > micros) {
0N/A break;
0N/A }
0N/A us = nextTime;
0N/A i++;
0N/A }
0N/A tick = ticks[i - 1] + microsec2ticks(micros - us, tempos[i - 1], resolution);
0N/A if (Printer.debug) Printer.debug("microsecond2tick(" + (micros / 1000)+") = "+tick+" ticks.");
0N/A //if (Printer.debug) Printer.debug(" -> convert back = " + (tick2microsecond(seq, tick, null) / 1000)+" microseconds");
0N/A }
0N/A cache.currTempo = tempos[i - 1];
0N/A return tick;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Binary search for the event indexes of the track
0N/A *
0N/A * @param tick - tick number of index to be found in array
0N/A * @return index in track which is on or after "tick".
0N/A * if no entries are found that follow after tick, track.size() is returned
0N/A */
0N/A public static int tick2index(Track track, long tick) {
0N/A int ret = 0;
0N/A if (tick > 0) {
0N/A int low = 0;
0N/A int high = track.size() - 1;
0N/A while (low < high) {
0N/A // take the middle event as estimate
0N/A ret = (low + high) >> 1;
0N/A // tick of estimate
0N/A long t = track.get(ret).getTick();
0N/A if (t == tick) {
0N/A break;
0N/A } else if (t < tick) {
0N/A // estimate too low
0N/A if (low == high - 1) {
0N/A // "or after tick"
0N/A ret++;
0N/A break;
0N/A }
0N/A low = ret;
0N/A } else { // if (t>tick)
0N/A // estimate too high
0N/A high = ret;
0N/A }
0N/A }
0N/A }
0N/A return ret;
0N/A }
0N/A
0N/A
6321N/A public static final class TempoCache {
0N/A long[] ticks;
0N/A int[] tempos; // in MPQ
0N/A // index in ticks/tempos at the snapshot
0N/A int snapshotIndex = 0;
0N/A // microsecond at the snapshot
0N/A int snapshotMicro = 0;
0N/A
0N/A int currTempo; // MPQ, used as return value for microsecond2tick
0N/A
0N/A private boolean firstTempoIsFake = false;
0N/A
0N/A public TempoCache() {
0N/A // just some defaults, to prevents weird stuff
0N/A ticks = new long[1];
0N/A tempos = new int[1];
0N/A tempos[0] = DEFAULT_TEMPO_MPQ;
0N/A snapshotIndex = 0;
0N/A snapshotMicro = 0;
0N/A }
0N/A
0N/A public TempoCache(Sequence seq) {
0N/A this();
0N/A refresh(seq);
0N/A }
0N/A
0N/A
0N/A public synchronized void refresh(Sequence seq) {
0N/A ArrayList list = new ArrayList();
0N/A Track[] tracks = seq.getTracks();
0N/A if (tracks.length > 0) {
0N/A // tempo events only occur in track 0
0N/A Track track = tracks[0];
0N/A int c = track.size();
0N/A for (int i = 0; i < c; i++) {
0N/A MidiEvent ev = track.get(i);
0N/A MidiMessage msg = ev.getMessage();
0N/A if (isMetaTempo(msg)) {
0N/A // found a tempo event. Add it to the list
0N/A list.add(ev);
0N/A }
0N/A }
0N/A }
0N/A int size = list.size() + 1;
0N/A firstTempoIsFake = true;
0N/A if ((size > 1)
0N/A && (((MidiEvent) list.get(0)).getTick() == 0)) {
0N/A // do not need to add an initial tempo event at the beginning
0N/A size--;
0N/A firstTempoIsFake = false;
0N/A }
0N/A ticks = new long[size];
0N/A tempos = new int[size];
0N/A int e = 0;
0N/A if (firstTempoIsFake) {
0N/A // add tempo 120 at beginning
0N/A ticks[0] = 0;
0N/A tempos[0] = DEFAULT_TEMPO_MPQ;
0N/A e++;
0N/A }
0N/A for (int i = 0; i < list.size(); i++, e++) {
0N/A MidiEvent evt = (MidiEvent) list.get(i);
0N/A ticks[e] = evt.getTick();
0N/A tempos[e] = getTempoMPQ(evt.getMessage());
0N/A }
0N/A snapshotIndex = 0;
0N/A snapshotMicro = 0;
0N/A }
0N/A
0N/A public int getCurrTempoMPQ() {
0N/A return currTempo;
0N/A }
0N/A
0N/A float getTempoMPQAt(long tick) {
0N/A return getTempoMPQAt(tick, -1.0f);
0N/A }
0N/A
0N/A synchronized float getTempoMPQAt(long tick, float startTempoMPQ) {
0N/A for (int i = 0; i < ticks.length; i++) {
0N/A if (ticks[i] > tick) {
0N/A if (i > 0) i--;
0N/A if (startTempoMPQ > 0 && i == 0 && firstTempoIsFake) {
0N/A return startTempoMPQ;
0N/A }
0N/A return (float) tempos[i];
0N/A }
0N/A }
0N/A return tempos[tempos.length - 1];
0N/A }
0N/A
0N/A }
0N/A}