Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/MidiInDevice.java
41161 views
/*1* Copyright (c) 1999, 2019, 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.MidiUnavailableException;28import javax.sound.midi.Transmitter;2930/**31* MidiInDevice class representing functionality of MidiIn devices.32*33* @author David Rivas34* @author Kara Kytle35* @author Florian Bomers36*/37final class MidiInDevice extends AbstractMidiDevice implements Runnable {3839private volatile Thread midiInThread;4041MidiInDevice(AbstractMidiDeviceProvider.Info info) {42super(info);43}4445// $$kk: 06.24.99: i have this both opening and starting the midi in device.46// may want to separate these??47@Override48protected synchronized void implOpen() throws MidiUnavailableException {49int index = ((MidiInDeviceProvider.MidiInDeviceInfo)getDeviceInfo()).getIndex();50id = nOpen(index); // can throw MidiUnavailableException5152if (id == 0) {53throw new MidiUnavailableException("Unable to open native device");54}5556// create / start a thread to get messages57if (midiInThread == null) {58midiInThread = JSSecurityManager.createThread(this,59"Java Sound MidiInDevice Thread", // name60false, // daemon61-1, // priority62true); // doStart63}6465nStart(id); // can throw MidiUnavailableException66}6768// $$kk: 06.24.99: i have this both stopping and closing the midi in device.69// may want to separate these??70@Override71protected synchronized void implClose() {72long oldId = id;73id = 0;7475super.implClose();7677// close the device78nStop(oldId);79if (midiInThread != null) {80try {81midiInThread.join(1000);82} catch (InterruptedException e) {83// IGNORE EXCEPTION84}85}86nClose(oldId);87}8889@Override90public long getMicrosecondPosition() {91long timestamp = -1;92if (isOpen()) {93timestamp = nGetTimeStamp(id);94}95return timestamp;96}9798// OVERRIDES OF ABSTRACT MIDI DEVICE METHODS99100@Override101protected boolean hasTransmitters() {102return true;103}104105@Override106protected Transmitter createTransmitter() {107return new MidiInTransmitter();108}109110/**111* An own class to distinguish the class name from112* the transmitter of other devices.113*/114private final class MidiInTransmitter extends BasicTransmitter {115private MidiInTransmitter() {116super();117}118}119120@Override121public void run() {122// while the device is started, keep trying to get messages.123// this thread returns from native code whenever stop() or close() is called124while (id!=0) {125// go into native code and retrieve messages126nGetMessages(id);127if (id!=0) {128try {129Thread.sleep(1);130} catch (InterruptedException e) {}131}132}133// let the thread exit134midiInThread = null;135}136137/**138* Callback from native code when a short MIDI event is received from hardware.139* @param packedMsg: status | data1 << 8 | data2 << 8140* @param timeStamp time-stamp in microseconds141*/142void callbackShortMessage(int packedMsg, long timeStamp) {143if (packedMsg == 0 || id == 0) {144return;145}146147/*if(Printer.verbose) {148int status = packedMsg & 0xFF;149int data1 = (packedMsg & 0xFF00)>>8;150int data2 = (packedMsg & 0xFF0000)>>16;151Printer.verbose(">> MidiInDevice callbackShortMessage: status: " + status + " data1: " + data1 + " data2: " + data2 + " timeStamp: " + timeStamp);152}*/153154getTransmitterList().sendMessage(packedMsg, timeStamp);155}156157void callbackLongMessage(byte[] data, long timeStamp) {158if (id == 0 || data == null) {159return;160}161getTransmitterList().sendMessage(data, timeStamp);162}163164private native long nOpen(int index) throws MidiUnavailableException;165private native void nClose(long id);166167private native void nStart(long id) throws MidiUnavailableException;168private native void nStop(long id);169private native long nGetTimeStamp(long id);170171// go into native code and get messages. May be blocking172private native void nGetMessages(long id);173}174175176