Path: blob/master/src/java.desktop/share/classes/javax/sound/midi/MetaMessage.java
41159 views
/*1* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.midi;2627/**28* A {@code MetaMessage} is a {@link MidiMessage} that is not meaningful to29* synthesizers, but that can be stored in a MIDI file and interpreted by a30* sequencer program. (See the discussion in the {@code MidiMessage} class31* description.) The Standard MIDI Files specification defines various types of32* meta-events, such as sequence number, lyric, cue point, and set tempo. There33* are also meta-events for such information as lyrics, copyrights, tempo34* indications, time and key signatures, markers, etc. For more information, see35* the Standard MIDI Files 1.0 specification, which is part of the Complete MIDI36* 1.0 Detailed Specification published by the MIDI Manufacturer's Association37* (<a href = http://www.midi.org>http://www.midi.org</a>).38* <p>39* When data is being transported using MIDI wire protocol, a40* {@link ShortMessage} with the status value {@code 0xFF} represents a system41* reset message. In MIDI files, this same status value denotes a42* {@code MetaMessage}. The types of meta-message are distinguished from each43* other by the first byte that follows the status byte {@code 0xFF}. The44* subsequent bytes are data bytes. As with system exclusive messages, there are45* an arbitrary number of data bytes, depending on the type of46* {@code MetaMessage}.47*48* @author David Rivas49* @author Kara Kytle50* @see MetaEventListener51*/52public class MetaMessage extends MidiMessage {5354/**55* Status byte for {@code MetaMessage} (0xFF, or 255), which is used in MIDI56* files. It has the same value as {@link ShortMessage#SYSTEM_RESET}, which57* is used in the real-time "MIDI wire" protocol.58*59* @see MidiMessage#getStatus60*/61public static final int META = 0xFF; // 2556263/**64* The length of the actual message in the data array. This is used to65* determine how many bytes of the data array is the message, and how many66* are the status byte, the type byte, and the variable-length-int67* describing the length of the message.68*/69private int dataLength = 0;7071/**72* Constructs a new {@code MetaMessage}. The contents of the message are not73* set here; use {@link #setMessage(int, byte[], int) setMessage} to set74* them subsequently.75*/76public MetaMessage() {77// Default meta message data: just the META status byte value78this(new byte[]{(byte) META, 0});79}8081/**82* Constructs a new {@code MetaMessage} and sets the message parameters. The83* contents of the message can be changed by using the {@code setMessage}84* method.85*86* @param type meta-message type (must be less than 128)87* @param data the data bytes in the MIDI message88* @param length an amount of bytes in the {@code data} byte array; it89* should be non-negative and less than or equal to90* {@code data.length}91* @throws InvalidMidiDataException if the parameter values do not specify a92* valid MIDI meta message93* @see #setMessage(int, byte[], int)94* @see #getType()95* @see #getData()96* @since 1.797*/98public MetaMessage(int type, byte[] data, int length)99throws InvalidMidiDataException {100super(null);101setMessage(type, data, length); // can throw InvalidMidiDataException102}103104/**105* Constructs a new {@code MetaMessage}.106*107* @param data an array of bytes containing the complete message. The108* message data may be changed using the {@code setMessage} method.109* @see #setMessage110*/111protected MetaMessage(byte[] data) {112super(data);113//$$fb 2001-10-06: need to calculate dataLength. Fix for bug #4511796114if (data.length>=3) {115dataLength=data.length-3;116int pos=2;117while (pos<data.length && (data[pos] & 0x80)!=0) {118dataLength--; pos++;119}120}121}122123/**124* Sets the message parameters for a {@code MetaMessage}. Since only one125* status byte value, {@code 0xFF}, is allowed for meta-messages, it does126* not need to be specified here. Calls to127* {@link MidiMessage#getStatus getStatus} return {@code 0xFF} for all128* meta-messages.129* <p>130* The {@code type} argument should be a valid value for the byte that131* follows the status byte in the {@code MetaMessage}. The {@code data}132* argument should contain all the subsequent bytes of the133* {@code MetaMessage}. In other words, the byte that specifies the type of134* {@code MetaMessage} is not considered a data byte.135*136* @param type meta-message type (must be less than 128)137* @param data the data bytes in the MIDI message138* @param length the number of bytes in the {@code data} byte array139* @throws InvalidMidiDataException if the parameter values do not specify a140* valid MIDI meta message141*/142public void setMessage(int type, byte[] data, int length) throws InvalidMidiDataException {143144if (type >= 128 || type < 0) {145throw new InvalidMidiDataException("Invalid meta event with type " + type);146}147if ((length > 0 && length > data.length) || length < 0) {148throw new InvalidMidiDataException("length out of bounds: "+length);149}150151this.length = 2 + getVarIntLength(length) + length;152this.dataLength = length;153this.data = new byte[this.length];154this.data[0] = (byte) META; // status value for MetaMessages (meta events)155this.data[1] = (byte) type; // MetaMessage type156writeVarInt(this.data, 2, length); // write the length as a variable int157if (length > 0) {158System.arraycopy(data, 0, this.data, this.length - this.dataLength, this.dataLength);159}160}161162/**163* Obtains the type of the {@code MetaMessage}.164*165* @return an integer representing the {@code MetaMessage} type166*/167public int getType() {168if (length>=2) {169return data[1] & 0xFF;170}171return 0;172}173174/**175* Obtains a copy of the data for the meta message. The returned array of176* bytes does not include the status byte or the message length data. The177* length of the data for the meta message is the length of the array. Note178* that the length of the entire message includes the status byte and the179* meta message type byte, and therefore may be longer than the returned180* array.181*182* @return array containing the meta message data183* @see MidiMessage#getLength184*/185public byte[] getData() {186byte[] returnedArray = new byte[dataLength];187System.arraycopy(data, (length - dataLength), returnedArray, 0, dataLength);188return returnedArray;189}190191/**192* Creates a new object of the same class and with the same contents as this193* object.194*195* @return a clone of this instance196*/197@Override198public Object clone() {199byte[] newData = new byte[length];200System.arraycopy(data, 0, newData, 0, newData.length);201return new MetaMessage(newData);202}203204// HELPER METHODS205206private int getVarIntLength(long value) {207int length = 0;208do {209value = value >> 7;210length++;211} while (value > 0);212return length;213}214215private static final long mask = 0x7F;216217private void writeVarInt(byte[] data, int off, long value) {218int shift=63; // number of bitwise left-shifts of mask219// first screen out leading zeros220while ((shift > 0) && ((value & (mask << shift)) == 0)) shift-=7;221// then write actual values222while (shift > 0) {223data[off++]=(byte) (((value & (mask << shift)) >> shift) | 0x80);224shift-=7;225}226data[off] = (byte) (value & mask);227}228}229230231