CLAM-Development
1.1
|
00001 /* 00002 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 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; //TODO big cludge for streaming 00043 } 00044 00045 int nBands = mNumBandsCtl.GetLastValue(); 00046 int nGainBands = 5; //it is one more than the actual gains 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);// make sure all bins are set to zero 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