Nsound  0.9.4
FilterPhaser.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterPhaser.cc 874 2014-09-08 02:21:29Z 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/FilterAllPass.h>
32 #include <Nsound/FilterPhaser.h>
33 #include <Nsound/Plotter.h>
34 #include <Nsound/Sine.h>
35 
36 #include <stdio.h>
37 #include <iostream>
38 
39 using namespace Nsound;
40 
41 using std::cerr;
42 using std::cout;
43 using std::endl;
44 
45 //-----------------------------------------------------------------------------
48  const float64 & sample_rate,
49  const uint32 n_stages,
50  const float64 & frequency,
51  const float64 & frequency_step_per_stage,
52  const float64 & max_delay_time_seconds)
53  :
54  Filter(sample_rate),
55  n_stages_(n_stages),
56  max_delay_(max_delay_time_seconds),
57  filters_(NULL),
58  frequencies_(),
59  waveform_(NULL),
60  waveform_position_()
61 {
63  M_ASSERT_VALUE(frequency, >, 0.0);
64  M_ASSERT_VALUE(frequency_step_per_stage, >, 0.0);
65  M_ASSERT_VALUE(max_delay_, >, 0.0);
66 
67  float64 f = frequency;
68 
69  if(f <= 0.0) f = 1.0;
70 
71  float64 fstep = frequency_step_per_stage;
72 
73  if(fstep <= 0.0) fstep = 1.0 / static_cast<float64>(n_stages_);
74 
75  if(max_delay_ <= 0.0) max_delay_ = 0.1;
76 
78 
79  waveform_ = new Buffer(static_cast<uint32>(sample_rate_));
80 
81  Sine sin(sample_rate_);
82 
83  *waveform_ = (1.0 + sin.generate(1.0, 1.0)) / 2.0;
84 
85  for(uint32 i = 0; i < n_stages_; ++i)
86  {
88  waveform_position_.push_back(0.0);
89  frequencies_.push_back(f + fstep * static_cast<float64>(i));
90  }
91 }
92 
93 //-----------------------------------------------------------------------------
96  :
97  Filter(copy.sample_rate_),
98  n_stages_(copy.n_stages_),
99  max_delay_(copy.max_delay_),
100  filters_(NULL),
101  frequencies_(),
102  waveform_(NULL),
103  waveform_position_(copy.waveform_position_)
104 {
106 
107  waveform_ = new Buffer(static_cast<uint32>(sample_rate_));
108 
109  for(uint32 i = 0; i < n_stages_; ++i)
110  {
112  waveform_position_.push_back(0.0);
113  }
114 
115  *this = copy;
116 }
117 
118 //-----------------------------------------------------------------------------
121 {
122  for(uint32 i = 0; i < n_stages_; ++i)
123  {
124  delete filters_[i];
125  }
126 
127  delete [] filters_;
128 
129  delete waveform_;
130 }
131 
134 filter(const AudioStream & x)
135 {
136  return Filter::filter(x);
137 }
138 
139 Buffer
141 filter(const Buffer & x)
142 {
143  return Filter::filter(x);
144 }
145 
146 float64
148 filter(const float64 & x)
149 {
150  float64 y = 0.0;
151 
152  for(uint32 i = 0; i < n_stages_; ++i)
153  {
154  float64 pos = waveform_position_[i]
155  + frequencies_[i];
156 
157  if(pos >= sample_rate_) pos -= sample_rate_;
158 
159  waveform_position_[i] = pos;
160 
161  float64 f = (*waveform_)[static_cast<uint32>(pos)];
162 
163  y += filters_[i]->filter(x, f * max_delay_);
164  }
165 
166  y /= static_cast<float64>(n_stages_);
167 
168  return (y+x) / 2.0;
169 }
170 
172 FilterPhaser &
175 {
176  if(this == &rhs)
177  {
178  return *this;
179  }
180 
182  max_delay_ = rhs.max_delay_;
183  *waveform_ = *rhs.waveform_;
186 
187  if(n_stages_ != rhs.n_stages_)
188  {
189  for(uint32 i = 0; i < n_stages_; ++i)
190  {
191  delete filters_[i];
192  }
193 
194  delete [] filters_;
195 
196  n_stages_ = rhs.n_stages_;
197 
198  filters_ = new FilterAllPass * [n_stages_];
199 
200  for(uint32 i = 0; i < n_stages_; ++i)
201  {
203  }
204  }
205 
206  for(uint32 i = 0; i < n_stages_; ++i)
207  {
208  *filters_[i] = *rhs.filters_[i];
209  }
210 
211  return *this;
212 }
213 
214 void
216 plot(boolean show_fc, boolean show_phase)
217 {
218  char title[128];
219  sprintf(title,
220  "Phaser Frequency Response\n"
221  "sr = %0.1f Hz, f = %0.1f Hz, delay = %0.3f ms",
222  sample_rate_,
223  frequencies_[0],
224  max_delay_ * 1000.0);
225 
226  Filter::plot(show_phase);
227 
228  Plotter pylab;
229 
230  uint32 n_rows = 1;
231 
232  if(show_phase)
233  {
234  n_rows = 2;
235  }
236 
237  if(show_fc)
238  {
239  pylab.subplot(n_rows, 1, 1);
240  }
241 
242  pylab.title(title);
243 }
244 
245 void
248 {
249  for(uint32 i = 0; i < n_stages_; ++i)
250  {
251  filters_[i]->reset();
252  waveform_position_[i] = 0.0;
253  }
254 }
unsigned int uint32
Definition: Nsound.h:153
FilterPhaser(const float64 &sample_rate, const uint32 n_stages, const float64 &frequency, const float64 &frequency_step_per_stage, const float64 &max_delay_in_seconds)
Definition: FilterPhaser.cc:47
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
FilterAllPass ** filters_
Definition: FilterPhaser.h:90
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
std::vector< float64 > frequencies_
Definition: FilterPhaser.h:91
std::vector< float64 > waveform_position_
Definition: FilterPhaser.h:93
double float64
Definition: Nsound.h:146
FilterPhaser & operator=(const FilterPhaser &rhs)
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
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
A class for filtering audio in the frequecy domain.
Definition: FilterPhaser.h:49
void plot(boolean show_phase=false)
Definition: Filter.cc:262
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream filter(const AudioStream &x)
void plot(boolean show_fc=false, boolean show_phase=false)
AudioStream filter(const AudioStream &x)
DOXME.
Definition: Sine.h:43
float64 sample_rate_
Definition: Filter.h:113