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); }
private synchronized void startMidi(InputStream bis, InputStream in) throws InvalidMidiDataException, MidiUnavailableException { Sequencer sequencer = null; Info info = null; // sequencer = MidiSystem.getSequencer( null ); sequencer = MidiSystem.getSequencer(); sequencer.open(); try { sequencer.setSequence(bis); } catch (IOException e) { throw new InvalidMidiDataException(e.getMessage()); } info = new Info(sequencer, in, null); infos.addElement(info); // fix for bug 4302884: Audio device is not released when AudioClip stops sequencer.addMetaEventListener(info); sequencer.start(); }
/** Open an audio channel. */ public synchronized void openChannel(InputStream in) { if (DEBUG) { System.out.println("AudioDevice: openChannel"); System.out.println("input stream =" + in); } Info info = null; // is this already playing? if so, then just return for (int i = 0; i < infos.size(); i++) { info = (AudioDevice.Info) infos.elementAt(i); if (info.in == in) { return; } } AudioInputStream as = null; if (in instanceof AudioStream) { if (((AudioStream) in).midiformat != null) { // it's a midi file try { startMidi(((AudioStream) in).stream, in); } catch (Exception e) { return; } } else if (((AudioStream) in).ais != null) { // it's sampled audio try { startSampled(((AudioStream) in).ais, in); } catch (Exception e) { return; } } } else if (in instanceof AudioDataStream) { if (in instanceof ContinuousAudioDataStream) { try { AudioInputStream ais = new AudioInputStream( in, ((AudioDataStream) in).getAudioData().format, AudioSystem.NOT_SPECIFIED); startSampled(ais, in); } catch (Exception e) { return; } } else { try { AudioInputStream ais = new AudioInputStream( in, ((AudioDataStream) in).getAudioData().format, ((AudioDataStream) in).getAudioData().buffer.length); startSampled(ais, in); } catch (Exception e) { return; } } } else { BufferedInputStream bis = new BufferedInputStream(in, 1024); try { try { as = AudioSystem.getAudioInputStream(bis); } catch (IOException ioe) { return; } startSampled(as, in); } catch (UnsupportedAudioFileException e) { try { try { MidiFileFormat mff = MidiSystem.getMidiFileFormat(bis); } catch (IOException ioe1) { return; } startMidi(bis, in); } catch (InvalidMidiDataException e1) { // $$jb:08.01.99: adding this section to make some of our other // legacy classes work..... // not MIDI either, special case handling for all others AudioFormat defformat = new AudioFormat(AudioFormat.Encoding.ULAW, 8000, 8, 1, 1, 8000, true); try { AudioInputStream defaif = new AudioInputStream(bis, defformat, AudioSystem.NOT_SPECIFIED); startSampled(defaif, in); } catch (UnsupportedAudioFileException es) { return; } catch (LineUnavailableException es2) { return; } } catch (MidiUnavailableException e2) { // could not open sequence return; } } catch (LineUnavailableException e) { return; } } // don't forget adjust for a new stream. notify(); }
public static void main(String[] args) throws Exception { InputStream sb = getInputStream("ding.sf2"); soundbank = MidiSystem.getSoundbank(sb); sb.close(); InputStream si = getInputStream("expresso.mid"); sequence = MidiSystem.getSequence(si); si.close(); AudioFormat format; Map<String, Object> info = new HashMap<String, Object>(); { format = new AudioFormat(22050, 16, 2, true, false); test(format, info); format = new AudioFormat(44100, 16, 2, true, false); test(format, info); } { format = new AudioFormat(44100, 8, 2, true, false); test(format, info); format = new AudioFormat(44100, 16, 2, true, false); test(format, info); format = new AudioFormat(44100, 24, 2, true, false); test(format, info); } { format = new AudioFormat(44100, 16, 1, true, false); test(format, info); format = new AudioFormat(44100, 16, 2, true, false); test(format, info); } { format = new AudioFormat(44100, 16, 2, true, false); info.clear(); info.put("control rate", 100f); test(format, info); info.clear(); info.put("control rate", 147f); test(format, info); } { format = new AudioFormat(44100, 16, 2, true, false); info.clear(); info.put("interpolation", "point"); test(format, info); info.clear(); info.put("interpolation", "linear"); test(format, info); info.clear(); info.put("interpolation", "cubic"); test(format, info); } { format = new AudioFormat(44100, 16, 2, true, false); info.clear(); info.put("max polyphony", 4); test(format, info); info.clear(); info.put("max polyphony", 16); test(format, info); info.clear(); } }