Source for javax.sound.midi.MetaMessage

   1: /* MetaMessage.java -- A meta message for MIDI files.
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.sound.midi;
  40: 
  41: /**
  42:  * A system exclusive MIDI message.
  43:  * 
  44:  * @author Anthony Green (green@redhat.com)
  45:  * @since 1.3
  46:  *
  47:  */
  48: public class MetaMessage extends MidiMessage
  49: {
  50:   /**
  51:    * The META status code.  Only valid for MIDI files, not the wire protocol.
  52:    */
  53:   public static final int META = 0xFF;
  54:   
  55:   // The length of the variable length data length encoding.
  56:   private int lengthLength = 0;
  57:   
  58:   /**
  59:    * Create a default valid meta message.
  60:    * 
  61:    * The official specs don't specify what message is to be
  62:    * created.  For now, we create a zero length meta message 
  63:    * with a type code of 0.
  64:    */
  65:   public MetaMessage()
  66:   {
  67:     super(new byte[4]);
  68:     data[0] = (byte) META;
  69:     data[1] = (byte) 0; // Type
  70:     data[2] = (byte) 1; // Length length
  71:     data[3] = (byte) 0; // Length
  72:     lengthLength = 1;
  73:   }
  74:   
  75:   /**
  76:    * Create a MetaMessage object.
  77:    * @param data a complete system exclusive message
  78:    */
  79:   protected MetaMessage(byte[] data)
  80:   {
  81:     super(data);
  82:     int index = 2;
  83:     lengthLength = 1;
  84:     while ((data[index++] & 0x80) > 0)
  85:       lengthLength++;
  86:   }
  87:   
  88:   /**
  89:    * Set the meta message.
  90:    *  
  91:    * @param type the meta type byte (< 128)
  92:    * @param data the message data
  93:    * @param length the length of the message data
  94:    * @throws InvalidMidiDataException if this message is invalid
  95:    */
  96:   public void setMessage(int type, byte[] data, int length)
  97:     throws InvalidMidiDataException
  98:   {
  99:     if (type > 127)
 100:       throw new InvalidMidiDataException("Meta type 0x"
 101:                                          + Integer.toHexString(type)
 102:                                          + " must be less than 128");
 103: 
 104:     // For a nice description of how variable length values are handled, 
 105:     // see http://www.borg.com/~jglatt/tech/midifile.htm
 106:     
 107:     // First compute the length of the length value
 108:     lengthLength = 0;
 109:     int lengthValue = length;
 110:     do {
 111:       lengthValue = lengthValue >> 7;
 112:       lengthLength++;
 113:     } while (lengthValue > 0);
 114:     
 115:     // Now allocate our data array
 116:     this.length = 2 + lengthLength + length;
 117:     this.data = new byte[this.length];
 118:     this.data[0] = (byte) META;
 119:     this.data[1] = (byte) type;
 120:     
 121:     // Now compute the length representation
 122:     long buffer = length & 0x7F;
 123:     while ((length >>= 7) > 0)
 124:     {
 125:       buffer <<= 8;
 126:       buffer |= ((length & 0x7F) | 0x80);
 127:     }
 128:     
 129:     // Now store the variable length length value
 130:     int index = 2;
 131:     do
 132:     {
 133:       this.data[index++] = (byte) (buffer & 0xFF);
 134:       if ((buffer & 0x80) == 0)
 135:         break;
 136:       buffer >>= 8;
 137:     } while (true);
 138:  
 139:     // Now copy the real data.
 140:     System.arraycopy(data, 0, this.data, index, length);
 141:   }
 142:   
 143:   /**
 144:    * Get the meta message type.
 145:    * 
 146:    * @return the meta message type
 147:    */
 148:   public int getType()
 149:   {
 150:     return data[1];
 151:   }
 152:   
 153:   /**
 154:    * Get the data for this message, not including the status,
 155:    * type, or length information.
 156:    * 
 157:    * @return the message data, not including status, type or lenght info
 158:    */
 159:   public byte[] getData()
 160:   {
 161:     int dataLength = length - 2 - lengthLength;
 162:     byte[] result = new byte[dataLength];
 163:     System.arraycopy(data, 2 + lengthLength, result, 0, dataLength);
 164:     return result;
 165:   }
 166:   
 167:   /* Create a deep-copy clone of this object.
 168:    * @see java.lang.Object#clone()
 169:    */
 170:   public Object clone()
 171:   {
 172:     byte message[] = new byte[length];
 173:     System.arraycopy(data, 0, message, 0, length);
 174:     return new MetaMessage(message); 
 175:   }
 176: }