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