Nsound  0.9.4
FilterSlinky.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterSlinky.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2008 to Present Nick Hilton
6 //
7 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
8 //
9 //-----------------------------------------------------------------------------
10 
11 //-----------------------------------------------------------------------------
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //
27 //-----------------------------------------------------------------------------////
28 
29 #include <Nsound/AudioStream.h>
30 #include <Nsound/Buffer.h>
32 #include <Nsound/FilterDelay.h>
33 #include <Nsound/FilterSlinky.h>
34 
35 using namespace Nsound;
36 
37 using std::cerr;
38 using std::cout;
39 using std::endl;
40 
41 #define CERR_HEADER __FILE__ << ":" << __LINE__ << ": "
42 
43 //-----------------------------------------------------------------------------
46  const float64 & sample_rate,
47  const float64 & delay_octave,
48  const float64 & frequency_octave)
49  :
50  Filter(sample_rate),
51  delay_lines_(),
52  filters_()
53 {
54  float64 f = 0.0;
55 
56  while( f < sample_rate_ / 2.0)
57  {
58  if(f == 0.0)
59  {
60  f = frequency_octave;
61  }
62  else
63  {
64  f += frequency_octave;
65  }
66  }
67 
68  // Create the highest frequency pass band filter.
69 
70  float64 f_high = f;
71  f -= frequency_octave;
72  float64 f_low = f;
73 
74  filters_.push_back(
77  6,
78  f_low,
79  f_high,
80  0.01));
81 
82  // Create pass band filters for the remaing frequency octaves.
83  float64 dt = delay_octave;
84  while(f >= frequency_octave)
85  {
86  f_high = f;
87  f -= frequency_octave;
88  f_low = f;
89 
90  if(f_high == frequency_octave)
91  {
92  f_low = 0.0;
93  }
94 
95  filters_.push_back(
98  6,
99  f_low,
100  f_high,
101  0.01));
102 
103  delay_lines_.push_back(
104  new FilterDelay(sample_rate_, dt));
105 
106  dt += delay_octave;
107  }
108 
109 //~ cout << "n_filters = " << filters_.size() << endl
110 //~ << "n_delays = " << delay_lines_.size() << endl;
111 }
112 
113 //-----------------------------------------------------------------------------
116 {
117  FilterVector::iterator filter = filters_.begin();
118  FilterVector::iterator end = filters_.end();
119 
120  DelayVector::iterator delay = delay_lines_.begin();
121 
122  delete *filter;
123  ++filter;
124 
125  while(filter != end)
126  {
127  delete *filter;
128  delete *delay;
129 
130  ++filter;
131  ++delay;
132  }
133 }
134 
135 
138 filter(const AudioStream & x)
139 {
140  uint32 n_channels = x.getNChannels();
141 
142  AudioStream y(x.getSampleRate(), n_channels);
143 
144  for(uint32 channel = 0; channel < n_channels; ++channel)
145  {
146  y[channel] = FilterSlinky::filter(x[channel]);
147  }
148 
149  return y;
150 }
151 
154 filter(const AudioStream & x, const Buffer & frequencies)
155 {
156  return FilterSlinky::filter(x);
157 }
158 
159 Buffer
161 filter(const Buffer & x)
162 {
163  Buffer y;
164 
165  uint32 n_samples = x.getLength();
166 
167  for(uint32 n = 0; n < n_samples; ++n)
168  {
169  y << FilterSlinky::filter(x[n]);
170  }
171 
172  return y;
173 }
174 
175 Buffer
177 filter(const Buffer & x, const Buffer & frequencies)
178 {
179  return FilterSlinky::filter(x);
180 }
181 
182 float64
184 filter(const float64 & x)
185 {
186  float64 y = 0.0;
187 
188  FilterVector::iterator filter = filters_.begin();
189  FilterVector::iterator end = filters_.end();
190 
191  DelayVector::iterator delay = delay_lines_.begin();
192 
193  // There is always one more filter than delay lines, this is because the
194  // highest frequency have zero delay.
195 
196  y += (*filter)->filter(x);
197 
198  ++filter;
199 
200  while(filter != end)
201  {
202  y += (*delay)->filter((*filter)->filter(x));
203 
204  ++filter;
205  ++delay;
206  }
207 
208  return y;
209 }
210 
211 float64
213 filter(const float64 & x, const float64 & frequency)
214 {
215  return FilterSlinky::filter(x);
216 }
217 
218 void
221 {
222  FilterVector::iterator filter = filters_.begin();
223  FilterVector::iterator end = filters_.end();
224 
225  DelayVector::iterator delay = delay_lines_.begin();
226 
227  (*filter)->reset();
228  ++filter;
229 
230  while(filter != end)
231  {
232  (*filter)->reset();
233  (*delay)->reset();
234 
235  ++filter;
236  ++delay;
237  }
238 
239 }
240 
unsigned int uint32
Definition: Nsound.h:153
FilterSlinky(const float64 &sample_rate, const float64 &delay_time, const float64 &frequency_window)
Definition: FilterSlinky.cc:45
FilterVector filters_
Definition: FilterSlinky.h:91
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
DelayVector delay_lines_
Definition: FilterSlinky.h:90
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
Base class for IIR Filters, defines the interface.
Definition: Filter.h:49
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
A Buffer for storing audio samples.
Definition: Buffer.h:60
A class for filtering audio in the frequecy domain.
Definition: FilterDelay.h:47
AudioStream filter(const AudioStream &x)
float64 sample_rate_
Definition: Filter.h:113