package party.bami.eqfix.Equalizer;

// This class contains the implementation of a constant Q graphic equalizer
public class GraphicEQ {

    // array to hold all the filter bands
    private Filter[] bands;

    private int sampleRate;

    // Do not call with 0 bands
    public GraphicEQ(int bands, float bandwidth, float startFreq, int sampleRate) {

        this.sampleRate = sampleRate;
        this.bands = new Filter[bands];

        this.bands[0] = new Filter(sampleRate, startFreq, bandwidth, Filter.filterType.PEAKING);
        for (int i = 1; i < this.bands.length; i++) {
            float cf = this.bands[i-1].getFrequency() * (float)Math.pow(2, bandwidth);
            this.bands[i] = new Filter(sampleRate, cf, bandwidth, Filter.filterType.PEAKING);
        }
    }

    // cascade the bands and filter the input
    public float[] filter(float[] input) {
        float[] output = input.clone();

        for (int i = 0; i < this.bands.length; i++) {
            output = bands[i].filter(output);
        }

        return output;

    }

    /********* GETTERS and SETTERS **********/
    // get the bandwidth used in the filter
    public float getBandwidth(int band) {
        return this.bands[band].getBandwidth();
    }

    // returns the amount of bands used in the filter
    public int getBandCount() {
        return this.bands.length;
    }

    // set the samplerate for the filters
    public void setSampleRate(int sampleRate) {
        for (int i = 0; i < this.bands.length; i++) {
            this.bands[i].setSampleRate(sampleRate);
        }
    }

    // return the current active samplerate of the EQ
    public int getSampleRate() {
        return this.sampleRate;
    }

    // set the gain of a specified band
    public void setGain(int band, float gain) {
        this.bands[band].setGain(gain);
    }

    // get the current gain of a specified band
    public float getGain(int band) {
        return this.bands[band].getGain();
    }

    /******** DEBUGGING ***********/
    public void printBands() {
        for (int i = 0; i < bands.length; i++) {
            System.out.println("Band " + i + " at freq: " + bands[i].getFrequency());
        }
    }
}
