You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BinauralBeats.cpp 2.9KB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright (C) 2008-2011 Nasca Octavian Paul
  3. Author: Nasca Octavian Paul
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of version 2 of the GNU General Public License
  6. as published by the Free Software Foundation.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License (version 2) for more details.
  11. You should have received a copy of the GNU General Public License (version 2)
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include <math.h>
  16. #include <stdio.h>
  17. #include "BinauralBeats.h"
  18. void BinauralBeatsParameters::add2XML(XMLwrapper *xml){
  19. xml->addpar("stereo_mode",(int) stereo_mode);
  20. xml->addparreal("mono",mono);
  21. xml->beginbranch("FREE_EDIT");
  22. free_edit.add2XML(xml);
  23. xml->endbranch();
  24. };
  25. void BinauralBeatsParameters::getfromXML(XMLwrapper *xml){
  26. stereo_mode=(BB_STEREO_MODE)xml->getpar("stereo_mode",(int) stereo_mode,0,2);
  27. mono=xml->getparreal("mono",mono);
  28. if (xml->enterbranch("FREE_EDIT")){
  29. free_edit.getfromXML(xml);
  30. xml->exitbranch();
  31. };
  32. };
  33. //coefficients of allpass filters are found by Olli Niemitalo
  34. const REALTYPE Hilbert::coefl[]={0.6923877778065, 0.9360654322959, 0.9882295226860, 0.9987488452737};
  35. const REALTYPE Hilbert::coefr[]={0.4021921162426, 0.8561710882420, 0.9722909545651, 0.9952884791278};
  36. BinauralBeats::BinauralBeats(int samplerate_){
  37. samplerate=samplerate_;
  38. hilbert_t=0.0;
  39. };
  40. void BinauralBeats::process(REALTYPE *smpsl,REALTYPE *smpsr,int nsmps,REALTYPE pos_percents){
  41. // for (int i=0;i<nsmps;i++) smpsl[i]=smpsr[i]=sin(30.0*M_PI*i*2.0/nsmps)*0.4;
  42. //printf(" enabled %d\n",pars.free_edit.get_enabled());
  43. if (pars.free_edit.get_enabled()){
  44. float mono=pars.mono*0.5;
  45. for (int i=0;i<nsmps;i++){
  46. REALTYPE inl=smpsl[i];
  47. REALTYPE inr=smpsr[i];
  48. REALTYPE outl=inl*(1.0-mono)+inr*mono;
  49. REALTYPE outr=inr*(1.0-mono)+inl*mono;
  50. smpsl[i]=outl;
  51. smpsr[i]=outr;
  52. };
  53. REALTYPE freq=pars.free_edit.get_value(pos_percents);
  54. // freq=300;//test
  55. freq*=0.5;//half down for left, half up for right
  56. for (int i=0;i<nsmps;i++){
  57. hilbert_t=fmod(hilbert_t+freq/samplerate,1.0);
  58. REALTYPE h1=0,h2=0;
  59. hl.process(smpsl[i],h1,h2);
  60. REALTYPE x=hilbert_t*2.0*M_PI;
  61. REALTYPE m1=h1*cos(x);
  62. REALTYPE m2=h2*sin(x);
  63. REALTYPE outl1=m1+m2;
  64. REALTYPE outl2=m1-m2;
  65. h1=0,h2=0;
  66. hr.process(smpsr[i],h1,h2);
  67. m1=h1*cos(x);
  68. m2=h2*sin(x);
  69. REALTYPE outr1=m1-m2;
  70. REALTYPE outr2=m1+m2;
  71. switch(pars.stereo_mode){
  72. case SM_LEFT_RIGHT:
  73. smpsl[i]=outl2;
  74. smpsr[i]=outr2;
  75. break;
  76. case SM_RIGHT_LEFT:
  77. smpsl[i]=outl1;
  78. smpsr[i]=outr1;
  79. break;
  80. case SM_SYMMETRIC:
  81. smpsl[i]=(outl1+outr1)*0.5;
  82. smpsr[i]=(outl2+outr2)*0.5;
  83. break;
  84. };
  85. };
  86. };
  87. };