J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.media.sound; |
| 27 | |
| 28 | import javax.sound.midi.*; |
| 29 | |
| 30 | /** |
| 31 | * an optimized ShortMessage that does not need an array |
| 32 | * |
| 33 | * @author Florian Bomers |
| 34 | */ |
| 35 | class FastShortMessage extends ShortMessage { |
| 36 | private int packedMsg; |
| 37 | |
| 38 | public FastShortMessage(int packedMsg) throws InvalidMidiDataException { |
| 39 | this.packedMsg = packedMsg; |
| 40 | getDataLength(packedMsg & 0xFF); // to check for validity |
| 41 | } |
| 42 | |
| 43 | /** Creates a FastShortMessage from this ShortMessage */ |
| 44 | public FastShortMessage(ShortMessage msg) { |
| 45 | this.packedMsg = msg.getStatus() |
| 46 | | (msg.getData1() << 8) |
| 47 | | (msg.getData2() << 16); |
| 48 | } |
| 49 | |
| 50 | int getPackedMsg() { |
| 51 | return packedMsg; |
| 52 | } |
| 53 | |
| 54 | public byte[] getMessage() { |
| 55 | int length = 0; |
| 56 | try { |
| 57 | // fix for bug 4851018: MidiMessage.getLength and .getData return wrong values |
| 58 | // fix for bug 4890405: Reading MidiMessage byte array fails in 1.4.2 |
| 59 | length = getDataLength(packedMsg & 0xFF) + 1; |
| 60 | } catch (InvalidMidiDataException imde) { |
| 61 | // should never happen |
| 62 | } |
| 63 | byte[] returnedArray = new byte[length]; |
| 64 | if (length>0) { |
| 65 | returnedArray[0] = (byte) (packedMsg & 0xFF); |
| 66 | if (length>1) { |
| 67 | returnedArray[1] = (byte) ((packedMsg & 0xFF00) >> 8); |
| 68 | if (length>2) { |
| 69 | returnedArray[2] = (byte) ((packedMsg & 0xFF0000) >> 16); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | return returnedArray; |
| 74 | } |
| 75 | |
| 76 | public int getLength() { |
| 77 | try { |
| 78 | return getDataLength(packedMsg & 0xFF) + 1; |
| 79 | } catch (InvalidMidiDataException imde) { |
| 80 | // should never happen |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | public void setMessage(int status) throws InvalidMidiDataException { |
| 86 | // check for valid values |
| 87 | int dataLength = getDataLength(status); // can throw InvalidMidiDataException |
| 88 | if (dataLength != 0) { |
| 89 | super.setMessage(status); // throws Exception |
| 90 | } |
| 91 | packedMsg = (packedMsg & 0xFFFF00) | (status & 0xFF); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | public void setMessage(int status, int data1, int data2) throws InvalidMidiDataException { |
| 96 | getDataLength(status); // can throw InvalidMidiDataException |
| 97 | packedMsg = (status & 0xFF) | ((data1 & 0xFF) << 8) | ((data2 & 0xFF) << 16); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | public void setMessage(int command, int channel, int data1, int data2) throws InvalidMidiDataException { |
| 102 | getDataLength(command); // can throw InvalidMidiDataException |
| 103 | packedMsg = (command & 0xF0) | (channel & 0x0F) | ((data1 & 0xFF) << 8) | ((data2 & 0xFF) << 16); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | public int getChannel() { |
| 108 | return packedMsg & 0x0F; |
| 109 | } |
| 110 | |
| 111 | public int getCommand() { |
| 112 | return packedMsg & 0xF0; |
| 113 | } |
| 114 | |
| 115 | public int getData1() { |
| 116 | return (packedMsg & 0xFF00) >> 8; |
| 117 | } |
| 118 | |
| 119 | public int getData2() { |
| 120 | return (packedMsg & 0xFF0000) >> 16; |
| 121 | } |
| 122 | |
| 123 | public int getStatus() { |
| 124 | return packedMsg & 0xFF; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Creates a new object of the same class and with the same contents |
| 129 | * as this object. |
| 130 | * @return a clone of this instance. |
| 131 | */ |
| 132 | public Object clone() { |
| 133 | try { |
| 134 | return new FastShortMessage(packedMsg); |
| 135 | } catch (InvalidMidiDataException imde) { |
| 136 | // should never happen |
| 137 | } |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | } // class FastShortMsg |