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