File: AudioMaskExample.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 (169 lines) | stat: -rw-r--r-- 5,138 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
/*
 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 "../fft/realFFT.H"
#include "AudioMask.H"

// Filter bank memory de-allocation routine
void FBDeMalloc(double **outputGT, int fCount){
  if (outputGT){
    for (int i=0;i<fCount;i++)
      if (outputGT[i]) delete [] outputGT[i];
    delete [] outputGT;
  }
}

// Filter bank memory allocation routine
double ** FBMalloc(int fCount, int sCount){
  // Find the output of the gammatone filters
  double **outputGT=NULL;
  if (!(outputGT=new double*[fCount])){
    cerr<<"filter bank malloc error initial"<<endl;
    exit(-1);
  } else {
    for (int i=0;i<fCount;i++)
      outputGT[i]=NULL;
    for (int i=0;i<fCount;i++)
      if (!(outputGT[i]=new double[sCount])){
        cerr<<"filter bank malloc error secondary"<<endl;
        FBDeMalloc(outputGT, fCount);
        exit(-1);
      }
  }
  return outputGT;
}

#define INPUTFILENAME "fa.dat"
#define TMASKFILENAME "fa.t.mask"
#define BMASKFILENAME "fa.b.mask"
#define POWFILENAME "fa.pow"
#define EXCITEFILENAME "fa.excite"
#include <fstream>

#define USEGCFB
#ifdef USEGCFB
#define PFB gcfb
#else
#define PFB gtfb
#endif

void main(void){
  int sampleCount=16384, halfSampleCount=(int)rint((double)sampleCount/2.0);
  int count=50; int lowFreq=100; int sampleFreq=8192;
  double input[sampleCount], **outputGT, **output;
  double **powOutput;

  //  bzero(input, sampleCount*sizeof(double));
  //input[0]=100.0;
  // Load the input
  ifstream inputF(INPUTFILENAME);
  int temp;
  for (int i=0; i<sampleCount;i++)
    inputF >> temp >> input[i];
  inputF.close();

  powOutput=FBMalloc(count, halfSampleCount);
  outputGT=FBMalloc(count, sampleCount);
  for (int i=0;i<count;i++)
      bzero(outputGT[i], sampleCount*sizeof(double));

  output=FBMalloc(count, sampleCount);
  for (int i=0;i<count;i++)
      bzero(output[i], sampleCount*sizeof(double));

#ifdef USEGCFB
  // Get our gammachirp filter bank and filter using it
  GCFB gcfb(lowFreq, sampleFreq, count);
  gcfb.filter((double*)input, outputGT, output, sampleCount);
#else
  GTFB gtfb(lowFreq, sampleFreq, count);
  for (int i=1; i<=count;i++)
    gtfb.grab(i)->filter(input, &outputGT[i-1][0], sampleCount);
#endif

  // Convert the output to the frequency domain ...
  realFFTData fftData(sampleCount);
  realFFT fft(&fftData);
  for (int i=0;i<count;i++){
    for (int j=0; j<sampleCount;j++)
      fftData.in[j]=outputGT[i][j];
    fft.fwdTransform();
    fftData.compPowerSpec();
    for (int j=0; j<halfSampleCount;j++){
      powOutput[i][j]=sqrt(fftData.power_spectrum[j]);
      //      powOutput[i][j]=fftData.power_spectrum[j];
      //cout<<powOutput[i][j]<<' ';
    }
    //    cout<<endl;
  }

  // Get our spreading function ...
  AudioMask mask(count);
  // Set up the frequencies of interest (filter bank centre freqs.)
  PFB.grab(1);
  for (int i=0; i<count;i++)
#ifdef USEGCFB
    mask.setCFreq(i, gcfb.prev()->gt->cf);
#else
    mask.setCFreq(i, gtfb.prev()->cf);
#endif
  // Find the spreading function
  mask.exciteTerhardt(powOutput, sampleCount, sampleFreq);

  PFB.grab(1);
  ofstream outputF(TMASKFILENAME);
  for (int i=0; i<count;i++)
#ifdef USEGCFB
    outputF <<gcfb.prev()->gt->cf*((double)sampleCount/(double)sampleFreq)<<'\t'<< 20*log10(mask.mask[i])<<'\n';
#else
    outputF <<gtfb.prev()->cf*((double)sampleCount/(double)sampleFreq)<<'\t'<< 20*log10(mask.mask[i])<<'\n';
#endif
    //for (int i=0; i<sampleCount;i++)
    //outputF << input[i]<<'\n';
  outputF.close();

  mask.exciteBeerends(powOutput, sampleCount, sampleFreq);
  cout<<"Done exciting"<<endl;
  PFB.grab(1);
  outputF.open(BMASKFILENAME);
  for (int i=0; i<count;i++)
#ifdef USEGCFB
    outputF <<gcfb.prev()->gt->cf*(sampleCount/sampleFreq)<<'\t'<< 10*log10(mask.mask[i])<<'\n';
#else
    outputF <<gtfb.prev()->cf*(sampleCount/sampleFreq)<<'\t'<< 10*log10(mask.mask[i])<<'\n';
#endif
    //for (int i=0; i<sampleCount;i++)
    //outputF << input[i]<<'\n';
  outputF.close();

  outputF.open(POWFILENAME);
  for (int j=0; j<sampleCount;j++)
    fftData.in[j]=input[j];
  fft.fwdTransform();
  fftData.compPowerSpec();
  for (int j=0; j<halfSampleCount;j++){
    outputF<<j+1<<'\t'<<20*log10(sqrt(fftData.power_spectrum[j]))<<endl;
    //outputF<<j+1<<'\t'<<20*log10(fftData.power_spectrum[j])<<endl;
  }
  outputF.close();


  FBDeMalloc(powOutput, count);
  FBDeMalloc(output, count);
  FBDeMalloc(outputGT, count);
}