title: How to Save Playing Audio to WAV Format in Java
body: <p>I am using Sonic Algorithm developed by <a href="https://github.com/waywardgeek/sonic" rel="nofollow noreferrer">waywardgeek</a> to speed and slow down my audio files. It's working fine. But what I need is I need to save that playing audio to <code>wav</code> file. I have tried to save using <code>ByteArrayInputStream</code> and <code>TargetDataLink</code>. But I am failed to do that. So anybody can help me to save that playing audio to <code>wav</code> file? And also I am new to Java Audio.</p>
  
  <p>Here is the code,</p>
  
  <p><strong>Main.java</strong></p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  
  import javax.sound.sampled.AudioFormat;
  import javax.sound.sampled.AudioInputStream;
  import javax.sound.sampled.AudioSystem;
  import javax.sound.sampled.DataLine;
  import javax.sound.sampled.LineUnavailableException;
  import javax.sound.sampled.SourceDataLine;
  import javax.sound.sampled.UnsupportedAudioFileException;
  
  public class Main {
  
      private static void runSonic(
              AudioInputStream audioStream,
              SourceDataLine line,
              float speed,
              float pitch,
              float rate,
              float volume,
              boolean emulateChordPitch,
              int quality,
              int sampleRate,
              int numChannels) throws IOException {
          Sonic sonic = new Sonic(sampleRate, numChannels);
          int bufferSize = line.getBufferSize();
          byte inBuffer[] = new byte[bufferSize];
          byte outBuffer[] = new byte[bufferSize];
          int numRead, numWritten;
  
          sonic.setSpeed(speed);
          sonic.setPitch(pitch);
          sonic.setRate(rate);
          sonic.setVolume(volume);
          sonic.setChordPitch(emulateChordPitch);
          sonic.setQuality(quality);
          do {
              numRead = audioStream.read(inBuffer, 0, bufferSize);
              if (numRead &lt;= 0) {
                  sonic.flushStream();
              } else {
                  sonic.writeBytesToStream(inBuffer, numRead);
              }
              do {
                  numWritten = sonic.readBytesFromStream(outBuffer, bufferSize);
                  if (numWritten &gt; 0) {
                      line.write(outBuffer, 0, numWritten);
                  }
              } while (numWritten &gt; 0);
          } while (numRead &gt; 0);
      }
  
      public static void main(String[] argv) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
          float speed = 1.0f;
          float pitch = 1.0f;
          float rate = 1.5f;
          float volume = 1.0f;
          boolean emulateChordPitch = false;
          int quality = 0;
  
          AudioInputStream stream = AudioSystem.getAudioInputStream(new File("C:\\audio1.wav"));
          AudioFormat format = stream.getFormat();
          int sampleRate = (int) format.getSampleRate();
          int numChannels = format.getChannels();
          SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, format,
                  ((int) stream.getFrameLength() * format.getFrameSize()));
          SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
          line.open(stream.getFormat());
          line.start();
          runSonic(stream, line, speed, pitch, rate, volume, emulateChordPitch, quality,
                  sampleRate, numChannels);
          line.drain();
          line.stop();
      }
  }
  </code></pre>
  
  <p>I have tried with the following code and it saved to <code>wav</code> file but when playing it has one-word audio. And also with the <code>TargetDataLink</code>, nothing works.</p>
  
  <pre><code>byteArrayOutputStream = new ByteArrayInputStream(outBuffer);
  audioStreams = new AudioInputStream(byteArrayOutputStream, line.getFormat(), bufferSize);
  AudioSystem.write(audioStreams, AudioFileFormat.Type.WAVE, new File("C:\\audio2.wav"));
  </code></pre>
  
  <p><strong>Sonic.java</strong></p>
  
  <p>Can't post the full code here because of the limited characters allowed. Code is <a href="https://raw.githubusercontent.com/waywardgeek/sonic/master/Sonic.java" rel="nofollow noreferrer">here</a>.</p>
  
  <p><strong>PS:</strong> The code is developed by <a href="https://stackoverflow.com/users/10042285/bill-cox">Bill Cox</a>. Thank you <a href="https://stackoverflow.com/users/10042285/bill-cox">Bill Cox</a> for this great code.</p>