package vermilion;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import javax.sound.sampled.AudioFormat;

public class AudioStreamHandler
{
    private AudioFormat format;

    public AudioStreamHandler(AudioFormat fmt)
    {
        format = fmt;
    }

    public ArrayList<Float> processData(byte[] audioBytes, int numBytesRead)
    {
        ArrayList<Float> data = new ArrayList<Float>();

        if (format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED)
        {
            if (format.getSampleSizeInBits() == 16)
            {
                if (format.isBigEndian() == false)
                {
                    System.out.println("Reading normal WAV file...");

                    ByteBuffer b = ByteBuffer.allocate(numBytesRead);
                    b.put(audioBytes, 0, numBytesRead);
                    for (int x = 0; x < numBytesRead/2; x++)
                    {
                        float f = Short.reverseBytes(b.getShort(x*2)) / 32768.0f;
                        data.add(f);
                    }
                }
            }
            else
            {
                // Read 8 bit WAVs.
            }
        }

        return data;
    }
}
