public void go() { setUpGui(); try { Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); // make a sequencer and open sequencer.addControllerEventListener(m1, new int[] {127}); Sequence seq = new Sequence(Sequence.PPQ, 4); Track track = seq.createTrack(); int r = 0; for (int i = 0; i < 300; i += 4) { r = (int) ((Math.random() * 50) + 1); track.add(makeEvent(144, 1, r, 100, i)); track.add(makeEvent(176, 1, 127, 0, i)); track.add(makeEvent(128, 1, r, 100, i + 2)); } // end loop sequencer.setSequence(seq); sequencer.start(); sequencer.setTempoInBPM(120); } catch (Exception ex) { ex.printStackTrace(); } } // close method
{ try { sq = MidiSystem.getSequencer(); } catch (MidiUnavailableException e) { e.printStackTrace(); } }
/** Creates a new MidiPlayer object. */ public MidiPlayer() { try { sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.addMetaEventListener(this); } catch (MidiUnavailableException ex) { sequencer = null; } }
/** * Sets up the sequencer with a given sequence. * * @param sequenceToUse * @throws MidiUnavailableException */ private void setUpSequencer() throws MidiUnavailableException { // First, get the system's default sequencer. try { sequencer = MidiSystem.getSequencer(); } catch (MidiUnavailableException ex) { // Something went wrong. ex.printStackTrace(); System.exit(1); } // If there is none, throw an exception. if (sequencer == null) { String msg = "Cannot find a sequencer"; throw new MidiUnavailableException(msg); } // Set up the transmitter and receiver of the synth to play the song. linkTransmitterToReceiver(); }
private static void playMidi(String name, boolean loop, double volume) throws MidiUnavailableException, FileNotFoundException, IOException, InvalidMidiDataException { // Obtains the default Sequencer connected to a default device. Sequencer sequencer = MidiSystem.getSequencer(); // Opens the device, indicating that it should now acquire any system resources it requires and // become operational. sequencer.open(); // create a stream from a file InputStream is = new BufferedInputStream(new FileInputStream(new File(path + name))); // Sets the current sequence on which the sequencer operates. // The stream must point to MIDI file data. sequencer.setSequence(is); // Set looping if (loop) { sequencer.setLoopCount(LOOP_CONTINUOUSLY); } // Starts playback of the MIDI data in the currently loaded sequence. sequencer.start(); midiMap.put(name, sequencer); }
public boolean open() { try { if (synthesizer == null) { if ((synthesizer = MidiSystem.getSynthesizer()) == null) { System.out.println("getSynthesizer() failed!"); return false; } } synthesizer.open(); sequencer = MidiSystem.getSequencer(); sequencer.addMetaEventListener(new ProcessMeta()); sequence = new Sequence(Sequence.PPQ, 10); } catch (Exception ex) { System.out.println("midi exception 1 "); ex.printStackTrace(); return false; } System.out.println("midi opening"); Soundbank sb = synthesizer.getDefaultSoundbank(); if (sb != null) { instruments = synthesizer.getDefaultSoundbank().getInstruments(); synthesizer.loadInstrument(instruments[0]); } MidiChannel midiChannels[] = synthesizer.getChannels(); numChannels = midiChannels.length; channels = new ChannelData[midiChannels.length]; if (channels.length == 0) return false; for (int i = 0; i < channels.length; i++) { channels[i] = new ChannelData(midiChannels[i], i); } cc = channels[0]; return true; }