/** Plays sequence */ public void playSequence(float speed) { try { fSequencer.open(); fSynthesizer.open(); // fSynthesizer.loadAllInstruments(fSynthesizer.getDefaultSoundbank()); Instrument[] instruments = fSynthesizer.getAvailableInstruments(); /* System.out.println("Instruments: "+fSynthesizer.getLoadedInstruments().length+", "+fSynthesizer.getAvailableInstruments().length); for (int i=0; i<instruments.length; i++) System.out.println("Instrument "+i+": "+instruments[i].getName()+", "+instruments[i].getPatch().getBank()+", "+instruments[i].getPatch().getProgram()); */ if (instruments.length > 180) { // System.out.println("Remap: "+fSynthesizer.remapInstrument(instruments[14], // instruments[157])); // fSynthesizer.unloadInstrument(instruments[14]); } } catch (MidiUnavailableException e) { System.out.println("Error opening sequencer: " + e); return; } try { fSequencer.setSequence(fSequence); setPlaybackSpeed(speed); fSequencer.start(); } catch (InvalidMidiDataException e) { System.out.println("Error playing sequence: " + e); } }
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 void programChange(int program) { cc.program = program; if (instruments != null) { synthesizer.loadInstrument(instruments[program]); cc.channel.programChange(program); } }
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 static void main(String[] args) throws Exception { parseArguments(args); List<Iterator<String>> segments = Splitter.split(inputFilePath, numberOfThreads); List<Future<Analytic>> futures = new ArrayList<Future<Analytic>>(segments.size()); ExecutorService threadPool = Executors.newFixedThreadPool(segments.size()); for (final Iterator<String> segment : segments) { Future<Analytic> future = threadPool.submit( new Callable<Analytic>() { @Override public Analytic call() throws Exception { return new Analyzer(segment).parse(); } }); futures.add(future); } try { ArrayList<Analytic> analytics = new ArrayList<Analytic>(futures.size()); for (Future<Analytic> future : futures) { analytics.add(future.get()); } Analytic result = Synthesizer.synthesize(analytics); writeResult(result); } finally { threadPool.shutdown(); } }
public MidiViewImpl() { try { this.synth = MidiSystem.getSynthesizer(); this.receiver = synth.getReceiver(); this.synth.open(); } catch (MidiUnavailableException e) { e.printStackTrace(); } }
/** * Sets the equalizer. * * @param f the equalizer */ final void setEqualizer(Object f) { if (spline == null) { spline = new Spline(); } equalizer = new Equalizer(f, spline, information); float af[] = equalizer.getBands(); if (filter1 != null) { filter1.setEQ(af); } if (filter2 != null) { filter2.setEQ(af); } information.put(FA_EQUALIZE, f); }
/** Frees all system resources, which are bounded to this object. */ @Override public void close() { super.close(); if (filter1 != null) { filter1.close(); } filter1 = null; if (filter2 != null) { filter2.close(); } filter2 = null; if (equalizer != null) { equalizer.close(); } equalizer = null; if (spline != null) { spline.close(); } spline = null; }
/** 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 close() { System.out.println("midi closing"); if (synthesizer != null) { synthesizer.close(); } if (sequencer != null) { sequencer.close(); } sequencer = null; synthesizer = null; instruments = null; channels = null; }
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; }
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; } } } }
public void stop() { fSequencer.stop(); fSequencer.close(); fSynthesizer.close(); fMidiOut.close(); }
public static void main(String[] args) { try { SynthesizerModeDesc desc = new SynthesizerModeDesc(Locale.ENGLISH); synth = Central.createSynthesizer(desc); synth.addEngineListener(new TestEngineListener()); synth.addSpeakableListener(new TestSpeakableListener()); synth.addEngineListener( new SynthesizerAdapter() { public void queueUpdated(SynthesizerEvent e) { System.out.println("wait till queue empty..."); try { synth.waitEngineState(synth.QUEUE_EMPTY); } catch (InterruptedException ex) { ex.printStackTrace(); } System.out.println("...waiting ended"); } }); synth.allocate(); synth.resume(); synth.waitEngineState(Synthesizer.ALLOCATED); SynthesizerProperties props = synth.getSynthesizerProperties(); // SAPI5 engine... // Voice v = new Voice("Microsoft Mary",Voice.GENDER_FEMALE, Voice.AGE_DONT_CARE, null); // ...or SAPI4 engine Voice v = new Voice("Mary", Voice.GENDER_FEMALE, Voice.AGE_DONT_CARE, null); props.setVoice(v); // This starts all events for this Synthesizer in the current Thread. // With this call, the waitEngineState call will prevent *any* other // speech events from being sent since it will halt the current thread ((CGEngineProperties) props).setEventsInNewThread(true); synth.speak("Hello World!", null); synth.waitEngineState(synth.QUEUE_EMPTY); System.out.println("all done"); } catch (Exception e) { e.printStackTrace(System.out); } catch (Error e1) { e1.printStackTrace(System.out); } finally { try { synth.deallocate(); synth.waitEngineState(synth.DEALLOCATED); } catch (Exception e2) { } System.exit(0); } }