Nsound  0.9.4
Public Member Functions | Protected Types | Protected Attributes | List of all members
Nsound::Vocoder Class Reference

#include <Nsound/Vocoder.h>

Public Member Functions

 Vocoder (const float64 &sample_rate, const float64 &window_length, const uint32 &n_bands, const float64 &freq_max=4000.0, const bool use_mel_scale=true, const bool plot_filter_bank=false)
 Default Constructor. More...
 
virtual ~Vocoder ()
 
Buffer filter (const Buffer &voice, const Buffer &carrier)
 
float64 filter (const float64 &voice, const float64 &carrier)
 
float64 getSampleRate () const
 
void reset ()
 

Protected Types

typedef std::vector< Filter * > FilterVector
 

Protected Attributes

float64 sample_rate_
 
uint32 window_size_
 
uint32 n_bands_
 
float64 freq_max_
 
FilterVector filters_input_
 
FilterVector filters_output_
 
FilterVector moving_average_
 

Detailed Description

Definition at line 44 of file Vocoder.h.

Member Typedef Documentation

typedef std::vector< Filter * > Nsound::Vocoder::FilterVector
protected

Definition at line 84 of file Vocoder.h.

Constructor & Destructor Documentation

Vocoder::Vocoder ( const float64 sample_rate,
const float64 window_length,
const uint32 n_bands,
const float64 freq_max = 4000.0,
const bool  use_mel_scale = true,
const bool  plot_filter_bank = false 
)

Default Constructor.

sample_rate: the sample rate window_length: the length of a window to average for the voice envelope n_bands: the number of band pass filters to use freq_max: the maximum frequency, band pass filters divide up the frequency range from 0 to freq_max

Definition at line 69 of file Vocoder.cc.

References f_to_mel(), Nsound::Plotter::figure(), filters_input_, filters_output_, freq_max_, Nsound::Buffer::getLength(), Nsound::Plotter::grid(), Nsound::Plotter::hold(), M_ASSERT_VALUE, M_CHECK_PTR, mel_to_f(), moving_average_, n_bands_, Nsound::Plotter::plot(), sample_rate_, window_size_, Nsound::Plotter::xlabel(), Nsound::Plotter::xlim(), Nsound::Plotter::ylabel(), and Nsound::Plotter::ylim().

76  :
77  sample_rate_(sample_rate),
78  window_size_(static_cast<uint32>(window_length * sample_rate_)),
79  n_bands_(n_bands),
80  freq_max_(freq_max),
84 {
86 
87  uint32 min_window_size = static_cast<uint32>(sample_rate_ * 0.005);
88 
89  M_ASSERT_VALUE(window_size_, >=, min_window_size);
90 
91  uint32 min_n_bands = 4;
92 
93  M_ASSERT_VALUE(n_bands_, >=, min_n_bands);
94 
95  float64 min_freq_max = 0.05 * sample_rate_;
96 
97  M_ASSERT_VALUE(freq_max_, >=, min_freq_max);
98 
99  float64 hi = freq_max_;
100 
101  // convert to mel scale
102 
103  if(use_mel_scale)
104  {
105  hi = f_to_mel(hi);
106  }
107 
108  // linear space from lo to hi.
109 
110  float64 band_width = hi / static_cast<float64>(n_bands_);
111 
112  Buffer freq_centers(n_bands_);
113 
114  for(uint32 i = 0; i <= n_bands_; ++i)
115  {
116  freq_centers << (i + 1) * band_width;
117  }
118 
119  // Shift to the center of each window.
120  freq_centers -= band_width / 2.0;
121 
122  // convert back to Hz scale.
123  if(use_mel_scale)
124  {
125  for(uint32 i = 0; i < freq_centers.getLength(); ++i)
126  {
127  freq_centers[i] = mel_to_f(freq_centers[i]);
128  }
129  }
130 
131  uint32 start = 0;
132  uint32 order = 6;
133  float64 ripple = 0.01;
134 
135  for(uint32 i = start; i < n_bands_; ++i)
136  {
137  float64 freq = freq_centers[i];
138 
139  // Cut offs are halfway between the filters.
140 
141  float64 f_lo = 0.0;
142 
143  if(i > 0)
144  {
145  f_lo = (freq + freq_centers[i - 1]) / 2.0;
146  }
147 
148  float64 f_hi = freq_max_;
149 
150  if(i + 1 < n_bands_)
151  {
152  f_hi = (freq + freq_centers[i + 1]) / 2.0;
153  }
154 
156  sample_rate_,
157  order,
158  f_lo,
159  f_hi,
160  ripple);
161 
162  M_CHECK_PTR(bp);
163 
164  filters_input_.push_back(bp);
165 
166  bp = new FilterBandPassIIR(
167  sample_rate_,
168  order,
169  f_lo,
170  f_hi,
171  ripple);
172 
173  M_CHECK_PTR(bp);
174 
175  filters_output_.push_back(bp);
176 
178 
179  M_CHECK_PTR(ma);
180 
181  moving_average_.push_back(ma);
182  }
183 
184  if(plot_filter_bank)
185  {
186  Plotter pylab;
187 
188  pylab.figure();
189  pylab.hold(true);
190 
191  for(size_t i = 0; i < filters_input_.size(); ++i)
192  {
193  Buffer x = filters_input_[i]->getFrequencyAxis();
194  Buffer y = filters_input_[i]->getFrequencyResponse().getdB();
195 
196  pylab.plot(x, y);
197  }
198 
199  pylab.xlim(0.0, freq_max_);
200  pylab.ylim(-40.0, 10.0);
201  pylab.xlabel("Frequency (Hz)");
202  pylab.ylabel("Filter Frequency Response (dB)");
203  pylab.grid(true);
204 
205 //~ pylab.show();
206  }
207 
208 }
unsigned int uint32
Definition: Nsound.h:153
void xlabel(const std::string &label, const std::string &kwargs="")
Add a label x axis.
Definition: Plotter.cc:1154
void grid(boolean flag)
Sets the grid state.
Definition: Plotter.cc:496
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
void plot(const Buffer &y, const std::string &fmt="", const std::string &kwargs="")
Plots the Buffer on the current figure.
Definition: Plotter.cc:765
void figure(const std::string &kwargs="") const
Creates a new figure window to plot in.
Definition: Plotter.cc:455
#define M_CHECK_PTR(ptr)
Definition: Macros.h:64
static float64 mel_to_f(float64 m)
Definition: Vocoder.cc:59
void xlim(const float64 &xmin, const float64 &xmax)
Sets the limit for the x & y axis.
Definition: Plotter.cc:389
uint32 n_bands_
Definition: Vocoder.h:81
double float64
Definition: Nsound.h:146
uint32 window_size_
Definition: Vocoder.h:80
void ylim(const float64 &ymin, const float64 &ymax)
Definition: Plotter.cc:422
FilterVector filters_input_
Definition: Vocoder.h:86
void hold(boolean flag)
Sets the hold state.
Definition: Plotter.cc:554
FilterVector filters_output_
Definition: Vocoder.h:87
static float64 f_to_mel(float64 f)
Definition: Vocoder.cc:52
float64 freq_max_
Definition: Vocoder.h:82
void ylabel(const std::string &label, const std::string &kwargs="")
Add a label y axis.
Definition: Plotter.cc:1180
float64 sample_rate_
Definition: Vocoder.h:79
A Buffer for storing audio samples.
Definition: Buffer.h:60
FilterVector moving_average_
Definition: Vocoder.h:88
Vocoder::~Vocoder ( )
virtual

Definition at line 212 of file Vocoder.cc.

References filters_input_, filters_output_, and moving_average_.

213 {
214  FilterVector::iterator fi = filters_input_.begin();
215  FilterVector::iterator fo = filters_output_.begin();
216  FilterVector::iterator ma = moving_average_.begin();
217 
218  FilterVector::iterator end = filters_input_.end();
219 
220  while(fi != end)
221  {
222  delete *fi;
223  delete *fo;
224  delete *ma;
225 
226  ++fi;
227  ++fo;
228  ++ma;
229  }
230 }
FilterVector filters_input_
Definition: Vocoder.h:86
FilterVector filters_output_
Definition: Vocoder.h:87
FilterVector moving_average_
Definition: Vocoder.h:88

Member Function Documentation

Buffer Vocoder::filter ( const Buffer voice,
const Buffer carrier 
)

Definition at line 234 of file Vocoder.cc.

References Nsound::Buffer::cbegin(), and Nsound::Buffer::getLength().

Referenced by main(), and my_main().

235 {
236  Buffer y;
237 
239 
240  uint32 n_samples = x.getLength();
241 
242  for(uint32 n = 0; n < n_samples; ++n, ++v)
243  {
244  y << Vocoder::filter(*v, x[n]);
245  }
246 
247  return y;
248 }
unsigned int uint32
Definition: Nsound.h:153
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer filter(const Buffer &voice, const Buffer &carrier)
Definition: Vocoder.cc:234
float64 Vocoder::filter ( const float64 voice,
const float64 carrier 
)

Definition at line 252 of file Vocoder.cc.

References filters_input_, filters_output_, and moving_average_.

253 {
254  FilterVector::iterator fi = filters_input_.begin();
255  FilterVector::iterator fo = filters_output_.begin();
256  FilterVector::iterator ma = moving_average_.begin();
257 
258  FilterVector::iterator end = filters_input_.end();
259 
260  float64 y = 0.0;
261 
262  while(fi != end)
263  {
264  // Filter the voice to use as an envelope..
265  float64 envelope = (*fi)->filter(voice);
266 
267  // Use the abs of the envelope.
268  envelope = std::fabs(envelope);
269 
270  // Smooth the filtered envelope.
271  envelope = (*ma)->filter(envelope);
272 
273  // Shape the filtered input signal.
274  y += envelope * (*fo)->filter(x);
275 
276  ++fi;
277  ++fo;
278  ++ma;
279  }
280 
281  return y;
282 }
double float64
Definition: Nsound.h:146
FilterVector filters_input_
Definition: Vocoder.h:86
FilterVector filters_output_
Definition: Vocoder.h:87
FilterVector moving_average_
Definition: Vocoder.h:88
float64 Nsound::Vocoder::getSampleRate ( ) const
inline

Definition at line 72 of file Vocoder.h.

References sample_rate_.

72 { return sample_rate_; };
float64 sample_rate_
Definition: Vocoder.h:79
void Vocoder::reset ( )

Definition at line 286 of file Vocoder.cc.

References filters_input_, filters_output_, and moving_average_.

Referenced by main().

287 {
288  FilterVector::iterator fi = filters_input_.begin();
289  FilterVector::iterator fo = filters_output_.begin();
290  FilterVector::iterator ma = moving_average_.begin();
291 
292  FilterVector::iterator end = filters_input_.end();
293 
294  while(fi != end)
295  {
296  (*fi)->reset();
297  (*fo)->reset();
298  (*ma)->reset();
299 
300  ++fi;
301  ++fo;
302  ++ma;
303  }
304 }
FilterVector filters_input_
Definition: Vocoder.h:86
FilterVector filters_output_
Definition: Vocoder.h:87
FilterVector moving_average_
Definition: Vocoder.h:88

Member Data Documentation

float64 Nsound::Vocoder::sample_rate_
protected

Definition at line 79 of file Vocoder.h.

Referenced by getSampleRate(), and Vocoder().

uint32 Nsound::Vocoder::window_size_
protected

Definition at line 80 of file Vocoder.h.

Referenced by Vocoder().

uint32 Nsound::Vocoder::n_bands_
protected

Definition at line 81 of file Vocoder.h.

Referenced by Vocoder().

float64 Nsound::Vocoder::freq_max_
protected

Definition at line 82 of file Vocoder.h.

Referenced by Vocoder().

FilterVector Nsound::Vocoder::filters_input_
protected

Definition at line 86 of file Vocoder.h.

Referenced by filter(), reset(), Vocoder(), and ~Vocoder().

FilterVector Nsound::Vocoder::filters_output_
protected

Definition at line 87 of file Vocoder.h.

Referenced by filter(), reset(), Vocoder(), and ~Vocoder().

FilterVector Nsound::Vocoder::moving_average_
protected

Definition at line 88 of file Vocoder.h.

Referenced by filter(), reset(), Vocoder(), and ~Vocoder().


The documentation for this class was generated from the following files: