package net.heartshield.signal;

import junit.framework.TestCase;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioDecoderTests extends TestCase {
    public void testA() throws IOException, UnsupportedAudioFileException {
        //System.out.println();
        Path currentRelativePath = Paths.get("");
        String s = currentRelativePath.toAbsolutePath().toString();
        System.out.println("Current relative path is: " + s);

        System.out.println("length=" + readAudio("signal/src/test/res/raw/test100hz.wav").length);
        /*
         * Frame Rate: 44100
         * Frame Size: 2
         * Channels: 1
         * Bits per sample: 16
         */
        ;
    }

    private int[] delArray(int[] arr, int i) {
        int[] newMeasurements = new int[arr.length-1];
        System.arraycopy(arr, 0, newMeasurements, 0, i);
        if(i < newMeasurements.length)
            System.arraycopy(arr, i+1, newMeasurements, i, (newMeasurements.length - i));

        return newMeasurements;
    }

    public void testArrayRestr() {
        int[] start = new int[]{1, 2, 3, 4};
        int[] res = delArray(start, 1);
        int[] exp = new int[]{1, 3, 4};
        if(!IVec.all(IVec.eq(res, exp)))
            throw new IllegalStateException("failed delArray() test");
    }

    private short[] readAudio(String fname) throws IOException, UnsupportedAudioFileException {
        AudioInputStream ais = AudioSystem.getAudioInputStream(new File(fname));
        AudioFormat af = ais.getFormat();
        //DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);

        int frameRate = (int)af.getFrameRate();
        int frameSize = af.getFrameSize();
        int channels = af.getChannels();
        int bps = af.getSampleSizeInBits();

        System.out.println("Frame Rate: " + frameRate);
        System.out.println("Frame Size: " + frameSize);
        //System.out.println("Buffer Size: " + bufSize);
        System.out.println("Channels: " + channels);
        System.out.println("Bits per sample: " + bps);

        List<Short> buf = new ArrayList<>();

        byte[] data = new byte[2048];
        int bytesRead;

        while((bytesRead = ais.read(data, 0, data.length)) != -1) {
            for(int i = 0; i < bytesRead; i += 2)
                buf.add((short) (data[i] + 256 * data[i+1]));
        }

        short[] arr = new short[buf.size()];
        for(int i = 0; i < arr.length; i++)
            arr[i] = buf.get(i);
        return arr;
    }
}
