Vocoder.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "Vocoder.hxx"
00023 #include "ProcessingFactory.hxx"
00024
00025 namespace CLAM
00026 {
00027 namespace Hidden
00028 {
00029 static const char * metadata[] = {
00030 "key", "Vocoder",
00031 "category", "Spectral Transformations",
00032 "description", "Vocoder",
00033 0
00034 };
00035 static FactoryRegistrator<ProcessingFactory, Vocoder> reg = metadata;
00036 }
00037
00038 bool Vocoder::Do(const Spectrum& in, Spectrum& out)
00039 {
00040 if ( !mConfig.GetPreserveOuts() )
00041 {
00042 out = in;
00043 }
00044
00045 int nBands = mNumBandsCtl.GetLastValue();
00046 int nGainBands = 5;
00047 int firstBand = 5;
00048 TData fFirstBand = 100;
00049
00050 DataArray gains(4);
00051 gains[0] = log2lin(mBand0GainCtl.GetLastValue());
00052 gains[1] = log2lin(mBand1GainCtl.GetLastValue());
00053 gains[2] = log2lin(mBand2GainCtl.GetLastValue());
00054 gains[3] = log2lin(mBand3GainCtl.GetLastValue());
00055
00056 int spectrumSize = in.GetSize();
00057 TData spectralRange = in.GetSpectralRange();
00058 TData spectralResolution = spectrumSize/spectralRange;
00059
00060 mMagBuffer.Resize(0);
00061 mMagBuffer.Resize(spectrumSize);
00062 mMagBuffer.SetSize(spectrumSize);
00063
00064 TData bandFactor = pow(TData(spectrumSize),1./nBands);
00065 TData fBandFactor = pow(TData(spectralRange),1./nBands);
00066
00067 TData fCurrentBandLimit = fFirstBand;
00068 int currentBandLimit = Round(fFirstBand*spectralResolution);
00069 int lastBandLimit = 0;
00070 TData fLastBandLimit = 0.;
00071
00072 int freqShift = Round(mFreqShiftCtl.GetLastValue()*spectralResolution);
00073
00074 int currentOscBin = int(spectrumSize/nBands) + freqShift;
00075 TData fCurrentOsc = spectralRange/nBands;
00076
00077 int currentGainIndex = 0;
00078 TData fCurrentGainBandLimit = fCurrentBandLimit*2;
00079 int currentGainBandLimit = Round(fCurrentGainBandLimit*spectralResolution);
00080
00081 TData gainBandFactor = pow(TData(spectrumSize/currentBandLimit),1./nGainBands);
00082 TData fGainBandFactor = pow(TData(spectralRange/fCurrentGainBandLimit),1./nGainBands);
00083
00084 TData bandEnergy =0;
00085 DataArray& inMag = in.GetMagBuffer();
00086 int currentBand = 0;
00087 for(int i = 0; i<spectrumSize; i++)
00088 {
00089 if(i>currentBandLimit)
00090 {
00091 bandEnergy = sqrt(bandEnergy);
00092 mMagBuffer[currentOscBin] = bandEnergy*gains[currentGainIndex];
00093 lastBandLimit = currentBandLimit;
00094 fCurrentBandLimit*=fBandFactor;
00095 currentBandLimit = Round(fCurrentBandLimit*spectralResolution);
00096 if(currentBandLimit>spectrumSize) currentBandLimit = spectrumSize;
00097 currentOscBin = int ((currentBandLimit-lastBandLimit)*0.5 + lastBandLimit) + freqShift;
00098 bandEnergy = 0;
00099 currentBand++;
00100 if(currentBand>nBands) break;
00101 }
00102 if (i>currentGainBandLimit)
00103 {
00104 if(currentGainIndex<3)
00105 currentGainIndex++;
00106 fCurrentGainBandLimit*=fGainBandFactor;
00107 currentGainBandLimit = Round(fCurrentGainBandLimit*spectralResolution);
00108 }
00109 bandEnergy += inMag[i];
00110
00111 }
00112 out.SetMagBuffer(mMagBuffer);
00113 return true;
00114 }
00115
00116
00117 }
00118