javax.sound.midi.MidiSystem Java Examples
The following examples show how to use
javax.sound.midi.MidiSystem.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExtraCharInSoundbank.java From openjdk-jdk9 with GNU General Public License v2.0 | 7 votes |
public static void main(String[] args) throws Exception { // the internal synthesizer needs a soundcard to work properly if (!isSoundcardInstalled()) { return; } Synthesizer theSynth = MidiSystem.getSynthesizer(); System.out.println("Got synth: "+theSynth); theSynth.open(); try { Soundbank theSoundbank = theSynth.getDefaultSoundbank(); System.out.println("Got soundbank: "+theSoundbank); theSynth.loadAllInstruments(theSoundbank); try { if (!checkInstrumentNames(theSynth)) { throw new Exception("Test failed"); } } finally { theSynth.unloadAllInstruments(theSoundbank); } } finally { theSynth.close(); } System.out.println("Test passed."); }
Example #2
Source File: MidiTest.java From kyoko with MIT License | 7 votes |
public static void main(String... args) throws MidiUnavailableException, IOException, InvalidMidiDataException { var synth = MidiSystem.getSynthesizer(); synth.loadAllInstruments(synth.getDefaultSoundbank()); var sequencer = MidiSystem.getSequencer(); var sequence = MidiSystem.getSequence(new File("test.mid")); // sequencer.getTransmitter().setReceiver(synth.getReceiver()); sequencer.open(); sequencer.setSequence(sequence); sequencer.setTempoFactor(1f); sequencer.addMetaEventListener(meta -> { if (meta.getType() == 47) { // track end sequencer.setTickPosition(0); sequencer.start(); } }); sequencer.start(); }
Example #3
Source File: Midi2WavRenderer.java From computoser with GNU Affero General Public License v3.0 | 7 votes |
public static AudioSynthesizer findAudioSynthesizer() throws MidiUnavailableException, IOException, InvalidMidiDataException { // First check if default synthesizer is AudioSynthesizer. Synthesizer synth = MidiSystem.getSynthesizer(); if (synth instanceof AudioSynthesizer) { return (AudioSynthesizer) synth; } // If default synhtesizer is not AudioSynthesizer, check others. Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < infos.length; i++) { MidiDevice dev = MidiSystem.getMidiDevice(infos[i]); if (dev instanceof AudioSynthesizer) { return (AudioSynthesizer) dev; } } // No AudioSynthesizer was found, return null. return null; }
Example #4
Source File: UnsupportedInfo.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public static void main(final String[] args) { final MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (final MidiDeviceProvider mdp : load(MidiDeviceProvider.class)) { for (final MidiDevice.Info info : infos) { if (mdp.isDeviceSupported(info)) { if (mdp.getDevice(info) == null) { throw new RuntimeException("MidiDevice is null"); } } else { try { mdp.getDevice(info); throw new RuntimeException( "IllegalArgumentException expected"); } catch (final IllegalArgumentException ignored) { // expected } } } } }
Example #5
Source File: ReceiverTransmitterAvailable.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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; }
Example #6
Source File: MidiOutGetMicrosecondPositionBug.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * 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; }
Example #7
Source File: PassportMidiInterface.java From jace with GNU General Public License v2.0 | 6 votes |
@Override public void resume() { if (isRunning() && midiOut != null) { return; } try { MidiDevice selectedDevice = MidiSystem.getSynthesizer(); MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); if (devices.length == 0) { System.out.println("No MIDI devices found"); } else { for (MidiDevice.Info dev : devices) { if (MidiSystem.getMidiDevice(dev).getMaxReceivers() == 0) { continue; } System.out.println("MIDI Device found: " + dev); if ((preferredMidiDevice.getValue() == null && dev.getName().contains("Java Sound") && dev instanceof Synthesizer) || preferredMidiDevice.getValue().equalsIgnoreCase(dev.getName()) ) { selectedDevice = MidiSystem.getMidiDevice(dev); break; } } } if (selectedDevice != null) { System.out.println("Selected MIDI device: " + selectedDevice.getDeviceInfo().getName()); selectedDevice.open(); midiOut = selectedDevice.getReceiver(); super.resume(); } } catch (MidiUnavailableException ex) { System.out.println("Could not open MIDI synthesizer"); Logger.getLogger(PassportMidiInterface.class.getName()).log(Level.SEVERE, null, ex); } }
Example #8
Source File: LMidiSound.java From RipplePower with Apache License 2.0 | 6 votes |
public LMidiSound(String fileName) throws IOException { bytes = new ArrayByte(UIRes.getStream(fileName), ArrayByte.BIG_ENDIAN); if (rendererStatus == UNINITIALIZED) { rendererStatus = INITIALIZING; Thread thread = new Thread() { public final void run() { try { Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); volumeSupported = (sequencer instanceof Synthesizer); sequencer.close(); available = true; } catch (Throwable e) { available = false; } rendererStatus = INITIALIZED; } }; thread.setDaemon(true); thread.start(); } }
Example #9
Source File: MidiEngine.java From javagame with MIT License | 6 votes |
/** * MIDI�t�@�C�������[�h * @param url MIDI�t�@�C����URL */ public static void load(URL url) throws MidiUnavailableException, InvalidMidiDataException, IOException { if (sequencer == null) { // �V�[�P���T���擾 sequencer = MidiSystem.getSequencer(); // �V�[�P���T���J�� sequencer.open(); // ���^�C�x���g���X�i�[��o�^ sequencer.addMetaEventListener(new MyMetaEventListener()); } // MIDI�V�[�P���X��o�^ sequences[counter] = MidiSystem.getSequence(url); counter++; }
Example #10
Source File: MidiOutGetMicrosecondPositionBug.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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!"); } } }
Example #11
Source File: MidiEngine.java From javagame with MIT License | 6 votes |
/** * MIDI�t�@�C�������[�h * @param url MIDI�t�@�C����URL */ public static void load(URL url) throws MidiUnavailableException, InvalidMidiDataException, IOException { if (sequencer == null) { // �V�[�P���T���擾 sequencer = MidiSystem.getSequencer(); // �V�[�P���T���J�� sequencer.open(); // ���^�C�x���g���X�i�[��o�^ sequencer.addMetaEventListener(new MyMetaEventListener()); } // MIDI�V�[�P���X��o�^ sequences[counter] = MidiSystem.getSequence(url); counter++; }
Example #12
Source File: ClosedReceiver.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * 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; }
Example #13
Source File: JavaSoundAudioClip.java From Bytecoder with Apache License 2.0 | 6 votes |
private boolean createSequencer(BufferedInputStream in) throws IOException { // get the sequencer try { sequencer = MidiSystem.getSequencer( ); } catch(MidiUnavailableException me) { if (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 (Printer.err) e.printStackTrace(); return false; } return true; }
Example #14
Source File: OpenClose.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * 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; }
Example #15
Source File: MidiEngine.java From javagame with MIT License | 6 votes |
/** * MIDI�t�@�C�������[�h * @param url MIDI�t�@�C����URL */ public static void load(URL url) throws MidiUnavailableException, InvalidMidiDataException, IOException { if (sequencer == null) { // �V�[�P���T���擾 sequencer = MidiSystem.getSequencer(); // �V�[�P���T���J�� sequencer.open(); // ���^�C�x���g���X�i�[��o�^ sequencer.addMetaEventListener(new MyMetaEventListener()); } // MIDI�V�[�P���X��o�^ sequences[counter] = MidiSystem.getSequence(url); counter++; }
Example #16
Source File: MidiFileTypeUniqueness.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Exception { boolean foundDuplicates = false; int[] aTypes = MidiSystem.getMidiFileTypes(); for (int i = 0; i < aTypes.length; i++) { for (int j = 0; j < aTypes.length; j++) { if (aTypes[i] == aTypes[j] && i != j) { foundDuplicates = true; } } } if (foundDuplicates) { throw new Exception("Test failed"); } else { System.out.println("Test passed"); } }
Example #17
Source File: LMidiSound.java From RipplePower with Apache License 2.0 | 6 votes |
public void playSound(InputStream in) { try { System.setProperty("javax.sound.midi.Sequencer", "com.sun.media.sound.RealTimeSequencerProvider"); if (this.sequencer == null) { this.sequencer = MidiSystem.getSequencer(); if (!this.sequencer.isOpen()) { this.sequencer.open(); } } Sequence seq = MidiSystem.getSequence(in); this.sequencer.setSequence(seq); this.sequencer.start(); this.sequencer.addMetaEventListener(this); if (this.volume != 1) { this.setSoundVolume(this.volume); } } catch (Exception e) { e.printStackTrace(); } }
Example #18
Source File: MidiEngine.java From javagame with MIT License | 6 votes |
/** * MIDI�t�@�C�������[�h * @param url MIDI�t�@�C����URL */ public static void load(URL url) throws MidiUnavailableException, InvalidMidiDataException, IOException { if (sequencer == null) { // �V�[�P���T���擾 sequencer = MidiSystem.getSequencer(); // �V�[�P���T���J�� sequencer.open(); // ���^�C�x���g���X�i�[��o�^ sequencer.addMetaEventListener(new MyMetaEventListener()); } // MIDI�V�[�P���X��o�^ sequences[counter] = MidiSystem.getSequence(url); counter++; }
Example #19
Source File: DefaultDevices.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Exception { boolean allOk = true; MidiDevice.Info[] infos; out("\nTesting MidiDevices retrieved via MidiSystem"); infos = MidiSystem.getMidiDeviceInfo(); allOk &= testDevices(infos, null); out("\nTesting MidiDevices retrieved from MidiDeviceProviders"); List providers = JDK13Services.getProviders(MidiDeviceProvider.class); for (int i = 0; i < providers.size(); i++) { MidiDeviceProvider provider = (MidiDeviceProvider)providers.get(i); infos = provider.getDeviceInfo(); allOk &= testDevices(infos, provider.getClass().getName()); } if (!allOk) { throw new Exception("Test failed"); } else { out("Test passed"); } }
Example #20
Source File: MidiToAudioSynth.java From tuxguitar with GNU Lesser General Public License v2.1 | 6 votes |
public void loadSoundbank(List<Patch> patchList, String soundbankPath) throws Throwable { Soundbank soundbank = null; if( soundbankPath == null || soundbankPath.length() == 0 ){ soundbank = this.synthesizer.getDefaultSoundbank(); } else{ soundbank = MidiSystem.getSoundbank(new File(soundbankPath)); } Iterator<Patch> it = patchList.iterator(); while( it.hasNext() ){ Patch patch = (Patch)it.next(); boolean percussion = (patch.getBank() == 128); int bank = (percussion ? 0 : patch.getBank()); int program = patch.getProgram(); Instrument instrument = soundbank.getInstrument(createModelPatch(bank, program, percussion)); if( instrument != null ){ this.synthesizer.loadInstrument(instrument); } } }
Example #21
Source File: SeqStartRecording.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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(); } }
Example #22
Source File: MidiCommunication.java From jmg with GNU General Public License v2.0 | 6 votes |
private void fillFrame(Frame f, List dataList, MidiDevice.Info[] info) { try { f.setSize(340, 200); f.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - 170, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - 100); String[] data = new String[info.length]; data[0] = "" + info[0]; data[1] = "" + info[1]; for(int i=2; i< info.length; i++) { data[i] = MidiSystem.getMidiDevice(info[i]).toString(); } for(int i=0; i< info.length; i++) { dataList.add(data[i]); } ScrollPane scrollPane = new ScrollPane(); scrollPane.add(dataList); f.add(scrollPane); } catch (Exception e) { System.out.println (e); System.exit (0); } }
Example #23
Source File: RTMidiIn.java From jmg with GNU General Public License v2.0 | 6 votes |
/** * Initialise the input source */ private boolean init() { if (trans == null) { try { if (MidiSystem.getReceiver() == null) { System.err.println("MidiSystem Receiver Unavailable"); return false; } MidiDevice.Info[] mdi=MidiSystem.getMidiDeviceInfo(); for(int i=0;i<mdi.length;i++){ System.out.println(mdi[i]); } trans = MidiSystem.getTransmitter(); trans.setReceiver(this); }catch (MidiUnavailableException e) { System.err.println("Midi System Unavailable:" + e); return false; } } return true; }
Example #24
Source File: TickLength.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
static boolean hasSequencer() { try { Sequencer seq = MidiSystem.getSequencer(); if (seq != null) { seq.open(); seq.close(); return true; } } catch (Exception e) {} System.out.println("No sequencer available! Cannot execute test."); return false; }
Example #25
Source File: JavaSoundAudioClip.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public JavaSoundAudioClip(InputStream in) throws IOException { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.<init>"); BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE); bis.mark(STREAM_BUFFER_SIZE); boolean success = false; try { AudioInputStream as = AudioSystem.getAudioInputStream(bis); // load the stream data into memory success = loadAudioData(as); if (success) { success = false; if (loadedAudioByteLength < CLIP_THRESHOLD) { success = createClip(); } if (!success) { success = createSourceDataLine(); } } } catch (UnsupportedAudioFileException e) { // not an audio file try { MidiFileFormat mff = MidiSystem.getMidiFileFormat(bis); success = createSequencer(bis); } catch (InvalidMidiDataException e1) { success = false; } } if (!success) { throw new IOException("Unable to create AudioClip from input stream"); } }
Example #26
Source File: JavaSoundAudioClip.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
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; }
Example #27
Source File: MetaCallback.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
static boolean hasSequencer() { try { Sequencer seq = MidiSystem.getSequencer(); if (seq != null) { seq.open(); seq.close(); return true; } } catch (Exception e) {} System.out.println("No sequencer available! Cannot execute test."); return false; }
Example #28
Source File: SequencerImplicitSynthOpen.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) { try { log("getting sequencer..."); Sequencer sequencer = MidiSystem.getSequencer(); log(" - got " + getDeviceStr(sequencer)); // obtain connected device (usually synthesizer) MidiDevice synth = getConnectedDevice(sequencer); if (synth == null) { log("could not get connected device, returning"); return; } log("connected device: " + getDeviceStr(synth)); int success = 0; for (int i=0; i<TEST_COUNT; i++) { if (test(sequencer)) { success++; } } if (success != TEST_COUNT) { throw new RuntimeException("test FAILS"); } } catch (MidiUnavailableException ex) { // this is not a failure log("Could not get Sequencer"); } log("test PASSED."); }
Example #29
Source File: Looping.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Execute the test on all available Sequencers. * * @return true if the test passed for all Sequencers, false otherwise */ private static boolean testAll() throws Exception { boolean result = true; MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < devices.length; i++) { MidiDevice device = MidiSystem.getMidiDevice(devices[i]); if (device instanceof Sequencer) { result &= testSequencer((Sequencer) device); } } return result; }
Example #30
Source File: JavaSoundAudioClip.java From hottub with GNU General Public License v2.0 | 5 votes |
public JavaSoundAudioClip(InputStream in) throws IOException { if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.<init>"); BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE); bis.mark(STREAM_BUFFER_SIZE); boolean success = false; try { AudioInputStream as = AudioSystem.getAudioInputStream(bis); // load the stream data into memory success = loadAudioData(as); if (success) { success = false; if (loadedAudioByteLength < CLIP_THRESHOLD) { success = createClip(); } if (!success) { success = createSourceDataLine(); } } } catch (UnsupportedAudioFileException e) { // not an audio file try { MidiFileFormat mff = MidiSystem.getMidiFileFormat(bis); success = createSequencer(bis); } catch (InvalidMidiDataException e1) { success = false; } } if (!success) { throw new IOException("Unable to create AudioClip from input stream"); } }