/*
 * Copyright (C) 2013 Darth Affe <http://wyrez.org> and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.wyrez.audio.util;

import java.util.ArrayList;
import java.util.List;
import org.wyrez.audio.decoder.Decoder;

/**
 * Helper class for sample-processing.
 *
 * @author Darth Affe
 */
public class SampleHelper {

    /**
     * Converts a list of floats to an array of floats.
     *
     * @param samples The List to convert
     * @returns The new float array
     */
    public static float[] convertToFloatArray(List<Float> samples) {
        float[] array = new float[samples.size()];
        for (int i = 0; i < array.length; i++) {
            array[i] = samples.get(i);
        }
        return array;
    }

    /**
     * Creates an float array with the given length containing only the one
     * given value.
     *
     * @param f The value to fill the array with
     * @param length The length of the array
     * @returns The new array
     */
    public static float[] createFloatArray(float f, int length) {
        float[] result = new float[length];
        for (int i = 0; i < length; i++) {
            result[i] = f;
        }
        return result;
    }

    /**
     * Splits an array of floats.
     *
     * @param samples The source array to split
     * @param offset The offset to split from
     * @param length The length of the new array
     *
     * @retrn The new array
     *
     * @throws IllegalArgumentException Thrown if there aren't enough samples to
     * proceed
     */
    public static float[] splitSampleArray(float[] samples, int offset, int length)
            throws IllegalArgumentException {
        if (offset + length > samples.length) {
            throw new IllegalArgumentException("There aren't enough samples in order to do that");
        }
        float[] result = new float[length];
        for (int i = 0, j = offset; i < length; i++, j++) {
            result[i] = samples[j];
        }
        return result;
    }

    /**
     * Reads all samples mono merged from the given decoder.
     *
     * @param deocder The decoder to read from
     * @returns The float array containing the samples
     */
    public static float[] readAllSamples(Decoder decoder) {
        List<Float> samples = new ArrayList<Float>();
        float[] buffer = new float[1024];
        while (decoder.readSamplesMergedMono(buffer) > 0) {
            for (int i = 0; i < buffer.length; i++) {
                samples.add(buffer[i]);
            }
        }
        return convertToFloatArray(samples);
    }

    /**
     * Creates a SampleBuffer from the data provided by the given decoder.
     *
     * @param deocder The decoder to read from
     * @returns The new SampleBuffer
     */
    public static SampleBuffer createSampleBuffer(Decoder decoder) {
        return new SampleBuffer(readAllSamples(decoder), decoder.getSamplingRate());
    }

    /**
     * Normalizes a given float array.
     *
     * @param data The float array to normalize
     * @param lowScale The lower bound of the normalized array
     * @param highScale The higher bound of the normalized array
     */
    public static void normalize(float[] data, float lowScale, float highScale) {
        float minValue = Float.MAX_VALUE;
        float maxValue = Float.MIN_VALUE;
        for (float f : data) {
            minValue = Math.min(minValue, f);
            maxValue = Math.max(maxValue, f);
        }

        for (int i = 0; i < data.length; i++) {
            data[i] = lowScale + (data[i] - minValue) * (highScale - lowScale) / (maxValue - minValue);
        }
    }
}
