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

DOXME. More...

#include <Nsound/Cosine.h>

Inheritance diagram for Nsound::Cosine:
Inheritance graph
[legend]

Public Member Functions

 Cosine (const float64 &sample_rate)
 DOXME. More...
 
void setRealtime (bool flag)
 Sets realtime mode, disables automatic reset() if set. More...
 
void addSlaveSync (Generator &slave)
 Adds a generator as a slave to this instance for syncing. More...
 
void buzzInit (const uint32 &max_harmonics)
 Initaializes Buzz settings for real-time calls. More...
 
float64 buzz (const float64 &frequency, const float64 &n_harmonics, const float64 &delay)
 Returns sample from a set of harmonics. Based on the Csound buzz opcode. More...
 
Buffer buzz (const float64 &duration, const float64 &frequency, const float64 &n_harmonics, const float64 &delay)
 Returns a set of harmonics. Based on the Csound buzz opcode. More...
 
Buffer buzz (const float64 &duration, const Buffer &frequency, const Buffer &n_harmonics, const Buffer &delay)
 Returns a set of harmonics. Based on the Csound buzz opcode. More...
 
void setChorus (const uint32 n_voices, const float64 &sigma=0.02)
 Chorus or Unison. More...
 
Buffer drawDecay (const float64 &duration, const float64 &alpha=2.0 *M_PI) const
 This method draws an exponential curve that decays from 1.0 to 0.0 over the duration. More...
 
Buffer drawGaussian (const float64 &duration, const float64 &mu, const float64 &sigma, const boolean &normalize=true) const
 This method draws a Gaussian curve over duration seconds. More...
 
Buffer drawFatGaussian (const float64 &duration, const float64 &pass_band_percent=0.01) const
 This method draws a standard Gaussian curve over duration seconds, with a specified pass band. More...
 
Buffer drawLine (const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
 This method draws a linear line beteween 2 points. More...
 
Buffer drawParabola (const float64 &duration, const float64 &y1, const float64 &x2, const float64 &y2, const float64 &y3) const
 This method draws a parabola between three points, intersecting the middle point. More...
 
Buffer drawSine (const float64 &duration, const float64 &frequency)
 This method draws a static sine wave. More...
 
Buffer drawSine (const float64 &duration, const Buffer &frequency)
 This method draws a dynamic sine wave. More...
 
float64 drawSine (const float64 &frequency)
 This method draws one sample of a sine wave in real-time. More...
 
Buffer drawSine2 (const float64 &duration, const float64 &frequency, const float64 &phase)
 This method draws a static sine wave. More...
 
Buffer drawSine2 (const float64 &duration, const Buffer &frequency, const Buffer &phase)
 This method draws a dynamic sine wave. More...
 
Buffer drawSine2 (const float64 &duration, const Buffer &frequency, const float64 &phase=0.0)
 This method draws a dynamic sine wave. More...
 
Buffer drawSine2 (const float64 &duration, const float64 &frequency, const Buffer &phase)
 This method draws a dynamic sine wave. More...
 
float64 drawSine2 (const float64 &frequency, const float64 &phase)
 This method draws one sample of a sine wave in real-time. More...
 
Buffer drawWindow (const float64 &duration, WindowType type) const
 Draws a window of the specified type. More...
 
Buffer drawWindowBartlett (const float64 &duration) const
 Draws a Bartlett window. More...
 
Buffer drawWindowBlackman (const float64 &duration) const
 Draws a Blackman window. More...
 
Buffer drawWindowBlackmanHarris (const float64 &duration) const
 Draws a Blackman-Harris window. More...
 
Buffer drawWindowHamming (const float64 &duration) const
 Draws a Hamming window. More...
 
Buffer drawWindowHanning (const float64 &duration) const
 Draws a Hanning window. More...
 
Buffer drawWindowKaiser (const float64 &duration, const float64 &beta=5.0) const
 Draws a Kaiser window. More...
 
Buffer drawWindowNuttall (const float64 &duration) const
 Draws a Nuttall window. More...
 
Buffer drawWindowParzen (const float64 &duration) const
 Draws a Parzen window. More...
 
Buffer drawWindowRectangular (const float64 &duration) const
 Draws a rectangular window. More...
 
virtual float64 generate (const float64 &frequency)
 This is a real-time method for the wavetable oscillator. More...
 
virtual Buffer generate (const float64 &duration, const float64 &frequency)
 This method oscillates the waveform stored in this generator. More...
 
virtual Buffer generate (const float64 &duration, const Buffer &frequencies)
 This method oscillates the waveform stored in this generator. More...
 
virtual float64 generate2 (const float64 &frequency, const float64 &phase)
 This is a real-time method for the wavetable oscillator. More...
 
virtual Buffer generate2 (const float64 &duration, const float64 &frequency, const float64 &phase)
 This method oscillates the waveform stored in this generator. More...
 
virtual Buffer generate2 (const float64 &duration, const float64 &frequencies, const Buffer &phase)
 This method oscillates the waveform stored in this generator. More...
 
virtual Buffer generate2 (const float64 &duration, const Buffer &frequencies, const float64 &phase)
 This method oscillates the waveform stored in this generator. More...
 
virtual Buffer generate2 (const float64 &duration, const Buffer &frequencies, const Buffer &phase)
 This method oscillates the waveform stored in this generator. More...
 
void removeSlaveSync (Generator &slave)
 Removes the generator from the sync list. More...
 
virtual void reset ()
 Resets the position pointer back to the begging of the waveform. More...
 
RandomNumberGeneratorgetRandomNumberGenerator ()
 
void setSeed (const uint32 seed)
 Sets the seed for the Generator's random number generator (rng). More...
 
Buffer silence (const float64 &duration) const
 This method generates silence. More...
 
float64 tell () const
 Returns the current position pointer, values are between 0.0 to 1.0. More...
 
Buffer whiteNoise (const float64 &duration) const
 This method generates noise from a uniform distribution. More...
 
Buffer gaussianNoise (const float64 &duration, const float64 &mu, const float64 &sigma) const
 This method generates noise from a Gaussian distribution. More...
 
Buffer tanh (const float64 &duration) const
 This method draws the tanh function accross duration samples. More...
 

Protected Member Functions

virtual void ctor (const float64 &sample_rate)
 DOXME. More...
 
virtual void ctor (const float64 &sample_rate, const Buffer &wavetable)
 DOXME. More...
 

Protected Attributes

bool is_realtime_
 
float64 last_frequency_
 
float64 position_
 Used for phase offset adjustment. More...
 
float64 sync_pos_
 The number of samples into the wavefrom. More...
 
float64 sample_rate_
 Used to determine when to create a sync sample. More...
 
float64 sample_time_
 The number of samples per second to generate. More...
 
float64 t_
 The time step between samples in seconds. More...
 
Bufferwaveform_
 The current time (for real time draw functions.) More...
 
RandomNumberGeneratorrng_
 The waveform to ossicialate. More...
 
uint32 buzz_max_harmonics_
 The random number generator. More...
 
std::vector< float64buzz_position_
 
boolean chorus_is_on_
 
uint32 chorus_n_voices_
 
std::vector< float64chorus_position_
 
std::vector< float64chorus_factor_
 
boolean sync_is_master_
 
boolean sync_is_slave_
 Indicates if this generator is the master. More...
 
uint32 sync_count_
 Indicates if this generator is a slave. More...
 
std::vector< uint32sync_vector_
 Indicates the number of samples since reset. More...
 
std::set< Generator * > sync_slaves_
 Stores sample counts when synced. More...
 

Detailed Description

DOXME.

Definition at line 44 of file Cosine.h.

Constructor & Destructor Documentation

Cosine::Cosine ( const float64 sample_rate)

DOXME.

Definition at line 38 of file Cosine.cc.

References Nsound::Generator::ctor(), and Nsound::Generator::drawSine2().

39  : Generator(sample_rate)
40 {
41  Buffer waveform = drawSine2(1.0, 1.0, 0.5);
42  ctor(sample_rate, waveform);
43 }
Buffer drawSine2(const float64 &duration, const float64 &frequency, const float64 &phase)
This method draws a static sine wave.
Definition: Generator.cc:591
virtual void ctor(const float64 &sample_rate)
DOXME.
Definition: Generator.cc:201
Generator()
DOXME.
Definition: Generator.cc:52
A Buffer for storing audio samples.
Definition: Buffer.h:60

Member Function Documentation

void Nsound::Generator::setRealtime ( bool  flag)
inlineinherited

Sets realtime mode, disables automatic reset() if set.

Definition at line 72 of file Generator.h.

References Nsound::Generator::is_realtime_.

72 { is_realtime_ = flag; }
void Generator::addSlaveSync ( Generator slave)
inherited

Adds a generator as a slave to this instance for syncing.

Definition at line 237 of file Generator.cc.

References Nsound::Generator::reset(), Nsound::Generator::sync_is_master_, Nsound::Generator::sync_is_slave_, and Nsound::Generator::sync_slaves_.

Referenced by main().

238 {
239  sync_is_master_ = true;
240  slave.sync_is_slave_ = true;
241 
242  if(sync_slaves_.count(&slave) == 0)
243  {
244  sync_slaves_.insert(&slave);
245  }
246 
247  reset();
248 }
std::set< Generator * > sync_slaves_
Stores sample counts when synced.
Definition: Generator.h:638
boolean sync_is_slave_
Indicates if this generator is the master.
Definition: Generator.h:635
boolean sync_is_master_
Definition: Generator.h:634
virtual void reset()
Resets the position pointer back to the begging of the waveform.
Definition: Generator.cc:1272
void Generator::buzzInit ( const uint32 max_harmonics)
inherited

Initaializes Buzz settings for real-time calls.

Definition at line 252 of file Generator.cc.

References Nsound::Generator::buzz_max_harmonics_, and Nsound::Generator::reset().

Referenced by Nsound::Generator::buzz().

253 {
254  buzz_max_harmonics_ = max_harmonics;
255  reset();
256 }
uint32 buzz_max_harmonics_
The random number generator.
Definition: Generator.h:624
virtual void reset()
Resets the position pointer back to the begging of the waveform.
Definition: Generator.cc:1272
float64 Generator::buzz ( const float64 frequency,
const float64 n_harmonics,
const float64 delay 
)
inherited

Returns sample from a set of harmonics. Based on the Csound buzz opcode.

Chorus can not be used with this function.

Definition at line 260 of file Generator.cc.

References Nsound::Generator::last_frequency_, Nsound::Generator::position_, and Nsound::Generator::sample_rate_.

Referenced by Nsound::Generator::buzz(), FFTransform_UnitTest(), and Generator_UnitTest().

264 {
265  // This function is based on the csound buzz opcode.
266 
267  float64 n = static_cast<int32>(n_harmonics);
268 
269  n = fabs(n);
270  if(n < 1.0) n = 1.0;
271 
272  float64 two_n_plus_1 = 2.0 * n + 1.0;
273  float64 scale = static_cast<float64>(0.5) / n;
274 
275  float64 sign = 1.0;
276  for(int i = 0; i < n; ++i) sign = -sign;
277 
278  float64 phase = fabs(phase_offset / static_cast<float64>(2.0));
279 
280  while(phase >= 0.5) phase -= 0.5;
281 
282  position_ += (phase - last_frequency_) * sample_rate_;
283 
284  last_frequency_ = phase;
285 
286  float64 y;
287 
289 
290  float64 denom = (*waveform_)[static_cast<int32>(position_ + 0.5)] * sign;
291 
292  if(fabs(denom) > 1e-12)
293  {
294  float64 up_phase = position_ * two_n_plus_1;
295  while(up_phase >= sample_rate_) up_phase -= sample_rate_;
296 
297  float64 num = (*waveform_)[static_cast<int32>(up_phase)];
298 
299  y = (num / denom - static_cast<float64>(1.0)) * scale;
300  }
301  else
302  {
303  y = 1.0; // this assumes cosine wave!
304  }
305 
306  position_ += 0.5*frequency;
307 
308  return y;
309 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
float64 position_
Used for phase offset adjustment.
Definition: Generator.h:614
double float64
Definition: Nsound.h:146
float64 last_frequency_
Definition: Generator.h:613
signed int int32
Definition: Nsound.h:142
Buffer Generator::buzz ( const float64 duration,
const float64 frequency,
const float64 n_harmonics,
const float64 delay 
)
inherited

Returns a set of harmonics. Based on the Csound buzz opcode.

Definition at line 313 of file Generator.cc.

References Nsound::Generator::buzz(), Nsound::Generator::buzzInit(), and Nsound::Generator::sample_rate_.

318 {
319  buzzInit(static_cast<uint32>(n_harmonics));
320 
321  int32 n_samples = static_cast<int32>(duration * sample_rate_);
322 
323  Buffer y(n_samples);
324 
325  for(int i = 0; i < n_samples; ++i)
326  {
327  y << buzz(frequency, n_harmonics, phase_offset+0.5);
328  }
329 
330  return y;
331 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
void buzzInit(const uint32 &max_harmonics)
Initaializes Buzz settings for real-time calls.
Definition: Generator.cc:252
float64 buzz(const float64 &frequency, const float64 &n_harmonics, const float64 &delay)
Returns sample from a set of harmonics. Based on the Csound buzz opcode.
Definition: Generator.cc:260
A Buffer for storing audio samples.
Definition: Buffer.h:60
signed int int32
Definition: Nsound.h:142
Buffer Generator::buzz ( const float64 duration,
const Buffer frequency,
const Buffer n_harmonics,
const Buffer delay 
)
inherited

Returns a set of harmonics. Based on the Csound buzz opcode.

Definition at line 335 of file Generator.cc.

References Nsound::Generator::buzz(), Nsound::Generator::buzzInit(), Nsound::Buffer::cbegin(), Nsound::Buffer::getMax(), and Nsound::Generator::sample_rate_.

340 {
341  buzzInit(static_cast<uint32>(n_harmonics.getMax()));
342 
343  int32 n_samples = static_cast<int32>(duration * sample_rate_);
344 
345  Buffer::const_circular_iterator f = frequencies.cbegin();
346  Buffer::const_circular_iterator n = n_harmonics.cbegin();
347  Buffer::const_circular_iterator p = phase_offset.cbegin();
348 
349  Buffer y(n_samples);
350 
351  for(int32 i = 0; i < n_samples; ++i, ++f, ++n, ++p)
352  {
353  y << buzz(*f, *n, *p+0.5);
354  }
355 
356  return y;
357 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
void buzzInit(const uint32 &max_harmonics)
Initaializes Buzz settings for real-time calls.
Definition: Generator.cc:252
float64 buzz(const float64 &frequency, const float64 &n_harmonics, const float64 &delay)
Returns sample from a set of harmonics. Based on the Csound buzz opcode.
Definition: Generator.cc:260
A Buffer for storing audio samples.
Definition: Buffer.h:60
signed int int32
Definition: Nsound.h:142
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
void Generator::setChorus ( const uint32  n_voices,
const float64 sigma = 0.02 
)
inherited

Chorus or Unison.

When chorus is not enabled, samples are generated using one frequency indexing into the wave table. However, when chorus is turned on, the output sample will be the average of N frequencies indexing into the wavetable. A set of scalar is randomally selected, each frequncy in the set is calculated by

\[ f_n = \left({1.0 + \alpha_n}\right) f \]

where $\alpha_n$ is a random scalar selected inside $\pm\sigma$.

Definition at line 361 of file Generator.cc.

References Nsound::Generator::chorus_factor_, Nsound::Generator::chorus_is_on_, Nsound::Generator::chorus_n_voices_, Nsound::Generator::chorus_position_, Nsound::RandomNumberGenerator::get(), Nsound::Generator::reset(), and Nsound::Generator::rng_.

Referenced by Generator_UnitTest(), and Sine_UnitTest().

362 {
363  if(n_voices == 0)
364  {
365  chorus_is_on_ = false;
366  return;
367  }
368 
369  chorus_is_on_ = true;
370 
371  chorus_n_voices_ = n_voices;
372 
373  chorus_factor_.clear();
374  chorus_position_.clear();
375 
376  for(uint32 i = 0; i < chorus_n_voices_; ++i)
377  {
378  chorus_factor_.push_back(1.0 + rng_->get(-amount, amount));
379 
380  chorus_position_.push_back(0.0);
381  }
382 
383  reset();
384 }
boolean chorus_is_on_
Definition: Generator.h:628
unsigned int uint32
Definition: Nsound.h:153
RandomNumberGenerator * rng_
The waveform to ossicialate.
Definition: Generator.h:621
std::vector< float64 > chorus_position_
Definition: Generator.h:630
virtual uint32 get()=0
Get a random number.
std::vector< float64 > chorus_factor_
Definition: Generator.h:631
virtual void reset()
Resets the position pointer back to the begging of the waveform.
Definition: Generator.cc:1272
uint32 chorus_n_voices_
Definition: Generator.h:629
Buffer Generator::drawDecay ( const float64 duration,
const float64 alpha = 2.0*M_PI 
) const
inherited

This method draws an exponential curve that decays from 1.0 to 0.0 over the duration.

\[ y\left[n\right]=e^{-\alpha n\tau} \]

where the default value for $\alpha$ is $2\pi$ and $\tau$ is the sample time.

Definition at line 388 of file Generator.cc.

References Nsound::Generator::drawLine(), and Nsound::Buffer::exp().

Referenced by Nsound::Granulator::Granulator(), main(), Nsound::DrumBD01::play(), Nsound::DrumKickBass::play(), and Nsound::Hat::play().

389 {
390  if(duration <= 0.0) return Buffer();
391 
392  Buffer t = drawLine(duration, 0.0, 1.0);
393 
394  t *= -alpha;
395 
396  t.exp();
397 
398  return t;
399 }
void exp()
Each sample in the Buffer becomes the power e^x.
Definition: Buffer.cc:510
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
Buffer Generator::drawGaussian ( const float64 duration,
const float64 mu,
const float64 sigma,
const boolean normalize = true 
) const
inherited

This method draws a Gaussian curve over duration seconds.

This method draws a Gaussian curve over duration seconds.

Parameters
durationthe number of seconds to draw
mu$\left(\mu\right)$ the location of the peak in seconds
sigma$\left(\sigma\right)$ the width of the Gaussian in seconds
normalizea flag to normalize the Gaussian so the peak is 1.0
Returns
Buffer

The generatl equation for a discrete Gaussian is given by

\[ y\left[n\right]=\frac{1}{\sqrt{2\pi\sigma^{2}}}e^{-\frac{\left(n\tau-\mu\right)^{2}}{2\sigma^{2}}} \]

where $\tau$ is the sample time.

Definition at line 403 of file Generator.cc.

References Nsound::Generator::drawLine(), Nsound::Buffer::exp(), M_2PI, and Nsound::Buffer::normalize().

Referenced by Nsound::Generator::drawFatGaussian(), Nsound::Generator::drawWindow(), Nsound::Granulator::Granulator(), and main().

408 {
409  if(duration <= 0.0) return Buffer();
410 
411  float64 variance = sigma * sigma;
412 
413  Buffer g = drawLine(duration, 0.0, duration);
414 
415  g -= mu;
416 
417  g *= g;
418 
419  g /= (2.0 * variance);
420 
421  g *= -1.0;
422 
423  g.exp();
424 
425  g /= ::sqrt(M_2PI * variance);
426 
427  if(normalize) g.normalize();
428 
429  return g;
430 }
double float64
Definition: Nsound.h:146
void normalize()
Multiplies the Buffer by a constant gain so the peak sample has magnitude 1.0.
Definition: Buffer.cc:1064
void exp()
Each sample in the Buffer becomes the power e^x.
Definition: Buffer.cc:510
A Buffer for storing audio samples.
Definition: Buffer.h:60
#define M_2PI
Definition: Generator.cc:46
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
Buffer Generator::drawFatGaussian ( const float64 duration,
const float64 pass_band_percent = 0.01 
) const
inherited

This method draws a standard Gaussian curve over duration seconds, with a specified pass band.

Parameters
durationthe number of seconds to draw
pass_band_percentthe width of the pass band in percent of duraiton
sigmathe standard deviation of the gaussian curve
Returns
Buffer

The curve is always normaized so the peak is 1.0.

Definition at line 434 of file Generator.cc.

References Nsound::Generator::drawGaussian(), Nsound::Generator::drawLine(), and M_ASSERT_VALUE.

Referenced by Nsound::Generator::drawWindow(), Nsound::Granulator::Granulator(), main(), and softTones().

437 {
438  M_ASSERT_VALUE(duration, >, 0.0);
439  M_ASSERT_VALUE(pass_band_percent, >, 0.0);
440  M_ASSERT_VALUE(pass_band_percent, <, 1.0);
441 
442  float64 pass_band_time = duration * pass_band_percent;
443 
444  float64 gauss_time = duration - pass_band_time;
445 
446  float64 h_gauss_time = gauss_time / 2.0;
447 
448  // Using a constant sigma ratio so the shape of the fat gaussian doesn't
449  // change given a duration.
450 
451  float64 sigma = 0.275 * h_gauss_time;
452 
453  Buffer y;
454 
455  y << drawGaussian(h_gauss_time, h_gauss_time, sigma)
456  << drawLine(pass_band_time, 1.0, 1.0)
457  << drawGaussian(h_gauss_time, 0.0, sigma);
458 
459  return y;
460 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
Buffer drawGaussian(const float64 &duration, const float64 &mu, const float64 &sigma, const boolean &normalize=true) const
This method draws a Gaussian curve over duration seconds.
Definition: Generator.cc:403
double float64
Definition: Nsound.h:146
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
Buffer Generator::drawLine ( const float64 duration,
const float64 amplitude_start,
const float64 amplitude_finish 
) const
inherited

This method draws a linear line beteween 2 points.

Parameters
durationthe number of seconds to draw
amplitude_startthe starting amplitude value
amplitude_finishthe finishing amplitude value
Returns
Buffer

The two points are located at (x1,y1) = (0.0, amplitude_1) and (x2,y2) = (duration, amplitude_2).

Definition at line 464 of file Generator.cc.

References M_ASSERT_VALUE, and Nsound::Generator::sample_rate_.

Referenced by Buffer_UnitTest(), DelayLine_UnitTest(), Nsound::Generator::drawDecay(), Nsound::Generator::drawFatGaussian(), Nsound::Generator::drawGaussian(), Nsound::Generator::drawParabola(), drawSquare(), drawTriangle(), Nsound::Generator::drawWindowBartlett(), Nsound::Generator::drawWindowBlackman(), Nsound::Generator::drawWindowBlackmanHarris(), Nsound::Generator::drawWindowHamming(), Nsound::Generator::drawWindowHanning(), Nsound::Generator::drawWindowKaiser(), Nsound::Generator::drawWindowNuttall(), Nsound::Generator::drawWindowParzen(), Nsound::Generator::drawWindowRectangular(), drum(), FilterCombLowPassFeedback_UnitTest(), FilterParametricEqualizer_UnitTest(), Nsound::Pluck::generate(), Generator_UnitTest(), Nsound::FFTChunk::getFrequencyAxis(), main(), Nsound::FilterLeastSquaresFIR::makeKernel(), my_main(), Nsound::AudioStream::ones(), Nsound::Buffer::ones(), Nsound::Stretcher::overlapAdd(), Nsound::GuitarBass::play(), Nsound::Clarinet::play(), Nsound::DrumBD01::play(), Nsound::FluteSlide::play(), Nsound::DrumKickBass::play(), Nsound::OrganPipe::play(), Nsound::Hat::play(), Nsound::AudioStream::plot(), Nsound::Generator::silence(), Sine_UnitTest(), Nsound::Square::Square(), Nsound::Mesh2D::strike(), testAutioStream(), Nsound::Triangle::Triangle(), Triangle_UnitTest(), Nsound::AudioStream::zeros(), and Nsound::Buffer::zeros().

468 {
469  M_ASSERT_VALUE(duration, >, 0.0);
470 
471  Buffer buffer;
472 
473  float64 n_samples = duration * sample_rate_;
474 
475  float64 slope = (y2 - y1) / n_samples;
476 
477  float64 current_sample = y1;
478 
479  for(uint32 i = 0; i < static_cast<uint32>(n_samples+0.5); ++i)
480  {
481  buffer << current_sample;
482  current_sample += slope;
483  }
484 
485  return buffer;
486 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
double float64
Definition: Nsound.h:146
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer Generator::drawParabola ( const float64 duration,
const float64 y1,
const float64 x2,
const float64 y2,
const float64 y3 
) const
inherited

This method draws a parabola between three points, intersecting the middle point.

Parameters
durationthe number of seconds to draw
y1the y1 amplitude
x2the x2 time
y2the y2 amplitude
y3the y3 amplitude
Returns
Buffer

The general equation for a discrete parabola is:

\[ y\left[n\right]=a\left(n\tau\right)^{2}+b\left(n\tau\right)+c \]

where $\tau$ is the sample time. The three points are located at (x1,y1), (x2,y2), (x3,y3) with x1 = 0.0 and x3 = duration.

Definition at line 490 of file Generator.cc.

References Nsound::Generator::drawLine(), and M_ASSERT_VALUE.

Referenced by drum().

496 {
497  M_ASSERT_VALUE(duration, >, 0.0);
498 
499  // In this discustion, amp = amplitude.
500  //
501  // The general equation for a parabola is:
502  //
503  // y = A*x^2 + B*x + C
504  //
505  // This alogorithm uses Gaussian elimination to solve for A,B,C.
506  //
507  // y1 = A * x1^2 + B * x1 + C (1)
508  //
509  // y2 = A * x2^2 + B * x2 + C (2)
510  //
511  // y3 = A * x3^2 + B * x3 + C (3)
512  //
513  // Solve for C using (1), with x1 = 0.0:
514  //
515  // C = y1
516  //
517  // Solve for A using (2) and substituting C:
518  //
519  // A = (y2 - y1 - B * x2) / (x2^2)
520  //
521  // Solve for B using (3) and substituting A,C, I used WolframAlpha, awesome!
522  //
523  // solve y_3 = (y_2 - y_1 - B*x_2) / (x_2 * x_2) * (x_3 * x_3) + B*(x_3) + y_1 for B
524  //
525  // B = [x2 * x2 * ( y3 - y1 ) + x3 * x3 * (y1 - y2)] / [x2 * x3 * (x2 - x3)]
526  //
527  M_ASSERT_VALUE(x2, <, duration);
528 
529  float64 x3 = duration;
530 
531  float64 C = y1;
532 
533  float64 B = ( x2*x2*(y3 - y1) + x3*x3*(y1 - y2) ) / ( x2*x3*(x2 - x3) );
534 
535  float64 A = (y2 - y1 - B*x2) / (x2*x2);
536 
537  Buffer t = drawLine(duration, 0.0, duration);
538 
539  return A*t*t + B*t + C;
540 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
double float64
Definition: Nsound.h:146
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
Buffer Generator::drawSine ( const float64 duration,
const float64 frequency 
)
inherited

This method draws a static sine wave.

Parameters
durationthe number of seconds to draw
frequency$\left(f\right)$ the frequency in Hz
Returns
Buffer

This is a special case of drawSine2(const float64 & duration, const float64 & frequency, const float64 & phase) with phase set to 0.0.

Definition at line 544 of file Generator.cc.

References Nsound::Generator::drawSine2(), M_ASSERT_VALUE, Nsound::Generator::sample_rate_, and Nsound::Generator::t_.

Referenced by Generator_UnitTest(), Nsound::Sawtooth::Sawtooth(), Nsound::Sine::Sine(), and Nsound::Square::Square().

547 {
548  M_ASSERT_VALUE(duration, >, 0.0);
549 
550  t_ = 0.0;
551 
552  Buffer y;
553 
554  uint64 n_samples = static_cast<uint64>(duration * sample_rate_ + 0.5);
555 
556  for(uint64 i = 0; i < n_samples; ++i)
557  {
558  y << drawSine2(frequency, 0.0);
559  }
560 
561  return y;
562 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
Buffer drawSine2(const float64 &duration, const float64 &frequency, const float64 &phase)
This method draws a static sine wave.
Definition: Generator.cc:591
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
float64 t_
The time step between samples in seconds.
Definition: Generator.h:618
unsigned long long uint64
Definition: Nsound.h:154
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer Generator::drawSine ( const float64 duration,
const Buffer frequency 
)
inherited

This method draws a dynamic sine wave.

Parameters
durationthe number of seconds to draw
frequency$\left(f\right)$ the frequency in Hz
Returns
Buffer

This is a special case of drawSine2(const float64 & duration, const Buffer & frequency, const Buffer & phase) with the phase set to 0.0.

Definition at line 566 of file Generator.cc.

References Nsound::Buffer::cbegin(), Nsound::Generator::drawSine2(), M_ASSERT_VALUE, Nsound::Generator::sample_rate_, and Nsound::Generator::t_.

569 {
570  M_ASSERT_VALUE(duration, >, 0.0);
571 
572  t_ = 0.0;
573 
574  Buffer y;
575 
576  uint64 n_samples = static_cast<uint64>(duration * sample_rate_ + 0.5);
577 
578  Buffer::const_circular_iterator f = frequency.cbegin();
579 
580  for(uint64 i = 0; i < n_samples; ++i)
581  {
582  y << drawSine2(*f, 0.0);
583  ++f;
584  }
585 
586  return y;
587 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
Buffer drawSine2(const float64 &duration, const float64 &frequency, const float64 &phase)
This method draws a static sine wave.
Definition: Generator.cc:591
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
float64 t_
The time step between samples in seconds.
Definition: Generator.h:618
unsigned long long uint64
Definition: Nsound.h:154
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 Generator::drawSine ( const float64 frequency)
inherited

This method draws one sample of a sine wave in real-time.

Parameters
frequency$\left(f\right)$ the frequency in Hz
Returns
float64

This is a special case of drawSine2(const float64 & frequency, const float64 & phase) with the phase = 0.0.

Definition at line 694 of file Generator.cc.

References Nsound::Generator::drawSine2().

695 {
696  return drawSine2(frequency, 0.0);
697 }
Buffer drawSine2(const float64 &duration, const float64 &frequency, const float64 &phase)
This method draws a static sine wave.
Definition: Generator.cc:591
Buffer Generator::drawSine2 ( const float64 duration,
const float64 frequency,
const float64 phase 
)
inherited

This method draws a static sine wave.

Parameters
durationthe number of seconds to draw
frequency$\left(f\right)$ the frequency in Hz
phase$\left(\varphi\right)$ the natural phase (0.0 to 1.0)
Returns
Buffer

The general discrete sine wave is given by

\[ y\left[n\right]=\sin\left(2\pi fn\tau+\pi\varphi\right) \]

where $\tau$ is the sample time. This function will produce a cosine wave if $\varphi$ is 0.5.

Definition at line 591 of file Generator.cc.

References M_ASSERT_VALUE, Nsound::Generator::sample_rate_, and Nsound::Generator::t_.

Referenced by Cosine(), Nsound::Generator::drawSine(), Nsound::Generator::drawSine2(), and Generator_UnitTest().

595 {
596  M_ASSERT_VALUE(duration, >, 0.0);
597 
598  t_ = 0.0;
599 
600  Buffer y;
601 
602  uint64 n_samples = static_cast<uint64>(duration * sample_rate_ + 0.5);
603 
604  for(uint64 i = 0; i < n_samples; ++i)
605  {
606  y << drawSine2(frequency, phase);
607  }
608 
609  return y;
610 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
Buffer drawSine2(const float64 &duration, const float64 &frequency, const float64 &phase)
This method draws a static sine wave.
Definition: Generator.cc:591
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
float64 t_
The time step between samples in seconds.
Definition: Generator.h:618
unsigned long long uint64
Definition: Nsound.h:154
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer Generator::drawSine2 ( const float64 duration,
const Buffer frequency,
const Buffer phase 
)
inherited

This method draws a dynamic sine wave.

Parameters
durationthe number of seconds to draw
frequency$\left(f\right)$ the frequency in Hz
phase$\left(\varphi\right)$ the natural phase (0.0 to 1.0)
Returns
Buffer

The general dynamic discrete sine wave is given by

\[ y\left[n\right]=\sin\left(2\pi s\left[n\right]\tau+\pi\varphi_{n}\right) \]

where $s\left[n\right]$ is given by

\[ s\left[n\right]=\sum_{i=0}^{n}f_{n} \]

and $s\left[n\right]=0$ when $n=0$ and $\tau$ is the sample time.

This function will produce a cosine wave if $\varphi_n$ is 0.5.

Definition at line 614 of file Generator.cc.

References Nsound::Buffer::cbegin(), Nsound::Generator::drawSine2(), M_ASSERT_VALUE, Nsound::Generator::sample_rate_, and Nsound::Generator::t_.

618 {
619  M_ASSERT_VALUE(duration, >, 0.0);
620 
621  t_ = 0.0;
622 
623  Buffer y;
624 
625  uint64 n_samples = static_cast<uint64>(duration * sample_rate_ + 0.5);
626 
627  Buffer::const_circular_iterator f = frequency.cbegin();
629 
630  for(uint64 i = 0; i < n_samples; ++i)
631  {
632  y << drawSine2(*f, *p);
633  ++f;
634  ++p;
635  }
636 
637  return y;
638 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
Buffer drawSine2(const float64 &duration, const float64 &frequency, const float64 &phase)
This method draws a static sine wave.
Definition: Generator.cc:591
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
float64 t_
The time step between samples in seconds.
Definition: Generator.h:618
unsigned long long uint64
Definition: Nsound.h:154
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer Generator::drawSine2 ( const float64 duration,
const Buffer frequency,
const float64 phase = 0.0 
)
inherited

This method draws a dynamic sine wave.

Parameters
durationthe number of seconds to draw
frequency$\left(f\right)$ the frequency in Hz
phase$\left(\varphi\right)$ the natural phase (0.0 to 1.0)
Returns
Buffer

This is a special case of drawSine2(const float64 & duration, const Buffer & frequency, const Buffer & phase) with the phase set to a constant value.

Definition at line 642 of file Generator.cc.

References Nsound::Generator::drawSine2().

646 {
647  Buffer p(1);
648  p << phase;
649 
650  return drawSine2(duration, frequency, p);
651 }
Buffer drawSine2(const float64 &duration, const float64 &frequency, const float64 &phase)
This method draws a static sine wave.
Definition: Generator.cc:591
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer Generator::drawSine2 ( const float64 duration,
const float64 frequency,
const Buffer phase 
)
inherited

This method draws a dynamic sine wave.

Parameters
durationthe number of seconds to draw
frequency$\left(f\right)$ the frequency in Hz
phase$\left(\varphi\right)$ the natural phase (0.0 to 1.0)
Returns
Buffer

This is a special case of drawSine2(const float64 & duration, const Buffer & frequency, const Buffer & phase) with the frequency set to a constant value.

Definition at line 655 of file Generator.cc.

References Nsound::Generator::drawSine2().

659 {
660  Buffer f(1);
661  f << frequency;
662 
663  return drawSine2(duration, f, phase);
664 }
Buffer drawSine2(const float64 &duration, const float64 &frequency, const float64 &phase)
This method draws a static sine wave.
Definition: Generator.cc:591
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 Generator::drawSine2 ( const float64 frequency,
const float64 phase 
)
inherited

This method draws one sample of a sine wave in real-time.

Parameters
frequency$\left(f\right)$ the frequency in Hz
phase$\left(\varphi\right)$ the natural phase (0.0 to 1.0)
Returns
float64

Definition at line 668 of file Generator.cc.

References Nsound::Generator::chorus_factor_, Nsound::Generator::chorus_is_on_, Nsound::Generator::chorus_n_voices_, Nsound::Generator::chorus_position_, M_2PI, M_PI, Nsound::Generator::sample_time_, and Nsound::Generator::t_.

669 {
670  float64 f = 0.0;
671  float64 sample = 0.0;
672 
673  if(chorus_is_on_)
674  {
675  for(uint32 i = 0; i < chorus_n_voices_; ++i)
676  {
677  f = M_2PI * chorus_position_[i] * sample_time_ + M_PI * phase;
678  sample += ::sin(f);
679  chorus_position_[i] += frequency * chorus_factor_[i];
680  }
681  sample /= static_cast<float64>(chorus_n_voices_);
682  }
683  else
684  {
685  sample = ::sin(M_2PI * t_ * sample_time_ + M_PI * phase);
686  t_ += frequency;
687  }
688 
689  return sample;
690 }
boolean chorus_is_on_
Definition: Generator.h:628
unsigned int uint32
Definition: Nsound.h:153
std::vector< float64 > chorus_position_
Definition: Generator.h:630
#define M_PI
Definition: Nsound.h:121
double float64
Definition: Nsound.h:146
float64 t_
The time step between samples in seconds.
Definition: Generator.h:618
std::vector< float64 > chorus_factor_
Definition: Generator.h:631
float64 sample_time_
The number of samples per second to generate.
Definition: Generator.h:617
#define M_2PI
Definition: Generator.cc:46
uint32 chorus_n_voices_
Definition: Generator.h:629
Buffer Generator::drawWindow ( const float64 duration,
WindowType  type 
) const
inherited

Draws a window of the specified type.

Definition at line 702 of file Generator.cc.

References Nsound::BARTLETT, Nsound::BLACKMAN, Nsound::BLACKMAN_HARRIS, Nsound::Generator::drawFatGaussian(), Nsound::Generator::drawGaussian(), Nsound::Generator::drawWindowBartlett(), Nsound::Generator::drawWindowBlackman(), Nsound::Generator::drawWindowBlackmanHarris(), Nsound::Generator::drawWindowHamming(), Nsound::Generator::drawWindowHanning(), Nsound::Generator::drawWindowKaiser(), Nsound::Generator::drawWindowNuttall(), Nsound::Generator::drawWindowParzen(), Nsound::Generator::drawWindowRectangular(), Nsound::GAUSSIAN, Nsound::GAUSSIAN_05, Nsound::GAUSSIAN_10, Nsound::GAUSSIAN_15, Nsound::GAUSSIAN_20, Nsound::GAUSSIAN_25, Nsound::GAUSSIAN_30, Nsound::GAUSSIAN_33, Nsound::GAUSSIAN_35, Nsound::GAUSSIAN_40, Nsound::GAUSSIAN_45, Nsound::GAUSSIAN_50, Nsound::GAUSSIAN_55, Nsound::GAUSSIAN_60, Nsound::GAUSSIAN_65, Nsound::GAUSSIAN_66, Nsound::GAUSSIAN_70, Nsound::GAUSSIAN_75, Nsound::GAUSSIAN_80, Nsound::GAUSSIAN_85, Nsound::GAUSSIAN_90, Nsound::GAUSSIAN_95, Nsound::GAUSSIAN_99, Nsound::HAMMING, Nsound::HANNING, Nsound::KAISER, M_THROW, Nsound::NUTTALL, Nsound::PARZEN, and Nsound::RECTANGULAR.

Referenced by Nsound::FFTransform::fft(), Nsound::FilterLeastSquaresFIR::setWindow(), and Nsound::Spectrogram::Spectrogram().

703 {
704  switch(type)
705  {
706  case BARTLETT: return drawWindowBartlett(duration);
707  case BLACKMAN: return drawWindowBlackman(duration);
708  case BLACKMAN_HARRIS: return drawWindowBlackmanHarris(duration);
709  case GAUSSIAN: return drawGaussian(duration, duration/2.0, 1.0);
710  case GAUSSIAN_05: return drawFatGaussian(duration, 0.05);
711  case GAUSSIAN_10: return drawFatGaussian(duration, 0.10);
712  case GAUSSIAN_15: return drawFatGaussian(duration, 0.15);
713  case GAUSSIAN_20: return drawFatGaussian(duration, 0.20);
714  case GAUSSIAN_25: return drawFatGaussian(duration, 0.25);
715  case GAUSSIAN_30: return drawFatGaussian(duration, 0.30);
716  case GAUSSIAN_33: return drawFatGaussian(duration, 0.3333);
717  case GAUSSIAN_35: return drawFatGaussian(duration, 0.35);
718  case GAUSSIAN_40: return drawFatGaussian(duration, 0.40);
719  case GAUSSIAN_45: return drawFatGaussian(duration, 0.45);
720  case GAUSSIAN_50: return drawFatGaussian(duration, 0.50);
721  case GAUSSIAN_55: return drawFatGaussian(duration, 0.55);
722  case GAUSSIAN_60: return drawFatGaussian(duration, 0.60);
723  case GAUSSIAN_65: return drawFatGaussian(duration, 0.65);
724  case GAUSSIAN_66: return drawFatGaussian(duration, 0.6666);
725  case GAUSSIAN_70: return drawFatGaussian(duration, 0.70);
726  case GAUSSIAN_75: return drawFatGaussian(duration, 0.75);
727  case GAUSSIAN_80: return drawFatGaussian(duration, 0.80);
728  case GAUSSIAN_85: return drawFatGaussian(duration, 0.85);
729  case GAUSSIAN_90: return drawFatGaussian(duration, 0.90);
730  case GAUSSIAN_95: return drawFatGaussian(duration, 0.95);
731  case GAUSSIAN_99: return drawFatGaussian(duration, 0.9999);
732  case HAMMING: return drawWindowHamming(duration);
733  case HANNING: return drawWindowHanning(duration);
734  case KAISER: return drawWindowKaiser(duration);
735  case NUTTALL: return drawWindowNuttall(duration);
736  case PARZEN: return drawWindowParzen(duration);
737  case RECTANGULAR: return drawWindowRectangular(duration);
738  default: M_THROW("Unkown window type " << type);
739  }
740 
741  return drawWindowRectangular(duration);
742 }
Buffer drawWindowKaiser(const float64 &duration, const float64 &beta=5.0) const
Draws a Kaiser window.
Definition: Generator.cc:879
Buffer drawWindowRectangular(const float64 &duration) const
Draws a rectangular window.
Definition: Generator.cc:965
Buffer drawFatGaussian(const float64 &duration, const float64 &pass_band_percent=0.01) const
This method draws a standard Gaussian curve over duration seconds, with a specified pass band...
Definition: Generator.cc:434
Buffer drawWindowBlackman(const float64 &duration) const
Draws a Blackman window.
Definition: Generator.cc:785
Buffer drawWindowHamming(const float64 &duration) const
Draws a Hamming window.
Definition: Generator.cc:808
Buffer drawGaussian(const float64 &duration, const float64 &mu, const float64 &sigma, const boolean &normalize=true) const
This method draws a Gaussian curve over duration seconds.
Definition: Generator.cc:403
Buffer drawWindowBartlett(const float64 &duration) const
Draws a Bartlett window.
Definition: Generator.cc:765
Buffer drawWindowHanning(const float64 &duration) const
Draws a Hanning window.
Definition: Generator.cc:819
Buffer drawWindowParzen(const float64 &duration) const
Draws a Parzen window.
Definition: Generator.cc:932
#define M_THROW(message)
Definition: Macros.h:108
Buffer drawWindowBlackmanHarris(const float64 &duration) const
Draws a Blackman-Harris window.
Definition: Generator.cc:796
Buffer drawWindowNuttall(const float64 &duration) const
Draws a Nuttall window.
Definition: Generator.cc:921
Buffer Generator::drawWindowBartlett ( const float64 duration) const
inherited

Draws a Bartlett window.

Definition at line 765 of file Generator.cc.

References Nsound::Generator::drawLine(), and Nsound::Generator::sample_rate_.

Referenced by Nsound::Generator::drawWindow().

766 {
767  Buffer window = drawLine(duration, 1.0, 1.0);
768 
769  int32 n = static_cast<int32>(duration * sample_rate_);
770 
771  float64 n_over_2 = static_cast<float64>(n / 2);
772 
773  for(int32 i = 0; i < n / 2; ++i)
774  {
775  float64 t = static_cast<float64>(i) / n_over_2;
776  window[i] *= t;
777  window[i + n/2] *= 1.0 - t;
778  }
779 
780  return window;
781 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
double float64
Definition: Nsound.h:146
A Buffer for storing audio samples.
Definition: Buffer.h:60
signed int int32
Definition: Nsound.h:142
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
Buffer Generator::drawWindowBlackman ( const float64 duration) const
inherited

Draws a Blackman window.

The general discrete cosine window is given by

\[ w\left[n\right]=a_{0}-a_{1}\cos\left(2\pi n\tau\right)+a_{2}\cos\left(4\pi n\tau\right)-a_{3}\cos\left(6\pi n\tau\right) \]

where $\tau$ is the sample time. The Blackman window is a special case of the cosesine window with $a_0$ = 0.42, $a_1$ = 0.50, $a_2$ = 0.08, $a_3$ = 0.00.

Definition at line 785 of file Generator.cc.

References cosinewindow(), and Nsound::Generator::drawLine().

Referenced by Nsound::Generator::drawWindow().

786 {
787  Buffer window = drawLine(duration, 1.0, 1.0);
788 
789  cosinewindow(window, 0.42, 0.50, 0.08, 0.00);
790 
791  return window;
792 }
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
static void cosinewindow(Buffer &win, const float64 &a0, const float64 &a1, const float64 &a2, const float64 &a3)
Definition: Generator.cc:746
Buffer Generator::drawWindowBlackmanHarris ( const float64 duration) const
inherited

Draws a Blackman-Harris window.

The general discrete cosine window is given by

\[ w\left[n\right]=a_{0}-a_{1}\cos\left(2\pi n\tau\right)+a_{2}\cos\left(4\pi n\tau\right)-a_{3}\cos\left(6\pi n\tau\right) \]

where $\tau$ is the sample time. The Blackman-Harris window is a special case of the cosesine window with $a_0$ = 0.35875, $a_1$ = 0.48829, $a_2$ = 0.14128, $a_3$ = 0.01168.

Definition at line 796 of file Generator.cc.

References cosinewindow(), and Nsound::Generator::drawLine().

Referenced by Nsound::Generator::drawWindow().

797 {
798  Buffer window = drawLine(duration, 1.0, 1.0);
799 
800  cosinewindow(window, 0.35875, 0.48829, 0.14128, 0.01168);
801 
802  return window;
803 }
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
static void cosinewindow(Buffer &win, const float64 &a0, const float64 &a1, const float64 &a2, const float64 &a3)
Definition: Generator.cc:746
Buffer Generator::drawWindowHamming ( const float64 duration) const
inherited

Draws a Hamming window.

The general discrete cosine window is given by

\[ w\left[n\right]=a_{0}-a_{1}\cos\left(2\pi n\tau\right)+a_{2}\cos\left(4\pi n\tau\right)-a_{3}\cos\left(6\pi n\tau\right) \]

where $\tau$ is the sample time. The Hamming window is a special case of the cosesine window with $a_0$ = 0.54, $a_1$ = 0.46, $a_2$ = 0.00, $a_3$ = 0.00.

Definition at line 808 of file Generator.cc.

References cosinewindow(), and Nsound::Generator::drawLine().

Referenced by Nsound::Generator::drawWindow().

809 {
810  Buffer window = drawLine(duration, 1.0, 1.0);
811 
812  cosinewindow(window, 0.54, 0.46, 0.00, 0.00);
813 
814  return window;
815 }
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
static void cosinewindow(Buffer &win, const float64 &a0, const float64 &a1, const float64 &a2, const float64 &a3)
Definition: Generator.cc:746
Buffer Generator::drawWindowHanning ( const float64 duration) const
inherited

Draws a Hanning window.

The general discrete cosine window is given by

\[ w\left[n\right]=a_{0}-a_{1}\cos\left(2\pi n\tau\right)+a_{2}\cos\left(4\pi n\tau\right)-a_{3}\cos\left(6\pi n\tau\right) \]

where $\tau$ is the sample time. The Hanning window is a special case of the cosesine window with $a_0$ = 0.50, $a_1$ = 0.50, $a_2$ = 0.00, $a_3$ = 0.00.

Definition at line 819 of file Generator.cc.

References cosinewindow(), and Nsound::Generator::drawLine().

Referenced by Nsound::Generator::drawWindow(), my_main(), and Nsound::Stretcher::Stretcher().

820 {
821  Buffer window = drawLine(duration, 1.0, 1.0);
822 
823  cosinewindow(window, 0.50, 0.50, 0.00, 0.00);
824 
825  return window;
826 }
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
static void cosinewindow(Buffer &win, const float64 &a0, const float64 &a1, const float64 &a2, const float64 &a3)
Definition: Generator.cc:746
Buffer Generator::drawWindowKaiser ( const float64 duration,
const float64 beta = 5.0 
) const
inherited

Draws a Kaiser window.

Definition at line 879 of file Generator.cc.

References bessel_i0(), Nsound::Generator::drawLine(), and Nsound::Buffer::getLength().

Referenced by Nsound::Generator::drawWindow(), Nsound::FilterLeastSquaresFIR::FilterLeastSquaresFIR(), and Nsound::FilterLeastSquaresFIR::setKernel().

880 {
881  Buffer window = drawLine(duration, 0.0, 0.0);
882 
883  int32 n_samples = window.getLength();
884  float64 f_n_samples = float64(n_samples);
885 
886  float64 b = ::fabs(beta);
887 
888  if(b < 1.0)
889  {
890  b = 1.0;
891  }
892 
893  // Calculate window coefficients
894  //
895  // alpha = n_samples / 2
896  //
897  // for n = 0 : n_samples -1
898  //
899  // w[n] = bessel_i0( beta * sqrt(1.0 - [(n - alpha)/alpha]^2)
900  // / bessel_i0(beta)
901  //
902  //
903 
904  float64 alpha = f_n_samples / 2.0;
905 
906  float64 denominator = bessel_i0(b);
907 
908  for(int32 i = 0 ; i < n_samples; ++i)
909  {
910  float64 temp = (float64(i) - alpha) / alpha;
911  temp *= temp;
912 
913  window[i] = bessel_i0( b * ::sqrt(1.0 - temp)) / denominator;
914  }
915 
916  return window;
917 }
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
float64 bessel_i0(const float64 &x)
Definition: Generator.cc:837
A Buffer for storing audio samples.
Definition: Buffer.h:60
signed int int32
Definition: Nsound.h:142
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
Buffer Generator::drawWindowNuttall ( const float64 duration) const
inherited

Draws a Nuttall window.

The general discrete cosine window is given by

\[ w\left[n\right]=a_{0}-a_{1}\cos\left(2\pi n\tau\right)+a_{2}\cos\left(4\pi n\tau\right)-a_{3}\cos\left(6\pi n\tau\right) \]

where $\tau$ is the sample time. The Nuttall window is a special case of the cosesine window with $a_0$ = 0.3635819, $a_1$ = 0.4891775, $a_2$ = 0.1365995, $a_3$ = 0.0106411.

Definition at line 921 of file Generator.cc.

References cosinewindow(), and Nsound::Generator::drawLine().

Referenced by Nsound::Generator::drawWindow().

922 {
923  Buffer window = drawLine(duration, 1.0, 1.0);
924 
925  cosinewindow(window, 0.3635819, 0.4891775, 0.1365995, 0.0106411);
926 
927  return window;
928 }
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
static void cosinewindow(Buffer &win, const float64 &a0, const float64 &a1, const float64 &a2, const float64 &a3)
Definition: Generator.cc:746
Buffer Generator::drawWindowParzen ( const float64 duration) const
inherited

Draws a Parzen window.

Definition at line 932 of file Generator.cc.

References Nsound::Generator::drawLine(), and Nsound::Generator::sample_rate_.

Referenced by Nsound::Generator::drawWindow().

933 {
934  Buffer window = drawLine(duration, 1.0, 1.0);
935 
936  int32 n = static_cast<int32>(duration * sample_rate_);
937 
938  int32 N = n -1;
939 
940  float64 Nf = static_cast<float64>(N);
941 
942  for(int32 i = 0; i < N / 4; ++i)
943  {
944  float64 m = 2 * pow(1.0 - (Nf / 2 - i) / (Nf / 2), 3.0);
945  window[i] *= m;
946  window[N - i] *= m;
947  }
948 
949  for(int32 i = N/4; i <= N/2; ++i)
950  {
951  int32 wn = i - N/2;
952  float64 m = 1.0
953  - 6.0 * pow(wn / (Nf/2),2.0)
954  * (1.0 - fabs(static_cast<float64>(wn)) / (Nf/2));
955 
956  window[i] *= m;
957  window[N-i] *= m;
958  }
959 
960  return window;
961 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
double float64
Definition: Nsound.h:146
A Buffer for storing audio samples.
Definition: Buffer.h:60
signed int int32
Definition: Nsound.h:142
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
Buffer Generator::drawWindowRectangular ( const float64 duration) const
inherited

Draws a rectangular window.

Definition at line 965 of file Generator.cc.

References Nsound::Generator::drawLine().

Referenced by Nsound::Generator::drawWindow().

966 {
967  return drawLine(duration, 1.0, 1.0);
968 }
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
float64 Generator::generate ( const float64 frequency)
virtualinherited

This is a real-time method for the wavetable oscillator.

Parameters
frequency$\left(f\right)$ the frequency in Hz
Returns
float64

This is a special case of generate2(const float64 & frequency, const float64 & phase) with phase = 0.0.

Reimplemented in Nsound::Pulse, Nsound::GeneratorDecay, and Nsound::Pluck.

Definition at line 972 of file Generator.cc.

References Nsound::Generator::generate2().

Referenced by Buffer_UnitTest(), BufferResample_UnitTest(), DelayLine_UnitTest(), drum(), Nsound::FilterFlanger::filter(), FilterDelay_UnitTest(), Nsound::FilterFlanger::FilterFlanger(), Nsound::FilterPhaser::FilterPhaser(), Nsound::Granulator::generate(), Nsound::Generator::generate(), Generator_UnitTest(), main(), Nsound::GuitarBass::play(), Nsound::Clarinet::play(), Nsound::DrumBD01::play(), Nsound::DrumKickBass::play(), Nsound::OrganPipe::play(), Nsound::Hat::play(), Sine_UnitTest(), softTones(), testAutioStream(), Triangle_UnitTest(), and Wavefile_UnitTest().

973 {
974  return generate2(frequency, 0.0);
975 }
virtual float64 generate2(const float64 &frequency, const float64 &phase)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:979
Buffer Generator::generate ( const float64 duration,
const float64 frequency 
)
virtualinherited

This method oscillates the waveform stored in this generator.

Parameters
durationthe number of seconds to oscillate the waveform
frequency$\left(f\right)$ the frequency in Hz
Returns
Buffer
Example:
// C++
Generator gen("some_waveform.wav");
Buffer b = gen.generate(1.0, 3.0);
// Python
gen = Generator("some_waveform.wav")
b = gen.generate(1.0, 3.0)

Reimplemented in Nsound::Pluck.

Definition at line 1065 of file Generator.cc.

References Nsound::Generator::generate(), Nsound::Generator::is_realtime_, M_ASSERT_VALUE, Nsound::Generator::reset(), and Nsound::Generator::sample_rate_.

1068 {
1069  M_ASSERT_VALUE(duration, >, 0.0);
1070 
1071  if(!is_realtime_) reset();
1072 
1073  Buffer buffer;
1074 
1075  uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
1076 
1077  for(uint64 i = 0; i < n_samples; ++i)
1078  {
1079  buffer << generate(frequency);
1080  }
1081 
1082  return buffer;
1083 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
unsigned long long uint64
Definition: Nsound.h:154
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
Buffer Generator::generate ( const float64 duration,
const Buffer frequencies 
)
virtualinherited

This method oscillates the waveform stored in this generator.

Parameters
durationthe number of seconds to oscillate the waveform
frequency$\left(f\right)$ the frequency in Hz
Returns
Buffer

This is a special case of generate2(const float64 & duration, const Buffer & frequencies, const Buffer & phase) with phase = 0.0.

Reimplemented in Nsound::Pluck.

Definition at line 1110 of file Generator.cc.

References Nsound::Buffer::cbegin(), Nsound::Generator::generate(), Nsound::Generator::is_realtime_, M_ASSERT_VALUE, Nsound::Generator::reset(), and Nsound::Generator::sample_rate_.

1113 {
1114  M_ASSERT_VALUE(duration, >, 0.0);
1115 
1116  if(!is_realtime_) reset();
1117 
1118  uint32 n_samples = static_cast<uint32>(std::ceil(duration * sample_rate_));
1119 
1120  Buffer y(n_samples);
1121 
1122  Buffer::const_circular_iterator freq = frequencies.cbegin();
1123 
1124  for(uint32 i = 0; i < n_samples; ++i, ++freq)
1125  {
1126  y << generate(*freq);
1127  }
1128 
1129  return y;
1130 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
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
float64 Generator::generate2 ( const float64 frequency,
const float64 phase 
)
virtualinherited

This is a real-time method for the wavetable oscillator.

Parameters
frequency$\left(f\right)$ the frequency in Hz
phase$\left(\varphi\right)$ the natural phase (0.0 to 1.0)
Returns
float64

The general wavetable oscillator equation is given by

\[ y\left[n\right]=wavetable\left[s\left[n\right]\right] \]

where the index into the table, $s\left[n\right]$ is given by the recursive function

\[ s\left[n\right]=\left(s\left[n-1\right]+f+\pi\varphi\right)\div N \]

where $\div$ is the modulus operator, $N$ is the wavetable length, when $n<0$, $s\left[n\right]=0$. In Nsound, the wavetable size, $N$, is equal to the sample rate.

Reimplemented in Nsound::Pulse, Nsound::GeneratorDecay, and Nsound::Pluck.

Definition at line 979 of file Generator.cc.

References Nsound::Generator::chorus_factor_, Nsound::Generator::chorus_is_on_, Nsound::Generator::chorus_n_voices_, Nsound::Generator::chorus_position_, M_CHECK_PTR, Nsound::Generator::position_, Nsound::Generator::sample_rate_, Nsound::Generator::sync_count_, Nsound::Generator::sync_is_master_, Nsound::Generator::sync_is_slave_, Nsound::Generator::sync_pos_, Nsound::Generator::sync_slaves_, Nsound::Generator::sync_vector_, and Nsound::Generator::waveform_.

Referenced by Nsound::Generator::generate(), Nsound::GeneratorDecay::generate2(), Nsound::Pulse::generate2(), Nsound::Generator::generate2(), and Sine_UnitTest().

980 {
982 
983  ++sync_count_;
984 
985  if(sync_is_slave_ && !sync_vector_.empty())
986  {
987  uint32 sync_count = sync_vector_.front();
988 
989  if(sync_count_ == sync_count)
990  {
991  sync_vector_.erase(sync_vector_.begin());
992  position_ = 0;
993  sync_pos_ = 0;
994  }
995  }
996 
997  // Move with phase
998  float64 ph = (phase * sample_rate_ / 2.0);
999  float64 position2 = position_ + ph + 0.5;
1000 
1001  // Range checks
1002 
1003  while(position2 >= sample_rate_)
1004  {
1005  position2 -= sample_rate_;
1006  }
1007 
1008  while(position2 < 0.0)
1009  {
1010  position2 += sample_rate_;
1011  }
1012 
1013  float64 sample = 0.0;
1014 
1015  if(chorus_is_on_)
1016  {
1017  for(uint32 i = 0; i < chorus_n_voices_; ++i)
1018  {
1019  float64 pos = chorus_position_[i]
1020  + chorus_factor_[i] * frequency
1021  + ph
1022  + 0.5;
1023 
1024  // Range check
1025  while(pos >= sample_rate_) pos -= sample_rate_;
1026  while(pos < 0) pos += sample_rate_;
1027 
1028  sample += (*waveform_)[static_cast<uint32>(pos)];
1029 
1030  chorus_position_[i] += frequency * chorus_factor_[i];
1031  }
1032 
1033  sample /= static_cast<float64>(chorus_n_voices_);
1034  }
1035  else
1036  {
1037  sample = (*waveform_)[static_cast<uint32>(position2)];
1038  }
1039 
1040  position_ += frequency;
1041  sync_pos_ += frequency;
1042 
1043  // limit
1044  if(sync_pos_ > sample_rate_)
1045  {
1047 
1048  if(sync_is_master_)
1049  {
1050  std::set<Generator *>::iterator itor = sync_slaves_.begin();
1051 
1052  while(itor != sync_slaves_.end())
1053  {
1054  (*itor)->sync_vector_.push_back(sync_count_);
1055  ++itor;
1056  }
1057  }
1058  }
1059 
1060  return sample;
1061 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
boolean chorus_is_on_
Definition: Generator.h:628
unsigned int uint32
Definition: Nsound.h:153
std::vector< float64 > chorus_position_
Definition: Generator.h:630
#define M_CHECK_PTR(ptr)
Definition: Macros.h:64
std::set< Generator * > sync_slaves_
Stores sample counts when synced.
Definition: Generator.h:638
float64 position_
Used for phase offset adjustment.
Definition: Generator.h:614
double float64
Definition: Nsound.h:146
float64 sync_pos_
The number of samples into the wavefrom.
Definition: Generator.h:615
boolean sync_is_slave_
Indicates if this generator is the master.
Definition: Generator.h:635
boolean sync_is_master_
Definition: Generator.h:634
Buffer * waveform_
The current time (for real time draw functions.)
Definition: Generator.h:619
std::vector< float64 > chorus_factor_
Definition: Generator.h:631
std::vector< uint32 > sync_vector_
Indicates the number of samples since reset.
Definition: Generator.h:637
uint32 sync_count_
Indicates if this generator is a slave.
Definition: Generator.h:636
uint32 chorus_n_voices_
Definition: Generator.h:629
Buffer Generator::generate2 ( const float64 duration,
const float64 frequency,
const float64 phase 
)
virtualinherited

This method oscillates the waveform stored in this generator.

Parameters
durationthe number of seconds to oscillate the waveform
frequency$\left(f\right)$ the frequency in Hz
phase$\left(\varphi\right)$ the natural phase (0.0 to 1.0)
Returns
Buffer
Example:
// C++
Generator gen("some_waveform.wav");
Buffer b = gen.generate2(1.0, 3.0, 0.5); // 90 degrees out of phase
// Python
gen = Generator("some_waveform.wav")
b = gen.generate(1.0, 3.0, 0.5)

Reimplemented in Nsound::Pulse, Nsound::GeneratorDecay, and Nsound::Pluck.

Definition at line 1087 of file Generator.cc.

References Nsound::Generator::generate2(), Nsound::Generator::is_realtime_, M_ASSERT_VALUE, Nsound::Generator::reset(), and Nsound::Generator::sample_rate_.

1091 {
1092  M_ASSERT_VALUE(duration, >, 0.0);
1093 
1094  if(!is_realtime_) reset();
1095 
1096  Buffer buffer;
1097 
1098  uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
1099 
1100  for(uint64 i = 0; i < n_samples; ++i)
1101  {
1102  buffer << generate2(frequency, phase);
1103  }
1104 
1105  return buffer;
1106 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
unsigned long long uint64
Definition: Nsound.h:154
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
virtual float64 generate2(const float64 &frequency, const float64 &phase)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:979
Buffer Generator::generate2 ( const float64 duration,
const float64 frequencies,
const Buffer phase 
)
virtualinherited

This method oscillates the waveform stored in this generator.

Parameters
durationthe number of seconds to oscillate the waveform
frequency$\left(f\right)$ the frequency in Hz
phase$\left(\varphi\right)$ the natural phase (0.0 to 1.0)
Returns
Buffer

This is a special case of generate2(const float64 & duration, const Buffer & frequencies, const Buffer & phase) with constant frequency.

Reimplemented in Nsound::Pulse, Nsound::GeneratorDecay, and Nsound::Pluck.

Definition at line 1134 of file Generator.cc.

References Nsound::Buffer::cbegin(), Nsound::Generator::generate2(), Nsound::Generator::is_realtime_, M_ASSERT_VALUE, Nsound::Generator::reset(), and Nsound::Generator::sample_rate_.

1138 {
1139  M_ASSERT_VALUE(duration, >, 0.0);
1140 
1141  if(!is_realtime_) reset();
1142 
1143  uint32 n_samples = static_cast<uint32>(std::ceil(duration * sample_rate_));
1144 
1145  Buffer y(n_samples);
1146 
1148 
1149  for(uint32 i = 0; i < n_samples; ++i, ++p)
1150  {
1151  y << generate2(frequency,*p);
1152  }
1153 
1154  return y;
1155 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
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
virtual float64 generate2(const float64 &frequency, const float64 &phase)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:979
Buffer Generator::generate2 ( const float64 duration,
const Buffer frequencies,
const float64 phase 
)
virtualinherited

This method oscillates the waveform stored in this generator.

Parameters
durationthe number of seconds to oscillate the waveform
frequency$\left(f\right)$ the frequency in Hz
phase$\left(\varphi\right)$ the natural phase (0.0 to 1.0)
Returns
Buffer

This is a special case of generate2(const float64 & duration, const Buffer & frequencies, const Buffer & phase) with constant phase.

Reimplemented in Nsound::Pulse, Nsound::GeneratorDecay, and Nsound::Pluck.

Definition at line 1159 of file Generator.cc.

References Nsound::Buffer::cbegin(), Nsound::Generator::generate2(), Nsound::Generator::is_realtime_, M_ASSERT_VALUE, Nsound::Generator::reset(), and Nsound::Generator::sample_rate_.

1163 {
1164  M_ASSERT_VALUE(duration, >, 0.0);
1165 
1166  if(!is_realtime_) reset();
1167 
1168  uint32 n_samples = static_cast<uint32>(std::ceil(duration * sample_rate_));
1169 
1170  Buffer y(n_samples);
1171 
1172  Buffer::const_circular_iterator f = frequencies.cbegin();
1173 
1174  for(uint32 i = 0; i < n_samples; ++i, ++f)
1175  {
1176  y << generate2(*f, phase);
1177  }
1178 
1179  return y;
1180 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
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
virtual float64 generate2(const float64 &frequency, const float64 &phase)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:979
Buffer Generator::generate2 ( const float64 duration,
const Buffer frequencies,
const Buffer phase 
)
virtualinherited

This method oscillates the waveform stored in this generator.

Parameters
durationthe number of seconds to oscillate the waveform
frequency$\left(f\right)$ the frequency in Hz
phase$\left(\varphi\right)$ the natural phase (0.0 to 1.0)
Returns
Buffer

Reimplemented in Nsound::GeneratorDecay, Nsound::Pulse, and Nsound::Pluck.

Definition at line 1184 of file Generator.cc.

References Nsound::Buffer::cbegin(), Nsound::Generator::generate2(), Nsound::Generator::is_realtime_, M_ASSERT_VALUE, Nsound::Generator::reset(), and Nsound::Generator::sample_rate_.

1188 {
1189  M_ASSERT_VALUE(duration, >, 0.0);
1190 
1191  if(!is_realtime_) reset();
1192 
1193  uint32 n_samples = static_cast<uint32>(std::ceil(duration * sample_rate_));
1194 
1195  Buffer y(n_samples);
1196 
1197  Buffer::const_circular_iterator f = frequencies.cbegin();
1199 
1200  for(uint32 i = 0; i < n_samples; ++i, ++f, ++p)
1201  {
1202  y << generate2(*f,*p);
1203  }
1204 
1205  return y;
1206 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
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
virtual float64 generate2(const float64 &frequency, const float64 &phase)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:979
void Generator::removeSlaveSync ( Generator slave)
inherited

Removes the generator from the sync list.

Definition at line 1259 of file Generator.cc.

References Nsound::Generator::reset(), Nsound::Generator::sync_is_slave_, and Nsound::Generator::sync_slaves_.

1260 {
1261  slave.sync_is_slave_ = false;
1262 
1263  if(sync_slaves_.count(&slave) == 0) return;
1264 
1265  sync_slaves_.erase(&slave);
1266 
1267  reset();
1268 }
std::set< Generator * > sync_slaves_
Stores sample counts when synced.
Definition: Generator.h:638
boolean sync_is_slave_
Indicates if this generator is the master.
Definition: Generator.h:635
virtual void reset()
Resets the position pointer back to the begging of the waveform.
Definition: Generator.cc:1272
void Generator::reset ( )
virtualinherited

Resets the position pointer back to the begging of the waveform.

Reimplemented in Nsound::Pulse.

Definition at line 1272 of file Generator.cc.

References Nsound::Generator::buzz_max_harmonics_, Nsound::Generator::buzz_position_, Nsound::Generator::chorus_n_voices_, Nsound::Generator::chorus_position_, Nsound::Generator::last_frequency_, Nsound::Generator::position_, Nsound::Generator::sync_count_, Nsound::Generator::sync_pos_, Nsound::Generator::sync_slaves_, and Nsound::Generator::t_.

Referenced by Nsound::Generator::addSlaveSync(), Nsound::Generator::buzzInit(), Nsound::Generator::generate(), Nsound::Generator::generate2(), Nsound::Generator::removeSlaveSync(), Nsound::FilterFlanger::reset(), and Nsound::Generator::setChorus().

1273 {
1274  last_frequency_ = -1.0;
1275  position_ = 0.0;
1276  sync_count_ = 0;
1277  sync_pos_ = 0.0;
1278  t_ = 0.0;
1279 
1281 
1282  for(uint32 i = 0; i < buzz_max_harmonics_; ++i)
1283  {
1284  buzz_position_[i] = 0.0;
1285  }
1286 
1287  for(uint32 i = 0; i < chorus_n_voices_; ++i)
1288  {
1289  chorus_position_[i] = 0.0;
1290  }
1291 
1292  std::set<Generator *>::iterator itor = sync_slaves_.begin();
1293 
1294  while(itor != sync_slaves_.end())
1295  {
1296  (*itor)->sync_vector_.clear();
1297  ++itor;
1298  }
1299 }
unsigned int uint32
Definition: Nsound.h:153
std::vector< float64 > chorus_position_
Definition: Generator.h:630
uint32 buzz_max_harmonics_
The random number generator.
Definition: Generator.h:624
std::vector< float64 > buzz_position_
Definition: Generator.h:625
std::set< Generator * > sync_slaves_
Stores sample counts when synced.
Definition: Generator.h:638
float64 position_
Used for phase offset adjustment.
Definition: Generator.h:614
float64 sync_pos_
The number of samples into the wavefrom.
Definition: Generator.h:615
float64 t_
The time step between samples in seconds.
Definition: Generator.h:618
float64 last_frequency_
Definition: Generator.h:613
uint32 sync_count_
Indicates if this generator is a slave.
Definition: Generator.h:636
uint32 chorus_n_voices_
Definition: Generator.h:629
RandomNumberGenerator& Nsound::Generator::getRandomNumberGenerator ( )
inlineinherited

Definition at line 562 of file Generator.h.

References Nsound::Generator::rng_.

562 {return *rng_; }
RandomNumberGenerator * rng_
The waveform to ossicialate.
Definition: Generator.h:621
void Generator::setSeed ( const uint32  seed)
inherited

Sets the seed for the Generator's random number generator (rng).

Definition at line 1303 of file Generator.cc.

References Nsound::Generator::rng_, and Nsound::RandomNumberGenerator::setSeed().

Referenced by Generator_UnitTest(), and Sine_UnitTest().

1304 {
1305  rng_->setSeed(seed);
1306 }
RandomNumberGenerator * rng_
The waveform to ossicialate.
Definition: Generator.h:621
virtual void setSeed(uint32 seed)=0
Set the seed to use.
Buffer Generator::silence ( const float64 duration) const
inherited

This method generates silence.

This is a special coase of drawLine(const float64 & duration, const float64 & amplitude_start, const float64 & amplitude_finish) const where the start and stopping amplitudes are 0.0.

Definition at line 1310 of file Generator.cc.

References Nsound::Generator::drawLine().

Referenced by Nsound::FFTransform::fft(), FilterDelay_UnitTest(), main(), my_main(), Nsound::Hat::play(), and Nsound::Spectrogram::Spectrogram().

1311 {
1312  return drawLine(duration,0.0,0.0);
1313 }
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
float64 Generator::tell ( ) const
inherited

Returns the current position pointer, values are between 0.0 to 1.0.

Definition at line 1317 of file Generator.cc.

References Nsound::Generator::position_, and Nsound::Generator::sample_rate_.

1318 {
1319  return position_ / sample_rate_;
1320 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
float64 position_
Used for phase offset adjustment.
Definition: Generator.h:614
Buffer Generator::whiteNoise ( const float64 duration) const
inherited

This method generates noise from a uniform distribution.

Definition at line 1325 of file Generator.cc.

References Nsound::RandomNumberGenerator::get(), M_ASSERT_VALUE, Nsound::Generator::rng_, and Nsound::Generator::sample_rate_.

Referenced by Nsound::Pluck::generate(), Nsound::Granulator::Granulator(), main(), Nsound::FluteSlide::play(), Nsound::DrumBD01::play(), Nsound::AudioStream::rand(), and Nsound::Buffer::rand().

1326 {
1327  M_ASSERT_VALUE(duration, >, 0.0);
1328 
1329  Buffer buffer;
1330 
1331  uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
1332 
1333  for(uint64 i = 0; i < n_samples; ++i)
1334  {
1335  buffer << rng_->get(-1.0f,1.0f);
1336  }
1337 
1338  return buffer;
1339 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
RandomNumberGenerator * rng_
The waveform to ossicialate.
Definition: Generator.h:621
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
virtual uint32 get()=0
Get a random number.
unsigned long long uint64
Definition: Nsound.h:154
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer Generator::gaussianNoise ( const float64 duration,
const float64 mu,
const float64 sigma 
) const
inherited

This method generates noise from a Gaussian distribution.

Definition at line 1343 of file Generator.cc.

References Nsound::RandomNumberGenerator::get(), M_ASSERT_VALUE, Nsound::Generator::rng_, and Nsound::Generator::sample_rate_.

1347 {
1348  Buffer buffer;
1349 
1350  uint32 n_samples = static_cast<uint32>(std::ceil(duration * sample_rate_));
1351 
1352  M_ASSERT_VALUE(n_samples, >, 0);
1353 
1354  // The polar form of the Box-Muller transformation.
1355 
1356  uint32 i = 0;
1357  while(true)
1358  {
1359  float64 f = 0.0;
1360  float64 x1 = 0.0;
1361  float64 x2 = 0.0;
1362  float64 r2 = 0.0;
1363 
1364  while(r2 == 0.0 || r2 >= 1.0)
1365  {
1366  x1 = rng_->get(-1.0f, 1.0f);
1367  x2 = rng_->get(-1.0f, 1.0f);
1368  r2 = x1 * x1 + x2 * x2;
1369  }
1370 
1371  f = ::sqrt(-2.0 * ::log(r2) / r2);
1372 
1373  buffer << mu + sigma * f * x1;
1374 
1375  ++i;
1376 
1377  if(i >= n_samples)
1378  {
1379  break;
1380  }
1381 
1382  buffer << mu + sigma * f * x2;
1383 
1384  ++i;
1385 
1386  if(i >= n_samples)
1387  {
1388  break;
1389  }
1390  }
1391 
1392  return buffer;
1393 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
unsigned int uint32
Definition: Nsound.h:153
RandomNumberGenerator * rng_
The waveform to ossicialate.
Definition: Generator.h:621
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
double float64
Definition: Nsound.h:146
virtual uint32 get()=0
Get a random number.
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer Generator::tanh ( const float64 duration) const
inherited

This method draws the tanh function accross duration samples.

Definition at line 1397 of file Generator.cc.

References M_ASSERT_VALUE, M_PI, and Nsound::Generator::sample_rate_.

1398 {
1399  M_ASSERT_VALUE(duration, >, 0.0);
1400 
1401  Buffer buffer;
1402 
1403  uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
1404 
1405  float64 step = (2.0 * M_PI) / n_samples;
1406 
1407  float64 x = -1.0 * M_PI;
1408 
1409  for(uint64 i = 0; i < n_samples; ++i)
1410  {
1411  buffer << std::tanh(x);
1412  x += step;
1413  }
1414 
1415  return buffer;
1416 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
#define M_PI
Definition: Nsound.h:121
double float64
Definition: Nsound.h:146
unsigned long long uint64
Definition: Nsound.h:154
A Buffer for storing audio samples.
Definition: Buffer.h:60
void Generator::ctor ( const float64 sample_rate)
protectedvirtualinherited

DOXME.

Definition at line 201 of file Generator.cc.

References Nsound::Generator::rng_, Nsound::Generator::sample_rate_, Nsound::Generator::sample_time_, and Nsound::Generator::waveform_.

Referenced by Cosine(), Nsound::Generator::ctor(), Nsound::Generator::Generator(), Nsound::Sawtooth::Sawtooth(), Nsound::Sine::Sine(), Nsound::Square::Square(), and Nsound::Triangle::Triangle().

202 {
203  sample_rate_ = sample_rate;
204  sample_time_ = 1.0 / sample_rate_;
205  waveform_ = NULL;
206  rng_ = new RngTausworthe();
207 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
RandomNumberGenerator * rng_
The waveform to ossicialate.
Definition: Generator.h:621
Buffer * waveform_
The current time (for real time draw functions.)
Definition: Generator.h:619
float64 sample_time_
The number of samples per second to generate.
Definition: Generator.h:617
void Generator::ctor ( const float64 sample_rate,
const Buffer wavetable 
)
protectedvirtualinherited

DOXME.

Definition at line 212 of file Generator.cc.

References Nsound::Generator::ctor(), Nsound::Buffer::getLength(), M_THROW, Nsound::Generator::rng_, Nsound::Generator::sample_rate_, Nsound::Generator::sample_time_, and Nsound::Generator::waveform_.

213 {
214  if(waveform.getLength() != sample_rate)
215  {
216  M_THROW("Generator::ctor(): waveform.getLength() != sample_rate ("
217  << waveform.getLength()
218  << " != "
219  << sample_rate
220  << ")");
221  ctor(sample_rate);
222  }
223  else
224  {
225  delete waveform_;
226  delete rng_;
227 
228  sample_rate_ = sample_rate;
229  sample_time_ = 1.0 / sample_rate_;
230  waveform_ = new Buffer(waveform);
231  rng_ = new RngTausworthe();
232  }
233 }
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
RandomNumberGenerator * rng_
The waveform to ossicialate.
Definition: Generator.h:621
virtual void ctor(const float64 &sample_rate)
DOXME.
Definition: Generator.cc:201
Buffer * waveform_
The current time (for real time draw functions.)
Definition: Generator.h:619
#define M_THROW(message)
Definition: Macros.h:108
float64 sample_time_
The number of samples per second to generate.
Definition: Generator.h:617
A Buffer for storing audio samples.
Definition: Buffer.h:60

Member Data Documentation

bool Nsound::Generator::is_realtime_
protectedinherited
float64 Nsound::Generator::last_frequency_
protectedinherited
float64 Nsound::Generator::position_
protectedinherited
float64 Nsound::Generator::sync_pos_
protectedinherited

The number of samples into the wavefrom.

Definition at line 615 of file Generator.h.

Referenced by Nsound::GeneratorDecay::generate2(), Nsound::Generator::generate2(), and Nsound::Generator::reset().

float64 Nsound::Generator::sample_rate_
protectedinherited
float64 Nsound::Generator::sample_time_
protectedinherited

The number of samples per second to generate.

Definition at line 617 of file Generator.h.

Referenced by Nsound::Generator::ctor(), Nsound::Generator::drawSine2(), Nsound::Pulse::generate2(), and Nsound::Generator::operator=().

float64 Nsound::Generator::t_
protectedinherited
Buffer* Nsound::Generator::waveform_
protectedinherited

The current time (for real time draw functions.)

Definition at line 619 of file Generator.h.

Referenced by Nsound::Generator::ctor(), Nsound::Generator::generate2(), Nsound::Generator::operator=(), and Nsound::Generator::~Generator().

RandomNumberGenerator* Nsound::Generator::rng_
protectedinherited
uint32 Nsound::Generator::buzz_max_harmonics_
protectedinherited

The random number generator.

Definition at line 624 of file Generator.h.

Referenced by Nsound::Generator::buzzInit(), Nsound::Generator::operator=(), and Nsound::Generator::reset().

std::vector<float64> Nsound::Generator::buzz_position_
protectedinherited

Definition at line 625 of file Generator.h.

Referenced by Nsound::Generator::operator=(), and Nsound::Generator::reset().

boolean Nsound::Generator::chorus_is_on_
protectedinherited
uint32 Nsound::Generator::chorus_n_voices_
protectedinherited
std::vector<float64> Nsound::Generator::chorus_position_
protectedinherited
std::vector<float64> Nsound::Generator::chorus_factor_
protectedinherited
boolean Nsound::Generator::sync_is_master_
protectedinherited
boolean Nsound::Generator::sync_is_slave_
protectedinherited
uint32 Nsound::Generator::sync_count_
protectedinherited

Indicates if this generator is a slave.

Definition at line 636 of file Generator.h.

Referenced by Nsound::GeneratorDecay::generate2(), Nsound::Generator::generate2(), Nsound::Generator::operator=(), and Nsound::Generator::reset().

std::vector<uint32> Nsound::Generator::sync_vector_
protectedinherited

Indicates the number of samples since reset.

Definition at line 637 of file Generator.h.

Referenced by Nsound::GeneratorDecay::generate2(), Nsound::Generator::generate2(), and Nsound::Generator::operator=().

std::set<Generator *> Nsound::Generator::sync_slaves_
protectedinherited

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