Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sound/midi/MetaMessage.java
38830 views
/*1* Copyright (c) 1999, 2013, 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;262728/**29* A <code>MetaMessage</code> is a <code>{@link MidiMessage}</code> that is not meaningful to synthesizers, but30* that can be stored in a MIDI file and interpreted by a sequencer program.31* (See the discussion in the <code>MidiMessage</code>32* class description.) The Standard MIDI Files specification defines33* various types of meta-events, such as sequence number, lyric, cue point,34* and set tempo. There are also meta-events35* for such information as lyrics, copyrights, tempo indications, time and key36* signatures, markers, etc. For more information, see the Standard MIDI Files 1.037* specification, which is part of the Complete MIDI 1.0 Detailed Specification38* published by the MIDI Manufacturer's Association39* (<a href = http://www.midi.org>http://www.midi.org</a>).40*41* <p>42* When data is being transported using MIDI wire protocol,43* a <code>{@link ShortMessage}</code> with the status value <code>0xFF</code> represents44* a system reset message. In MIDI files, this same status value denotes a <code>MetaMessage</code>.45* The types of meta-message are distinguished from each other by the first byte46* that follows the status byte <code>0xFF</code>. The subsequent bytes are data47* bytes. As with system exclusive messages, there are an arbitrary number of48* data bytes, depending on the type of <code>MetaMessage</code>.49*50* @see MetaEventListener51*52* @author David Rivas53* @author Kara Kytle54*/5556public class MetaMessage extends MidiMessage {575859// Status byte defines6061/**62* Status byte for <code>MetaMessage</code> (0xFF, or 255), which is used63* in MIDI files. It has the same value as SYSTEM_RESET, which64* is used in the real-time "MIDI wire" protocol.65* @see MidiMessage#getStatus66*/67public static final int META = 0xFF; // 2556869// Instance variables7071/**72* The length of the actual message in the data array.73* This is used to determine how many bytes of the data array74* is the message, and how many are the status byte, the75* type byte, and the variable-length-int describing the76* length of the message.77*/78private int dataLength = 0;798081/**82* Constructs a new <code>MetaMessage</code>. The contents of83* the message are not set here; use84* {@link #setMessage(int, byte[], int) setMessage}85* to set them subsequently.86*/87public MetaMessage() {88// Default meta message data: just the META status byte value89this(new byte[]{(byte) META, 0});90}9192/**93* Constructs a new {@code MetaMessage} and sets the message parameters.94* The contents of the message can be changed by using95* the {@code setMessage} method.96*97* @param type meta-message type (must be less than 128)98* @param data the data bytes in the MIDI message99* @param length an amount of bytes in the {@code data} byte array;100* it should be non-negative and less than or equal to101* {@code data.length}102* @throws InvalidMidiDataException if the parameter values do not specify103* a valid MIDI meta message104* @see #setMessage(int, byte[], int)105* @see #getType()106* @see #getData()107* @since 1.7108*/109public MetaMessage(int type, byte[] data, int length)110throws InvalidMidiDataException {111super(null);112setMessage(type, data, length); // can throw InvalidMidiDataException113}114115116/**117* Constructs a new <code>MetaMessage</code>.118* @param data an array of bytes containing the complete message.119* The message data may be changed using the <code>setMessage</code>120* method.121* @see #setMessage122*/123protected MetaMessage(byte[] data) {124super(data);125//$$fb 2001-10-06: need to calculate dataLength. Fix for bug #4511796126if (data.length>=3) {127dataLength=data.length-3;128int pos=2;129while (pos<data.length && (data[pos] & 0x80)!=0) {130dataLength--; pos++;131}132}133}134135136/**137* Sets the message parameters for a <code>MetaMessage</code>.138* Since only one status byte value, <code>0xFF</code>, is allowed for meta-messages,139* it does not need to be specified here. Calls to <code>{@link MidiMessage#getStatus getStatus}</code> return140* <code>0xFF</code> for all meta-messages.141* <p>142* The <code>type</code> argument should be a valid value for the byte that143* follows the status byte in the <code>MetaMessage</code>. The <code>data</code> argument144* should contain all the subsequent bytes of the <code>MetaMessage</code>. In other words,145* the byte that specifies the type of <code>MetaMessage</code> is not considered a data byte.146*147* @param type meta-message type (must be less than 128)148* @param data the data bytes in the MIDI message149* @param length the number of bytes in the <code>data</code>150* byte array151* @throws InvalidMidiDataException if the152* parameter values do not specify a valid MIDI meta message153*/154public void setMessage(int type, byte[] data, int length) throws InvalidMidiDataException {155156if (type >= 128 || type < 0) {157throw new InvalidMidiDataException("Invalid meta event with type " + type);158}159if ((length > 0 && length > data.length) || length < 0) {160throw new InvalidMidiDataException("length out of bounds: "+length);161}162163this.length = 2 + getVarIntLength(length) + length;164this.dataLength = length;165this.data = new byte[this.length];166this.data[0] = (byte) META; // status value for MetaMessages (meta events)167this.data[1] = (byte) type; // MetaMessage type168writeVarInt(this.data, 2, length); // write the length as a variable int169if (length > 0) {170System.arraycopy(data, 0, this.data, this.length - this.dataLength, this.dataLength);171}172}173174175/**176* Obtains the type of the <code>MetaMessage</code>.177* @return an integer representing the <code>MetaMessage</code> type178*/179public int getType() {180if (length>=2) {181return data[1] & 0xFF;182}183return 0;184}185186187188/**189* Obtains a copy of the data for the meta message. The returned190* array of bytes does not include the status byte or the message191* length data. The length of the data for the meta message is192* the length of the array. Note that the length of the entire193* message includes the status byte and the meta message type194* byte, and therefore may be longer than the returned array.195* @return array containing the meta message data.196* @see MidiMessage#getLength197*/198public byte[] getData() {199byte[] returnedArray = new byte[dataLength];200System.arraycopy(data, (length - dataLength), returnedArray, 0, dataLength);201return returnedArray;202}203204205/**206* Creates a new object of the same class and with the same contents207* as this object.208* @return a clone of this instance209*/210public Object clone() {211byte[] newData = new byte[length];212System.arraycopy(data, 0, newData, 0, newData.length);213214MetaMessage event = new MetaMessage(newData);215return event;216}217218// HELPER METHODS219220private int getVarIntLength(long value) {221int length = 0;222do {223value = value >> 7;224length++;225} while (value > 0);226return length;227}228229private final static long mask = 0x7F;230231private void writeVarInt(byte[] data, int off, long value) {232int shift=63; // number of bitwise left-shifts of mask233// first screen out leading zeros234while ((shift > 0) && ((value & (mask << shift)) == 0)) shift-=7;235// then write actual values236while (shift > 0) {237data[off++]=(byte) (((value & (mask << shift)) >> shift) | 0x80);238shift-=7;239}240data[off] = (byte) (value & mask);241}242243}244245246