// fbank.cc
/*
    Filter bank and related classes for the Vocoder
    Copyright (C) 2002 Jarno Wuolijoki and Pilvari Pirtola

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdlib.h>
#include <spkit/spkit.h>
#include "vocoder.h"
#include "fbank.h"
#include <spkit/bwbandpa.h>
#include <math.h>

SPKitJoiner::SPKitJoiner() {}
SPKitError SPKitJoiner::setInputs(int cnt, SPKitProcessor **inps) {
  sum=new SPKitSum();
  if (cnt>0 && inps && inps[0]) {
    sum->setInput(inps[0]);
    setInput(inps[0]);
    for (int i=1; i<cnt; i++) if (inps[i]) sum->addInput(inps[i]);
  }
  return 0;
}

int SPKitJoiner::getSample(SPKitSample &outputSample, SPKitProcessor *caller) {
  return sum->getSample(outputSample, caller);
}

SPKitFilterbank::SPKitFilterbank() { 
  outputs=0; 
}

SPKitFilterbankBW::SPKitFilterbankBW() {
    mux=new SPKitMux(); frequencies=0; bandwidths=0;
}

SPKitError SPKitFilterbankBW::setInput(SPKitProcessor *input) {
    SPKitProcessor::setInput(input);
    mux->setInput(input);
    for (int i=0; i<filterCount; i++) {
        outputs[i]->setInput(mux);
        if (frequencies) ((SPKitBWBandPassFilter*)outputs[i])->setCenterFreqAndBW(frequencies[i], bandwidths[i]);
    }
    return 0;
}

SPKitError SPKitFilterbankBW::setFilterCount(int cnt) {
    filterCount=cnt;
    outputs=(SPKitProcessor**)malloc(filterCount*sizeof(SPKitProcessor*)); //FIXME: eti new:n syntaksi arrayille ja käytä sitä
    for (int i=0; i<filterCount; i++) {
        outputs[i]=new SPKitBWBandPassFilter();
        ((SPKitBWBandPassFilter*)outputs[i])->setCenterFreqAndBW(110*pow(2, i), 55*pow(2, i));
    }
    return 1;
}

void SPKitFilterbankBW::setFrqs(SPKitFloat *frqs, SPKitFloat *bws) {
    frequencies=frqs; bandwidths=bws;
    for (int i=0; i<filterCount; i++) {
        ((SPKitBWBandPassFilter*)outputs[i])->setCenterFreqAndBW(frqs[i], bws[i]);
    }
}













