List of usage examples for javax.sound.midi Synthesizer getReceiver
Receiver getReceiver() throws MidiUnavailableException;
From source file:PlayerPiano.java
public static void main(String[] args) throws MidiUnavailableException, InvalidMidiDataException, IOException { int instrument = 0; int tempo = 120; String filename = null;/*from ww w . j a v a 2s . c o m*/ // Parse the options // -i <instrument number> default 0, a piano. Allowed values: 0-127 // -t <beats per minute> default tempo is 120 quarter notes per minute // -o <filename> save to a midi file instead of playing int a = 0; while (a < args.length) { if (args[a].equals("-i")) { instrument = Integer.parseInt(args[a + 1]); a += 2; } else if (args[a].equals("-t")) { tempo = Integer.parseInt(args[a + 1]); a += 2; } else if (args[a].equals("-o")) { filename = args[a + 1]; a += 2; } else break; } char[] notes = args[a].toCharArray(); // 16 ticks per quarter note. Sequence sequence = new Sequence(Sequence.PPQ, 16); // Add the specified notes to the track addTrack(sequence, instrument, tempo, notes); if (filename == null) { // no filename, so play the notes // Set up the Sequencer and Synthesizer objects Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); Synthesizer synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); sequencer.getTransmitter().setReceiver(synthesizer.getReceiver()); // Specify the sequence to play, and the tempo to play it at sequencer.setSequence(sequence); sequencer.setTempoInBPM(tempo); // Let us know when it is done playing sequencer.addMetaEventListener(new MetaEventListener() { public void meta(MetaMessage m) { // A message of this type is automatically sent // when we reach the end of the track if (m.getType() == END_OF_TRACK) System.exit(0); } }); // And start playing now. sequencer.start(); } else { // A file name was specified, so save the notes int[] allowedTypes = MidiSystem.getMidiFileTypes(sequence); if (allowedTypes.length == 0) { System.err.println("No supported MIDI file types."); } else { MidiSystem.write(sequence, allowedTypes[0], new File(filename)); System.exit(0); } } }
From source file:Main.java
public static void streamMidiSequence(URL url) throws IOException, InvalidMidiDataException, MidiUnavailableException { Sequencer sequencer = null; // Converts a Sequence to MIDI events Synthesizer synthesizer = null; // Plays notes in response to MIDI events try {//w ww . j ava 2 s . c om // Create, open, and connect a Sequencer and Synthesizer // They are closed in the finally block at the end of this method. sequencer = MidiSystem.getSequencer(); sequencer.open(); synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); sequencer.getTransmitter().setReceiver(synthesizer.getReceiver()); // Specify the InputStream to stream the sequence from sequencer.setSequence(url.openStream()); // This is an arbitrary object used with wait and notify to // prevent the method from returning before the music finishes final Object lock = new Object(); // Register a listener to make the method exit when the stream is // done. See Object.wait() and Object.notify() sequencer.addMetaEventListener(new MetaEventListener() { public void meta(MetaMessage e) { if (e.getType() == END_OF_TRACK) { synchronized (lock) { lock.notify(); } } } }); // Start playing the music sequencer.start(); // Now block until the listener above notifies us that we're done. synchronized (lock) { while (sequencer.isRunning()) { try { lock.wait(); } catch (InterruptedException e) { } } } } finally { // Always relinquish the sequencer, so others can use it. if (sequencer != null) sequencer.close(); if (synthesizer != null) synthesizer.close(); } }
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 w ww . j a v a2 s .c om // 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(); }