40 using namespace Nsound;
46 #define M_2PI (2.0*M_PI)
48 #define CERR_HEADER __FILE__ << ":" << __LINE__ << ": "
62 buzz_max_harmonics_(0),
68 sync_is_master_(false),
69 sync_is_slave_(false),
89 buzz_max_harmonics_(0),
95 sync_is_master_(false),
96 sync_is_slave_(false),
109 last_frequency_(0.0),
117 buzz_max_harmonics_(0),
119 chorus_is_on_(false),
123 sync_is_master_(false),
124 sync_is_slave_(false),
138 last_frequency_(0.0),
146 buzz_max_harmonics_(0),
148 chorus_is_on_(false),
152 sync_is_master_(false),
153 sync_is_slave_(false),
158 ctor(sample_rate, waveform);
166 last_frequency_(0.0),
174 buzz_max_harmonics_(0),
176 chorus_is_on_(false),
180 sync_is_master_(false),
181 sync_is_slave_(false),
216 M_THROW(
"Generator::ctor(): waveform.getLength() != sample_rate ("
272 float64 two_n_plus_1 = 2.0 * n + 1.0;
276 for(
int i = 0; i < n; ++i) sign = -sign;
278 float64 phase = fabs(phase_offset / static_cast<float64>(2.0));
280 while(phase >= 0.5) phase -= 0.5;
292 if(fabs(denom) > 1e-12)
297 float64 num = (*waveform_)[
static_cast<int32>(up_phase)];
299 y = (num / denom -
static_cast<float64>(1.0)) * scale;
319 buzzInit(static_cast<uint32>(n_harmonics));
325 for(
int i = 0; i < n_samples; ++i)
327 y <<
buzz(frequency, n_harmonics, phase_offset+0.5);
337 const Buffer & frequencies,
338 const Buffer & n_harmonics,
339 const Buffer & phase_offset)
351 for(
int32 i = 0; i < n_samples; ++i, ++f, ++n, ++p)
353 y <<
buzz(*f, *n, *p+0.5);
390 if(duration <= 0.0)
return Buffer();
407 const boolean & normalize)
const
409 if(duration <= 0.0)
return Buffer();
411 float64 variance = sigma * sigma;
419 g /= (2.0 * variance);
425 g /= ::sqrt(
M_2PI * variance);
436 const float64 & pass_band_percent)
const
442 float64 pass_band_time = duration * pass_band_percent;
444 float64 gauss_time = duration - pass_band_time;
446 float64 h_gauss_time = gauss_time / 2.0;
451 float64 sigma = 0.275 * h_gauss_time;
456 <<
drawLine(pass_band_time, 1.0, 1.0)
475 float64 slope = (y2 - y1) / n_samples;
479 for(
uint32 i = 0; i < static_cast<uint32>(n_samples+0.5); ++i)
481 buffer << current_sample;
482 current_sample += slope;
533 float64 B = ( x2*x2*(y3 - y1) + x3*x3*(y1 - y2) ) / ( x2*x3*(x2 - x3) );
535 float64 A = (y2 - y1 - B*x2) / (x2*x2);
539 return A*t*t + B*t + C;
556 for(
uint64 i = 0; i < n_samples; ++i)
580 for(
uint64 i = 0; i < n_samples; ++i)
604 for(
uint64 i = 0; i < n_samples; ++i)
630 for(
uint64 i = 0; i < n_samples; ++i)
650 return drawSine2(duration, frequency, p);
738 default:
M_THROW(
"Unkown window type " << type);
754 for(
int32 i = 0; i < n; ++i)
757 - a1 * ::cos(2.0 *
M_PI * i / n)
758 + a2 * ::cos(4.0 *
M_PI * i / n)
759 - a3 * ::cos(6.0 *
M_PI * i / n);
773 for(
int32 i = 0; i < n / 2; ++i)
777 window[i + n/2] *= 1.0 - t;
800 cosinewindow(window, 0.35875, 0.48829, 0.14128, 0.01168);
864 y = ::pow(x / 3.75, 2);
865 result = p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7)))));
870 result = (::exp(ax) / ::sqrt(ax))
871 * (q1+y*(q2+y*(q3+y*(q4+y*(q5+y*(q6+y*(q7+y*(q8+y*q9))))))));
904 float64 alpha = f_n_samples / 2.0;
908 for(
int32 i = 0 ; i < n_samples; ++i)
913 window[i] =
bessel_i0( b * ::sqrt(1.0 - temp)) / denominator;
925 cosinewindow(window, 0.3635819, 0.4891775, 0.1365995, 0.0106411);
942 for(
int32 i = 0; i < N / 4; ++i)
944 float64 m = 2 * pow(1.0 - (Nf / 2 - i) / (Nf / 2), 3.0);
949 for(
int32 i = N/4; i <= N/2; ++i)
953 - 6.0 * pow(wn / (Nf/2),2.0)
954 * (1.0 - fabs(static_cast<float64>(wn)) / (Nf/2));
967 return drawLine(duration, 1.0, 1.0);
1008 while(position2 < 0.0)
1028 sample += (*waveform_)[
static_cast<uint32>(pos)];
1037 sample = (*waveform_)[
static_cast<uint32>(position2)];
1050 std::set<Generator *>::iterator itor =
sync_slaves_.begin();
1077 for(
uint64 i = 0; i < n_samples; ++i)
1100 for(
uint64 i = 0; i < n_samples; ++i)
1112 const Buffer & frequencies)
1124 for(
uint32 i = 0; i < n_samples; ++i, ++freq)
1149 for(
uint32 i = 0; i < n_samples; ++i, ++p)
1161 const Buffer & frequencies,
1174 for(
uint32 i = 0; i < n_samples; ++i, ++f)
1186 const Buffer & frequencies,
1200 for(
uint32 i = 0; i < n_samples; ++i, ++f, ++p)
1292 std::set<Generator *>::iterator itor =
sync_slaves_.begin();
1296 (*itor)->sync_vector_.clear();
1333 for(
uint64 i = 0; i < n_samples; ++i)
1335 buffer <<
rng_->
get(-1.0f,1.0f);
1364 while(r2 == 0.0 || r2 >= 1.0)
1368 r2 = x1 * x1 + x2 * x2;
1371 f = ::sqrt(-2.0 * ::log(r2) / r2);
1373 buffer << mu + sigma * f * x1;
1382 buffer << mu + sigma * f * x2;
1409 for(
uint64 i = 0; i < n_samples; ++i)
1411 buffer << std::tanh(x);
float64 sample_rate_
Used to determine when to create a sync sample.
RandomNumberGenerator * rng_
The waveform to ossicialate.
Buffer drawSine2(const float64 &duration, const float64 &frequency, const float64 &phase)
This method draws a static sine wave.
float64 tell() const
Returns the current position pointer, values are between 0.0 to 1.0.
#define M_ASSERT_VALUE(a, op, value)
Buffer drawWindowKaiser(const float64 &duration, const float64 &beta=5.0) const
Draws a Kaiser window.
Buffer drawWindowRectangular(const float64 &duration) const
Draws a rectangular window.
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...
Nsound::Generator & operator=(const Nsound::Generator &rhs)
Assignment operator.
Buffer drawWindowBlackman(const float64 &duration) const
Draws a Blackman window.
virtual void ctor(const float64 &sample_rate)
DOXME.
std::vector< float64 > chorus_position_
uint32 buzz_max_harmonics_
The random number generator.
std::vector< float64 > buzz_position_
Buffer tanh(const float64 &duration) const
This method draws the tanh function accross duration samples.
std::set< Generator * > sync_slaves_
Stores sample counts when synced.
Buffer drawWindowHamming(const float64 &duration) const
Draws a Hamming window.
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.
float64 position_
Used for phase offset adjustment.
Buffer drawWindow(const float64 &duration, WindowType type) const
Draws a window of the specified type.
Buffer drawWindowBartlett(const float64 &duration) const
Draws a Bartlett window.
virtual void setSeed(uint32 seed)=0
Set the seed to use.
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
float64 sync_pos_
The number of samples into the wavefrom.
Buffer drawSine(const float64 &duration, const float64 &frequency)
This method draws a static sine wave.
virtual uint32 get()=0
Get a random number.
Buffer drawWindowHanning(const float64 &duration) const
Draws a Hanning window.
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. ...
uint32 getLength() const
Returns the number of samples in the Buffer.
void addSlaveSync(Generator &slave)
Adds a generator as a slave to this instance for syncing.
Buffer silence(const float64 &duration) const
This method generates silence.
float64 t_
The time step between samples in seconds.
boolean sync_is_slave_
Indicates if this generator is the master.
void buzzInit(const uint32 &max_harmonics)
Initaializes Buzz settings for real-time calls.
Buffer drawWindowParzen(const float64 &duration) const
Draws a Parzen window.
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
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.
void setChorus(const uint32 n_voices, const float64 &sigma=0.02)
Chorus or Unison.
Buffer * waveform_
The current time (for real time draw functions.)
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.
unsigned long long uint64
void normalize()
Multiplies the Buffer by a constant gain so the peak sample has magnitude 1.0.
float64 bessel_i0(const float64 &x)
std::vector< float64 > chorus_factor_
virtual ~Generator()
Deletes the baseclass part of the object.
virtual void reset()
Resets the position pointer back to the begging of the waveform.
Buffer gaussianNoise(const float64 &duration, const float64 &mu, const float64 &sigma) const
This method generates noise from a Gaussian distribution.
void exp()
Each sample in the Buffer becomes the power e^x.
float64 sample_time_
The number of samples per second to generate.
Buffer drawWindowBlackmanHarris(const float64 &duration) const
Draws a Blackman-Harris window.
A Buffer for storing audio samples.
std::vector< uint32 > sync_vector_
Indicates the number of samples since reset.
Buffer whiteNoise(const float64 &duration) const
This method generates noise from a uniform distribution.
uint32 sync_count_
Indicates if this generator is a slave.
Buffer drawLine(const float64 &duration, const float64 &litude_start, const float64 &litude_finish) const
This method draws a linear line beteween 2 points.
float64 getMax() const
Returns the maximum sample value in the Buffer.
Buffer drawWindowNuttall(const float64 &duration) const
Draws a Nuttall window.
void setSeed(const uint32 seed)
Sets the seed for the Generator's random number generator (rng).
A class the provides draw utilities and a wavetable oscillator.
static void cosinewindow(Buffer &win, const float64 &a0, const float64 &a1, const float64 &a2, const float64 &a3)
void removeSlaveSync(Generator &slave)
Removes the generator from the sync list.
virtual float64 generate2(const float64 &frequency, const float64 &phase)
This is a real-time method for the wavetable oscillator.