Search in sources :

Example 6 with Receiver

use of javax.sound.midi.Receiver 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 7 with Receiver

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

the class TestPreciseTimestampRendering method test.

public static void test(Soundbank soundbank) throws Exception {
    // Create instance of synthesizer using the testing soundbank above
    AudioSynthesizer synth = new SoftSynthesizer();
    AudioInputStream stream = synth.openStream(format, null);
    synth.unloadAllInstruments(synth.getDefaultSoundbank());
    synth.loadAllInstruments(soundbank);
    Receiver recv = synth.getReceiver();
    // Set volume to max and turn reverb off
    ShortMessage reverb_off = new ShortMessage();
    reverb_off.setMessage(ShortMessage.CONTROL_CHANGE, 91, 0);
    recv.send(reverb_off, -1);
    ShortMessage full_volume = new ShortMessage();
    full_volume.setMessage(ShortMessage.CONTROL_CHANGE, 7, 127);
    recv.send(full_volume, -1);
    Random random = new Random(3485934583945l);
    // Create random timestamps
    long[] test_timestamps = new long[30];
    for (int i = 1; i < test_timestamps.length; i++) {
        test_timestamps[i] = i * 44100 + (int) (random.nextDouble() * 22050.0);
    }
    // Send midi note on message to synthesizer
    for (int i = 0; i < test_timestamps.length; i++) {
        ShortMessage midi_on = new ShortMessage();
        midi_on.setMessage(ShortMessage.NOTE_ON, 69, 127);
        recv.send(midi_on, (long) ((test_timestamps[i] / 44100.0) * 1000000.0));
    }
    // Measure timing from rendered audio
    float[] fbuffer = new float[100];
    byte[] buffer = new byte[fbuffer.length * format.getFrameSize()];
    long firsts = -1;
    int counter = 0;
    long s = 0;
    long max_jitter = 0;
    outerloop: for (int k = 0; k < 10000000; k++) {
        stream.read(buffer);
        AudioFloatConverter.getConverter(format).toFloatArray(buffer, fbuffer);
        for (int i = 0; i < fbuffer.length; i++) {
            if (fbuffer[i] != 0) {
                if (firsts == -1)
                    firsts = s;
                long measure_time = (s - firsts);
                long predicted_time = test_timestamps[counter];
                long jitter = Math.abs(measure_time - predicted_time);
                if (jitter > 10)
                    max_jitter = jitter;
                counter++;
                if (counter == test_timestamps.length)
                    break outerloop;
            }
            s++;
        }
    }
    synth.close();
    if (counter == 0)
        throw new Exception("Nothing was measured!");
    if (max_jitter != 0) {
        throw new Exception("Jitter has occurred! " + "(max jitter = " + max_jitter + ")");
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Random(java.util.Random) ShortMessage(javax.sound.midi.ShortMessage) Receiver(javax.sound.midi.Receiver) SoftSynthesizer(com.sun.media.sound.SoftSynthesizer) AudioSynthesizer(com.sun.media.sound.AudioSynthesizer)

Example 8 with Receiver

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

the class GetReceivers method main.

public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    synth.open(new DummySourceDataLine(), null);
    assertTrue(synth.getReceivers().size() == 0);
    Receiver recv = synth.getReceiver();
    assertTrue(synth.getReceivers().size() == 1);
    recv.close();
    assertTrue(synth.getReceivers().size() == 0);
    synth.close();
}
Also used : Receiver(javax.sound.midi.Receiver)

Example 9 with Receiver

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

the class GetReceiver method main.

public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    synth.open(new DummySourceDataLine(), null);
    Receiver recv = synth.getReceiver();
    assertTrue(recv != null);
    Receiver recv2 = synth.getReceiver();
    assertTrue(recv2 != null);
    assertTrue(recv2 != recv);
    synth.close();
}
Also used : Receiver(javax.sound.midi.Receiver)

Example 10 with Receiver

use of javax.sound.midi.Receiver 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)

Aggregations

Receiver (javax.sound.midi.Receiver)10 MidiUnavailableException (javax.sound.midi.MidiUnavailableException)4 AudioSynthesizer (com.sun.media.sound.AudioSynthesizer)3 SoftSynthesizer (com.sun.media.sound.SoftSynthesizer)3 MidiDevice (javax.sound.midi.MidiDevice)2 MidiDeviceReceiver (javax.sound.midi.MidiDeviceReceiver)2 ShortMessage (javax.sound.midi.ShortMessage)2 Transmitter (javax.sound.midi.Transmitter)2 AudioInputStream (javax.sound.sampled.AudioInputStream)2 SoftReceiver (com.sun.media.sound.SoftReceiver)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Random (java.util.Random)1 MetaMessage (javax.sound.midi.MetaMessage)1 MidiDeviceTransmitter (javax.sound.midi.MidiDeviceTransmitter)1 MidiEvent (javax.sound.midi.MidiEvent)1 MidiMessage (javax.sound.midi.MidiMessage)1 Soundbank (javax.sound.midi.Soundbank)1 Synthesizer (javax.sound.midi.Synthesizer)1 Track (javax.sound.midi.Track)1