예제 #1
0
  /**
   * 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();
 }
  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();
  }