package com.javadaw.audio;

import com.javadaw.gui.javaMain;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class writeAudio {
    private ArrayList toFileBuffer;
    private File outFile;
    private String fileName;
    private int bitsPerSample;
    private float sampleRate;
    private int channels;
    private int frameSizeInBytes;
    private AudioFormat playToFileFormat;
    private int bytesPerSample;
    private byte[] bytes;
    private byte[] bytes2;
    public int offset;
    private javaMain parent;
    private int maxBuffer = 1000000;

    public writeAudio(javaMain parent) {
        
        this.parent = parent;
        this.bitsPerSample = 16;
        this.sampleRate = 44100;
        this.channels = 2;

        toFileBuffer = new ArrayList();

        boolean signed = true;
        boolean bigEndian = true;

        playToFileFormat =
                new AudioFormat(sampleRate, bitsPerSample, channels, signed,
                                bigEndian);

        frameSizeInBytes = playToFileFormat.getFrameSize();

        bytesPerSample = (bitsPerSample / 8);
    }
    
    public void init(int maxBuffer) {
        
        this.maxBuffer = maxBuffer;
        
        //System.out.println("maxbuffer " + maxBuffer);
        
        this.bytes = new byte[this.maxBuffer];
        
    }
    
    public void setFileName(String fileName) {
        
        this.fileName = fileName;
        
        System.out.println(fileName + " START");

    }

    public void closeFileFromArray() {
        if (offset > maxBuffer) {
            offset = maxBuffer;
        }
        closeFile();
    }

    public void closeFile() {

        bytes2 = new byte[offset];
        
        //System.out.println("bytes 2 " + bytes2.length);
        //System.out.println("bytes   " + bytes .length);
        
        int i = 0;
       
        while (i < offset){ 
               bytes2[i] = bytes[i];
               i++;
        }
        ByteArrayInputStream byteInputArrayStream =
            new ByteArrayInputStream(bytes2);
        
        AudioInputStream audioInputStream =
            new AudioInputStream(byteInputArrayStream, playToFileFormat,
                                 bytes2.length / frameSizeInBytes);

        outFile = new File(fileName);
        
        
        try { AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, outFile);
            
              parent.commonutils.loadClipArray(parent.cc.clipid, false);

              System.out.println(outFile + " STOP " + audioInputStream.getFrameLength());
            
        } catch (Throwable t) {System.out.println(t); }
        
    }
    public void  putSamples(byte[] data) throws IllegalStateException {

            int i = offset;
            for (int k = 0; k < data.length; ) {
                if (i < maxBuffer) {
                    bytes[i] = data[k];
                } else { parent.changeTransportStatus(); 
                         closeFileFromArray();
                         break;
                    }
                ++i;
                ++k;
            }
            offset += data.length;
    }
    public void  putSamplesX(byte[] data) throws IllegalStateException {

            int i = offset;
            for (int k = 0; k < data.length; ) {
                if (i < maxBuffer) {
                    bytes[i] = data[k];
                } 
                ++i;
                ++k;
            }
            offset += data.length;
    }
}


