Skip to content

Commit

Permalink
4933700: RFE: Add way to get device from Receiver and Transmitter
Browse files Browse the repository at this point in the history
Reviewed-by: art
  • Loading branch information
Alex Menkov committed Sep 14, 2010
1 parent bf0670e commit ded2109
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ protected void finalize() {
This is necessary for Receivers retrieved via MidiSystem.getReceiver()
(which opens the device implicitely).
*/
protected abstract class AbstractReceiver implements Receiver {
protected abstract class AbstractReceiver implements MidiDeviceReceiver {
private boolean open = true;


Expand Down Expand Up @@ -508,6 +508,10 @@ public void close() {
AbstractMidiDevice.this.closeInternal(this);
}

public MidiDevice getMidiDevice() {
return AbstractMidiDevice.this;
}

protected boolean isOpen() {
return open;
}
Expand All @@ -529,7 +533,7 @@ protected boolean isOpen() {
* Also, it has some optimizations regarding sending to the Receivers,
* for known Receivers, and managing itself in the TransmitterList.
*/
protected class BasicTransmitter implements Transmitter {
protected class BasicTransmitter implements MidiDeviceTransmitter {

private Receiver receiver = null;
TransmitterList tlist = null;
Expand Down Expand Up @@ -568,6 +572,9 @@ public void close() {
}
}

public MidiDevice getMidiDevice() {
return AbstractMidiDevice.this;
}

} // class BasicTransmitter

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.media.sound;

import javax.sound.midi.*;


/**
* Helper class which allows to convert {@code Receiver}
* to {@code MidiDeviceReceiver}.
*
* @author Alex Menkov
*/
public class MidiDeviceReceiverEnvelope implements MidiDeviceReceiver {

private final MidiDevice device;
private final Receiver receiver;

/**
* Creates a new {@code MidiDeviceReceiverEnvelope} object which
* envelops the specified {@code Receiver}
* and is owned by the specified {@code MidiDevice}.
*
* @param device the owner {@code MidiDevice}
* @param receiver the {@code Receiver} to be enveloped
*/
public MidiDeviceReceiverEnvelope(MidiDevice device, Receiver receiver) {
if (device == null || receiver == null) {
throw new NullPointerException();
}
this.device = device;
this.receiver = receiver;
}

// Receiver implementation
public void close() {
receiver.close();
}

public void send(MidiMessage message, long timeStamp) {
receiver.send(message, timeStamp);
}

// MidiDeviceReceiver implementation
public MidiDevice getMidiDevice() {
return device;
}

/**
* Obtains the receiver enveloped
* by this {@code MidiDeviceReceiverEnvelope} object.
*
* @return the enveloped receiver
*/
public Receiver getReceiver() {
return receiver;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.media.sound;

import javax.sound.midi.*;


/**
* Helper class which allows to convert {@code Transmitter}
* to {@code MidiDeviceTransmitter}.
*
* @author Alex Menkov
*/
public class MidiDeviceTransmitterEnvelope implements MidiDeviceTransmitter {

private final MidiDevice device;
private final Transmitter transmitter;

/**
* Creates a new {@code MidiDeviceTransmitterEnvelope} object which
* envelops the specified {@code Transmitter}
* and is owned by the specified {@code MidiDevice}.
*
* @param device the owner {@code MidiDevice}
* @param transmitter the {@code Transmitter} to be enveloped
*/
public MidiDeviceTransmitterEnvelope(MidiDevice device, Transmitter transmitter) {
if (device == null || transmitter == null) {
throw new NullPointerException();
}
this.device = device;
this.transmitter = transmitter;
}

// Transmitter implementation
public void setReceiver(Receiver receiver) {
transmitter.setReceiver(receiver);
}

public Receiver getReceiver() {
return transmitter.getReceiver();
}

public void close() {
transmitter.close();
}


// MidiDeviceReceiver implementation
public MidiDevice getMidiDevice() {
return device;
}

/**
* Obtains the transmitter enveloped
* by this {@code MidiDeviceTransmitterEnvelope} object.
*
* @return the enveloped transmitter
*/
public Transmitter getTransmitter() {
return transmitter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.TreeMap;

import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiDeviceReceiver;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.ShortMessage;

Expand Down
14 changes: 14 additions & 0 deletions jdk/src/share/classes/javax/sound/midi/MidiDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ public interface MidiDevice extends AutoCloseable {
* MIDI data. The returned receiver must be closed when the application
* has finished using it.
*
* <p>Usually the returned receiver implements
* the {@code MidiDeviceReceiver} interface.
*
* <p>Obtaining a <code>Receiver</code> with this method does not
* open the device. To be able to use the device, it has to be
* opened explicitly by calling {@link #open}. Also, closing the
Expand All @@ -223,6 +226,10 @@ public interface MidiDevice extends AutoCloseable {
* connected with this MidiDevice.
* A receiver can be removed
* from the device by closing it.
*
* <p>Usually the returned receivers implement
* the {@code MidiDeviceReceiver} interface.
*
* @return an unmodifiable list of the open receivers
* @since 1.5
*/
Expand All @@ -234,6 +241,9 @@ public interface MidiDevice extends AutoCloseable {
* MIDI data The returned transmitter must be closed when the application
* has finished using it.
*
* <p>Usually the returned transmitter implements
* the {@code MidiDeviceTransmitter} interface.
*
* <p>Obtaining a <code>Transmitter</code> with this method does not
* open the device. To be able to use the device, it has to be
* opened explicitly by calling {@link #open}. Also, closing the
Expand All @@ -253,6 +263,10 @@ public interface MidiDevice extends AutoCloseable {
* connected with this MidiDevice.
* A transmitter can be removed
* from the device by closing it.
*
* <p>Usually the returned transmitters implement
* the {@code MidiDeviceTransmitter} interface.
*
* @return an unmodifiable list of the open transmitters
* @since 1.5
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,20 +22,18 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.media.sound;

import javax.sound.midi.MidiDevice;
import javax.sound.midi.Receiver;
package javax.sound.midi;

/**
* A Receiver with reference to it's MidiDevice object.
* <p>{@code MidiDeviceReceiver} is a {@code Receiver} which represents
* a MIDI input connector of a {@code MidiDevice}
* (see {@link MidiDevice#getReceiver()}).
*
* @author Karl Helgason
* @since 1.7
*/
public interface MidiDeviceReceiver extends Receiver {

/** Obtains the MidiDevice object associated with this Receiver.
/** Obtains a MidiDevice object which is an owner of this Receiver.
*/
public MidiDevice getMidiDevice();

}
41 changes: 41 additions & 0 deletions jdk/src/share/classes/javax/sound/midi/MidiDeviceTransmitter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package javax.sound.midi;


/**
* <p>{@code MidiDeviceTransmitter} is a {@code Transmitter} which represents
* a MIDI input connector of a {@code MidiDevice}
* (see {@link MidiDevice#getTransmitter()}).
*
* @since 1.7
*/
public interface MidiDeviceTransmitter extends Transmitter {

/** Obtains a MidiDevice object which is an owner of this Transmitter.
*/
public MidiDevice getMidiDevice();
}
12 changes: 12 additions & 0 deletions jdk/src/share/classes/javax/sound/midi/MidiSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import com.sun.media.sound.JDK13Services;
import com.sun.media.sound.ReferenceCountingDevice;
import com.sun.media.sound.AutoConnectSequencer;
import com.sun.media.sound.MidiDeviceReceiverEnvelope;
import com.sun.media.sound.MidiDeviceTransmitterEnvelope;


/**
Expand Down Expand Up @@ -225,6 +227,8 @@ public static MidiDevice getMidiDevice(MidiDevice.Info info) throws MidiUnavaila
/**
* Obtains a MIDI receiver from an external MIDI port
* or other default device.
* The returned receiver always implements
* the {@code MidiDeviceReceiver} interface.
*
* <p>If the system property
* <code>javax.sound.midi.Receiver</code>
Expand Down Expand Up @@ -261,13 +265,18 @@ public static Receiver getReceiver() throws MidiUnavailableException {
} else {
receiver = device.getReceiver();
}
if (!(receiver instanceof MidiDeviceReceiver)) {
receiver = new MidiDeviceReceiverEnvelope(device, receiver);
}
return receiver;
}


/**
* Obtains a MIDI transmitter from an external MIDI port
* or other default source.
* The returned transmitter always implements
* the {@code MidiDeviceTransmitter} interface.
*
* <p>If the system property
* <code>javax.sound.midi.Transmitter</code>
Expand Down Expand Up @@ -301,6 +310,9 @@ public static Transmitter getTransmitter() throws MidiUnavailableException {
} else {
transmitter = device.getTransmitter();
}
if (!(transmitter instanceof MidiDeviceReceiver)) {
transmitter = new MidiDeviceTransmitterEnvelope(device, transmitter);
}
return transmitter;
}

Expand Down
Loading

0 comments on commit ded2109

Please sign in to comment.