Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/MidiOutDevice.java
38924 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.media.sound;2627import javax.sound.midi.*;28293031/**32* MidiOutDevice class representing functionality of MidiOut devices.33*34* @author David Rivas35* @author Kara Kytle36* @author Florian Bomers37*/38final class MidiOutDevice extends AbstractMidiDevice {3940// CONSTRUCTOR4142MidiOutDevice(AbstractMidiDeviceProvider.Info info) {43super(info);44if(Printer.trace) Printer.trace("MidiOutDevice CONSTRUCTOR");45}464748// IMPLEMENTATION OF ABSTRACT MIDI DEVICE METHODS4950protected synchronized void implOpen() throws MidiUnavailableException {51if (Printer.trace) Printer.trace("> MidiOutDevice: implOpen()");52int index = ((AbstractMidiDeviceProvider.Info)getDeviceInfo()).getIndex();53id = nOpen(index); // can throw MidiUnavailableException54if (id == 0) {55throw new MidiUnavailableException("Unable to open native device");56}57if (Printer.trace) Printer.trace("< MidiOutDevice: implOpen(): completed.");58}596061protected synchronized void implClose() {62if (Printer.trace) Printer.trace("> MidiOutDevice: implClose()");63// prevent further action64long oldId = id;65id = 0;6667super.implClose();6869// close the device70nClose(oldId);71if (Printer.trace) Printer.trace("< MidiOutDevice: implClose(): completed");72}737475public long getMicrosecondPosition() {76long timestamp = -1;77if (isOpen()) {78timestamp = nGetTimeStamp(id);79}80return timestamp;81}82838485// OVERRIDES OF ABSTRACT MIDI DEVICE METHODS8687/** Returns if this device supports Receivers.88This implementation always returns true.89@return true, if the device supports Receivers, false otherwise.90*/91protected boolean hasReceivers() {92return true;93}949596protected Receiver createReceiver() {97return new MidiOutReceiver();98}99100101// INNER CLASSES102103final class MidiOutReceiver extends AbstractReceiver {104105void implSend(final MidiMessage message, final long timeStamp) {106final int length = message.getLength();107final int status = message.getStatus();108if (length <= 3 && status != 0xF0 && status != 0xF7) {109int packedMsg;110if (message instanceof ShortMessage) {111if (message instanceof FastShortMessage) {112packedMsg = ((FastShortMessage) message).getPackedMsg();113} else {114ShortMessage msg = (ShortMessage) message;115packedMsg = (status & 0xFF)116| ((msg.getData1() & 0xFF) << 8)117| ((msg.getData2() & 0xFF) << 16);118}119} else {120packedMsg = 0;121byte[] data = message.getMessage();122if (length>0) {123packedMsg = data[0] & 0xFF;124if (length>1) {125/* We handle meta messages here. The message126system reset (FF) doesn't get until here,127because it's length is only 1. So if we see128a status byte of FF, it's sure that we129have a Meta message. */130if (status == 0xFF) {131return;132}133packedMsg |= (data[1] & 0xFF) << 8;134if (length>2) {135packedMsg |= (data[2] & 0xFF) << 16;136}137}138}139}140nSendShortMessage(id, packedMsg, timeStamp);141} else {142final byte[] data;143if (message instanceof FastSysexMessage) {144data = ((FastSysexMessage) message).getReadOnlyMessage();145} else {146data = message.getMessage();147}148final int dataLength = Math.min(length, data.length);149if (dataLength > 0) {150nSendLongMessage(id, data, dataLength, timeStamp);151}152}153}154155/** shortcut for the Sun implementation */156synchronized void sendPackedMidiMessage(int packedMsg, long timeStamp) {157if (isOpen() && id != 0) {158nSendShortMessage(id, packedMsg, timeStamp);159}160}161162163} // class MidiOutReceiver164165166// NATIVE METHODS167168private native long nOpen(int index) throws MidiUnavailableException;169private native void nClose(long id);170171private native void nSendShortMessage(long id, int packedMsg, long timeStamp);172private native void nSendLongMessage(long id, byte[] data, int size, long timeStamp);173private native long nGetTimeStamp(long id);174175} // class MidiOutDevice176177178