package party.bami.eqfix.channels;

public class ChanParse {
    /**
     * Splits a stereo PCM into its left and right components.
     * @param pcm Stereo PCM.
     * @return An array of float arrays containing the left and right channels.
     */
    static public float[][] deinterleave(float[] pcm) {
        float[][] splitPCM = new float[2][pcm.length / 2];

        for (int i = 0; i < pcm.length; i++) {
            if ((i % 2) == 0) {
                splitPCM[0][i / 2] = pcm[i];
            }
            else {
                splitPCM[1][i / 2] = pcm[i];
            }
        }

        return splitPCM;
    }

    /**
     * Combines left and right channels into 1 stereo PCM.
     * @param splitPCM An array of float arrays containing the left and right channels.
     * @return Stereo PCM.
     */
    static public float[] interleave(float[][] splitPCM) {
        float[] pcm = new float[splitPCM[0].length * 2];

        for (int i = 0; i < pcm.length; i++) {
            if ((i % 2) == 0) {
                pcm[i] = splitPCM[0][i / 2];
            }
            else {
                pcm[i] = splitPCM[1][i / 2];
            }
        }

        return pcm;
    }
}
