javax.sound.midi.MidiUnavailableException Java Examples

The following examples show how to use javax.sound.midi.MidiUnavailableException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InstrumentTester.java    From jsyn with Apache License 2.0 6 votes vote down vote up
public int setupMidiKeyboard() throws MidiUnavailableException, IOException, InterruptedException {
    messageParser = new MyParser();

    int result = 2;
    MidiDevice keyboard = MidiDeviceTools.findKeyboard();
    Receiver receiver = new CustomReceiver();
    // Just use default synthesizer.
    if (keyboard != null) {
        // If you forget to open them you will hear no sound.
        keyboard.open();
        // Put the receiver in the transmitter.
        // This gives fairly low latency playing.
        keyboard.getTransmitter().setReceiver(receiver);
        System.out.println("Play MIDI keyboard: " + keyboard.getDeviceInfo().getDescription());
        result = 0;
    } else {
        System.out.println("Could not find a keyboard.");
    }
    return result;
}
 
Example #2
Source File: SequencerImplicitSynthOpen.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static boolean test(Sequencer sequencer) throws MidiUnavailableException {
    log("");
    log("opening sequencer...");
    sequencer.open();   // opens connected synthesizer implicitly
    MidiDevice synth = getConnectedDevice(sequencer);
    log("  connected device: " + getDeviceStr(synth));

    log("closing sequencer...");
    sequencer.close();  // closes the synth implicitly
    log("  synth is " + getDeviceStr(synth));
    MidiDevice synth2 = getConnectedDevice(sequencer);
    log("  currently connected device: " + getDeviceStr(synth2));

    if (synth != null && synth.isOpen()) {
        log("FAIL.");
        return false;
    }
    log("OK.");
    return true;
}
 
Example #3
Source File: UseMidiKeyboard.java    From jsyn with Apache License 2.0 6 votes vote down vote up
public int test() throws MidiUnavailableException, IOException, InterruptedException {
    setupSynth();

    int result = 2;
    MidiDevice keyboard = MidiDeviceTools.findKeyboard();
    Receiver receiver = new CustomReceiver();
    // Just use default synthesizer.
    if (keyboard != null) {
        // If you forget to open them you will hear no sound.
        keyboard.open();
        // Put the receiver in the transmitter.
        // This gives fairly low latency playing.
        keyboard.getTransmitter().setReceiver(receiver);
        System.out.println("Play MIDI keyboard: " + keyboard.getDeviceInfo().getDescription());
        result = 0;
    } else {
        System.out.println("Could not find a keyboard.");
    }
    return result;
}
 
Example #4
Source File: MiPortProvider.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static MidiDevice getDevice(String inDeviceName) throws MiException
{
MidiDevice.Info[]	infos = MidiSystem.getMidiDeviceInfo();

for(int i = 0; i < infos.length; i++)
	{
	if(infos[i].getName().equals(inDeviceName))
		{
		try {
			MidiDevice device = MidiSystem.getMidiDevice(infos[i]);

			if(	device.getMaxTransmitters() == 0 ||
				device instanceof Sequencer)
				continue;

			return(device);
			}
		catch(MidiUnavailableException mue)
			{
			throw new MiException(TuxGuitar.getProperty("midiinput.error.midi.unavailable"), mue);
			}
		}
	}

return(null);
}
 
Example #5
Source File: SequencerImplicitSynthOpen.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static boolean test(Sequencer sequencer) throws MidiUnavailableException {
    log("");
    log("opening sequencer...");
    sequencer.open();   // opens connected synthesizer implicitly
    MidiDevice synth = getConnectedDevice(sequencer);
    log("  connected device: " + getDeviceStr(synth));

    log("closing sequencer...");
    sequencer.close();  // closes the synth implicitly
    log("  synth is " + getDeviceStr(synth));
    MidiDevice synth2 = getConnectedDevice(sequencer);
    log("  currently connected device: " + getDeviceStr(synth2));

    if (synth != null && synth.isOpen()) {
        log("FAIL.");
        return false;
    }
    log("OK.");
    return true;
}
 
Example #6
Source File: SequencerImplicitSynthOpen.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    try {
        log("getting sequencer...");
        Sequencer sequencer = MidiSystem.getSequencer();
        log("  - got " + getDeviceStr(sequencer));

        // obtain connected device (usually synthesizer)
        MidiDevice synth = getConnectedDevice(sequencer);
        if (synth == null) {
            log("could not get connected device, returning");
            return;
        }

        log("connected device: " + getDeviceStr(synth));

        int success = 0;
        for (int i=0; i<TEST_COUNT; i++) {
            if (test(sequencer)) {
                success++;
            }
        }

        if (success != TEST_COUNT) {
            throw new RuntimeException("test FAILS");
        }
    } catch (MidiUnavailableException ex) {
        // this is not a failure
        log("Could not get Sequencer");
    }
    log("test PASSED.");
}
 
Example #7
Source File: JavaSoundAudioClip.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean createSequencer(BufferedInputStream in) throws IOException {

        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSequencer()");

        // get the sequencer
        try {
            sequencer = MidiSystem.getSequencer( );
        } catch(MidiUnavailableException me) {
            if (DEBUG || Printer.err)me.printStackTrace();
            return false;
        }
        if (sequencer==null) {
            return false;
        }

        try {
            sequence = MidiSystem.getSequence(in);
            if (sequence == null) {
                return false;
            }
        } catch (InvalidMidiDataException e) {
            if (DEBUG || Printer.err)e.printStackTrace();
            return false;
        }

        if (DEBUG || Printer.debug)Printer.debug("Created Sequencer.");
        return true;
    }
 
Example #8
Source File: RealTimeSequencerProvider.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public MidiDevice getDevice(MidiDevice.Info info) {
    if ((info != null) && (!info.equals(RealTimeSequencer.info))) {
        return null;
    }

    try {
        return new RealTimeSequencer();
    } catch (MidiUnavailableException e) {
        return null;
    }
}
 
Example #9
Source File: AbstractMidiDevice.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** Open the device from an application program.
 * Setting the open reference count to -1 here prevents Transmitters and Receivers that
 * opened the device implicitly from closing it. The only way to close the device after
 * this call is a call to close().
 */
@Override
public final void open() throws MidiUnavailableException {
    synchronized(this) {
        openRefCount = -1;
        doOpen();
    }
}
 
Example #10
Source File: AbstractMidiDevice.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** Retrieve a Transmitter and open the device implicitly.
    This method is called by MidiSystem.getTransmitter().
 */
@Override
public final Transmitter getTransmitterReferenceCounting()
        throws MidiUnavailableException {
    /* Keep this order of commands! If getTransmitter() throws an exception,
       openInternal() should not be called!
    */
    Transmitter transmitter;
    synchronized (traRecLock) {
        transmitter = getTransmitter();
        AbstractMidiDevice.this.openInternal(transmitter);
    }
    return transmitter;
}
 
Example #11
Source File: bug6415669.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
boolean test() {
    openThread = new Thread(new Runnable() {
        public void run() {
            try {
                log("openThread: getting transmitter...");
                transmitter = MidiSystem.getTransmitter();
                log("openThread:   - OK: " + transmitter);
            } catch (MidiUnavailableException ex) {
                log("openThread:   - Exception: ");
                ex.printStackTrace(System.out);
                log("openThread: skipping...");
            }
            log("openThread: exiting...");
        }
    });
    log("starting openThread...");
    openThread.start();

    while (openThread.isAlive())
        delay(500);
    // make additional delay
    delay(500);

    if (transmitter == null) {
        return true;   // midi is not available, just ignore
    }

    log("closing transmitter");
    transmitter.close();
    log("  - OK");

    return true;
}
 
Example #12
Source File: GetTransmitter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    synth.open(new DummySourceDataLine(), null);
    try
    {
        synth.getTransmitter();
        throw new Exception("MidiUnavailableException not thrown!");
    } catch (MidiUnavailableException e) {
    }
    synth.close();

}
 
Example #13
Source File: RealTimeSequencerProvider.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public MidiDevice getDevice(MidiDevice.Info info) {
    if ((info != null) && (!info.equals(RealTimeSequencer.info))) {
        return null;
    }

    try {
        return new RealTimeSequencer();
    } catch (MidiUnavailableException e) {
        return null;
    }
}
 
Example #14
Source File: SoftSynthesizer.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void open() throws MidiUnavailableException {
    if (isOpen()) {
        synchronized (control_mutex) {
            implicitOpen = false;
        }
        return;
    }
    open(null, null);
}
 
Example #15
Source File: SoftSynthesizer.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public Receiver getReceiver() throws MidiUnavailableException {

        synchronized (control_mutex) {
            SoftReceiver receiver = new SoftReceiver(this);
            receiver.open = open;
            recvslist.add(receiver);
            return receiver;
        }
    }
 
Example #16
Source File: MidiOutDevice.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected synchronized void implOpen() throws MidiUnavailableException {
    if (Printer.trace) Printer.trace("> MidiOutDevice: implOpen()");
    int index = ((AbstractMidiDeviceProvider.Info)getDeviceInfo()).getIndex();
    id = nOpen(index); // can throw MidiUnavailableException
    if (id == 0) {
        throw new MidiUnavailableException("Unable to open native device");
    }
    if (Printer.trace) Printer.trace("< MidiOutDevice: implOpen(): completed.");
}
 
Example #17
Source File: GetTransmitter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    synth.open(new DummySourceDataLine(), null);
    try
    {
        synth.getTransmitter();
        throw new Exception("MidiUnavailableException not thrown!");
    } catch (MidiUnavailableException e) {
    }
    synth.close();

}
 
Example #18
Source File: MidiReciever.java    From Panako with GNU Affero General Public License v3.0 5 votes vote down vote up
public MidiReciever()
    {
        MidiDevice device;
        MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
        for (int i = 0; i < infos.length; i++) {
            try {
            device = MidiSystem.getMidiDevice(infos[i]);
            //does the device have any transmitters?
            //if it does, add it to the device list
            System.out.println(infos[i]);

            //get all transmitters
            List<Transmitter> transmitters = device.getTransmitters();
            //and for each transmitter

            for(int j = 0; j<transmitters.size();j++) {
                //create a new receiver
                transmitters.get(j).setReceiver(
                        //using my own MidiInputReceiver
                        new MidiInputReceiver(device.getDeviceInfo().toString())
                );
            }

            Transmitter trans = device.getTransmitter();
            trans.setReceiver(new MidiInputReceiver(device.getDeviceInfo().toString()));

            //open each device
            device.open();
            //if code gets this far without throwing an exception
            //print a success message
            System.out.println(device.getDeviceInfo()+" Was Opened");


        } catch (MidiUnavailableException e) {}
    }


}
 
Example #19
Source File: JavaSoundAudioClip.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private boolean createSequencer(BufferedInputStream in) throws IOException {

        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSequencer()");

        // get the sequencer
        try {
            sequencer = MidiSystem.getSequencer( );
        } catch(MidiUnavailableException me) {
            if (DEBUG || Printer.err)me.printStackTrace();
            return false;
        }
        if (sequencer==null) {
            return false;
        }

        try {
            sequence = MidiSystem.getSequence(in);
            if (sequence == null) {
                return false;
            }
        } catch (InvalidMidiDataException e) {
            if (DEBUG || Printer.err)e.printStackTrace();
            return false;
        }

        if (DEBUG || Printer.debug)Printer.debug("Created Sequencer.");
        return true;
    }
 
Example #20
Source File: SoftSynthesizer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public Receiver getReceiver() throws MidiUnavailableException {

        synchronized (control_mutex) {
            SoftReceiver receiver = new SoftReceiver(this);
            receiver.open = open;
            recvslist.add(receiver);
            return receiver;
        }
    }
 
Example #21
Source File: SoftSynthesizer.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Receiver getReceiver() throws MidiUnavailableException {

        synchronized (control_mutex) {
            SoftReceiver receiver = new SoftReceiver(this);
            receiver.open = open;
            recvslist.add(receiver);
            return receiver;
        }
    }
 
Example #22
Source File: SoftSynthesizer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public Receiver getReceiverReferenceCounting()
        throws MidiUnavailableException {

    if (!isOpen()) {
        open();
        synchronized (control_mutex) {
            implicitOpen = true;
        }
    }

    return getReceiver();
}
 
Example #23
Source File: SequencerImplicitSynthOpen.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    try {
        log("getting sequencer...");
        Sequencer sequencer = MidiSystem.getSequencer();
        log("  - got " + getDeviceStr(sequencer));

        // obtain connected device (usually synthesizer)
        MidiDevice synth = getConnectedDevice(sequencer);
        if (synth == null) {
            log("could not get connected device, returning");
            return;
        }

        log("connected device: " + getDeviceStr(synth));

        int success = 0;
        for (int i=0; i<TEST_COUNT; i++) {
            if (test(sequencer)) {
                success++;
            }
        }

        if (success != TEST_COUNT) {
            throw new RuntimeException("test FAILS");
        }
    } catch (MidiUnavailableException ex) {
        // this is not a failure
        log("Could not get Sequencer");
    }
    log("test PASSED.");
}
 
Example #24
Source File: GetTransmitter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    synth.open(new DummySourceDataLine(), null);
    try
    {
        synth.getTransmitter();
        throw new Exception("MidiUnavailableException not thrown!");
    } catch (MidiUnavailableException e) {
    }
    synth.close();

}
 
Example #25
Source File: SequencerImplicitSynthOpen.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    try {
        log("getting sequencer...");
        Sequencer sequencer = MidiSystem.getSequencer();
        log("  - got " + getDeviceStr(sequencer));

        // obtain connected device (usually synthesizer)
        MidiDevice synth = getConnectedDevice(sequencer);
        if (synth == null) {
            log("could not get connected device, returning");
            return;
        }

        log("connected device: " + getDeviceStr(synth));

        int success = 0;
        for (int i=0; i<TEST_COUNT; i++) {
            if (test(sequencer)) {
                success++;
            }
        }

        if (success != TEST_COUNT) {
            throw new RuntimeException("test FAILS");
        }
    } catch (MidiUnavailableException ex) {
        // this is not a failure
        log("Could not get Sequencer");
    }
    log("test PASSED.");
}
 
Example #26
Source File: InitializationHang.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] argv) {
    try {
        MidiSystem.getReceiver();
        Toolkit.getDefaultToolkit();
    } catch (final MidiUnavailableException ignored) {
        // the test is not applicable
    }
}
 
Example #27
Source File: JavaSoundAudioClip.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean createSequencer(BufferedInputStream in) throws IOException {

        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSequencer()");

        // get the sequencer
        try {
            sequencer = MidiSystem.getSequencer( );
        } catch(MidiUnavailableException me) {
            if (DEBUG || Printer.err)me.printStackTrace();
            return false;
        }
        if (sequencer==null) {
            return false;
        }

        try {
            sequence = MidiSystem.getSequence(in);
            if (sequence == null) {
                return false;
            }
        } catch (InvalidMidiDataException e) {
            if (DEBUG || Printer.err)e.printStackTrace();
            return false;
        }

        if (DEBUG || Printer.debug)Printer.debug("Created Sequencer.");
        return true;
    }
 
Example #28
Source File: MidiOutDevice.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
protected synchronized void implOpen() throws MidiUnavailableException {
    int index = ((AbstractMidiDeviceProvider.Info)getDeviceInfo()).getIndex();
    id = nOpen(index); // can throw MidiUnavailableException
    if (id == 0) {
        throw new MidiUnavailableException("Unable to open native device");
    }
}
 
Example #29
Source File: MidiDeviceGetReceivers.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void doAllTests() {
    MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
    for (int i = 0; i < infos.length; i++) {
        MidiDevice device = null;
        try {
            device = MidiSystem.getMidiDevice(infos[i]);
            doTest(device);
        } catch (MidiUnavailableException e) {
            out("Exception occured when retrieving device "+infos[i]+": "+e);
        }
    }
    if (infos.length == 0) {
        out("No MIDI devices exist or sound drivers not installed!");
    }
}
 
Example #30
Source File: SoftSynthesizer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void open() throws MidiUnavailableException {
    if (isOpen()) {
        synchronized (control_mutex) {
            implicitOpen = false;
        }
        return;
    }
    open(null, null);
}