paulstretch_cpp

PaulStretch
Log | Files | Refs | LICENSE

BinauralBeats.cpp (3012B)


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