Search in sources :

Example 1 with MidiUnavailableException

use of javax.sound.midi.MidiUnavailableException in project jdk8u_jdk by JetBrains.

the class TestAllDevices method main.

public static void main(String[] args) throws Exception {
    out("default receiver:");
    try {
        Receiver recv = MidiSystem.getReceiver();
        out("  receiver: " + recv);
        if (recv instanceof MidiDeviceReceiver) {
            out("    OK");
        } else {
            out("    ERROR: not an instance of MidiDeviceReceiver");
            failed = true;
        }
    } catch (MidiUnavailableException ex) {
        // this is not an error
        out("  receiver: MidiUnavailableException (test NOT failed)");
    }
    out("default transmitter:");
    try {
        Transmitter tran = MidiSystem.getTransmitter();
        out("  transmitter: " + tran);
        if (tran instanceof MidiDeviceTransmitter) {
            out("    OK");
        } else {
            out("    ERROR: not an instance of MidiDeviceTransmitter");
            failed = true;
        }
    } catch (MidiUnavailableException ex) {
        // this is not an error
        out("  transmitter: MidiUnavailableException (test NOT failed)");
    }
    MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
    for (MidiDevice.Info info : infos) {
        out(info.toString() + ":");
        try {
            MidiDevice dev = MidiSystem.getMidiDevice(info);
            dev.open();
            try {
                Receiver recv = dev.getReceiver();
                out("  receiver: " + recv);
                if (recv instanceof MidiDeviceReceiver) {
                    MidiDeviceReceiver devRecv = (MidiDeviceReceiver) recv;
                    MidiDevice retDev = devRecv.getMidiDevice();
                    if (retDev == dev) {
                        out("    OK");
                    } else {
                        out("    ERROR: getMidiDevice returned incorrect device: " + retDev);
                        failed = true;
                    }
                } else {
                    out("    ERROR: not an instance of MidiDeviceReceiver");
                    failed = true;
                }
            } catch (MidiUnavailableException ex) {
                // this is not an error
                out("  receiver: MidiUnavailableException (test NOT failed)");
            }
            try {
                Transmitter tran = dev.getTransmitter();
                out("  transmitter: " + tran);
                if (tran instanceof MidiDeviceTransmitter) {
                    MidiDeviceTransmitter devTran = (MidiDeviceTransmitter) tran;
                    MidiDevice retDev = devTran.getMidiDevice();
                    if (retDev == dev) {
                        out("    OK");
                    } else {
                        out("    ERROR: getMidiDevice retur4ned incorrect device: " + retDev);
                        failed = true;
                    }
                } else {
                    out("    ERROR: not an instance of MidiDeviceTransmitter");
                    failed = true;
                }
            } catch (MidiUnavailableException ex) {
                // this is not an error
                out("  transmitter: MidiUnavailableException (test NOT failed)");
            }
            dev.close();
        } catch (MidiUnavailableException ex) {
            out("  device: MidiUnavailableException (test NOT failed)");
        }
    }
    if (failed) {
        throw new Exception("Test failed.");
    }
}
Also used : MidiUnavailableException(javax.sound.midi.MidiUnavailableException) MidiDeviceTransmitter(javax.sound.midi.MidiDeviceTransmitter) Transmitter(javax.sound.midi.Transmitter) MidiDeviceReceiver(javax.sound.midi.MidiDeviceReceiver) Receiver(javax.sound.midi.Receiver) MidiDeviceReceiver(javax.sound.midi.MidiDeviceReceiver) MidiDevice(javax.sound.midi.MidiDevice) MidiDeviceTransmitter(javax.sound.midi.MidiDeviceTransmitter) MidiUnavailableException(javax.sound.midi.MidiUnavailableException)

Example 2 with MidiUnavailableException

use of javax.sound.midi.MidiUnavailableException in project jdk8u_jdk by JetBrains.

the class SequencerImplicitSynthOpen method main.

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.");
}
Also used : Sequencer(javax.sound.midi.Sequencer) MidiUnavailableException(javax.sound.midi.MidiUnavailableException) MidiDevice(javax.sound.midi.MidiDevice)

Example 3 with MidiUnavailableException

use of javax.sound.midi.MidiUnavailableException in project jdk8u_jdk by JetBrains.

the class SoftMidiAudioFileReader method getAudioInputStream.

public AudioInputStream getAudioInputStream(Sequence seq) throws UnsupportedAudioFileException, IOException {
    AudioSynthesizer synth = (AudioSynthesizer) new SoftSynthesizer();
    AudioInputStream stream;
    Receiver recv;
    try {
        stream = synth.openStream(format, null);
        recv = synth.getReceiver();
    } catch (MidiUnavailableException e) {
        throw new IOException(e.toString());
    }
    float divtype = seq.getDivisionType();
    Track[] tracks = seq.getTracks();
    int[] trackspos = new int[tracks.length];
    int mpq = 500000;
    int seqres = seq.getResolution();
    long lasttick = 0;
    long curtime = 0;
    while (true) {
        MidiEvent selevent = null;
        int seltrack = -1;
        for (int i = 0; i < tracks.length; i++) {
            int trackpos = trackspos[i];
            Track track = tracks[i];
            if (trackpos < track.size()) {
                MidiEvent event = track.get(trackpos);
                if (selevent == null || event.getTick() < selevent.getTick()) {
                    selevent = event;
                    seltrack = i;
                }
            }
        }
        if (seltrack == -1)
            break;
        trackspos[seltrack]++;
        long tick = selevent.getTick();
        if (divtype == Sequence.PPQ)
            curtime += ((tick - lasttick) * mpq) / seqres;
        else
            curtime = (long) ((tick * 1000000.0 * divtype) / seqres);
        lasttick = tick;
        MidiMessage msg = selevent.getMessage();
        if (msg instanceof MetaMessage) {
            if (divtype == Sequence.PPQ) {
                if (((MetaMessage) msg).getType() == 0x51) {
                    byte[] data = ((MetaMessage) msg).getData();
                    if (data.length < 3) {
                        throw new UnsupportedAudioFileException();
                    }
                    mpq = ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
                }
            }
        } else {
            recv.send(msg, curtime);
        }
    }
    long totallen = curtime / 1000000;
    long len = (long) (stream.getFormat().getFrameRate() * (totallen + 4));
    stream = new AudioInputStream(stream, stream.getFormat(), len);
    return stream;
}
Also used : MidiUnavailableException(javax.sound.midi.MidiUnavailableException) MidiMessage(javax.sound.midi.MidiMessage) Receiver(javax.sound.midi.Receiver) MidiEvent(javax.sound.midi.MidiEvent) IOException(java.io.IOException) AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) MetaMessage(javax.sound.midi.MetaMessage) Track(javax.sound.midi.Track)

Example 4 with MidiUnavailableException

use of javax.sound.midi.MidiUnavailableException in project jdk8u_jdk by JetBrains.

the class SoftSynthesizer method openStream.

public AudioInputStream openStream(AudioFormat targetFormat, Map<String, Object> info) throws MidiUnavailableException {
    if (isOpen())
        throw new MidiUnavailableException("Synthesizer is already open");
    synchronized (control_mutex) {
        gmmode = 0;
        voice_allocation_mode = 0;
        processPropertyInfo(info);
        open = true;
        implicitOpen = false;
        if (targetFormat != null)
            setFormat(targetFormat);
        if (load_default_soundbank) {
            Soundbank defbank = getDefaultSoundbank();
            if (defbank != null) {
                loadAllInstruments(defbank);
            }
        }
        voices = new SoftVoice[maxpoly];
        for (int i = 0; i < maxpoly; i++) voices[i] = new SoftVoice(this);
        mainmixer = new SoftMainMixer(this);
        channels = new SoftChannel[number_of_midi_channels];
        for (int i = 0; i < channels.length; i++) channels[i] = new SoftChannel(this, i);
        if (external_channels == null) {
            // when the synhtesizer is closed.
            if (channels.length < 16)
                external_channels = new SoftChannelProxy[16];
            else
                external_channels = new SoftChannelProxy[channels.length];
            for (int i = 0; i < external_channels.length; i++) external_channels[i] = new SoftChannelProxy();
        } else {
            // into the new one
            if (channels.length > external_channels.length) {
                SoftChannelProxy[] new_external_channels = new SoftChannelProxy[channels.length];
                for (int i = 0; i < external_channels.length; i++) new_external_channels[i] = external_channels[i];
                for (int i = external_channels.length; i < new_external_channels.length; i++) {
                    new_external_channels[i] = new SoftChannelProxy();
                }
            }
        }
        for (int i = 0; i < channels.length; i++) external_channels[i].setChannel(channels[i]);
        for (SoftVoice voice : getVoices()) voice.resampler = resampler.openStreamer();
        for (Receiver recv : getReceivers()) {
            SoftReceiver srecv = ((SoftReceiver) recv);
            srecv.open = open;
            srecv.mainmixer = mainmixer;
            srecv.midimessages = mainmixer.midimessages;
        }
        return mainmixer.getInputStream();
    }
}
Also used : MidiUnavailableException(javax.sound.midi.MidiUnavailableException) Soundbank(javax.sound.midi.Soundbank) Receiver(javax.sound.midi.Receiver)

Example 5 with MidiUnavailableException

use of javax.sound.midi.MidiUnavailableException in project jdk8u_jdk by JetBrains.

the class SoftSynthesizer method open.

public void open(SourceDataLine line, Map<String, Object> info) throws MidiUnavailableException {
    if (isOpen()) {
        synchronized (control_mutex) {
            implicitOpen = false;
        }
        return;
    }
    synchronized (control_mutex) {
        try {
            if (line != null) {
                // can throw IllegalArgumentException
                setFormat(line.getFormat());
            }
            AudioInputStream ais = openStream(getFormat(), info);
            weakstream = new WeakAudioStream(ais);
            ais = weakstream.getAudioInputStream();
            if (line == null) {
                if (testline != null) {
                    line = testline;
                } else {
                    // can throw LineUnavailableException,
                    // IllegalArgumentException, SecurityException
                    line = AudioSystem.getSourceDataLine(getFormat());
                }
            }
            double latency = this.latency;
            if (!line.isOpen()) {
                int bufferSize = getFormat().getFrameSize() * (int) (getFormat().getFrameRate() * (latency / 1000000f));
                // can throw LineUnavailableException,
                // IllegalArgumentException, SecurityException
                line.open(getFormat(), bufferSize);
                // Remember that we opened that line
                // so we can close again in SoftSynthesizer.close()
                sourceDataLine = line;
            }
            if (!line.isActive())
                line.start();
            int controlbuffersize = 512;
            try {
                controlbuffersize = ais.available();
            } catch (IOException e) {
            }
            // Tell mixer not fill read buffers fully.
            // This lowers latency, and tells DataPusher
            // to read in smaller amounts.
            //mainmixer.readfully = false;
            //pusher = new DataPusher(line, ais);
            int buffersize = line.getBufferSize();
            buffersize -= buffersize % controlbuffersize;
            if (buffersize < 3 * controlbuffersize)
                buffersize = 3 * controlbuffersize;
            if (jitter_correction) {
                ais = new SoftJitterCorrector(ais, buffersize, controlbuffersize);
                if (weakstream != null)
                    weakstream.jitter_stream = ais;
            }
            pusher = new SoftAudioPusher(line, ais, controlbuffersize);
            pusher_stream = ais;
            pusher.start();
            if (weakstream != null) {
                weakstream.pusher = pusher;
                weakstream.sourceDataLine = sourceDataLine;
            }
        } catch (final LineUnavailableException | SecurityException | IllegalArgumentException e) {
            if (isOpen()) {
                close();
            }
            // am: need MidiUnavailableException(Throwable) ctor!
            MidiUnavailableException ex = new MidiUnavailableException("Can not open line");
            ex.initCause(e);
            throw ex;
        }
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) MidiUnavailableException(javax.sound.midi.MidiUnavailableException) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException)

Aggregations

MidiUnavailableException (javax.sound.midi.MidiUnavailableException)5 Receiver (javax.sound.midi.Receiver)3 IOException (java.io.IOException)2 MidiDevice (javax.sound.midi.MidiDevice)2 AudioInputStream (javax.sound.sampled.AudioInputStream)2 MetaMessage (javax.sound.midi.MetaMessage)1 MidiDeviceReceiver (javax.sound.midi.MidiDeviceReceiver)1 MidiDeviceTransmitter (javax.sound.midi.MidiDeviceTransmitter)1 MidiEvent (javax.sound.midi.MidiEvent)1 MidiMessage (javax.sound.midi.MidiMessage)1 Sequencer (javax.sound.midi.Sequencer)1 Soundbank (javax.sound.midi.Soundbank)1 Track (javax.sound.midi.Track)1 Transmitter (javax.sound.midi.Transmitter)1 LineUnavailableException (javax.sound.sampled.LineUnavailableException)1 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)1