List of usage examples for javax.sound.midi Transmitter setReceiver
void setReceiver(Receiver receiver);
From source file:SoundPlayer.java
public SoundPlayer(File f, boolean isMidi) throws IOException, UnsupportedAudioFileException, LineUnavailableException, MidiUnavailableException, InvalidMidiDataException { if (isMidi) { // The file is a MIDI file midi = true;/*from ww w . j a va2 s . c o m*/ // First, get a Sequencer to play sequences of MIDI events // That is, to send events to a Synthesizer at the right time. sequencer = MidiSystem.getSequencer(); // Used to play sequences sequencer.open(); // Turn it on. // Get a Synthesizer for the Sequencer to send notes to Synthesizer synth = MidiSystem.getSynthesizer(); synth.open(); // acquire whatever resources it needs // The Sequencer obtained above may be connected to a Synthesizer // by default, or it may not. Therefore, we explicitly connect it. Transmitter transmitter = sequencer.getTransmitter(); Receiver receiver = synth.getReceiver(); transmitter.setReceiver(receiver); // Read the sequence from the file and tell the sequencer about it sequence = MidiSystem.getSequence(f); sequencer.setSequence(sequence); audioLength = (int) sequence.getTickLength(); // Get sequence length } else { // The file is sampled audio midi = false; // Getting a Clip object for a file of sampled audio data is kind // of cumbersome. The following lines do what we need. AudioInputStream ain = AudioSystem.getAudioInputStream(f); try { DataLine.Info info = new DataLine.Info(Clip.class, ain.getFormat()); clip = (Clip) AudioSystem.getLine(info); clip.open(ain); } finally { // We're done with the input stream. ain.close(); } // Get the clip length in microseconds and convert to milliseconds audioLength = (int) (clip.getMicrosecondLength() / 1000); } // Now create the basic GUI play = new JButton("Play"); // Play/stop button progress = new JSlider(0, audioLength, 0); // Shows position in sound time = new JLabel("0"); // Shows position as a # // When clicked, start or stop playing the sound play.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (playing) stop(); else play(); } }); // Whenever the slider value changes, first update the time label. // Next, if we're not already at the new position, skip to it. progress.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { int value = progress.getValue(); // Update the time label if (midi) time.setText(value + ""); else time.setText(value / 1000 + "." + (value % 1000) / 100); // If we're not already there, skip there. if (value != audioPosition) skip(value); } }); // This timer calls the tick() method 10 times a second to keep // our slider in sync with the music. timer = new javax.swing.Timer(100, new ActionListener() { public void actionPerformed(ActionEvent e) { tick(); } }); // put those controls in a row Box row = Box.createHorizontalBox(); row.add(play); row.add(progress); row.add(time); // And add them to this component. setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); this.add(row); // Now add additional controls based on the type of the sound if (midi) addMidiControls(); else addSampledControls(); }
From source file:de.ailis.midi4js.Midi4JS.java
/** * Sets the receiver of a transmitter.// w w w .j av a 2s. com * * @param transmitterHandle * The handle of the transmitter. * @param receiverHandle * The handle of the receiver. 0 to unset. */ public void setTransmitterReceiver(final int transmitterHandle, final int receiverHandle) { final Transmitter transmitter = resolveTransmitterHandle(transmitterHandle); final Receiver receiver = receiverHandle == 0 ? null : resolveReceiverHandle(receiverHandle); transmitter.setReceiver(receiver); }
From source file:org.monome.pages.Configuration.java
/** * Enables a MIDI in device to receive MIDI clock. * //from w w w.ja v a 2 s . co m * @param midiInDevice The MIDI input device to enable */ public void toggleMidiInDevice(MidiDevice midiInDevice) { // close the currently open device if we have one for (int i = 0; i < this.midiInDevices.size(); i++) { if (this.midiInDevices.get(i).equals(midiInDevice)) { MidiDevice inDevice = this.midiInDevices.get(i); System.out .println("closing midi in device " + i + " / " + this.midiInDevices.get(i).getDeviceInfo()); Transmitter transmitter = this.midiInTransmitters.get(i); this.midiInTransmitters.remove(i); this.midiInDevices.remove(i); transmitter.close(); inDevice.close(); return; } } // try to open the new midi in device try { midiInDevice.open(); Transmitter transmitter = midiInDevice.getTransmitter(); transmitter.setReceiver(this); this.midiInDevices.add(midiInDevice); this.midiInTransmitters.add(transmitter); } catch (MidiUnavailableException e) { e.printStackTrace(); } }