jdk/src/share/classes/com/sun/media/sound/MidiOutDevice.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) 1999, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * MidiOutDevice class representing functionality of MidiOut devices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author David Rivas
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
class MidiOutDevice extends AbstractMidiDevice {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    // CONSTRUCTOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    MidiOutDevice(AbstractMidiDeviceProvider.Info info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                super(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                if(Printer.trace) Printer.trace("MidiOutDevice CONSTRUCTOR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // IMPLEMENTATION OF ABSTRACT MIDI DEVICE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    protected synchronized void implOpen() throws MidiUnavailableException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (Printer.trace) Printer.trace("> MidiOutDevice: implOpen()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int index = ((AbstractMidiDeviceProvider.Info)getDeviceInfo()).getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        id = nOpen(index); // can throw MidiUnavailableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (id == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            throw new MidiUnavailableException("Unable to open native device");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (Printer.trace) Printer.trace("< MidiOutDevice: implOpen(): completed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected synchronized void implClose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (Printer.trace) Printer.trace("> MidiOutDevice: implClose()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // prevent further action
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        long oldId = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        id = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        super.implClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // close the device
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        nClose(oldId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (Printer.trace) Printer.trace("< MidiOutDevice: implClose(): completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
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 long getMicrosecondPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        long timestamp = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            timestamp = nGetTimeStamp(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // OVERRIDES OF ABSTRACT MIDI DEVICE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** Returns if this device supports Receivers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        This implementation always returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        @return true, if the device supports Receivers, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected boolean hasReceivers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    protected Receiver createReceiver() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return new MidiOutReceiver();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // INNER CLASSES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    class MidiOutReceiver extends AbstractReceiver {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        protected void implSend(MidiMessage message, long timeStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            int length = message.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            int status = message.getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (length <= 3 && status != 0xF0 && status != 0xF7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                int packedMsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (message instanceof ShortMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    if (message instanceof FastShortMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        packedMsg = ((FastShortMessage) message).getPackedMsg();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        ShortMessage msg = (ShortMessage) message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        packedMsg = (status & 0xFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                            | ((msg.getData1() & 0xFF) << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            | ((msg.getData2() & 0xFF) << 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    packedMsg = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    byte[] data = message.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    if (length>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        packedMsg = data[0] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        if (length>1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                            /* We handle meta messages here. The message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                               system reset (FF) doesn't get until here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                               because it's length is only 1. So if we see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                               a status byte of FF, it's sure that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                               have a Meta message. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                            if (status == 0xFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                            packedMsg |= (data[1] & 0xFF) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                            if (length>2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                packedMsg |= (data[2] & 0xFF) << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                nSendShortMessage(id, packedMsg, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (message instanceof FastSysexMessage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    nSendLongMessage(id, ((FastSysexMessage) message).getReadOnlyMessage(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                     length, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    nSendLongMessage(id, message.getMessage(), length, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        /** shortcut for the Sun implementation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        synchronized void sendPackedMidiMessage(int packedMsg, long timeStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (isOpen() && id != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                nSendShortMessage(id, packedMsg, timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    } // class MidiOutReceiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // NATIVE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private native long nOpen(int index) throws MidiUnavailableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private native void nClose(long id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private native void nSendShortMessage(long id, int packedMsg, long timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private native void nSendLongMessage(long id, byte[] data, int size, long timeStamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private native long nGetTimeStamp(long id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
} // class MidiOutDevice