/** * Attempt to enable the {@link MidiDevice} which requires the high resolution timer. */ public static synchronized void enable() { if (null != MIDI_DEVICE && !MIDI_DEVICE.isOpen()) { try { MIDI_DEVICE.open(); } catch (final MidiUnavailableException ignore) { isEnabled = false; return; } isEnabled = true; } }
synthesizer.open(); Instrument[] orchestra = synthesizer.getAvailableInstruments();
device.open();
/** * Attempt to enable the {@link MidiDevice} which requires the high resolution timer. */ public static synchronized void enable() { if (null != MIDI_DEVICE && !MIDI_DEVICE.isOpen()) { try { MIDI_DEVICE.open(); } catch (final MidiUnavailableException ignore) { isEnabled = false; return; } isEnabled = true; } }
/** * Attempt to enable the {@link MidiDevice} which requires the high resolution timer. */ public static synchronized void enable() { if (null != MIDI_DEVICE && !MIDI_DEVICE.isOpen()) { try { MIDI_DEVICE.open(); } catch (final MidiUnavailableException ignore) { isEnabled = false; return; } isEnabled = true; } }
public boolean ensureDevOpen() { try { if (myDevice != null) { if (!(myDevice.isOpen())) { myDevice.open(); } return true; } else { return false; } } catch (Throwable t) { getLogger().error("Problem opening device for: {}", this, t); return false; } }
/***** static methods **********************************************/ public static Keyboard fromMidiDevice(List<String> names, Receiver targetReceiver) { MidiDevice midiInput = MidiUtil.findMidiDevice(names.toArray(new String[0]), false, true); if (midiInput == null) { System.out.printf("Unable to find keyboard device matching name: %s\n", names); return null; } try { midiInput.open(); Keyboard keyboard = new Keyboard(midiInput.getTransmitter(), targetReceiver); return keyboard; } catch (Exception e) { e.printStackTrace(); System.exit(1); } return null; }
/** * 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); } } }
import javax.sound.midi.*; import java.net.URL; class PlayMidi { public static void main(String[] args) throws Exception { URL url = new URL("http://www.vgmusic.com/music/computer/microsoft/windows/touhou_6_stage3_boss.mid"); Sequence sequence = MidiSystem.getSequence(url); Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.setSequence(sequence); sequencer.start(); } }
sequencer.open(); sequencer.setSequence(myseq); sequencer.setLoopCount(loops);
import javax.sound.midi.*; public class MidiSynthesizerSample { public static void main(String[] args) { int[] notes = new int[]{60, 62, 64, 65, 67, 69, 71, 72, 72, 71, 69, 67, 65, 64, 62, 60}; try { Synthesizer synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); MidiChannel channel = synthesizer.getChannels()[0]; for (int note : notes) { channel.noteOn(note, 50); try { Thread.sleep(200); } catch (InterruptedException e) { break; } finally { channel.noteOff(note); } } } catch (MidiUnavailableException e) { e.printStackTrace(); } } }
import javax.sound.midi.*; import javax.swing.JOptionPane; import java.net.URL; class LoopMidi { public static void main(String[] args) throws Exception { URL url = new URL("http://pscode.org/media/EverLove.mid"); Sequence sequence = MidiSystem.getSequence(url); Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.setSequence(sequence); //sequencer.setLoopStartPoint(2000); //sequencer.setLoopEndPoint(4000); sequencer.setLoopCount(5); sequencer.start(); JOptionPane.showMessageDialog(null, "Everlasting Love"); } }
public static void main(String args[]) throws Exception { String[] lppNames = new String[] { "Launchpad", "Standalone" }; lppInput = MidiUtil.findMidiDevice(lppNames, false, true); if (lppInput == null) { System.err.printf("Unable to find controller input device matching name: %s\n", StringUtils.join(lppNames, ", ")); System.exit(1); } lppOutput = MidiUtil.findMidiDevice(lppNames, true, false); if (lppOutput == null) { System.err.printf("Unable to find controller output device matching name: %s\n", StringUtils.join(lppNames, ", ")); System.exit(1); } lppInput.open(); transmitter = lppInput.getTransmitter(); lppOutput.open(); receiver = lppOutput.getReceiver(); KillListener listener = new KillListener(); LaunchpadPro lpp = new LaunchpadPro(receiver, listener); listener.setLpp(lpp); transmitter.setReceiver(lpp); lpp.initialize(); int millis = 1; int repeat = 300; fastLights(lpp, 100, 0); stop.await(); }
public static void main(String args[]) throws Exception { String[] names = new String[] { "CMD LC-1" }; input = MidiUtil.findMidiDevice(names, false, true); if (input == null) { System.err.printf("Unable to find controller input device matching name: %s\n", StringUtils.join(names, ", ")); System.exit(1); } output = MidiUtil.findMidiDevice(names, true, false); if (output == null) { System.err.printf("Unable to find controller output device matching name: %s\n", StringUtils.join(names, ", ")); System.exit(1); } input.open(); transmitter = input.getTransmitter(); output.open(); receiver = output.getReceiver(); BehringerLC1 lc1 = new BehringerLC1(receiver, null); TestListener listener = new TestListener(lc1, Color.fromIndex(1), Color.fromIndex(0)); transmitter.setReceiver(lc1); lc1.setListener(listener); lc1.initialize(); stop.await(); }
import javax.sound.midi.*; import java.io.IOException; import java.io.InputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; public class MidiPlayer{ public static void main(String[] args) { try { Sequencer sequencer = MidiSystem.getSequencer(); if (sequencer == null) throw new MidiUnavailableException(); sequencer.open(); FileInputStream is = new FileInputStream("music.mid"); Sequence Seq = MidiSystem.getSequence(is); sequencer.setSequence(Seq); sequencer.start(); } catch (Exception e) { e.printStackTrace(); } } here is a sample code that shows you how to play MIDI files in your java program, hope it helps
Sequence sequence = MidiSystem.getSequence(url); final Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.setSequence(sequence); Runnable r = new Runnable() {
public static Transmitter getTransmitter(MidiDevice current) { if (current.getMaxTransmitters() != 0) { try { if (!current.isOpen()) { current.open(); } } catch (MidiUnavailableException e) { return null; } try { return current.getTransmitter(); } catch (MidiUnavailableException e) { current.close(); } } return null; }
public static Receiver getReceiver(MidiDevice current) { if (current.getMaxReceivers() != 0) { try { if (!current.isOpen()) { current.open(); } } catch (MidiUnavailableException e) { return null; } try { return current.getReceiver(); } catch (MidiUnavailableException e) { current.close(); } } return null; }
midiSynth.open();
midiDevice.open(); sm_openedMidiDeviceList.add(midiDevice); Receiver midiDevReceiver = midiDevice.getReceiver();