Package javax.sound.midi

Examples of javax.sound.midi.Receiver


    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();
                        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));
View Full Code Here


                return;

            if (!sequencer.isOpen())
                sequencer.open();

            sequencer.getTransmitter().setReceiver(new Receiver() {

                @Override
                public void send(MidiMessage message, long timeStamp) {

                    if(players.isEmpty()) {
View Full Code Here

                return;
            }
            if (!sequencer.isOpen()) {
                sequencer.open();
            }
            sequencer.getTransmitter().setReceiver(new Receiver() {

                @Override
                public void send(MidiMessage message, long timeStamp) {
                    if ((message.getStatus() & 0xF0) == ShortMessage.PROGRAM_CHANGE) {
                        ShortMessage msg = (ShortMessage) message;
View Full Code Here

        sm_sequencer.open();
        sm_sequencer.setSequence(sequence);
        if (!(sm_sequencer instanceof Synthesizer)) {
            sm_synthesizer = MidiSystem.getSynthesizer();
            sm_synthesizer.open();
            Receiver synthReceiver = sm_synthesizer.getReceiver();
            Transmitter seqTransmitter = sm_sequencer.getTransmitter();
            seqTransmitter.setReceiver(synthReceiver);
        }
    }
View Full Code Here

   
  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();     
   
  }
View Full Code Here

  }
   
  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();     

  }
View Full Code Here

       
    ReferenceCountingDevice rcd = (ReferenceCountingDevice)synth;   
         
    // Test single open/close cycle
   
    Receiver recv = rcd.getReceiverReferenceCounting();
    if(!synth.isOpen())
      throw new Exception("Synthesizer not open!");
    recv.close();
    if(synth.isOpen())
      throw new Exception("Synthesizer not closed!");

    // Test using 2 receiver cycle
   
    Receiver recv1 = rcd.getReceiverReferenceCounting();
    if(!synth.isOpen())
      throw new Exception("Synthesizer not open!");   
    Receiver recv2 = rcd.getReceiverReferenceCounting();
    if(!synth.isOpen())
      throw new Exception("Synthesizer not open!");

    recv2.close();
    if(!synth.isOpen())
      throw new Exception("Synthesizer was closed!");
    recv1.close();
    if(synth.isOpen())
      throw new Exception("Synthesizer not closed!");
   
    // Test for explicit,implicit conflict
   
    synth.open();
    Receiver recv3 = rcd.getReceiverReferenceCounting();
    if(!synth.isOpen())
      throw new Exception("Synthesizer not open!");
    recv3.close();
    if(!synth.isOpen())
      throw new Exception("Synthesizer was closed!");
    synth.close();
    if(synth.isOpen())
      throw new Exception("Synthesizer not closed!");

    // Test for implicit,explicit conflict
   
    recv3 = rcd.getReceiverReferenceCounting();
    synth.open();
    if(!synth.isOpen())
      throw new Exception("Synthesizer not open!");
    recv3.close();
    if(!synth.isOpen())
      throw new Exception("Synthesizer was closed!");
    synth.close();
    if(synth.isOpen())
      throw new Exception("Synthesizer not closed!");
View Full Code Here

        // 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];
View Full Code Here

    public static void main(String[] args) throws Exception {

        AudioSynthesizer synth = new SoftSynthesizer();
        synth.openStream(null, null);
        Receiver recv = synth.getReceiver();
        if (((SoftReceiver) recv).getMidiDevice() != synth) {
            throw new Exception("SoftReceiver.getMidiDevice() doesn't return "
                    + "instance of the synthesizer");
        }
        synth.close();
View Full Code Here

      throw new RuntimeException("assertTrue fails!");
  }
   
  public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    Receiver recv = synth.getReceiver();
    assertTrue(recv != null);
    ShortMessage sm = new ShortMessage();
    sm.setMessage(ShortMessage.NOTE_OFF, 0, 64, 64);
        synth.open(new DummySourceDataLine(), null);
    recv.send(sm, -1);   
    synth.close();     
    try
    {
      recv.send(sm, -1);
      throw new RuntimeException("Exception not thrown!");
    }
    catch(Exception e)
    {
      // Just checking if exception is thrown
View Full Code Here

TOP

Related Classes of javax.sound.midi.Receiver

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.