/** * Creates a WAV file based on the Sequence, using the default soundbank. * * @param sequence * @param outputFile * @throws MidiUnavailableException * @throws InvalidMidiDataException * @throws IOException */ public void createWavFile(final Sequence sequence, final File outputFile) throws MidiUnavailableException, InvalidMidiDataException, IOException { /* * Open synthesizer in pull mode in the format 96000hz 24 bit stereo * using Sinc interpolation for highest quality. With 1024 as max * polyphony. */ final AudioFormat format = new AudioFormat(96000, 24, 2, true, false); final Map<String, Object> map = new HashMap<String, Object>(); map.put("interpolation", "sinc"); map.put("max polyphony", "1024"); AudioInputStream stream = synth.openStream(format, map); // Play Sequence into AudioSynthesizer Receiver. final double total = send(sequence, synth.getReceiver()); // Calculate how long the WAVE file needs to be. final long len = (long) (stream.getFormat().getFrameRate() * (total + 40)); stream = new AudioInputStream(stream, stream.getFormat(), len); // Write WAVE file to disk. AudioSystem.write(stream, AudioFileFormat.Type.WAVE, outputFile); this.synth.close(); }
public static void main(String[] args) throws Exception { AudioSynthesizer synth = new SoftSynthesizer(); synth.openStream(null, null); Soundbank defsbk = synth.getDefaultSoundbank(); if (defsbk != null) { assertTrue(defsbk.getInstruments().length == synth.getLoadedInstruments().length); } synth.close(); }
/** * Creates a WAV file based on a MIDI file, using the default sound bank. * * @param midiFile The MIDI file. * @param outputFile An output file. * @throws MidiUnavailableException When the synthesizer is not available. * @throws InvalidMidiDataException When the MIDI data is invalid. * @throws IOException If the WAV file can not be written. */ public void createWavFile(final File midiFile, final File outputFile) throws MidiUnavailableException, InvalidMidiDataException, IOException { // Create a AdvancedAudioPlayer with this Synthesizer, and get a Sequence final Sequence sequence = MidiSystem.getSequence(midiFile); final Sequencer sequencer = MidiSystem.getSequencer(false); sequencer.getTransmitter().setReceiver(synth.getReceiver()); createWavFile(sequence, outputFile); }
/** * Creates a WAV file based on the Sequence, using the sounds from the specified soundbank; to * prevent memory problems, this method asks for an array of patches (instruments) to load. * * @param soundbankFile * @param midiFile * @param outputFile * @throws MidiUnavailableException * @throws InvalidMidiDataException * @throws IOException */ public void createWavFile(final File soundbankFile, final File midiFile, final File outputFile) throws MidiUnavailableException, InvalidMidiDataException, IOException { // Load soundbank final Soundbank soundbank = loadSoundbank(soundbankFile); // Open the Synthesizer and load the requested instruments this.synth.open(); this.synth.unloadAllInstruments(soundbank); final Instrument[] instruments = soundbank.getInstruments(); for (final Instrument instrument : instruments) { synth.loadInstrument(instrument); } createWavFile(midiFile, outputFile); }
public static void render(OutputStream os, AudioFormat format, Map<String, Object> info) throws Exception { AudioSynthesizer synth = (AudioSynthesizer) new SoftSynthesizer(); AudioInputStream stream = synth.openStream(format, info); Receiver recv = synth.getReceiver(); Soundbank defsbk = synth.getDefaultSoundbank(); if (defsbk != null) synth.unloadAllInstruments(defsbk); synth.loadAllInstruments(soundbank); double totalTime = 5; send(sequence, recv); long len = (long) (stream.getFormat().getFrameRate() * (totalTime + 4)); stream = new AudioInputStream(stream, stream.getFormat(), len); long t = System.currentTimeMillis(); AudioSystem.write(stream, AudioFileFormat.Type.WAVE, os); t = System.currentTimeMillis() - t; stream.close(); }