< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/MidiOutDevice.java

Print this page




   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) {


 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


   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.MidiMessage;
  29 import javax.sound.midi.MidiUnavailableException;
  30 import javax.sound.midi.Receiver;
  31 import javax.sound.midi.ShortMessage;
  32 
  33 /**
  34  * MidiOutDevice class representing functionality of MidiOut devices.
  35  *
  36  * @author David Rivas
  37  * @author Kara Kytle
  38  * @author Florian Bomers
  39  */
  40 final class MidiOutDevice extends AbstractMidiDevice {
  41 


  42     MidiOutDevice(AbstractMidiDeviceProvider.Info info) {
  43                 super(info);
  44                 if(Printer.trace) Printer.trace("MidiOutDevice CONSTRUCTOR");
  45     }
  46 
  47     @Override


  48     protected synchronized void implOpen() throws MidiUnavailableException {
  49         if (Printer.trace) Printer.trace("> MidiOutDevice: implOpen()");
  50         int index = ((AbstractMidiDeviceProvider.Info)getDeviceInfo()).getIndex();
  51         id = nOpen(index); // can throw MidiUnavailableException
  52         if (id == 0) {
  53             throw new MidiUnavailableException("Unable to open native device");
  54         }
  55         if (Printer.trace) Printer.trace("< MidiOutDevice: implOpen(): completed.");
  56     }
  57 
  58     @Override
  59     protected synchronized void implClose() {
  60         if (Printer.trace) Printer.trace("> MidiOutDevice: implClose()");
  61         // prevent further action
  62         long oldId = id;
  63         id = 0;
  64 
  65         super.implClose();
  66 
  67         // close the device
  68         nClose(oldId);
  69         if (Printer.trace) Printer.trace("< MidiOutDevice: implClose(): completed");
  70     }
  71 
  72     @Override
  73     public long getMicrosecondPosition() {
  74         long timestamp = -1;
  75         if (isOpen()) {
  76             timestamp = nGetTimeStamp(id);
  77         }
  78         return timestamp;
  79     }
  80 




  81     /** Returns if this device supports Receivers.
  82         This implementation always returns true.
  83         @return true, if the device supports Receivers, false otherwise.
  84     */
  85     @Override
  86     protected boolean hasReceivers() {
  87         return true;
  88     }
  89 
  90     @Override
  91     protected Receiver createReceiver() {
  92         return new MidiOutReceiver();
  93     }
  94 



  95     final class MidiOutReceiver extends AbstractReceiver {
  96 
  97         @Override
  98         void implSend(final MidiMessage message, final long timeStamp) {
  99             final int length = message.getLength();
 100             final int status = message.getStatus();
 101             if (length <= 3 && status != 0xF0 && status != 0xF7) {
 102                 int packedMsg;
 103                 if (message instanceof ShortMessage) {
 104                     if (message instanceof FastShortMessage) {
 105                         packedMsg = ((FastShortMessage) message).getPackedMsg();
 106                     } else {
 107                         ShortMessage msg = (ShortMessage) message;
 108                         packedMsg = (status & 0xFF)
 109                             | ((msg.getData1() & 0xFF) << 8)
 110                             | ((msg.getData2() & 0xFF) << 16);
 111                     }
 112                 } else {
 113                     packedMsg = 0;
 114                     byte[] data = message.getMessage();
 115                     if (length>0) {
 116                         packedMsg = data[0] & 0xFF;
 117                         if (length>1) {


 134             } else {
 135                 final byte[] data;
 136                 if (message instanceof FastSysexMessage) {
 137                     data = ((FastSysexMessage) message).getReadOnlyMessage();
 138                 } else {
 139                     data = message.getMessage();
 140                 }
 141                 final int dataLength = Math.min(length, data.length);
 142                 if (dataLength > 0) {
 143                     nSendLongMessage(id, data, dataLength, timeStamp);
 144                 }
 145             }
 146         }
 147 
 148         /** shortcut for the Sun implementation */
 149         synchronized void sendPackedMidiMessage(int packedMsg, long timeStamp) {
 150             if (isOpen() && id != 0) {
 151                 nSendShortMessage(id, packedMsg, timeStamp);
 152             }
 153         }


 154     } // class MidiOutReceiver
 155 



 156     private native long nOpen(int index) throws MidiUnavailableException;
 157     private native void nClose(long id);
 158 
 159     private native void nSendShortMessage(long id, int packedMsg, long timeStamp);
 160     private native void nSendLongMessage(long id, byte[] data, int size, long timeStamp);
 161     private native long nGetTimeStamp(long id);
 162 
 163 } // class MidiOutDevice
< prev index next >