/** * Loads a sequence from an input stream. Returns null if an error occurs. * * @param is * @return */ public Sequence getSequence(InputStream is) { try { if (!is.markSupported()) { is = new BufferedInputStream(is); } Sequence s = MidiSystem.getSequence(is); is.close(); return s; } catch (InvalidMidiDataException ex) { ex.printStackTrace(); return null; } catch (IOException ex) { ex.printStackTrace(); return null; } }
public void go() { setUpGui(); try { Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); // make a sequencer and open sequencer.addControllerEventListener(m1, new int[] {127}); Sequence seq = new Sequence(Sequence.PPQ, 4); Track track = seq.createTrack(); int r = 0; for (int i = 0; i < 300; 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(); } } // close method
{ try { sq = MidiSystem.getSequencer(); } catch (MidiUnavailableException e) { e.printStackTrace(); } }
/** * Exports to a file. * * @param outputFileName The output file name * @throws InvalidFileFormatException If the file name doesn't end in .mid or .midi */ public void exportToFile(String outputFileName) throws InvalidFileFormatException { // Check for a valid file format. if (!outputFileName.endsWith(".mid") && !outputFileName.endsWith(".midi")) { String msg = "File names must end in .mid or .midi"; throw new InvalidFileFormatException(msg); } // Find a supported file type, and export the file. int[] types = MidiSystem.getMidiFileTypes(sequence); try { File outputFile = new File(outputFileName); MidiSystem.write(sequence, types[0], outputFile); } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } }
/** Creates a new MidiPlayer object. */ public MidiPlayer() { try { sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.addMetaEventListener(this); } catch (MidiUnavailableException ex) { sequencer = null; } }
public boolean saveMidiFile(File file) { try { int[] fileTypes = MidiSystem.getMidiFileTypes(sequence); if (fileTypes.length == 0) { System.out.println("Can't save sequence"); return false; } else { if (MidiSystem.write(sequence, fileTypes[0], file) == -1) { throw new IOException("Problems writing to file"); } return true; } } catch (SecurityException ex) { ex.printStackTrace(); return false; } catch (Exception ex) { ex.printStackTrace(); return false; } }
/** Carrega a seqüência do sistema de arquivos. Retorna null se um erro ocorrer. */ public Sequence getSequence(String name) { String filename = "/recursos/sons/" + name; try { return MidiSystem.getSequence(getClass().getResource((filename))); } catch (InvalidMidiDataException ex) { ex.printStackTrace(); return null; } catch (IOException ex) { ex.printStackTrace(); return 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; }
/** * Sets up the sequencer with a given sequence. * * @param sequenceToUse * @throws MidiUnavailableException */ private void setUpSequencer() throws MidiUnavailableException { // First, get the system's default sequencer. try { sequencer = MidiSystem.getSequencer(); } catch (MidiUnavailableException ex) { // Something went wrong. ex.printStackTrace(); System.exit(1); } // If there is none, throw an exception. if (sequencer == null) { String msg = "Cannot find a sequencer"; throw new MidiUnavailableException(msg); } // Set up the transmitter and receiver of the synth to play the song. linkTransmitterToReceiver(); }
/** 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); } }
private static void playMidi(String name, boolean loop, double volume) throws MidiUnavailableException, FileNotFoundException, 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 InputStream is = new BufferedInputStream(new FileInputStream(new File(path + name))); // Sets the current sequence on which the sequencer operates. // The stream must point to MIDI file data. sequencer.setSequence(is); // Set looping if (loop) { sequencer.setLoopCount(LOOP_CONTINUOUSLY); } // Starts playback of the MIDI data in the currently loaded sequence. sequencer.start(); midiMap.put(name, sequencer); }
public void play(String name, String version, String vendor, String desc) throws Exception { Info port = null; for (Info info : MidiSystem.getMidiDeviceInfo()) { if (name == null && version == null && vendor == null && desc == null) { System.err.println( "At least one of name, version, vendor, or description must be specified."); break; } if (name != null && !info.getName().equals(name)) { continue; } if (vendor != null && !info.getVendor().equals(vendor)) { continue; } if (version != null && !info.getVersion().equals(version)) { continue; } if (desc != null && !info.getDescription().equals(desc)) { continue; } try (MidiDevice dev = MidiSystem.getMidiDevice(info)) { Receiver r = null; try { r = dev.getReceiver(); } catch (Exception e) { r = null; } if (r == null) { continue; } } if (port != null) { System.err.println( "Multiple MIDI ports match the given parameters. Please be more specific."); port = null; break; } port = info; continue; } if (port == null) { System.err.println("Unable to locate MIDI port"); System.err.println("Available ports:"); for (Info info : MidiSystem.getMidiDeviceInfo()) { try (MidiDevice dev = MidiSystem.getMidiDevice(info)) { Receiver r = null; try { r = dev.getReceiver(); } catch (Exception e) { r = null; } if (r == null) { continue; } } System.out.println( "Device [name=" + info.getName() + ", version=" + info.getVersion() + ", vendor=" + info.getVendor() + ", desc=" + info.getDescription() + "]"); } return; } System.out.println( "Using device [name=" + port.getName() + ", version=" + port.getVersion() + ", vendor=" + port.getVendor() + ", desc=" + port.getDescription() + "]"); try (MidiDevice dev = MidiSystem.getMidiDevice(port)) { int channel = CHANNEL_RAW_STEREO; dev.open(); try (Receiver rx = dev.getReceiver()) { rxCleanup = rx; try (DataInputStream dis = new DataInputStream(new FileInputStream(ymz))) { resetController(rx, channel, registers); long startTime = System.nanoTime(); // read a buffer long prevClock = 0; while (true) { long clock = dis.readLong(); byte register = dis.readByte(); byte value = dis.readByte(); if (prevClock != clock) { sendAll(rx, channel, registers); prevClock = clock; } sleepUntil(startTime, clock); System.out.println( "Clock: " + clock + " register: " + register + ", value: " + (value & 0xff)); if (register < 0 || register > 13) { System.err.println("Invalid register " + register + " found, ignoring."); continue; } registers[register] = value; } } catch (EOFException e) { System.err.println("EOF reached"); } finally { resetController(rx, channel, registers); rxCleanup = null; } } } }