1 /*
   2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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 final 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     final class MidiOutReceiver extends AbstractReceiver {
 105 
 106         void implSend(final MidiMessage message, final long timeStamp) {
 107             final int length = message.getLength();
 108             final 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                 final byte[] data;
 144                 if (message instanceof FastSysexMessage) {
 145                     data = ((FastSysexMessage) message).getReadOnlyMessage();
 146                 } else {
 147                     data = message.getMessage();
 148                 }
 149                 final int dataLength = Math.min(length, data.length);
 150                 if (dataLength > 0) {
 151                     nSendLongMessage(id, data, dataLength, timeStamp);
 152                 }
 153             }
 154         }
 155 
 156         /** shortcut for the Sun implementation */
 157         synchronized void sendPackedMidiMessage(int packedMsg, long timeStamp) {
 158             if (isOpen() && id != 0) {
 159                 nSendShortMessage(id, packedMsg, timeStamp);
 160             }
 161         }
 162 
 163 
 164     } // class MidiOutReceiver
 165 
 166 
 167     // NATIVE METHODS
 168 
 169     private native long nOpen(int index) throws MidiUnavailableException;
 170     private native void nClose(long id);
 171 
 172     private native void nSendShortMessage(long id, int packedMsg, long timeStamp);
 173     private native void nSendLongMessage(long id, byte[] data, int size, long timeStamp);
 174     private native long nGetTimeStamp(long id);
 175 
 176 } // class MidiOutDevice