public void play() { if (isPlaying) { // 如果已经在播放,返回 return; } try { sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.setSequence(sequence); sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY ); sequencer.addMetaEventListener(this); } catch (InvalidMidiDataException ex) { } catch (MidiUnavailableException e) { } thread = new Thread(this); thread.start(); }
/** * Updates the sequencer state. This method should be invoked after setting the sequencer in the plugin. */ public void updateSequencer() { lock.lock(); try { MidiUtils.open(sequencer); // Close existing and create a sequencer transmitter that sends Sponge events. MidiUtils.close(sequencerTransmitter); if (sequencer != null) { sequencerTransmitter = sequencer.getTransmitter(); sequencerTransmitter.setReceiver(new MidiSpongeEventReceiver(this)); sequencer.addMetaEventListener(message -> { getEngine().getOperations().event(new MidiMetaMessageEvent(getMidiMetaMessageEventName(), getEngine().getDefaultParameters().getEventClonePolicy(), message)).send(); }); } } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } finally { lock.unlock(); } }
/** * Updates the synthesizer state. This method should be invoked after setting the synthesizer in the plugin. */ public void updateSynthesizer() { lock.lock(); try { MidiUtils.open(synthesizer); // Close existing and create a new synthesizer receiver to generate sounds. MidiUtils.close(synthesizerReceiver); if (synthesizer != null) { synthesizerReceiver = synthesizer.getReceiver(); } } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } finally { lock.unlock(); } }
/** * Updates the input device state. This method should be invoked after setting the input device in the plugin. */ public void updateInputDevice() { lock.lock(); try { MidiUtils.open(inputDevice); // Close existing and create an input device transmitter that sends Sponge events. MidiUtils.close(inputTransmitter); if (inputDevice != null) { inputTransmitter = inputDevice.getTransmitter(); inputTransmitter.setReceiver(new MidiSpongeEventReceiver(this)); } } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } finally { lock.unlock(); } }
private void open () throws MidiUnavailableException{ synth = MidiSystem.getSynthesizer(); MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); MidiDevice.Info msInfo = null; StringBuilder sb = new StringBuilder(); sb.append("Available MidiDevice are\n"); for ( MidiDevice.Info i:infos ){ if ( i.toString().contains("Microsoft GS Wavetable Synth") ){ msInfo = i; sb.append(" *****"); } sb.append("\t" + i.toString() + ": " + i.getDescription() + '\n'); } // MidiDevice msDevice = MidiSystem.getMidiDevice(msInfo); synth.open(); sb.append("synth=" + synth.getDeviceInfo().toString() + " with default soundbank " + synth.getDefaultSoundbank().getDescription() + '\n'); sb.append("max synthesizer latency =" + synth.getLatency() + " us\n"); log.info(sb.toString()); channels = synth.getChannels(); channel = channels[PERCUSSION_CHANNEL]; }
static boolean test(Sequencer sequencer) throws MidiUnavailableException { log(""); log("opening sequencer..."); sequencer.open(); // opens connected synthesizer implicitly MidiDevice synth = getConnectedDevice(sequencer); log(" connected device: " + getDeviceStr(synth)); log("closing sequencer..."); sequencer.close(); // closes the synth implicitly log(" synth is " + getDeviceStr(synth)); MidiDevice synth2 = getConnectedDevice(sequencer); log(" currently connected device: " + getDeviceStr(synth2)); if (synth != null && synth.isOpen()) { log("FAIL."); return false; } log("OK."); return true; }
@Override protected synchronized void implOpen() throws MidiUnavailableException { if (Printer.trace) Printer.trace("> MidiInDevice: implOpen()"); int index = ((MidiInDeviceProvider.MidiInDeviceInfo)getDeviceInfo()).getIndex(); id = nOpen(index); // can throw MidiUnavailableException if (id == 0) { throw new MidiUnavailableException("Unable to open native device"); } // create / start a thread to get messages if (midiInThread == null) { midiInThread = JSSecurityManager.createThread(this, "Java Sound MidiInDevice Thread", // name false, // daemon -1, // priority true); // doStart } nStart(id); // can throw MidiUnavailableException if (Printer.trace) Printer.trace("< MidiInDevice: implOpen() completed"); }
@Override protected void implOpen() throws MidiUnavailableException { if (Printer.trace) Printer.trace(">> RealTimeSequencer: implOpen()"); //openInternalSynth(); // create PlayThread playThread = new PlayThread(); //id = nOpen(); //if (id == 0) { // throw new MidiUnavailableException("unable to open sequencer"); //} if (sequence != null) { playThread.setSequence(sequence); } // propagate caches propagateCaches(); if (doAutoConnectAtNextOpen) { doAutoConnect(); } if (Printer.trace) Printer.trace("<< RealTimeSequencer: implOpen() succeeded"); }
static boolean runTest( boolean reloadInstr, // reload all instruments? boolean unloadTo, // unload "to" instrument? boolean unloadFrom // unload "from" instrument? ) { boolean success = false; try { success = test(reloadInstr, unloadFrom, unloadTo); } catch (final MidiUnavailableException ignored) { // the test is not applicable success = true; } catch (Exception ex) { log("Exception: " + ex.toString()); } cleanup(); return success; }
public static void main(String argv[]) { Sequencer seq = null; try { seq = MidiSystem.getSequencer(); seq.open(); } catch (final MidiUnavailableException ignored) { // the test is not applicable return; } try { seq.startRecording(); System.out.println("Test passed."); } catch (NullPointerException npe) { System.out.println("Caught NPE: "+npe); npe.printStackTrace(); throw new RuntimeException("Test FAILED!"); } catch (Exception e) { System.out.println("Unexpected Exception: "+e); e.printStackTrace(); System.out.println("Test NOT failed."); } finally { seq.close(); } }
/** * Execute Receiver.send() and expect that there is no exception. */ private static boolean testReceiverSend() { boolean result = true; Receiver receiver; ShortMessage shMsg = new ShortMessage(); try { receiver = MidiSystem.getReceiver(); shMsg.setMessage(ShortMessage.NOTE_ON, 0,60, 93); try { receiver.send( shMsg, -1 ); } catch(IllegalStateException ilEx) { ilEx.printStackTrace(System.out); out("IllegalStateException was thrown incorrectly!"); result = false; } receiver.close(); } catch(MidiUnavailableException e) { out("Midi unavailable, cannot test."); } catch(InvalidMidiDataException ine) { out("InvalidMidiDataException, cannot test."); } return result; }
private static void doAllTests() { boolean problemOccured = false; boolean succeeded = true; MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < infos.length; i++) { MidiDevice device = null; try { device = MidiSystem.getMidiDevice(infos[i]); succeeded &= doTest(device); } catch (MidiUnavailableException e) { out("exception occured; cannot test"); problemOccured = true; } } if (infos.length == 0) { out("Soundcard does not exist or sound drivers not installed!"); out("This test requires sound drivers for execution."); } isTestExecuted = !problemOccured; isTestPassed = succeeded; }
public static void openInput() throws Exception { setup(); if (bUseDefaultSynthesizer) { Synthesizer synth = MidiSystem.getSynthesizer(); synth.open(); r = synth.getReceiver(); try { Transmitter t = launchpad_s_plus.Launchpad.getInputDevice() .getTransmitter(); t.setReceiver(r); } catch (MidiUnavailableException e) { out("wasn't able to connect the device's Transmitter to the default Synthesizer:"); out(e); launchpad_s_plus.Launchpad.getInputDevice().close(); System.exit(1); } } out("\nNow taking input."); }
/** * Retrieve a MidiDevice.Info for a given name. * * This method tries to return a MidiDevice.Info whose name matches the * passed name. If no matching MidiDevice.Info is found, null is returned. * If bForOutput is true, then only output devices are searched, otherwise * only input devices. * * @param strDeviceName * the name of the device for which an info object should be * retrieved. * @param bForOutput * If true, only output devices are considered. If false, only * input devices are considered. * * @return A MidiDevice.Info object matching the passed device name or null * if none could be found. */ public static MidiDevice.Info getMidiDeviceInfo(String strDeviceName, boolean bForOutput) { MidiDevice.Info[] aInfos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < aInfos.length; i++) { if (aInfos[i].getName().contains(strDeviceName)) { try { MidiDevice device = MidiSystem.getMidiDevice(aInfos[i]); boolean bAllowsInput = (device.getMaxTransmitters() != 0); boolean bAllowsOutput = (device.getMaxReceivers() != 0); if ((bAllowsOutput && bForOutput) || (bAllowsInput && !bForOutput)) { return aInfos[i]; } } catch (MidiUnavailableException e) { e.printStackTrace(); } } } return null; }
private void init() throws MidiUnavailableException { devIn = MidiSystem.getMidiDevice(inInfo); devIn.open(); devOut = MidiSystem.getMidiDevice(outInfo); devOut.open(); midiIn = new MidiInput(); devIn.getTransmitter().setReceiver(midiIn); midiOut = (MidiDeviceReceiver)devOut.getReceiver(); //MIDI Message Sender Thread: midiTx = new Thread(() -> { while(true) { int size; while((size=cue.size()) > 0) { MidiMessage msg; synchronized(this) { msg = cue.get(0); cue.remove(0); } midiOut.send(msg, -1); if(size > 10000) { //Keep array under 10,000... synchronized(this) { size=cue.size(); while(size > 50) { cue.remove(size-1); size--; }} debug("Buffer had to be force-cleared!"); } if(Thread.interrupted()) return; } if(Thread.interrupted()) return; }}); midiTx.setPriority(1); midiTx.start(); programMode(); setAll(-1); //Reset LaunchPad Lights. }
private Synthesizer findAudioSynthesizer() throws MidiUnavailableException, ClassNotFoundException { Class<?> audioSynth = Class.forName("com.sun.media.sound.AudioSynthesizer"); // First check if default synthesizer is AudioSynthesizer. Synthesizer synth = MidiSystem.getSynthesizer(); if (audioSynth.isAssignableFrom(synth.getClass())) { return synth; } // If default synthesizer is not AudioSynthesizer, check others. MidiDevice.Info[] midiDeviceInfo = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < midiDeviceInfo.length; i++) { MidiDevice dev = MidiSystem.getMidiDevice(midiDeviceInfo[i]); if (audioSynth.isAssignableFrom(dev.getClass())) { return (Synthesizer)dev; } } return null; }
private static void playWarningSound() { // if (2 > 1) { // return; // } try { // int velocity = 127; // max volume int velocity = 90; // max volume int sound = 65; Synthesizer synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); MidiChannel channel = synthesizer.getChannels()[9]; // drums channel. for (int i = 0; i < 10; i++) { Thread.sleep(100); channel.noteOn(sound + i, velocity); Thread.sleep(100); channel.noteOff(sound + i); } } catch (MidiUnavailableException | InterruptedException e1) { e1.printStackTrace(); } }
public static MidiDevice getDevice(String inDeviceName) throws MiException { MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for(int i = 0; i < infos.length; i++) { if(infos[i].getName().equals(inDeviceName)) { try { MidiDevice device = MidiSystem.getMidiDevice(infos[i]); if( device.getMaxTransmitters() == 0 || device instanceof Sequencer) continue; return(device); } catch(MidiUnavailableException mue) { throw new MiException(TuxGuitar.getProperty("midiinput.error.midi.unavailable"), mue); } } } return(null); }
/** * This method initializes the MusicPlayer */ public static boolean init() { try { if (synth == null) synth = MidiSystem.getSynthesizer(); if (!synth.isOpen()) synth.open(); return true; } catch (MidiUnavailableException e) { e.printStackTrace(); return false; } }
public MidiReciever() { MidiDevice device; MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < infos.length; i++) { try { device = MidiSystem.getMidiDevice(infos[i]); //does the device have any transmitters? //if it does, add it to the device list System.out.println(infos[i]); //get all transmitters List<Transmitter> transmitters = device.getTransmitters(); //and for each transmitter for(int j = 0; j<transmitters.size();j++) { //create a new receiver transmitters.get(j).setReceiver( //using my own MidiInputReceiver new MidiInputReceiver(device.getDeviceInfo().toString()) ); } Transmitter trans = device.getTransmitter(); trans.setReceiver(new MidiInputReceiver(device.getDeviceInfo().toString())); //open each device device.open(); //if code gets this far without throwing an exception //print a success message System.out.println(device.getDeviceInfo()+" Was Opened"); } catch (MidiUnavailableException e) {} } }
public SoundPlayer() { try { synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); midiChannel = synthesizer.getChannels()[0]; } catch (MidiUnavailableException e) { e.printStackTrace(); } }
/** * Starts up this plugin. Obtains and opens the required MIDI devices and sets up receivers and transmitters. Any setters for MIDI * devices must be invoked before the Sponge startup. */ @Override public void onStartup() { try { logMidiSystemInfo(); // Use the default sequencer if not set. if (sequencer == null) { sequencer = MidiSystem.getSequencer(sequencerConnectedToSynthesizer); if (sequencer == null) { throw new SpongeException("The sequencer device is not supported"); } } // Use the default synthesizer if not set. if (synthesizer == null) { synthesizer = MidiSystem.getSynthesizer(); if (synthesizer == null) { throw new SpongeException("The synthesizer device is not supported"); } } update(); if (loadAllInstruments) { loadAllInstruments(); } } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } }
/** * Returns all available MIDI devices. * * @return all available MIDI devices. */ public static List<MidiDevice> getDevices() { return Arrays.stream(MidiSystem.getMidiDeviceInfo()).map(info -> { try { return MidiSystem.getMidiDevice(info); } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } }).collect(Collectors.toList()); }
/** * Returns the default input MIDI device. * * @return the default input MIDI device or {@code null} if not found. */ public static MidiDevice getDefaultInputDevice() { try { Transmitter transmitter = MidiSystem.getTransmitter(); if (transmitter != null && transmitter instanceof MidiDeviceTransmitter) { return ((MidiDeviceTransmitter) transmitter).getMidiDevice(); } return null; } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } }
/** * Returns the map of all available MIDI devices. * * @return the map of all available MIDI devices. */ public static Map<MidiDevice.Info, MidiDevice> getDeviceMap() { Map<MidiDevice.Info, MidiDevice> result = new LinkedHashMap<>(); try { for (Info info : MidiSystem.getMidiDeviceInfo()) { result.put(info, MidiSystem.getMidiDevice(info)); } } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } return result; }
/** * Opens the MIDI device. * * @param device the MIDI device. */ public static void open(MidiDevice device) { if (device != null && !device.isOpen()) { try { device.open(); } catch (MidiUnavailableException e) { throw SpongeUtils.wrapException(e); } } }
public Instru() { try { // On récupère le synthétiseur, on l'ouvre et on obtient un canal synthetiseur = MidiSystem.getSynthesizer(); synthetiseur.open(); } catch (MidiUnavailableException ex) { Logger.getLogger(Instru.class.getName()).log(Level.SEVERE, null, ex); } canal = synthetiseur.getChannels()[0]; // On initialise l'instrument 0 (le piano) pour le canal canal.programChange(0); }
public static void main(String[ ] args) throws MidiUnavailableException { // We don't need a Sequencer in this example, since we send MIDI // events directly to the Synthesizer instead. Synthesizer synthesizer = MidiSystem.getSynthesizer( ); synthesizer.open( ); JFrame frame = new KeyboardDrumsDemo(synthesizer); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(50, 128); // We use window width as volume control frame.setVisible(true); }
/** Constructs a new DrumSound, using the midi channel number given. * The synthesizer is opened. * The note and duration are also set from stored preferences for this drumNumber. * * @param drumNumber an arbitrary drum number - has nothing to do with sound emitted. */ public DrumSound (int drumNumber) throws MidiUnavailableException{ this.drumNumber = drumNumber; open(); this.note = prefs.getInt(prefsKey() + ".note",PERCUSSION_NOTE_NUMBERS[0]); this.durationMs = prefs.getInt(prefsKey() + ".durationMs",500); this.velocity = prefs.getInt(prefsKey() + ".velocity",127); findSoundNameAndIndex(); }
private boolean createSequencer(BufferedInputStream in) throws IOException { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSequencer()"); // get the sequencer try { sequencer = MidiSystem.getSequencer( ); } catch(MidiUnavailableException me) { if (DEBUG || Printer.err)me.printStackTrace(); return false; } if (sequencer==null) { return false; } try { sequence = MidiSystem.getSequence(in); if (sequence == null) { return false; } } catch (InvalidMidiDataException e) { if (DEBUG || Printer.err)e.printStackTrace(); return false; } if (DEBUG || Printer.debug)Printer.debug("Created Sequencer."); return true; }
public MidiDevice getDevice(MidiDevice.Info info) { if ((info != null) && (!info.equals(RealTimeSequencer.info))) { return null; } try { return new RealTimeSequencer(); } catch (MidiUnavailableException e) { return null; } }
public void open() throws MidiUnavailableException { if (isOpen()) { synchronized (control_mutex) { implicitOpen = false; } return; } open(null, null); }
public Receiver getReceiver() throws MidiUnavailableException { synchronized (control_mutex) { SoftReceiver receiver = new SoftReceiver(this); receiver.open = open; recvslist.add(receiver); return receiver; } }
public Receiver getReceiverReferenceCounting() throws MidiUnavailableException { if (!isOpen()) { open(); synchronized (control_mutex) { implicitOpen = true; } } return getReceiver(); }
private static void checkAudio() { try { if (javax.sound.midi.MidiSystem.getSequencer() == null) System.out.println("MidiSystem sequencer unavailable."); else if (javax.sound.sampled.AudioSystem.getMixer(null) == null) System.out.println("AudioSystem unavailable."); } catch (MidiUnavailableException e) { e.printStackTrace(); } System.out.println("AudioSystem and MidiSystem Sequencer found."); }