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(); }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
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; }
static MidiDevice getConnectedDevice(Sequencer sequencer) { List<Transmitter> trans = sequencer.getTransmitters(); log(" sequencer has " + trans.size() + " opened transmitters:"); for (Transmitter tr: trans) { Receiver r = tr.getReceiver(); log(" " + getClassStr(tr) + " connected to " + getClassStr(r)); if (r instanceof MidiDeviceReceiver) { MidiDeviceReceiver recv = (MidiDeviceReceiver)r; MidiDevice dev = recv.getMidiDevice(); log(" - receiver of " + getClassStr(dev)); return dev; } else { log(" - does NOT implement MidiDeviceReceiver"); } } return null; }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class<?> typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String name = typeClass.getName(); String value = AccessController.doPrivileged( (PrivilegedAction<String>) () -> System.getProperty(name)); if (value == null) { value = getProperties().getProperty(name); } if ("".equals(value)) { value = null; } return value; }
public static void doTest(Sequencer seq) throws Exception { seq.setTempoInMPQ(3.0f); System.out.println("Setting tempo in MPQ to "+3.0f); if (!equalsFloat(seq.getTempoInMPQ(), 3.0f)) { System.err.println("getTempoInMPQ() returns wrong value : " + seq.getTempoInMPQ()); failed = true; } System.out.println("Setting tempo factor to "+2.0f); seq.setTempoFactor(2.0f); if (!equalsFloat(seq.getTempoFactor(), 2.0f)) { System.err.println("getTempoFactor() returns: " + seq.getTempoFactor()); failed = true; } float bpmTempo = 120.0f; System.out.println("Setting tempo to "+120.0f+"bpm"); seq.setTempoInBPM(bpmTempo); if (!equalsFloat(seq.getTempoInMPQ(), (60000000.0f/seq.getTempoInBPM()))) { System.err.println("getTempoInMPQ() returns: " + seq.getTempoInMPQ()); System.err.println("getTempoInBPM() returns: " + seq.getTempoInBPM()); failed = true; } }
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(); } }
/** * Returns true if at least one MIDI (port) device is correctly installed on * the system. */ public static boolean isMidiInstalled() { boolean result = false; MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < devices.length; i++) { try { MidiDevice device = MidiSystem.getMidiDevice(devices[i]); result = ! (device instanceof Sequencer) && ! (device instanceof Synthesizer); } catch (Exception e1) { System.err.println(e1); } if (result) break; } return result; }
/** * Returns true if at least one MIDI (port) device is correctly installed on * the system. */ private static boolean isMidiInstalled() { boolean result = false; MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < devices.length; i++) { try { MidiDevice device = MidiSystem.getMidiDevice(devices[i]); result = !(device instanceof Sequencer) && !(device instanceof Synthesizer); } catch (Exception e1) { System.err.println(e1); } if (result) break; } return result; }
private static void doAll() throws Exception { MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i=0; i < infos.length; i++) { MidiDevice device = MidiSystem.getMidiDevice(infos[i]); if ((! (device instanceof Sequencer)) && (! (device instanceof Synthesizer)) && (device.getMaxReceivers() > 0 || device.getMaxReceivers() == -1)) { System.out.println("--------------"); System.out.println("Testing MIDI device: " + infos[i]); testDevice(device); } if (infos.length==0) { System.out.println("No MIDI devices available!"); } } }
/** * Returns true if at least one MIDI (port) device is correctly installed on * the system. */ public static boolean isMidiInstalled() { boolean result = false; MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < devices.length; i++) { try { MidiDevice device = MidiSystem.getMidiDevice(devices[i]); result = ! (device instanceof Sequencer) && ! (device instanceof Synthesizer); } catch (Exception e1) { System.err.println(e1); } if (result) break; } if (!result) { System.err.println("Soundcard does not exist or sound drivers not installed!"); System.err.println("This test requires sound drivers for execution."); } return result; }
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); }
public DesktopMidiPlayer() throws MidiUnavailableException, 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 // FileHandle fh = Gdx.files.internal(Constants.MUSIC); // InputStream is = new BufferedInputStream(new FileInputStream(fh.file())); InputStream is = new BufferedInputStream(new FileInputStream(new File(Constants.MUSIC2))); // Sets the current sequence on which the sequencer operates. // The stream must point to MIDI file data. sequencer.setSequence(is); // Starts playback of the MIDI data in the currently loaded sequence. sequencer.start(); /*try { this.sequencer = MidiSystem.getSequencer(); } catch (MidiUnavailableException e) { // Log.error("Error opening midi device.", e); }*/ }
public void go() { setUpGui(); try { Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.addControllerEventListener(ml, new int[] {127}); Sequence seq = new Sequence(Sequence.PPQ, 4); Track track = seq.createTrack(); int r = 0; for (int i = 0; i < 60; 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();} }
public void go() { try { Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); int[] eventsIWant = {127}; sequencer.addControllerEventListener(this, eventsIWant); Sequence seq = new Sequence(Sequence.PPQ, 4); Track track = seq.createTrack(); for (int i = 5; i < 60; i+= 4) { track.add(makeEvent(144, 1, i, 100, i)); track.add(makeEvent(176, 1, 127, 0, i)); track.add(makeEvent(128, 1, i, 100, i + 2)); } // end loop sequencer.setSequence(seq); sequencer.setTempoInBPM(220); sequencer.start(); } catch (Exception ex) {ex.printStackTrace();} }
public static void main(String[] args) { try { // make and open a sequencer Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); // make a sequence and a track Sequence seq = new Sequence(Sequence.PPQ, 4); Track track = seq.createTrack(); // make a bunch of events to make the notes keep going up // (from piano note 5 to piano note 61) for (int i = 5; i < 61; i+=4) { track.add(makeEvent(144, 1, i, 100, i)); track.add(makeEvent(128, 1, i, 100, i + 2)); } // end loop sequencer.setSequence(seq); sequencer.setTempoInBPM(220); sequencer.start(); } catch (Exception ex) {ex.printStackTrace();} }
public static void startMusic(int song) { if(!mute){ stopMusic(); if (sequencer != null) { try { sequencer.open(); sequencer.setSequence((Sequence)null); sequencer.setSequence(songs[song]); int i = Sequencer.LOOP_CONTINUOUSLY; sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY); sequencer.start(); } catch (Exception e) { } } } }
/** Obtain the value of a default provider property. @param typeClass The type of the default provider property. This should be one of Receiver.class, Transmitter.class, Sequencer.class, Synthesizer.class, SourceDataLine.class, TargetDataLine.class, Clip.class or Port.class. @return The complete value of the property, if available. If the property is not set, null is returned. */ private static synchronized String getDefaultProvider(Class typeClass) { if (!SourceDataLine.class.equals(typeClass) && !TargetDataLine.class.equals(typeClass) && !Clip.class.equals(typeClass) && !Port.class.equals(typeClass) && !Receiver.class.equals(typeClass) && !Transmitter.class.equals(typeClass) && !Synthesizer.class.equals(typeClass) && !Sequencer.class.equals(typeClass)) { return null; } String value; String propertyName = typeClass.getName(); value = JSSecurityManager.getProperty(propertyName); if (value == null) { value = getProperties().getProperty(propertyName); } if ("".equals(value)) { value = null; } return value; }