BinauralBeats.h (2217B)
1 /* 2 3 Copyright (C) 2011 Nasca Octavian Paul 4 Author: Nasca Octavian Paul 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of version 2 of the GNU General Public License 8 as published by the Free Software Foundation. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License (version 2) for more details. 14 15 You should have received a copy of the GNU General Public License (version 2) 16 along with this program; if not, write to the Free Software Foundation, 17 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 20 #ifndef BINAURAL_BEATS 21 #define BINAURAL_BEATS 22 #include "FreeEdit.h" 23 24 #include "globals.h" 25 26 class AP{//allpass 27 public: 28 AP(REALTYPE a_=0.5){ 29 in1=in2=out1=out2=0.0; 30 set(a_); 31 }; 32 REALTYPE process(REALTYPE in){ 33 REALTYPE out=a*(in+out2)-in2; 34 in2=in1;in1=in; 35 out2=out1;out1=out; 36 37 return out; 38 }; 39 REALTYPE set(REALTYPE a_){ 40 a=a_*a_; 41 }; 42 private: 43 REALTYPE in1,in2,out1,out2; 44 REALTYPE a; 45 }; 46 47 48 49 class Hilbert{ 50 public: 51 Hilbert(){ 52 for (int i=0;i<N;i++){ 53 apl[i].set(coefl[i]); 54 apr[i].set(coefr[i]); 55 }; 56 oldl=0.0; 57 }; 58 void process(REALTYPE in, REALTYPE &out1, REALTYPE &out2){ 59 out1=oldl; 60 out2=in; 61 62 for (int i=0;i<N;i++){ 63 out1=apl[i].process(out1); 64 out2=apr[i].process(out2); 65 }; 66 67 68 oldl=in; 69 }; 70 71 private: 72 static const int N=4; 73 static const REALTYPE coefl[]; 74 static const REALTYPE coefr[]; 75 AP apl[N],apr[N]; 76 REALTYPE oldl; 77 }; 78 79 enum BB_STEREO_MODE{ 80 SM_LEFT_RIGHT=0, 81 SM_RIGHT_LEFT=1, 82 SM_SYMMETRIC=2 83 }; 84 85 struct BinauralBeatsParameters{ 86 BinauralBeatsParameters(){ 87 stereo_mode=SM_LEFT_RIGHT; 88 mono=0.5; 89 }; 90 91 BB_STEREO_MODE stereo_mode; 92 float mono; 93 FreeEdit free_edit; 94 void add2XML(XMLwrapper *xml); 95 void getfromXML(XMLwrapper *xml); 96 }; 97 98 class BinauralBeats{ 99 public: 100 BinauralBeats(int samplerate_); 101 void process(REALTYPE *smpsl,REALTYPE *smpsr,int nsmps,REALTYPE pos_percents); 102 BinauralBeatsParameters pars; 103 private: 104 int samplerate; 105 REALTYPE hilbert_t; 106 Hilbert hl,hr; 107 }; 108 109 #endif