File: AudioMasker.cc

package info (click to toggle)
libaudiomask 1.0-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 17,668 kB
  • ctags: 636
  • sloc: cpp: 787; perl: 108; makefile: 67
file content (323 lines) | stat: -rw-r--r-- 9,579 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 libaudiomask - hybrid simultaneous audio masking threshold evaluation library
    Copyright (C) 2000-2010  Dr Matthew Raphael Flax

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "AudioMasker.H"
#include <math.h>
#include <strings.h>

AudioMasker::
AudioMasker(int sampFreq, int fBankCount) : AudioMask(sampFreq, fBankCount){
  output=powOutput=NULL;
  input=NULL;
  //gtfb=NULL;
  pfb=NULL;
  fftData=NULL;
  fft=NULL;

  bankCount=fBankCount;
  std::cout<<"Bank Count "<<bankCount<<std::endl;
  //  sampleFreq=DEFAULT_SAMPLEFREQ;
  sampleCount=DEFAULT_SAMPLECOUNT;
  FBMalloc();
}

AudioMasker::
AudioMasker(void) : AudioMask(DEFAULT_SAMPLEFREQ, DEFAULT_FBCOUNT){
  output=powOutput=NULL;
  input=NULL;
  //gtfb=NULL;
  pfb=NULL;
  fftData=NULL;
  fft=NULL;

  bankCount=DEFAULT_FBCOUNT;
  //  std::cout<<"Bank Count "<<bankCount<<std::endl;
  //sampleFreq=DEFAULT_SAMPLEFREQ;
  sampleCount=DEFAULT_SAMPLECOUNT;
  FBMalloc();
}

AudioMasker::
~AudioMasker(void){
  //std::cout<<"AudioMasker::~AudioMasker"<<std::endl;
  FBDeMalloc();
}

// Filter bank memory de-allocation routine
void AudioMasker::
FBDeMalloc(void){
  //std::cout<<"AudioMasker::FBDeMalloc"<<std::endl;
  if (output){
    for (int i=0;i<bankCount;i++)
      if (output[i]) delete [] output[i];
    delete [] output;
  }
  output=NULL;

  if (powOutput){
    for (int i=0;i<bankCount;i++)
      if (powOutput[i]) delete [] powOutput[i];
    delete [] powOutput;
  }
  powOutput=NULL;

  if (input) delete [] input;
  input=NULL;

  if (pfb) delete pfb;
  pfb=NULL;

  //  if (gtfb) delete gtfb;
  //gtfb=NULL;

  if (fftData) delete fftData;
  fftData=NULL;
  if (fft) delete fft;
  fft=NULL;
}

// Filter bank memory allocation routine
void AudioMasker::
FBMalloc(void){
  FBDeMalloc(); //Ensure not malloced already
  // Allocate the output matrix of the perceptual filters
  if (!(output=new double*[bankCount])){
    std::cerr<<"AudioMasker::FBMalloc : filter bank malloc error initial"<<std::endl;
    exit(-1);
  } else {
    for (int i=0;i<bankCount;i++)
      output[i]=NULL;
    for (int i=0;i<bankCount;i++)
      if (!(output[i]=new double[sampleCount])){
        std::cerr<<"AudioMasker::FBMalloc : filter bank malloc error secondary"<<std::endl;
        FBDeMalloc();
        exit(-1);
      }
  }

  // Allocate the powOutput matrix of the perceptual filters
  if (!(powOutput=new double*[bankCount])){
    std::cerr<<"AudioMasker::FBMalloc : filter bank malloc error initial"<<std::endl;
    exit(-1);
  } else {
    for (int i=0;i<bankCount;i++)
      powOutput[i]=NULL;
    for (int i=0;i<bankCount;i++)
      //      if (!(powOutput[i]=new double[(int)ceil((double)sampleCount/2.0)])){
      if (!(powOutput[i]=new double[(int)ceil((double)fs/2.0)])){
        std::cerr<<"AudioMasker::FBMalloc : filter bank malloc error secondary"<<std::endl;
        FBDeMalloc();
        exit(-1);
      }
  }

  if (!(input=new double[sampleCount])){
    std::cerr<<"AudioMasker::FBMalloc : input malloc error"<<std::endl;
    FBDeMalloc();
    exit(-1);
  }

  //  if (!(gtfb= new GTFB(DEFAULT_LOWFERQ, sampleFreq, bankCount))){
  //  std::cerr<<"AudioMasker::FBMalloc : gtfb malloc error"<<std::endl;
  //  FBDeMalloc();
  //  exit(-1);
  //}


  if (!(pfb= new DepUKFB(fs, bankCount))){
    std::cerr<<"AudioMasker::FBMalloc : pfb malloc error"<<std::endl;
    FBDeMalloc();
    exit(-1);
  }

  //  if (!(fftData=new realFFTData(sampleCount))){
  if (!(fftData=new realFFTData(fs))){
    std::cerr<<"AudioMasker::FBMalloc : fftData malloc error"<<std::endl;
    FBDeMalloc();
    exit(-1);
  }

  if (!(fft=new realFFT(fftData))){
    std::cerr<<"AudioMasker::FBMalloc : fft malloc error"<<std::endl;
    FBDeMalloc();
    exit(-1);
  }
}

/** These should be implemented differently for different Input types
 */
void AudioMasker::
excite(short int *Input, int sCount){
  if (sCount != sampleCount)// Check for matrix re-size
    FBDeMalloc();

  sampleCount=sCount;

  if (!output) // Check for null matrix
    FBMalloc();

  for (int i=0;i<sCount;i++) //copy the input as double
    input[i]=(double)Input[i];

  process(); //Do the processing
}

void AudioMasker::
excite(double *Input, int sCount){
  if (sCount != sampleCount)// Check for matrix re-size
    FBDeMalloc();

  sampleCount=sCount;

  if (!output) // Check for null matrix
    FBMalloc();

  for (int i=0;i<sCount;i++) //copy the input as double
    input[i]=Input[i];

  process(); //Do the processing
}

double AudioMasker::
findThreshold(double freq){
  for (int i=bankCount-1;i>=0;i--){
    //    if (freq>=gtfb->edgeFreq[i])
    if (freq>=pfb->ef[i]){
      //      std::cout<<i<<std::endl;
      return mask[i];
    }
  }
  std::cerr <<"AudioMasker::findThreshold : freq !=> gtfb->edgeFreq["<<bankCount-1<<"] returning 0"<<std::endl;
  return 0;
}

#include <fstream>
void AudioMasker::
process(void){
  bzero(fftData->in, fs*sizeof(fftw_real));//Ensure we start with a zero array
  for (int j=0; j<sampleCount;j++)//Find pow spec of input
    fftData->in[j]=input[j];
  fft->fwdTransform();
  fftData->compPowerSpec();
  for (int j=0; j<(int)rint((double)fs/2.0);j++)
    fftData->power_spectrum[j]=sqrt(fftData->power_spectrum[j]);
  //ofstream output("w");
  //int halfSampleCount=(int)rint((double)sampleCount/2.0);
  int halfFS=(int)rint(fs/2.0);
  for (int i=0;i<bankCount;i++){ // Find power spectra and copy over
    //    for (int j=0; j<halfSampleCount;j++){
    for (int j=0; j<halfFS;j++){
      //  output<<(*pfb)(i,j,halfSampleCount)<<'\t';
      //      //powOutput[i][j]=fftData->power_spectrum[j]*(*pfb)(i,j,halfSampleCount);
      powOutput[i][j]=fftData->power_spectrum[j]*(*pfb)(i,j,halfFS);
    }
    //output<<std::endl;
  }
  /*
  ofstream outF("filter.dat");
  for (int i=0;i<bankCount;i++){
    for (int j=0;j<sampleCount/2;j++)
      outF<<powOutput[i][j]<<'\t';
    outF<<std::endl;
  }
  outF.close();
  */

  //  gtfb->grab(1);
  for (int i=0; i<bankCount;i++)//Set up freq of interest (pfb centre freqs.)
    setCFreq(i, pfb->cf[i]);
  //    setCFreq(i, gtfb->prev()->cf);
  exciteTerhardt(powOutput, fs);// Find the masking function
  //  exciteTerhardt(powOutput, sampleCount);// Find the masking function
  //exciteBeerends(powOutput, sampleCount);// Find the masking function
}

/*
#include <fstream>
#include "../gammatone/GTSensitivity.H"
void AudioMasker::
process(void){
  //for (int i=0;i<bankCount;i++){ //Zero the filter bank output(due to IIR nature)
  //  bzero(output1[i], sampleCount*sizeof(double));
  //  bzero(output2[i], sampleCount*sizeof(double));
  //  bzero(output[i], sampleCount*sizeof(double));
  //  }
  LinkList<double **> outputs;
  outputs.add(output1);
  outputs.add(output2);
  outputs.grab(1);

  for (int i=0;i<bankCount;i++) //Zero filter bank output(due to IIR nature)
    bzero(outputs.current()[i], sampleCount*sizeof(double));
  for (int i=1; i<=bankCount;i++) // Filter the input
    gtfb->grab(i)->filter(input, &outputs.current()[i-1][0], sampleCount);

  double **last, **now;
  for (int j=1;j<RECURSE;j++){
    last=outputs.current();
    now=outputs.next();
    for (int i=0;i<bankCount;i++) //Zero filter bank output(due to IIR nature)
      bzero(now[i], sampleCount*sizeof(double));
    for (int i=1; i<=bankCount;i++) // Filter the input
      gtfb->grab(i)->filter(&last[i-1][0], &now[i-1][0], sampleCount);
  }

  for (int i=1; i<=bankCount;i++) // Filter the input - originally once
    gtfb->grab(i)->filter(input, &output[i-1][0], sampleCount);

  //  GTSensitivity gts; // sensitivity adjustement for the IIR gammatone filterbank

  for (int i=0;i<bankCount;i++){ // Find power spectra and copy over
    for (int j=0; j<sampleCount;j++)
      fftData->in[j]=outputs.current()[i][j];
    //fftData->in[j]=output[i][j];
    fft->fwdTransform();
    fftData->compPowerSpec();

    double cf=gtfb->grab(i+1)->cf;
    for (int j=0; j<(int)rint((double)sampleCount/2.0);j++){
      //      powOutput[i][j]=pow(10.0,gts.find(j,cf,fs)/20.0)*sqrt(fftData->power_spectrum[j]);
      powOutput[i][j]=sqrt(fftData->power_spectrum[j]);
    }

    //double shift=fftData->power_spectrum[fftData->minPowerBin];
    ////The following tries to correct for the shift up in the gammatone response
    //if (shift>10.0)
    //  for (int j=0; j<(int)rint((double)sampleCount/2.0);j++)
    //powOutput[i][j]=sqrt(fftData->power_spectrum[j]/shift);
    //else
    //  for (int j=0; j<(int)rint((double)sampleCount/2.0);j++)
    //powOutput[i][j]=sqrt(fftData->power_spectrum[j]);
  }

  ofstream outF("filter.dat");
  for (int i=0;i<bankCount;i++){
    for (int j=0;j<sampleCount/2;j++)
      outF<<powOutput[i][j]<<'\t';
    outF<<std::endl;
  }
  outF.close();

  gtfb->grab(1);
  for (int i=0; i<bankCount;i++)//Set up freq of interest (pfb centre freqs.)
    setCFreq(i, gtfb->prev()->cf);

  exciteTerhardt(powOutput, sampleCount);// Find the masking function
  //exciteBeerends(powOutput, sampleCount);// Find the masking function
}
*/