package nxm.audio.lib;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

/** A class that handles writing out audio data to a file
 *  @author Jay Hallam
 *  @since  NeXtMidas 2.1.0
 */
public class AudioWriter {

  private AudioWriter() {}

  /** Write out audio data to a file
      @param filename destination file
      @param mulaw_f true if mu-law format
      @param data buffer with audio data
      @param rate sample rate
      @param size sample size in bits
      @throws IOException on write error
   */
  public static void write(String filename, boolean mulaw_f, byte [] data, float rate,int size) throws IOException {
    AudioFormat af = new AudioFormat(rate,size,1,true,false);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    AudioInputStream ais = new AudioInputStream(bais, af, data.length);
    AudioSystem.write(ais, AudioFileFormat.Type.AU, new File(filename),mulaw_f);
  }

  /** Calls {@link #write(String, boolean, byte[], float, int)} to write out audio data to a file.
   *  A default size of 8 is used.
      @param filename file to write
      @param mulaw_f true if mu-law format
      @param data buffer with audio data
      @param rate sample rate
      @throws IOException on write error
   */
  public static void write(String filename, boolean mulaw_f, byte [] data, float rate) throws IOException {
    write(filename, mulaw_f, data, rate, 8);
  }
}