logo
Main Page | Namespaces | Classes | Compounds | Files | Compound Members | Related

sndutils.h

00001 //
00002 //  SYNTOPIA. See http://Syntopia.sourceforge.net for details and documentation.        
00003 //
00004 //      Author of this file: Mikael Hvidtfeldt Christensen (mikaelc@users.sourceforge.net)
00005 //
00006 
00007 
00008 
00009 
00010 #ifndef __sndutils__
00011 #define __sndutils__
00012 
00013 #include "math.h"
00014 
00015 
00016 namespace SynthCore {
00017 
00018 // -----------------------------------------------------------------------
00019 
00020 
00022 
00028 class DelayLine {
00029 public:
00030             float * bufferStart, * bufferEnd, * readPtr;
00031                 float output1;
00032                 float * temp;
00033 
00035                 int bufferLength;
00036                 
00038                 float read(float input);
00039 
00041                 void setDelay(int D) {;}
00042 
00044                 DelayLine(int Length);
00045 
00047                 ~DelayLine();
00048 
00050                 float readNBack(int N);
00051 
00053                 float readNForward(int N); 
00054 };
00055 
00056 // -----------------------------------------------------------------------
00057 
00058 class Sample;
00059 
00061 
00069 class BiFilter {
00070         
00071 private:
00072 
00073         // Filter Coefficients and buffer vars
00074         float a0,a0INV,a1,a2,b0,b1,b2,x0,x1,x2,y0,y1,y2;
00075         float omega;
00076         float cs;
00077         float sn;
00078     float alpha;
00079         float beta;
00080 public: 
00082         BiFilter() ; 
00083         
00085         ~BiFilter() {};
00086 
00088         float read(float input); 
00089         
00091         void bypassFreqAndQ(float freq, float Q);
00092 
00094         void lowpassFreqAndQ(float freq, float Q);
00095         
00097         void notchFreqAndQ(float freq, float Q);
00098 
00100         void bandpass2FreqAndQ(float freq, float Q);
00101         
00103         void bandpass1FreqAndQ(float freq, float Q);
00104         
00106         void highpassFreqAndQ(float freq, float Q);
00107 
00109         void allpassFreqAndQ(float freq, float Q);
00110         
00112         void highShelfFreqAndQAndA(float freq, float Q, float A);
00113         
00115         void lowShelfFreqAndQAndA(float freq, float Q, float A);
00116         
00118         void peakingEQFreqAndQAndA(float freq, float Q, float A);
00119         
00121         float getFreqResponse(float wT) ;       
00122 
00124         float getPhaseResponse(float wT) ;
00125 
00127         float getFreqResponse2(float wT) ;
00128 
00130         void getPolesAndZeroes(float &p1r,float &p1i,float &p2r,float &p2i,float &z1r,float &z1i,float &z2r,float &z2i);
00131 
00134         void clone(BiFilter * from);
00135 
00136 
00138         Sample * getFreqResponseSample(int resolution);
00139 
00141         Sample * getPhaseResponseSample(int resolution);
00142         
00143 };
00144 
00145 // -----------------------------------------------------------------------
00146 
00148 
00152 class Sample {
00153 
00154 public:
00156                 int             length;         
00157                 
00159                 int             wrap;                   
00160                 
00162                 float   samplingFreq;   
00163 
00165                 float   samplingTune;   
00166 
00168                 float * samples;                
00169 
00171                 Sample() {length=0; samplingFreq=0; samplingTune=0; wrap = 0;} 
00172 
00174                 Sample(int sampleLength) {length=sampleLength; wrap=0; samples = new float[sampleLength];}
00175 
00177                 Sample(int sampleLength, int wrapAround) {length=sampleLength; wrap=wrapAround; samples = new float[sampleLength+wrapAround];}
00178 
00180                 ~Sample() {if (length != 0) {delete [] samples;} };
00181                 
00183                 float getMax() {
00184                         float max = 0;
00185 
00186                         if (length != 0) {max = samples[0];}
00187 
00188                         for (int i=1; i<length; i++) {
00189                                 if (samples[i]>max) {max = samples[i];}
00190                         }
00191 
00192                         return max;
00193                 }
00194 
00196                 float getMin() {
00197                         float min = 0;
00198 
00199                         if (length != 0) {min = samples[0];}
00200 
00201                         for (int i=1; i<length; i++) {
00202                                 if (samples[i]<min) {min = samples[i];}
00203                         }
00204 
00205                         return min;
00206                 }
00207 
00209                 void makeWrap()
00210                 {
00211                         for (int m=0; m<wrap; m++) {
00212                                 samples[length+m] = samples[m]; 
00213                         }
00214                 }
00215 
00217                 void wavenormalize()
00218                 {
00219                         // normalize to values between -1,1.
00220                         float min=getMin();
00221                         float max=getMax();
00222                         if (max!=min)
00223                                 for (int i=0; i<length+wrap; i++) {
00224                                         samples[i]=-1.0f+2.0f*(samples[i]-min)/(max-min);
00225                                 }
00226                 }
00227 
00229                 void normalize()
00230                 {
00231                         // normalize to values between -1,1.
00232                         float min=getMin();
00233                         float max=getMax();
00234                         if (max!=min)
00235                                 for (int i=0; i<length+wrap; i++) {
00236                                         samples[i]=(samples[i]-min)/(max-min);
00237                                 }
00238                 }
00239 
00240                 
00241                         
00242 };
00243 
00244 // -------------------------------------------------------------------------
00245 
00247  
00248 class Fourier {
00249 public:
00255         void static transform(float data[], unsigned long nn, int isign);
00256 
00258         Sample static * sawtooth(int sampleLength, int partials, int wrapAround);
00259         
00261         Sample static * square(int sampleLength, int partials, int wrapAround);
00262 
00264         Sample static * triangle(int sampleLength, int partials, int wrapAround);
00265 
00268         Sample static * sine(int sampleLength, int partials, int wrapAround);
00269 
00270         Sample static * wave1(int sampleLength, int partials, int wrapAround);
00271         Sample static * wave2(int sampleLength, int partials, int wrapAround);
00272         Sample static * wave3(int sampleLength, int partials, int wrapAround);
00273         Sample static * wave4(int sampleLength, int partials, int wrapAround);
00274 
00275 };
00276 
00277 
00278 // ------------------------------------------------------------------------
00279 
00281 
00282 class FreqTable {
00283 private:
00284         double k ;      // 12th root of 2
00285 public: 
00287         float freqTab[128];
00288         
00299         FreqTable() {
00300                 k = 1.059463094359;
00301         
00302 
00303                 double a = 6.875;a *= k;        // b
00304                 a *= k; // bb
00305                 a *= k; // c, frequency of midi note 0
00306                 for (int i = 0; i < 128; i++)   
00307                 {
00308                         freqTab[i] = (float)a;
00309                         a *= k;
00310                 }
00311         }
00312 
00314         ~FreqTable() {;};
00315 
00318         int getMaxPartialsFromFreq(float freq)
00319         {
00320                 return ((int) floor(24000.0f/freq));
00321         }
00322 
00324         int getMaxPartialsFromMidi(int midikey)
00325         {
00326                 return getMaxPartialsFromFreq(freqTab[midikey]);
00327         }
00328 };
00329 
00330 
00331 }; // end of namespace: SynthCore
00332 
00333 #endif

Syntopia Project. Visit the web page, or the SourceForge page.
Docs made by Doxygen. Email: Mikael Christensen