Nsound  0.9.4
FilterFlanger.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterFlanger.cc 878 2014-11-23 04:51:23Z weegreenblobbie $
4 //
5 // Copyright (c) 2010 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>
31 #include <Nsound/FilterDelay.h>
32 #include <Nsound/FilterFlanger.h>
33 #include <Nsound/Generator.h>
34 #include <Nsound/Plotter.h>
35 #include <Nsound/Sine.h>
36 #include <Nsound/Triangle.h>
37 
38 
39 #include <stdio.h>
40 #include <iostream>
41 //~#include <string.h>
42 
43 using namespace Nsound;
44 
45 using std::cerr;
46 using std::cout;
47 using std::endl;
48 
49 //-----------------------------------------------------------------------------
52  const float64 & sample_rate,
53  const float64 & frequency,
54  const float64 & max_delay_time_seconds)
55  :
56  Filter(sample_rate),
57  frequency_(frequency),
58  max_delay_(max_delay_time_seconds),
59  delay_(NULL),
60  gen_(NULL)
61 {
62  M_ASSERT_VALUE(frequency_, >, 0.0);
63  M_ASSERT_VALUE(max_delay_, >, 0.0);
64 
66 
67  Sine sin(sample_rate_);
68 
69  Buffer waveform = (1.0 + sin.generate(1.0, 1.0)) / 2.0;
70 
71  gen_ = new Generator(sample_rate_, waveform);
72 }
73 
74 //-----------------------------------------------------------------------------
77  :
78  Filter(copy.sample_rate_),
79  frequency_(copy.frequency_),
80  max_delay_(copy.max_delay_),
81  delay_(NULL),
82  gen_(NULL)
83 {
85 
86  Sine sin(sample_rate_);
87 
88  Buffer waveform = (1.0 + sin.generate(1.0, 1.0)) / 2.0;
89 
90  gen_ = new Generator(sample_rate_, waveform);
91 
92  *this = copy;
93 }
94 
95 //-----------------------------------------------------------------------------
98 {
99  delete delay_;
100  delete gen_;
101 }
102 
105 filter(const AudioStream & x)
106 {
107  return Filter::filter(x);
108 }
109 
112 filter(const AudioStream & x, const float64 & frequency)
113 {
114  return Filter::filter(x, frequency);
115 }
116 
120  const AudioStream & x,
121  const float64 & frequency,
122  const float64 & delay)
123 {
124  uint32 n_channels = x.getNChannels();
125 
126  AudioStream y(x.getSampleRate(), n_channels);
127 
128  for(uint32 channel = 0; channel < n_channels; ++channel)
129  {
130  y[channel] = filter(x[channel], frequency, delay);
131  }
132 
133  return y;
134 }
135 
139  const AudioStream & x,
140  const Buffer & frequency,
141  const Buffer & delay)
142 {
143  uint32 n_channels = x.getNChannels();
144 
145  AudioStream y(x.getSampleRate(), n_channels);
146 
147  for(uint32 channel = 0; channel < n_channels; ++channel)
148  {
149  y[channel] = filter(x[channel], frequency, delay);
150  }
151 
152  return y;
153 }
154 
155 Buffer
157 filter(const Buffer & x)
158 {
159  reset();
160 
161  Buffer y(x.getLength());
162 
163  uint32 x_samples = x.getLength();
164 
165  for(uint32 i = 0; i < x_samples; ++i)
166  {
167  y << filter(x[i], frequency_, max_delay_);
168  }
169 
170  return y;
171 }
172 
173 Buffer
175 filter(const Buffer & x, const float64 & frequency)
176 {
177  reset();
178 
179  Buffer y(x.getLength());
180 
181  uint32 x_samples = x.getLength();
182 
183  for(uint32 i = 0; i < x_samples; ++i)
184  {
185  y << filter(x[i], frequency, max_delay_);
186  }
187 
188  return y;
189 }
190 
191 Buffer
193 filter(const Buffer & x, const float64 & frequency, const float64 & delay)
194 {
195  reset();
196 
197  Buffer y(x.getLength());
198 
199  uint32 x_samples = x.getLength();
200 
201  for(uint32 i = 0; i < x_samples; ++i)
202  {
203  y << filter(x[i], frequency, delay);
204  }
205 
206  return y;
207 }
208 
209 Buffer
211 filter(const Buffer & x, const Buffer & frequency, const Buffer & delay)
212 {
213  reset();
214 
215  Buffer y(x.getLength());
216 
217  uint32 x_samples = x.getLength();
218 
219  Buffer::const_circular_iterator f = frequency.cbegin();
221 
222  for(uint32 i = 0; i < x_samples; ++i)
223  {
224  y << filter(x[i], *f, *d);
225  }
226 
227  return y;
228 }
229 
230 float64
232 filter(const float64 & x)
233 {
234  return filter(x, frequency_, max_delay_);
235 
236 }
237 
238 float64
240 filter(const float64 & x, const float64 & frequency)
241 {
242  return filter(x, frequency, max_delay_);
243 
244 }
245 
246 float64
248 filter(const float64 & x, const float64 & frequency, const float64 & delay)
249 {
250  float64 f = gen_->generate(frequency);
251 
252  float64 y = delay_->filter(x, f*delay);
253 
254  return (y+x) / 2.0;
255 }
256 
261 {
262  if(this == &rhs)
263  {
264  return *this;
265  }
266 
268  frequency_ = rhs.frequency_;
269  max_delay_ = rhs.max_delay_;
270  *delay_ = *rhs.delay_;
271  *gen_ = *rhs.gen_;
272 
273  reset();
274 
275  return *this;
276 }
277 
278 void
280 plot(boolean show_fc, boolean show_phase)
281 {
282  char title[128];
283  sprintf(title,
284  "Flanger Frequency Response\n"
285  "sr = %0.1f Hz, f = %0.1f Hz, delay = %0.3f ms",
286  sample_rate_,
287  frequency_,
288  max_delay_ * 1000.0);
289 
290  Filter::plot(show_phase);
291 
292  Plotter pylab;
293 
294  uint32 n_rows = 1;
295 
296  if(show_phase)
297  {
298  n_rows = 2;
299  }
300 
301  if(show_fc)
302  {
303  pylab.subplot(n_rows, 1, 1);
304  }
305 
306  pylab.title(title);
307 }
308 
309 void
312 {
313  delay_->reset();
314  gen_->reset();
315 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
FilterFlanger(const float64 &sample_rate, const float64 &frequency, const float64 &max_delay_time_seconds)
void plot(boolean show_fc=false, boolean show_phase=false)
AudioStream filter(const AudioStream &x)
FilterDelay * delay_
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
void title(const std::string &title, const std::string &kwargs="")
Add a title to the plot at the top and centered.
Definition: Plotter.cc:1127
double float64
Definition: Nsound.h:146
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
AudioStream filter(const AudioStream &x)
Definition: FilterDelay.cc:98
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
Axes subplot(const uint32 n_rows, const uint32 n_cols, const uint32 n, const std::string &kwargs="", Axes *sharex=NULL, Axes *sharey=NULL)
Creates a figure in a subplot, subplot(A, B, C, **kwargs)
Definition: Plotter.cc:1031
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
void plot(boolean show_phase=false)
Definition: Filter.cc:262
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
virtual void reset()
Resets the position pointer back to the begging of the waveform.
Definition: Generator.cc:1272
A Buffer for storing audio samples.
Definition: Buffer.h:60
A class for filtering audio in the frequecy domain.
Definition: FilterDelay.h:47
A class for filtering audio in the frequecy domain.
Definition: FilterFlanger.h:49
DOXME.
Definition: Sine.h:43
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
float64 sample_rate_
Definition: Filter.h:113
FilterFlanger & operator=(const FilterFlanger &rhs)