Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/MidiInDevice.java
38924 views
/*1* Copyright (c) 1999, 2016, 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* MidiInDevice class representing functionality of MidiIn devices.33*34* @author David Rivas35* @author Kara Kytle36* @author Florian Bomers37*/38final class MidiInDevice extends AbstractMidiDevice implements Runnable {3940private volatile Thread midiInThread;4142// CONSTRUCTOR4344MidiInDevice(AbstractMidiDeviceProvider.Info info) {45super(info);46if(Printer.trace) Printer.trace("MidiInDevice CONSTRUCTOR");47}484950// IMPLEMENTATION OF ABSTRACT MIDI DEVICE METHODS5152// $$kk: 06.24.99: i have this both opening and starting the midi in device.53// may want to separate these??54protected synchronized void implOpen() throws MidiUnavailableException {55if (Printer.trace) Printer.trace("> MidiInDevice: implOpen()");5657int index = ((MidiInDeviceProvider.MidiInDeviceInfo)getDeviceInfo()).getIndex();58id = nOpen(index); // can throw MidiUnavailableException5960if (id == 0) {61throw new MidiUnavailableException("Unable to open native device");62}6364// create / start a thread to get messages65if (midiInThread == null) {66midiInThread = JSSecurityManager.createThread(this,67"Java Sound MidiInDevice Thread", // name68false, // daemon69-1, // priority70true); // doStart71}7273nStart(id); // can throw MidiUnavailableException74if (Printer.trace) Printer.trace("< MidiInDevice: implOpen() completed");75}767778// $$kk: 06.24.99: i have this both stopping and closing the midi in device.79// may want to separate these??80protected synchronized void implClose() {81if (Printer.trace) Printer.trace("> MidiInDevice: implClose()");82long oldId = id;83id = 0;8485super.implClose();8687// close the device88nStop(oldId);89if (midiInThread != null) {90try {91midiInThread.join(1000);92} catch (InterruptedException e) {93// IGNORE EXCEPTION94}95}96nClose(oldId);97if (Printer.trace) Printer.trace("< MidiInDevice: implClose() completed");98}99100101public long getMicrosecondPosition() {102long timestamp = -1;103if (isOpen()) {104timestamp = nGetTimeStamp(id);105}106return timestamp;107}108109110// OVERRIDES OF ABSTRACT MIDI DEVICE METHODS111112113protected boolean hasTransmitters() {114return true;115}116117118protected Transmitter createTransmitter() {119return new MidiInTransmitter();120}121122/**123* An own class to distinguish the class name from124* the transmitter of other devices125*/126private final class MidiInTransmitter extends BasicTransmitter {127private MidiInTransmitter() {128super();129}130}131132// RUNNABLE METHOD133134public void run() {135// while the device is started, keep trying to get messages.136// this thread returns from native code whenever stop() or close() is called137while (id!=0) {138// go into native code and retrieve messages139nGetMessages(id);140if (id!=0) {141try {142Thread.sleep(1);143} catch (InterruptedException e) {}144}145}146if(Printer.verbose) Printer.verbose("MidiInDevice Thread exit");147// let the thread exit148midiInThread = null;149}150151152// CALLBACKS FROM NATIVE153154/**155* Callback from native code when a short MIDI event is received from hardware.156* @param packedMsg: status | data1 << 8 | data2 << 8157* @param timeStamp time-stamp in microseconds158*/159void callbackShortMessage(int packedMsg, long timeStamp) {160if (packedMsg == 0 || id == 0) {161return;162}163164/*if(Printer.verbose) {165int status = packedMsg & 0xFF;166int data1 = (packedMsg & 0xFF00)>>8;167int data2 = (packedMsg & 0xFF0000)>>16;168Printer.verbose(">> MidiInDevice callbackShortMessage: status: " + status + " data1: " + data1 + " data2: " + data2 + " timeStamp: " + timeStamp);169}*/170171getTransmitterList().sendMessage(packedMsg, timeStamp);172}173174void callbackLongMessage(byte[] data, long timeStamp) {175if (id == 0 || data == null) {176return;177}178getTransmitterList().sendMessage(data, timeStamp);179}180181// NATIVE METHODS182183private native long nOpen(int index) throws MidiUnavailableException;184private native void nClose(long id);185186private native void nStart(long id) throws MidiUnavailableException;187private native void nStop(long id);188private native long nGetTimeStamp(long id);189190// go into native code and get messages. May be blocking191private native void nGetMessages(long id);192193194}195196197