gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
AudioMask.C
Go to the documentation of this file.
1 /*
2  libaudiomask - hybrid simultaneous audio masking threshold evaluation library
3  Copyright (C) 2000-2018 Dr Matthew Raphael Flax
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
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 for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <math.h>
20 #include <complex>
21 #include "AudioMask/AudioMask.H"
22 
23 #if !defined(_MSC_VER) && !defined(__CYGWIN__) && !defined(__MINGW32__) // Mingw/Microsoft doesn't have this header
24 #include <values.h>
25 #else
26 #if defined(__CYGWIN__) || defined(__MINGW32__)
27 #include <float.h>
28 #endif
29 #define MAXDOUBLE DBL_MAX
30 #endif
31 
32 #include <assert.h>
33 
35 AudioMask(int sampFreq, int fBankCount) : MooreSpread(fBankCount){
36  fs=sampFreq;
37  mask=Lvmu=NULL;
38  if (!(excitation=new double[fBankCount]))
39  std::cerr << "AudioMask: excitation malloc: Out of Memory"<<std::endl;
40  if (!(mask=new double[fBankCount]))
41  std::cerr << "AudioMask: mask malloc: Out of Memory"<<std::endl;
42  if (!(Lvmu=new double[fBankCount*fBankCount]))
43  std::cerr << "AudioMask: Lvmu malloc: Out of Memory"<<std::endl;
44 }
45 
47 ~AudioMask(void){
48  if (excitation) delete [] excitation;
49  excitation=NULL;
50  if (mask) delete [] mask;
51  mask=NULL;
52  if (Lvmu) delete [] Lvmu;
53  Lvmu=NULL;
54 }
55 
56 #include <fstream>
57 #define F2CB(f) (13.3*atan(0.75*f/1000))
58 void AudioMask::
59 exciteTerhardt(double **filterBankOutput, int sampleCount){
60  max=-MAXDOUBLE;
61  // Find the factor to scale by and scale ...
62  factor=fabs(bankCount-F2CB((double)fs/2.0));
63  // std::cout <<"factor "<<factor<<std::endl;
64 
65  // Find the excitation ...
66  for (int i=0;i<bankCount;i++){
67  // int which=bankCount-i-1;
68  //int which=i;
69  excitation[i]=0.0;
70  for (int j=0;j<sampleCount;j++)
71  excitation[i]+=filterBankOutput[i][j];
72  if (sampleCount!=fs)
73  excitation[i]*=((fs/2.0)/sampleCount);
74  excitation[i]=10.0*log10(excitation[i]);
75 // if (sampleCount!=fs){
76 // // std::cout<<"adjusting"<<std::endl;
77 // excitation[i]+=20*log10((fs/2.0-sampleCount)/sampleCount);
78 // }
79  }
80 
81  /* ofstream ex("excite.dat");
82  for (int i=0;i<bankCount;i++)
83  ex<<excitation[i]<<'\n';
84  ex.close();*/
85 
86  // Find the spreading function ...
87  //MooreSpread::excite(filterBankOutput, sampleCount, fs);
88  MooreSpread::excite(filterBankOutput, fs, fs);
89  /*
90  ofstream os("spread.dat");
91  for (int i=0;i<bankCount;i++){
92  for (int j=0;j<bankCount;j++)
93  os<<10.0*log10(spread[i][j])<<'\t';
94  os<<std::endl;
95  }
96  os.close();
97  */
98  // Find the mask ...
99  for (int i=0;i<bankCount;i++){
100  for (int j=0;j<bankCount;j++)
101  Lvmu[j+i*bankCount]=excitation[j]+10.0*log10(spread[i][j]);
102  }
103 
104  /* ofstream lvmu("Lvmu.dat");
105  for (int i=0;i<bankCount;i++){
106  for (int j=0;j<bankCount;j++){
107  lvmu<<Lvmu[j+i*bankCount]<<'\t';
108  }
109  lvmu<<std::endl;
110  }*/
111 
112  for (int i=0;i<bankCount;i++){
113  mask[i]=0.0;
114  for (int j=0;j<i;j++) // Lower Freqs.
115  mask[i]+=pow(10.0,Lvmu[i+j*bankCount]/20.0);
116  for (int j=i+1;j<bankCount;j++) // Higher Freqs.
117  mask[i]+=pow(10.0,Lvmu[i+j*bankCount]/20.0);
118  mask[i]/=factor;
119  if (mask[i]>max) max=mask[i];
120  // std::cout<<excitation[i]<< '\t'<<mask[i]<<std::endl;
121  }
122 }
123 
124 #define ALPHA 0.8
125 void AudioMask::
126 exciteBeerends(double **filterBankOutput, int sampleCount){
127  assert(-1); // this method requres debugging for sample counts which aren't the same as the sample rate.
128  max=-1000000.0;
129  // Find the factor to scale by and scale ...
130  factor=fabs(bankCount-F2CB((double)fs/2.0));
131 
132  // Find the excitation ...
133  for (int i=0;i<bankCount;i++){
134  int which=bankCount-i-1;
135  excitation[which]=0.0;
136  for (int j=0;j<sampleCount;j++){
137  excitation[which]+=filterBankOutput[i][j];
138  }
139  excitation[which]=10.0*log10(excitation[which]);
140  }
141 
142  // Find the spreading function ...
143  MooreSpread::excite(filterBankOutput, sampleCount, fs);
144 
145  // Find the mask ...
146  for (int i=0;i<bankCount;i++){
147  for (int j=0;j<bankCount;j++)
148  Lvmu[j+i*bankCount]=excitation[j]+10.0*log10(spread[i][j]);
149  }
150 
151  for (int i=0;i<bankCount;i++){
152  mask[i]=0.0;
153  for (int j=0;j<i;j++) // Lower Freqs.
154  mask[i]+=pow(10.0,pow(Lvmu[i+j*bankCount],ALPHA)/20.0);
155  for (int j=i+1;j<bankCount;j++) // Higher Freqs.
156  mask[i]+=pow(10.0,pow(Lvmu[i+j*bankCount],ALPHA)/20.0);
157  mask[i]=pow(mask[i], 1.0/ALPHA);
158  mask[i]/=factor;
159  if (mask[i]>max) max=mask[i];
160  // std::cout<<excitation[i]<< '\t'<<mask[i]<<std::endl;
161  }
162 }
163 
double ** spread
The Moore/Glasberg spreading due to the filters.
Definition: MooreSpread.H:38
void excite(double **filterBankOutput, int sampleCount, int sampleFreq)
Definition: MooreSpread.C:57
#define F2CB(f)
Definition: AudioMask.C:57
#define ALPHA
Definition: AudioMask.C:124
int fs
Sample frequency.
Definition: AudioMask.H:34
double max
The maximum value of the mask.
Definition: AudioMask.H:37
void exciteTerhardt(double **filterBankOutput, int sampleCount)
Definition: AudioMask.C:59
double * excitation
The excitation of the roex filters.
Definition: AudioMask.H:30
double * mask
The audio mask.
Definition: AudioMask.H:36
double * Lvmu
Definition: AudioMask.H:31
void exciteBeerends(double **filterBankOutput, int sampleCount)
Definition: AudioMask.C:126
AudioMask(int sampFreq, int fBankCount)
Definition: AudioMask.C:35
~AudioMask(void)
Deconstructor.
Definition: AudioMask.C:47
int bankCount
The number of sub-bankds in the filter bank.
Definition: MooreSpread.H:36
double factor
Definition: AudioMask.H:32
gtkIOStream: /tmp/gtkiostream/src/AudioMask/AudioMask.C Source File
GTK+ IOStream  Beta