public boolean init(int nbells) { fNBells = nbells; try { fSequencer = MidiSystem.getSequencer(); fSequencer.addMetaEventListener(fListener); fSynthesizer = MidiSystem.getSynthesizer(); fMidiOut = fSynthesizer.getReceiver(); fSequencer.getTransmitter().setReceiver(fMidiOut); /* System.out.println(" Listing available midi devices: "); MidiDevice.Info[] info = MidiSystem.getMidiDeviceInfo(); for (int i=0; i<info.length; i++) { Class c = MidiSystem.getMidiDevice(info[i]).getClass(); System.out.println(" MIDI device "+i+": "+info[i]+" is a "+c); } System.out.println("Using Sequencer "+fSequencer.getClass()+" and receiver "+fMidiOut.getClass()); */ } catch (MidiUnavailableException e) { System.out.println("Could not obtain MIDI device: " + e); return false; } return true; }
public Sequencer(int instrument, int tempo) { // Set up initial settings for the sequencer this.instrument = instrument; this.tempo = tempo; Synthesizer synth; ticks = 0; velocity = 64; // Mid volume try { // Setup values to create sequencer sequence = new Sequence(Sequence.PPQ, 16); sequencer = MidiSystem.getSequencer(); sequencer.open(); synth = MidiSystem.getSynthesizer(); synth.open(); sequencer.getTransmitter().setReceiver(synth.getReceiver()); sequencer.setTempoInBPM(tempo); track = sequence.createTrack(); } catch (InvalidMidiDataException e) { e.printStackTrace(); } catch (MidiUnavailableException e) { e.printStackTrace(); } }
public MidiViewImpl() { try { this.synth = MidiSystem.getSynthesizer(); this.receiver = synth.getReceiver(); this.synth.open(); } catch (MidiUnavailableException e) { e.printStackTrace(); } }
/** Gets the default transmitter and receiver, and then links them. */ private void linkTransmitterToReceiver() { try { // Set up the sequencer (including its tempo) sequencer.open(); sequencer.setSequence(sequence); sequencer.setTempoInBPM(song.getBPM()); // Get the system's default synthesizer and set that up, too. Synthesizer synth = MidiSystem.getSynthesizer(); synth.open(); // Get the receiver and transmitter to use and set those up. Receiver receiver = synth.getReceiver(); Transmitter transmitter = sequencer.getTransmitter(); transmitter.setReceiver(receiver); } catch (Exception ex) { // Something went wrong. ex.printStackTrace(); System.exit(1); } }
public void run() { try { if (waitfor != null) waitfor.join(); Resource res = Loading.waitforint(this.res); try { seq = MidiSystem.getSequencer(false); synth = MidiSystem.getSynthesizer(); seq.open(); seq.setSequence(res.layer(Resource.Music.class).seq); synth.open(); seq.getTransmitter().setReceiver(synth.getReceiver()); } catch (MidiUnavailableException e) { return; } catch (InvalidMidiDataException e) { return; } catch (IllegalArgumentException e) { /* The soft synthesizer appears to be throwing * non-checked exceptions through from the sampled * audio system. Ignore them and only them. */ if (e.getMessage().startsWith("No line matching")) return; throw (e); } seq.addMetaEventListener( new MetaEventListener() { public void meta(MetaMessage msg) { debug("Meta " + msg.getType()); if (msg.getType() == 47) { synchronized (Player.this) { done = true; Player.this.notifyAll(); } } } }); do { debug("Start loop"); done = false; seq.start(); synchronized (this) { while (!done) this.wait(); } seq.setTickPosition(0); } while (loop); } catch (InterruptedException e) { } finally { try { debug("Exit player"); if (seq != null) seq.close(); try { if (synth != null) synth.close(); } catch (Throwable e2) { if (e2 instanceof InterruptedException) { /* XXX: There appears to be a bug in Sun's * software MIDI implementation that throws back * an unchecked InterruptedException here when two * interrupts come close together (such as in the * case when the current player is first stopped, * and then another started immediately afterwards * on a new song before the first one has had time * to terminate entirely). */ } else { throw (new RuntimeException(e2)); } } } finally { synchronized (Music.class) { if (player == this) player = null; } } } }