Nsound::Square Class Reference

Square generator. More...

#include <Nsound/Square.h>

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

List of all members.

Public Member Functions

 Square (const float64 &sample_rate)
 Square (const float64 &sample_rate, const int32 n_harmonics)
 Square (const float64 &sample_rate, const float64 &percent_lambda_1, const float64 &amplitude_1, const float64 &percent_lambda_2, const float64 &percent_lambda_3, const float64 &amplitude_3, const float64 &percent_lambda_4)
void addSlaveSync (Generator &slave)
void buzzInit (const uint32 &max_harmonics)
float64 buzz (const float64 &frequency, const float64 &n_harmonics, const float64 &delay)
 Chorus can not be used with this function.
Buffer buzz (const float64 &duration, const float64 &frequency, const float64 &n_harmonics, const float64 &delay)
Buffer buzz (const float64 &duration, const Buffer &frequency, const Buffer &n_harmonics, const Buffer &delay)
void setChorus (const uint32 n_voices, const float64 &sigma=0.02)
Buffer drawDecay (const float64 &duration, const float64 &alpha=2.0 *M_PI) const
Buffer drawGaussian (const float64 &duration, const float64 &mu, const float64 &sigma, const boolean &normalize=true) const
Buffer drawFatGaussian (const float64 &duration, const float64 &pass_band_percent=0.01) const
Buffer drawLine (const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
Buffer drawParabola (const float64 &duration, const float64 &y1, const float64 &x2, const float64 &y2, const float64 &y3) const
Buffer drawSine (const float64 &duration, const float64 &frequency)
Buffer drawSine (const float64 &duration, const Buffer &frequency)
float64 drawSine (const float64 &frequency)
Buffer drawSine2 (const float64 &duration, const float64 &frequency, const float64 &phase)
Buffer drawSine2 (const float64 &duration, const Buffer &frequency, const Buffer &phase)
Buffer drawSine2 (const float64 &duration, const Buffer &frequency, const float64 &phase=0.0)
Buffer drawSine2 (const float64 &duration, const float64 &frequency, const Buffer &phase)
float64 drawSine2 (const float64 &frequency, const float64 &phase)
Buffer drawWindow (const float64 &duration, WindowType type) const
Buffer drawWindowBartlett (const float64 &duration) const
Buffer drawWindowBlackman (const float64 &duration) const
Buffer drawWindowBlackmanHarris (const float64 &duration) const
Buffer drawWindowHamming (const float64 &duration) const
Buffer drawWindowHanning (const float64 &duration) const
Buffer drawWindowKaiser (const float64 &duration, const float64 &beta=5.0) const
Buffer drawWindowNuttall (const float64 &duration) const
Buffer drawWindowParzen (const float64 &duration) const
Buffer drawWindowRectangular (const float64 &duration) const
virtual float64 generate (const float64 &frequency)
virtual Buffer generate (const float64 &duration, const float64 &frequency, bool reset_first=true)
virtual Buffer generate (const float64 &duration, const Buffer &frequencies, bool reset_first=true)
virtual float64 generate2 (const float64 &frequency, const float64 &phase)
virtual Buffer generate2 (const float64 &duration, const float64 &frequency, const float64 &phase, bool reset_first=true)
virtual Buffer generate2 (const float64 &duration, const float64 &frequencies, const Buffer &phase, bool reset_first=true)
virtual Buffer generate2 (const float64 &duration, const Buffer &frequencies, const float64 &phase, bool reset_first=true)
virtual Buffer generate2 (const float64 &duration, const Buffer &frequencies, const Buffer &phase, bool reset_first=true)
void removeSlaveSync (Generator &slave)
virtual void reset ()
void setSeed (const uint32 seed)
Buffer silence (const float64 &duration) const
float64 tell () const
Buffer whiteNoise (const float64 &duration) const
Buffer gaussianNoise (const float64 &duration, const float64 &mu, const float64 &sigma) const
Buffer tanh (const float64 &duration) const

Protected Member Functions

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

Protected Attributes

float64 last_frequency_
float64 position_
 Used for phase offset adjustment.
float64 sample_rate_
 The number of samples into the wavefrom.
float64 sample_time_
 The number of samples per second to generate.
float64 t_
 The time step between samples in seconds.
Bufferwaveform_
 The current time (for real time draw functions.).
RngTauswortherng_
 The waveform to ossicialate.
uint32 buzz_max_harmonics_
 The random number generator.
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.
uint32 sync_count_
 Indicates if this generator is a slave.
std::vector< uint32sync_vector_
 Indicates the number of samples since reset.
std::vector< Generator * > sync_slaves_
 Stores sample counts when synced.

Detailed Description

Square generator.

1----2 | | | | | | --1 2--3 4-- | | | | | | 3----4

|-----lambda------|

Definition at line 50 of file Square.h.


Constructor & Destructor Documentation

Square::Square ( const float64 sample_rate  ) 

Definition at line 93 of file Square.cc.

References Nsound::Generator::ctor(), drawSquare(), and Nsound::Buffer::getLength().

00093                                     : Generator(sample_rate)
00094 {
00095     Buffer waveform(drawSquare(
00096         sample_rate,
00097         0.0,
00098         1.0,
00099         0.5,
00100         0.5,
00101         -1.0,
00102         1.0));
00103 
00104     while(waveform.getLength() < static_cast<uint32>(sample_rate))
00105     {
00106         waveform << -1.0;
00107     }
00108 
00109     ctor(sample_rate, waveform);
00110 }

Square::Square ( const float64 sample_rate,
const int32  n_harmonics 
)

Definition at line 114 of file Square.cc.

References Nsound::Generator::ctor(), Nsound::Generator::drawSine(), Nsound::Generator::sample_rate_, and Nsound::Buffer::zeros().

00118     :
00119     Generator(sample_rate)
00120 {
00121     // From wikipedia's definition of a square wave.
00122 
00123     float64 Nf = static_cast<float64>(std::abs(n_harmonics));
00124 
00125     if(Nf < 1.0) Nf = 1.0;
00126 
00127     Buffer waveform = Buffer::zeros(static_cast<uint32>(sample_rate_));
00128 
00129     for(float64 k = 1.0; k <= Nf; k += 1.0)
00130     {
00131         float64 kk = 2.0 * k - 1.0;
00132 
00133         waveform += drawSine(1.0, kk) / kk;
00134     }
00135 
00136     waveform *= 4.0 / M_PI;
00137 
00138     ctor(sample_rate, waveform);
}

Square::Square ( const float64 sample_rate,
const float64 percent_lambda_1,
const float64 amplitude_1,
const float64 percent_lambda_2,
const float64 percent_lambda_3,
const float64 amplitude_3,
const float64 percent_lambda_4 
)

Definition at line 143 of file Square.cc.

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

00150                                       : Generator(sample_rate)
00151 {
00152     ctor(sample_rate,
00153         drawSquare(
00154             sample_rate,
00155             percent_lambda_1,
00156             amplitude_1,
00157             percent_lambda_2,
00158             percent_lambda_3,
00159             amplitude_3,
00160             percent_lambda_4));
00161 }


Member Function Documentation

void Generator::addSlaveSync ( Generator slave  )  [inherited]

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

Definition at line 241 of file Generator.cc.

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

00242 {
00243     sync_is_master_ = true;
00244     slave.sync_is_slave_ = true;
00245 
00246     // Only add if it is unique.
00247     std::vector<Generator *>::iterator itor = sync_slaves_.begin();
00248     std::vector<Generator *>::iterator end = sync_slaves_.end();
00249 
00250     boolean is_found = false;
00251 
00252     while(itor != end)
00253     {
00254         if(*itor == &slave) is_found = true;
00255 
00256         ++itor;
00257     }
00258 
00259     if(!is_found) sync_slaves_.push_back(&slave);
00260 
00261     reset();
00262 }

void Generator::buzzInit ( const uint32 max_harmonics  )  [inherited]

Initaializes Buzz settings for real-time calls.

Definition at line 267 of file Generator.cc.

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

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

00268 {
00269     buzz_max_harmonics_ = max_harmonics;
00270     reset();
00271 }

float64 Generator::buzz ( const float64 frequency,
const float64 n_harmonics,
const float64 delay 
) [inherited]

Chorus can not be used with this function.

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

Definition at line 276 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().

00280 {
00281     // This function is based on the csound buzz opcode.
00282 
00283     float64 n = static_cast<int32>(n_harmonics);
00284 
00285     n = fabs(n);
00286     if(n < 1.0) n = 1.0;
00287 
00288     float64 two_n_plus_1 = 2.0 * n + 1.0;
00289     float64 scale = static_cast<float64>(0.5) / n;
00290 
00291     float64 sign = 1.0;
00292     for(int i = 0; i < n; ++i) sign = -sign;
00293 
00294     float64 phase = fabs(phase_offset / static_cast<float64>(2.0));
00295 
00296     while(phase >= 0.5) phase -= 0.5;
00297 
00298     position_ += (phase - last_frequency_) * sample_rate_;
00299 
00300     last_frequency_ = phase;
00301 
00302     float64 y;
00303 
00304     while(position_ >= sample_rate_) position_ -= sample_rate_;
00305 
00306     float64 denom = (*waveform_)[static_cast<int32>(position_ + 0.5)] * sign;
00307 
00308     if(fabs(denom) > 1e-12)
00309     {
00310         float64 up_phase = position_ * two_n_plus_1;
00311         while(up_phase >= sample_rate_) up_phase -= sample_rate_;
00312 
00313         float64 num = (*waveform_)[static_cast<int32>(up_phase)];
00314 
00315         y = (num / denom - static_cast<float64>(1.0)) * scale;
00316     }
00317     else
00318     {
00319         y = 1.0; // this assumes cosine wave!
00320     }
00321 
00322     position_ += 0.5*frequency;
00323 
00324     return y;
00325 }

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 330 of file Generator.cc.

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

00335 {
00336     buzzInit(static_cast<uint32>(n_harmonics));
00337 
00338     int32 n_samples = static_cast<int32>(duration * sample_rate_);
00339 
00340     Buffer y(n_samples);
00341 
00342     for(int i = 0; i < n_samples; ++i)
00343     {
00344         y << buzz(frequency, n_harmonics, phase_offset+0.5);
00345     }
00346 
00347     return y;
00348 }

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 353 of file Generator.cc.

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

00358 {
00359     buzzInit(static_cast<uint32>(n_harmonics.getMax()));
00360 
00361     int32 n_samples = static_cast<int32>(duration * sample_rate_);
00362 
00363     Buffer::circular_iterator f = frequencies.cbegin();
00364     Buffer::circular_iterator n = n_harmonics.cbegin();
00365     Buffer::circular_iterator p = phase_offset.cbegin();
00366 
00367     Buffer y(n_samples);
00368 
00369     for(int32 i = 0; i < n_samples; ++i, ++f, ++n, ++p)
00370     {
00371         y << buzz(*f, *n, *p+0.5);
00372     }
00373 
00374     return y;
00375 }

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 380 of file Generator.cc.

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

Referenced by Sine_UnitTest().

00381 {
00382     if(n_voices == 0)
00383     {
00384         chorus_is_on_ = false;
00385         return;
00386     }
00387 
00388     chorus_is_on_ = true;
00389 
00390     chorus_n_voices_ = n_voices;
00391 
00392     chorus_factor_.clear();
00393     chorus_position_.clear();
00394 
00395     for(uint32 i = 0; i < chorus_n_voices_; ++i)
00396     {
00397         chorus_factor_.push_back(1.0 + rng_->get(-amount, amount));
00398 
00399         chorus_position_.push_back(0.0);
00400     }
00401 
00402     reset();
00403 }

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 408 of file Generator.cc.

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

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

00409 {
00410     if(duration <= 0.0) return Buffer();
00411 
00412     Buffer t = drawLine(duration, 0.0, 1.0);
00413 
00414     t *= -alpha;
00415 
00416     t.exp();
00417 
00418     return t;
00419 }

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:
duration the 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
normalize a 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 424 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().

00429 {
00430     if(duration <= 0.0) return Buffer();
00431 
00432     float64 variance = sigma * sigma;
00433 
00434     Buffer g = drawLine(duration, 0.0, duration);
00435 
00436     g -= mu;
00437 
00438     g *= g;
00439 
00440     g /= (2.0 * variance);
00441 
00442     g *= -1.0;
00443 
00444     g.exp();
00445 
00446     g /= ::sqrt(M_2PI * variance);
00447 
00448     if(normalize) g.normalize();
00449 
00450     return g;
00451 }

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:
duration the number of seconds to draw
pass_band_percent the width of the pass band in percent of duraiton
sigma the standard deviation of the gaussian curve
Returns:
Buffer

The curve is always normaized so the peak is 1.0.

Definition at line 456 of file Generator.cc.

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

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

00459 {
00460     if(duration <= 0.0) return Buffer();
00461 
00462     if(pass_band_percent <= 0.0)
00463     {
00464         std::cerr
00465             << CERR_HEADER
00466             << "drawFatGaussian(): Warning: creating all zero Buffer!"
00467             << std::endl;
00468 
00469         return drawLine(duration, 0.0, 0.0);
00470     }
00471 
00472     else if(pass_band_percent >= 1.0)
00473     {
00474         std::cerr
00475             << CERR_HEADER
00476             << "drawFatGaussian(): Warning: creating all 1.0 Buffer!"
00477             << std::endl;
00478 
00479         return drawLine(duration, 1.0, 1.0);
00480     }
00481 
00482     float64 pass_band_time = duration * pass_band_percent;
00483 
00484     float64 gauss_time = duration - pass_band_time;
00485 
00486     float64 h_gauss_time = gauss_time / 2.0;
00487 
00488     // Using a constant sigma ratio so the shape of the fat gaussian doesn't
00489     // change given a duration.
00490 
00491     float64 sigma = 0.275 * h_gauss_time;
00492 
00493     Buffer y;
00494 
00495     y << drawGaussian(h_gauss_time, h_gauss_time, sigma)
00496       << drawLine(pass_band_time, 1.0, 1.0)
00497       << drawGaussian(h_gauss_time, 0.0, sigma);
00498 
00499     return y;
00500 }

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:
duration the number of seconds to draw
amplitude_start the starting amplitude value
amplitude_finish the 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 505 of file Generator.cc.

References Nsound::Generator::sample_rate_.

Referenced by Nsound::AdsrEnvelope::AdsrEnvelope(), 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(), FilterBandPassFIR_UnitTest(), FilterBandPassIIR_UnitTest(), FilterBandRejectFIR_UnitTest(), FilterBandRejectIIR_UnitTest(), FilterCombLowPassFeedback_UnitTest(), FilterHighPassFIR_UnitTest(), FilterHighPassIIR_UnitTest(), FilterLowPassFIR_UnitTest(), FilterLowPassIIR_UnitTest(), FilterParametricEqualizer_UnitTest(), Nsound::Pluck::generate(), Generator_UnitTest(), Nsound::FFTChunk::getFrequencyAxis(), main(), Nsound::FilterLeastSquaresFIR::makeKernel(), Nsound::Buffer::ones(), Nsound::AudioStream::ones(), Nsound::Stretcher::overlapAdd(), Nsound::OrganPipe::play(), Nsound::Hat::play(), Nsound::GuitarBass::play(), Nsound::FluteSlide::play(), Nsound::DrumKickBass::play(), Nsound::DrumBD01::play(), Nsound::Clarinet::play(), Nsound::AudioStream::plot(), Nsound::Generator::silence(), Sine_UnitTest(), Nsound::Mesh2D::strike(), testAutioStream(), Nsound::Triangle::Triangle(), Triangle_UnitTest(), Nsound::Buffer::zeros(), and Nsound::AudioStream::zeros().

00509 {
00510     if(duration <= 0.0) return Buffer();
00511 
00512     Buffer buffer;
00513 
00514     float64 n_samples = duration * sample_rate_;
00515 
00516     float64 slope = (y2 - y1) / n_samples;
00517 
00518     float64 current_sample = y1;
00519 
00520     for(uint32 i = 0; i < static_cast<uint32>(n_samples+0.5); ++i)
00521     {
00522         buffer << current_sample;
00523         current_sample += slope;
00524     }
00525 
00526     return buffer;
00527 }

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:
duration the number of seconds to draw
y1 the y1 amplitude
x2 the x2 time
y2 the y2 amplitude
y3 the 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 532 of file Generator.cc.

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

Referenced by drum().

00538 {
00539     if(duration <= 0.0) return Buffer();
00540 
00541     // In this discustion, amp = amplitude.
00542     //
00543     // The general equation for a parabola is:
00544     //
00545     // y = A*x^2 + B*x + C
00546     //
00547     // This alogorithm uses Gaussian elimination to solve for A,B,C.
00548     //
00549     // y1 = A * x1^2 + B * x1 + C            (1)
00550     //
00551     // y2 = A * x2^2 + B * x2 + C            (2)
00552     //
00553     // y3 = A * x3^2 + B * x3 + C            (3)
00554     //
00555     // Solve for C using (1), with x1 = 0.0:
00556     //
00557     // C = y1
00558     //
00559     // Solve for A using (2) and substituting C:
00560     //
00561     // A = (y2 - y1 - B * x2) / (x2^2)
00562     //
00563     // Solve for B using (3) and substituting A,C, I used WolframAlpha, awesome!
00564     //
00565     // 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
00566     //
00567     //  B = [x2 * x2 * ( y3 - y1 ) + x3 * x3 * (y1 - y2)] / [x2 * x3 * (x2 - x3)]
00568     //
00569     if (x2 >= duration)
00570     {
00571         std::cerr
00572             << CERR_HEADER
00573             << "drawParabola(): x2 > duration"
00574             << std::endl;
00575 
00576         return Buffer();
00577     }
00578 
00579     float64 x3 = duration;
00580 
00581     float64 C = y1;
00582 
00583     float64 B = (  x2*x2*(y3 - y1) + x3*x3*(y1 - y2)  ) / ( x2*x3*(x2 - x3) );
00584 
00585     float64 A = (y2 - y1 - B*x2) / (x2*x2);
00586 
00587     Buffer t = drawLine(duration, 0.0, duration);
00588 
00589     return A*t*t + B*t + C;
00590 }

Buffer Generator::drawSine ( const float64 duration,
const float64 frequency 
) [inherited]

This method draws a static sine wave.

Parameters:
duration the 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 595 of file Generator.cc.

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

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

00598 {
00599     if(duration <= 0.0) return Buffer();
00600 
00601     t_ = 0.0;
00602 
00603     Buffer y;
00604 
00605     uint64 n_samples = static_cast<uint64>(duration * sample_rate_ + 0.5);
00606 
00607     for(uint64 i = 0; i < n_samples; ++i)
00608     {
00609         y << drawSine2(frequency, 0.0);
00610     }
00611 
00612     return y;
00613 }

Buffer Generator::drawSine ( const float64 duration,
const Buffer frequency 
) [inherited]

This method draws a dynamic sine wave.

Parameters:
duration the 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 618 of file Generator.cc.

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

00621 {
00622     if(duration <= 0.0) return Buffer();
00623 
00624     t_ = 0.0;
00625 
00626     Buffer y;
00627 
00628     uint64 n_samples = static_cast<uint64>(duration * sample_rate_ + 0.5);
00629 
00630     Buffer::circular_iterator f = frequency.cbegin();
00631 
00632     for(uint64 i = 0; i < n_samples; ++i)
00633     {
00634         y << drawSine2(*f, 0.0);
00635         ++f;
00636     }
00637 
00638     return y;
00639 }

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 752 of file Generator.cc.

References Nsound::Generator::drawSine2().

00753 {
00754     return drawSine2(frequency, 0.0);
00755 }

Buffer Generator::drawSine2 ( const float64 duration,
const float64 frequency,
const float64 phase 
) [inherited]

This method draws a static sine wave.

Parameters:
duration the 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 644 of file Generator.cc.

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

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

00648 {
00649     if(duration <= 0.0) return Buffer();
00650 
00651     t_ = 0.0;
00652 
00653     Buffer y;
00654 
00655     uint64 n_samples = static_cast<uint64>(duration * sample_rate_ + 0.5);
00656 
00657     for(uint64 i = 0; i < n_samples; ++i)
00658     {
00659         y << drawSine2(frequency, phase);
00660     }
00661 
00662     return y;
00663 }

Buffer Generator::drawSine2 ( const float64 duration,
const Buffer frequency,
const Buffer phase 
) [inherited]

This method draws a dynamic sine wave.

Parameters:
duration the 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 668 of file Generator.cc.

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

00672 {
00673     if(duration <= 0.0) return Buffer();
00674 
00675     t_ = 0.0;
00676 
00677     Buffer y;
00678 
00679     uint64 n_samples = static_cast<uint64>(duration * sample_rate_ + 0.5);
00680 
00681     Buffer::circular_iterator f = frequency.cbegin();
00682     Buffer::circular_iterator p = phase.cbegin();
00683 
00684     for(uint64 i = 0; i < n_samples; ++i)
00685     {
00686         y << drawSine2(*f, *p);
00687         ++f;
00688         ++p;
00689     }
00690 
00691     return y;
00692 }

Buffer Generator::drawSine2 ( const float64 duration,
const Buffer frequency,
const float64 phase = 0.0 
) [inherited]

This method draws a dynamic sine wave.

Parameters:
duration the 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 697 of file Generator.cc.

References Nsound::Generator::drawSine2().

00701 {
00702     Buffer p(1);
00703     p << phase;
00704 
00705     return drawSine2(duration, frequency, p);
00706 }

Buffer Generator::drawSine2 ( const float64 duration,
const float64 frequency,
const Buffer phase 
) [inherited]

This method draws a dynamic sine wave.

Parameters:
duration the 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 711 of file Generator.cc.

References Nsound::Generator::drawSine2().

00715 {
00716     Buffer f(1);
00717     f << frequency;
00718 
00719     return drawSine2(duration, f, phase);
00720 }

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 725 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, Nsound::Generator::sample_time_, and Nsound::Generator::t_.

00726 {
00727     float64 f = 0.0;
00728     float64 sample = 0.0;
00729 
00730     if(chorus_is_on_)
00731     {
00732         for(uint32 i = 0; i < chorus_n_voices_; ++i)
00733         {
00734             f = M_2PI * chorus_position_[i] * sample_time_ + M_PI * phase;
00735             sample += ::sin(f);
00736             chorus_position_[i] += frequency * chorus_factor_[i];
00737         }
00738         sample /= static_cast<float64>(chorus_n_voices_);
00739     }
00740     else
00741     {
00742         sample = ::sin(M_2PI * t_ * sample_time_ + M_PI * phase);
00743         t_ += frequency;
00744     }
00745 
00746     return sample;
00747 }

Buffer Generator::drawWindow ( const float64 duration,
WindowType  type 
) const [inherited]

Draws a window of the specified type.

Definition at line 761 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, Nsound::NUTTALL, Nsound::PARZEN, and Nsound::RECTANGULAR.

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

00762 {
00763     switch(type)
00764     {
00765         case BARTLETT:        return drawWindowBartlett(duration);
00766         case BLACKMAN:        return drawWindowBlackman(duration);
00767         case BLACKMAN_HARRIS: return drawWindowBlackmanHarris(duration);
00768         case GAUSSIAN:        return drawGaussian(duration, duration/2.0, 1.0);
00769         case GAUSSIAN_05:     return drawFatGaussian(duration, 0.05);
00770         case GAUSSIAN_10:     return drawFatGaussian(duration, 0.10);
00771         case GAUSSIAN_15:     return drawFatGaussian(duration, 0.15);
00772         case GAUSSIAN_20:     return drawFatGaussian(duration, 0.20);
00773         case GAUSSIAN_25:     return drawFatGaussian(duration, 0.25);
00774         case GAUSSIAN_30:     return drawFatGaussian(duration, 0.30);
00775         case GAUSSIAN_33:     return drawFatGaussian(duration, 0.3333);
00776         case GAUSSIAN_35:     return drawFatGaussian(duration, 0.35);
00777         case GAUSSIAN_40:     return drawFatGaussian(duration, 0.40);
00778         case GAUSSIAN_45:     return drawFatGaussian(duration, 0.45);
00779         case GAUSSIAN_50:     return drawFatGaussian(duration, 0.50);
00780         case GAUSSIAN_55:     return drawFatGaussian(duration, 0.55);
00781         case GAUSSIAN_60:     return drawFatGaussian(duration, 0.60);
00782         case GAUSSIAN_65:     return drawFatGaussian(duration, 0.65);
00783         case GAUSSIAN_66:     return drawFatGaussian(duration, 0.6666);
00784         case GAUSSIAN_70:     return drawFatGaussian(duration, 0.70);
00785         case GAUSSIAN_75:     return drawFatGaussian(duration, 0.75);
00786         case GAUSSIAN_80:     return drawFatGaussian(duration, 0.80);
00787         case GAUSSIAN_85:     return drawFatGaussian(duration, 0.85);
00788         case GAUSSIAN_90:     return drawFatGaussian(duration, 0.90);
00789         case GAUSSIAN_95:     return drawFatGaussian(duration, 0.95);
00790         case GAUSSIAN_99:     return drawFatGaussian(duration, 0.9999);
00791         case HAMMING:         return drawWindowHamming(duration);
00792         case HANNING:         return drawWindowHanning(duration);
00793         case KAISER:          return drawWindowKaiser(duration);
00794         case NUTTALL:         return drawWindowNuttall(duration);
00795         case PARZEN:          return drawWindowParzen(duration);
00796         case RECTANGULAR:     return drawWindowRectangular(duration);
00797     }
00798 
00799     return drawWindowRectangular(duration);
00800 }

Buffer Generator::drawWindowBartlett ( const float64 duration  )  const [inherited]

Draws a Bartlett window.

Definition at line 825 of file Generator.cc.

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

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

00826 {
00827     Buffer window = drawLine(duration, 1.0, 1.0);
00828 
00829     int32 n = static_cast<int32>(duration * sample_rate_);
00830 
00831     float64 n_over_2 = static_cast<float64>(n / 2);
00832 
00833     for(int32 i = 0; i < n / 2; ++i)
00834     {
00835         float64 t = static_cast<float64>(i) / n_over_2;
00836         window[i] *= t;
00837         window[i + n/2] *= 1.0 - t;
00838     }
00839 
00840     return window;
00841 }

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 846 of file Generator.cc.

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

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

00847 {
00848     Buffer window = drawLine(duration, 1.0, 1.0);
00849 
00850     cosinewindow(window, 0.42, 0.50, 0.08, 0.00);
00851 
00852     return window;
00853 }

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 858 of file Generator.cc.

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

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

00859 {
00860     Buffer window = drawLine(duration, 1.0, 1.0);
00861 
00862     cosinewindow(window, 0.35875, 0.48829, 0.14128, 0.01168);
00863 
00864     return window;
00865 }

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 871 of file Generator.cc.

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

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

00872 {
00873     Buffer window = drawLine(duration, 1.0, 1.0);
00874 
00875     cosinewindow(window, 0.54, 0.46, 0.00, 0.00);
00876 
00877     return window;
00878 }

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 883 of file Generator.cc.

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

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

00884 {
00885     Buffer window = drawLine(duration, 1.0, 1.0);
00886 
00887     cosinewindow(window, 0.50, 0.50, 0.00, 0.00);
00888 
00889     return window;
00890 }

Buffer Generator::drawWindowKaiser ( const float64 duration,
const float64 beta = 5.0 
) const [inherited]

Draws a Kaiser window.

Definition at line 945 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().

00946 {
00947     Buffer window = drawLine(duration, 0.0, 0.0);
00948 
00949     int32 n_samples = window.getLength();
00950     float64 f_n_samples = float64(n_samples);
00951 
00952     float64 b = ::fabs(beta);
00953 
00954     if(b < 1.0)
00955     {
00956         b = 1.0;
00957     }
00958 
00960     // Calculate window coefficients
00961     //
00962     // alpha = n_samples / 2
00963     //
00964     // for n = 0 : n_samples -1
00965     //
00966     //     w[n] = bessel_i0( beta * sqrt(1.0 - [(n - alpha)/alpha]^2)
00967     //          / bessel_i0(beta)
00968     //
00969     //
00970 
00971     float64 alpha = f_n_samples / 2.0;
00972 
00973     float64 denominator = bessel_i0(b);
00974 
00975     for(int32 i = 0 ; i < n_samples; ++i)
00976     {
00977         float64 temp = (float64(i) - alpha) / alpha;
00978         temp *= temp;
00979 
00980         window[i] = bessel_i0( b * ::sqrt(1.0 - temp)) / denominator;
00981     }
00982 
00983     return window;
00984 }

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 989 of file Generator.cc.

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

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

00990 {
00991     Buffer window = drawLine(duration, 1.0, 1.0);
00992 
00993     cosinewindow(window, 0.3635819, 0.4891775, 0.1365995, 0.0106411);
00994 
00995     return window;
00996 }

Buffer Generator::drawWindowParzen ( const float64 duration  )  const [inherited]

Draws a Parzen window.

Definition at line 1001 of file Generator.cc.

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

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

01002 {
01003     Buffer window = drawLine(duration, 1.0, 1.0);
01004 
01005     int32 n = static_cast<int32>(duration * sample_rate_);
01006 
01007     int32 N = n -1;
01008 
01009     float64 Nf = static_cast<float64>(N);
01010 
01011     for(int32 i = 0; i < N / 4; ++i)
01012     {
01013         float64 m = 2 * ::pow(1.0 - (Nf / 2 - i) / (Nf / 2), 3.0);
01014         window[i] *= m;
01015         window[N - i] *= m;
01016     }
01017 
01018     for(int32 i = N/4; i <= N/2; ++i)
01019     {
01020         int32 wn = i - N/2;
01021         float64 m = 1.0
01022                   - 6.0 * ::pow(wn / (Nf/2),2.0)
01023                       * (1.0 - ::fabs(wn) / (Nf/2));
01024 
01025         window[i] *= m;
01026         window[N-i] *= m;
01027     }
01028 
01029     return window;
01030 }

Buffer Generator::drawWindowRectangular ( const float64 duration  )  const [inherited]

Draws a rectangular window.

Definition at line 1035 of file Generator.cc.

References Nsound::Generator::drawLine().

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

01036 {
01037     return drawLine(duration, 1.0, 1.0);
01038 }

float64 Generator::generate ( const float64 frequency  )  [virtual, inherited]
Buffer Generator::generate ( const float64 duration,
const float64 frequency,
bool  reset_first = true 
) [virtual, inherited]

This method oscillates the waveform stored in this generator.

Parameters:
duration the number of seconds to oscillate the waveform
frequency $\left(f\right)$ the frequency in Hz
reset_first a flag to reset the oscilator's positon in the wavetable
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, and Nsound::Pulse.

Definition at line 1118 of file Generator.cc.

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

01122 {
01123     if(duration <= 0.0) return Buffer();
01124 
01125     if(reset_first)
01126     {
01127         reset();
01128     }
01129 
01130     Buffer buffer;
01131 
01132     uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
01133 
01134     for(uint64 i = 0; i < n_samples; ++i)
01135     {
01136         buffer << generate(frequency);
01137     }
01138 
01139     return buffer;
01140 }

Buffer Generator::generate ( const float64 duration,
const Buffer frequencies,
bool  reset_first = true 
) [virtual, inherited]

This method oscillates the waveform stored in this generator.

Parameters:
duration the 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, bool reset_first) with phase = 0.0.

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

Definition at line 1173 of file Generator.cc.

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

01177 {
01178     if(duration <= 0.0) return Buffer();
01179 
01180     if(reset_first)
01181     {
01182         reset();
01183     }
01184 
01185     uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
01186 
01187     Buffer y(n_samples);
01188 
01189     Buffer::circular_iterator freq = frequencies.cbegin();
01190 
01191     for(uint64 i = 0; i < n_samples; ++i, ++freq)
01192     {
01193         y << generate(*freq);
01194     }
01195 
01196     return y;
01197 }

float64 Generator::generate2 ( const float64 frequency,
const float64 phase 
) [virtual, inherited]

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::Pluck, and Nsound::Pulse.

Definition at line 1051 of file Generator.cc.

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

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

01052 {
01053     if(waveform_ == NULL) return 0.0;
01054 
01055     ++sync_count_;
01056 
01057     if(sync_is_slave_ && !sync_vector_.empty())
01058     {
01059         uint32 sync_count = sync_vector_.front();
01060 
01061         if(sync_count_ == sync_count)
01062         {
01063             sync_vector_.erase(sync_vector_.begin());
01064             position_ = 0;
01065         }
01066     }
01067 
01068     // Move with phase
01069     float64 ph = (phase * sample_rate_ / 2.0);
01070     float64 position2 = position_ + ph + 0.5;
01071 
01072     // Range checks
01073     while(position2 >= sample_rate_)
01074     {
01075         position2 -= sample_rate_;
01076     }
01077 
01078     while(position2 < 0.0)
01079     {
01080         position2 += sample_rate_;
01081     }
01082 
01083     float64 sample = 0.0;
01084 
01085     if(chorus_is_on_)
01086     {
01087         for(uint32 i = 0; i < chorus_n_voices_; ++i)
01088         {
01089             float64 pos = chorus_position_[i]
01090                         + chorus_factor_[i] * frequency
01091                         + ph
01092                         + 0.5;
01093 
01094             // Range check
01095             while(pos >= sample_rate_) pos -= sample_rate_;
01096             while(pos <  0)            pos += sample_rate_;
01097 
01098             sample += (*waveform_)[static_cast<uint64>(pos)];
01099 
01100             chorus_position_[i] += frequency * chorus_factor_[i];
01101         }
01102 
01103         sample /= static_cast<float64>(chorus_n_voices_);
01104     }
01105     else
01106     {
01107         sample = (*waveform_)[static_cast<uint64>(position2)];
01108     }
01109 
01110     position_ += frequency;
01111 
01112     return sample;
01113 }

Buffer Generator::generate2 ( const float64 duration,
const float64 frequency,
const float64 phase,
bool  reset_first = true 
) [virtual, inherited]

This method oscillates the waveform stored in this generator.

Parameters:
duration the 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)
reset_first a flag to reset the oscilator's positon in the wavetable
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::Pluck, and Nsound::Pulse.

Definition at line 1145 of file Generator.cc.

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

01150 {
01151     if(duration <= 0.0) return Buffer();
01152 
01153     if(reset_first)
01154     {
01155         reset();
01156     }
01157 
01158     Buffer buffer;
01159 
01160     uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
01161 
01162     for(uint64 i = 0; i < n_samples; ++i)
01163     {
01164         buffer << generate2(frequency, phase);
01165     }
01166 
01167     return buffer;
01168 }

Buffer Generator::generate2 ( const float64 duration,
const float64 frequencies,
const Buffer phase,
bool  reset_first = true 
) [virtual, inherited]

This method oscillates the waveform stored in this generator.

Parameters:
duration the 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, bool reset_first) with constant frequency.

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

Definition at line 1202 of file Generator.cc.

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

01207 {
01208     if(duration <= 0.0) return Buffer();
01209 
01210     if(reset_first)
01211     {
01212         reset();
01213     }
01214 
01215     uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
01216 
01217     Buffer y(n_samples);
01218 
01219     Buffer::circular_iterator p = phase.cbegin();
01220 
01221     for(uint64 i = 0; i < n_samples; ++i, ++p)
01222     {
01223         y << generate2(frequency,*p);
01224     }
01225 
01226     return y;
01227 }

Buffer Generator::generate2 ( const float64 duration,
const Buffer frequencies,
const float64 phase,
bool  reset_first = true 
) [virtual, inherited]

This method oscillates the waveform stored in this generator.

Parameters:
duration the 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, bool reset_first) with constant phase.

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

Definition at line 1232 of file Generator.cc.

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

01237 {
01238     if(duration <= 0.0) return Buffer();
01239 
01240     if(reset_first)
01241     {
01242         reset();
01243     }
01244 
01245     uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
01246 
01247     Buffer y(n_samples);
01248 
01249     Buffer::circular_iterator f = frequencies.cbegin();
01250 
01251     for(uint64 i = 0; i < n_samples; ++i, ++f)
01252     {
01253         y << generate2(*f, phase);
01254     }
01255 
01256     return y;
01257 }

Buffer Generator::generate2 ( const float64 duration,
const Buffer frequencies,
const Buffer phase,
bool  reset_first = true 
) [virtual, inherited]

This method oscillates the waveform stored in this generator.

Parameters:
duration the 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::Pluck, and Nsound::Pulse.

Definition at line 1262 of file Generator.cc.

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

01267 {
01268     if(duration <= 0.0) return Buffer();
01269 
01270     if(reset_first)
01271     {
01272         reset();
01273     }
01274 
01275     uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
01276 
01277     Buffer y(n_samples);
01278 
01279     Buffer::circular_iterator f = frequencies.cbegin();
01280     Buffer::circular_iterator p = phase.cbegin();
01281 
01282     for(uint64 i = 0; i < n_samples; ++i, ++f, ++p)
01283     {
01284         y << generate2(*f,*p);
01285     }
01286 
01287     return y;
01288 }

void Generator::removeSlaveSync ( Generator slave  )  [inherited]

Removes the generator from the sync list.

Definition at line 1343 of file Generator.cc.

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

01344 {
01345     slave.sync_is_slave_ = false;
01346 
01347     std::vector<Generator *>::iterator itor = sync_slaves_.begin();
01348     std::vector<Generator *>::iterator end = sync_slaves_.end();
01349 
01350     while(*itor != (&slave))
01351     {
01352         ++itor;
01353     }
01354 
01355     if(*itor == (&slave))
01356     {
01357         sync_slaves_.erase(itor);
01358     }
01359 
01360     reset();
01361 }

void Generator::reset (  )  [virtual, inherited]

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

Reimplemented in Nsound::Pulse.

Definition at line 1367 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_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().

01368 {
01369     position_ = 0.0;
01370     last_frequency_ = -1.0;
01371     sync_count_ = 0;
01372     t_ = 0.0;
01373 
01374     buzz_position_.reserve(buzz_max_harmonics_);
01375 
01376     for(uint32 i = 0; i < buzz_max_harmonics_; ++i)
01377     {
01378         buzz_position_[i] = 0.0;
01379     }
01380 
01381     for(uint32 i = 0; i < chorus_n_voices_; ++i)
01382     {
01383         chorus_position_[i] = 0.0;
01384     }
01385 
01386     for(uint32 i = 0; i < sync_slaves_.size(); ++i)
01387     {
01388         sync_slaves_[i]->sync_vector_.clear();
01389     }
01390 }

void Generator::setSeed ( const uint32  seed  )  [inherited]

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

Definition at line 1395 of file Generator.cc.

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

Referenced by Sine_UnitTest().

01396 {
01397     rng_->setSeed(seed);
01398 }

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 1403 of file Generator.cc.

References Nsound::Generator::drawLine().

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

01404 {
01405     if(duration <= 0.0) return Buffer();
01406 
01407     return drawLine(duration,0.0,0.0);
01408 }

float64 Generator::tell (  )  const [inherited]

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

Definition at line 1413 of file Generator.cc.

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

01414 {
01415     return position_ / sample_rate_;
01416 }

Buffer Generator::whiteNoise ( const float64 duration  )  const [inherited]

This method generates noise from a uniform distribution.

Definition at line 1422 of file Generator.cc.

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

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

01423 {
01424     if(duration <= 0.0) return Buffer();
01425 
01426     Buffer buffer;
01427 
01428     uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
01429 
01430     for(uint64 i = 0; i < n_samples; ++i)
01431     {
01432         buffer << rng_->get(-1.0f,1.0f);
01433     }
01434 
01435     return buffer;
01436 }

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 1441 of file Generator.cc.

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

01445 {
01446     if(duration <= 0.0) return Buffer();
01447 
01448     Buffer buffer;
01449 
01450     uint32 n_samples = static_cast<uint32>(std::ceil(duration * sample_rate_));
01451 
01452     // The polar form of the Box-Muller transformation.
01453 
01454     uint32 i = 0;
01455     while(true)
01456     {
01457         float64 f = 0.0;
01458         float64 x1 = 0.0;
01459         float64 x2 = 0.0;
01460         float64 r2 = 0.0;
01461 
01462         while(r2 == 0.0 || r2 >= 1.0)
01463         {
01464             x1 = rng_->get(-1.0f, 1.0f);
01465             x2 = rng_->get(-1.0f, 1.0f);
01466             r2 = x1 * x1 + x2 * x2;
01467         }
01468 
01469         f = ::sqrt(-2.0 * ::log(r2) / r2);
01470 
01471         buffer << mu + sigma * f * x1;
01472 
01473         ++i;
01474 
01475         if(i >= n_samples)
01476         {
01477             break;
01478         }
01479 
01480         buffer << mu + sigma * f * x2;
01481 
01482         ++i;
01483 
01484         if(i >= n_samples)
01485         {
01486             break;
01487         }
01488     }
01489 
01490     return buffer;
01491 }

Buffer Generator::tanh ( const float64 duration  )  const [inherited]

This method draws the tanh function accross duration samples.

Definition at line 1496 of file Generator.cc.

References Nsound::Generator::sample_rate_.

01497 {
01498     if(duration <= 0.0) return Buffer();
01499 
01500     Buffer buffer;
01501 
01502     uint64 n_samples = static_cast<uint64>(std::ceil(duration * sample_rate_));
01503 
01504     float64 step = (2.0 * M_PI) / n_samples;
01505 
01506     float64 x = -1.0 * M_PI;
01507 
01508     for(uint64 i = 0; i < n_samples; ++i)
01509     {
01510         buffer << std::tanh(x);
01511         x += step;
01512     }
01513 
01514     return buffer;
01515 }

void Generator::ctor ( const float64 sample_rate  )  [protected, virtual, inherited]
void Generator::ctor ( const float64 sample_rate,
const Buffer wavetable 
) [protected, virtual, inherited]

DOXME

Definition at line 216 of file Generator.cc.

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

00217 {
00218     if(waveform.getLength() != sample_rate)
00219     {
00220         cerr << ERROR_HEADER << "waveform.getLength() != sample_rate ("
00221              << waveform.getLength()
00222              << " != "
00223              << sample_rate
00224              << ")"
00225              << endl;
00226 
00227         ctor(sample_rate);
00228     }
00229     else
00230     {
00231         sample_rate_ = sample_rate;
00232         sample_time_ = 1.0 / sample_rate_;
00233         waveform_ = new Buffer(waveform);
00234         rng_ = new RngTausworthe();
00235     }
00236 }


Member Data Documentation

float64 Nsound::Generator::position_ [protected, inherited]
float64 Nsound::Generator::sample_rate_ [protected, inherited]
float64 Nsound::Generator::sample_time_ [protected, inherited]

The number of samples per second to generate.

Definition at line 676 of file Generator.h.

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

float64 Nsound::Generator::t_ [protected, inherited]
Buffer* Nsound::Generator::waveform_ [protected, inherited]

The current time (for real time draw functions.).

Definition at line 678 of file Generator.h.

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

RngTausworthe* Nsound::Generator::rng_ [protected, inherited]

The random number generator.

Definition at line 684 of file Generator.h.

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

std::vector<float64> Nsound::Generator::buzz_position_ [protected, inherited]

Definition at line 685 of file Generator.h.

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

boolean Nsound::Generator::chorus_is_on_ [protected, inherited]
std::vector<float64> Nsound::Generator::chorus_position_ [protected, inherited]
std::vector<float64> Nsound::Generator::chorus_factor_ [protected, inherited]

Definition at line 696 of file Generator.h.

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

Indicates if this generator is the master.

Definition at line 697 of file Generator.h.

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

uint32 Nsound::Generator::sync_count_ [protected, inherited]

Indicates if this generator is a slave.

Definition at line 698 of file Generator.h.

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

std::vector<uint32> Nsound::Generator::sync_vector_ [protected, inherited]

Indicates the number of samples since reset.

Definition at line 699 of file Generator.h.

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

std::vector<Generator *> Nsound::Generator::sync_slaves_ [protected, inherited]

The documentation for this class was generated from the following files:
Generated on Sun Apr 15 20:10:51 2012 for nsound by  doxygen 1.6.3