Nsound  0.9.4
Generator.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Generator.h 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Nsound is a C++ library and Python module for audio synthesis featuring
6 // dynamic digital filters. Nsound lets you easily shape waveforms and write
7 // to disk or plot them. Nsound aims to be as powerful as Csound but easy to
8 // use.
9 //
10 // Copyright (c) 2005-Present Nick Hilton
11 //
12 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
13 //
14 //-----------------------------------------------------------------------------
15 
16 //-----------------------------------------------------------------------------
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 // This program is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 // GNU Library General Public License for more details.
27 //
28 // You should have received a copy of the GNU General Public License
29 // along with this program; if not, write to the Free Software
30 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 //
32 //-----------------------------------------------------------------------------
33 
34 #ifndef _NSOUND_GENERATOR_H_
35 #define _NSOUND_GENERATOR_H_
36 
37 #include <Nsound/Nsound.h>
38 #include <Nsound/WindowType.h>
39 
40 namespace Nsound
41 {
42 
43 // Forwared declarion of Buffer
44 class Buffer;
45 class RandomNumberGenerator;
46 
47 //-----------------------------------------------------------------------------
48 // Generator Class
50 class Generator
51 {
52  public:
53 
55  Generator(const float64 & sample_rate);
56 
58  Generator(const float64 & sample_rate, const Buffer & waveform);
59 
61  //
63  Generator(const std::string & wave_filename);
64 
66  Generator(const Nsound::Generator & gen);
67 
69  virtual ~Generator();
70 
72  void setRealtime(bool flag) { is_realtime_ = flag; }
73 
75  void
76  addSlaveSync(Generator & slave);
77 
79  void
80  buzzInit(const uint32 & max_harmonics);
81 
83  //
85  float64
86  buzz(
87  const float64 & frequency,
88  const float64 & n_harmonics,
89  const float64 & delay);
90 
92  Buffer
93  buzz(
94  const float64 & duration,
95  const float64 & frequency,
96  const float64 & n_harmonics,
97  const float64 & delay);
98 
100  Buffer
101  buzz(
102  const float64 & duration,
103  const Buffer & frequency,
104  const Buffer & n_harmonics,
105  const Buffer & delay);
106 
108  //
117  void
118  setChorus(
119  const uint32 n_voices,
120  const float64 & sigma = 0.02);
121 
123  //
127  Buffer
128  drawDecay(const float64 & duration, const float64 & alpha = 2.0*M_PI) const;
129 
131  //
145  Buffer
146  drawGaussian(
147  const float64 & duration,
148  const float64 & mu,
149  const float64 & sigma,
150  const boolean & normalize = true) const;
151 
153  //
160  Buffer
162  const float64 & duration,
163  const float64 & pass_band_percent = 0.01) const;
164 
166  //
174  Buffer
175  drawLine(
176  const float64 & duration,
177  const float64 & amplitude_start,
178  const float64 & amplitude_finish) const;
179 
181  //
196  Buffer
197  drawParabola(
198  const float64 & duration,
199  const float64 & y1,
200  const float64 & x2,
201  const float64 & y2,
202  const float64 & y3) const;
203 
205  //
213  Buffer
214  drawSine(
215  const float64 & duration,
216  const float64 & frequency);
217 
219  //
227  Buffer
228  drawSine(
229  const float64 & duration,
230  const Buffer & frequency);
231 
233  //
243  Buffer
244  drawSine2(
245  const float64 & duration,
246  const float64 & frequency,
247  const float64 & phase);
248 
250  //
263  Buffer
264  drawSine2(
265  const float64 & duration,
266  const Buffer & frequency,
267  const Buffer & phase);
268 
270  //
279  Buffer
280  drawSine2(
281  const float64 & duration,
282  const Buffer & frequency,
283  const float64 & phase=0.0);
284 
286  //
295  Buffer
296  drawSine2(
297  const float64 & duration,
298  const float64 & frequency,
299  const Buffer & phase);
300 
302  //
309  float64
310  drawSine(
311  const float64 & frequency);
312 
314  //
318  float64
319  drawSine2(
320  const float64 & frequency,
321  const float64 & phase);
322 
324  Buffer
325  drawWindow(const float64 & duration, WindowType type) const;
326 
328  //
329  Buffer
330  drawWindowBartlett(const float64 & duration) const;
331 
333  //
342  Buffer
343  drawWindowBlackman(const float64 & duration) const;
344 
346  //
355  Buffer
356  drawWindowBlackmanHarris(const float64 & duration) const;
357 
359  //
368  Buffer
369  drawWindowHamming(const float64 & duration) const;
370 
372  //
381  Buffer
382  drawWindowHanning(const float64 & duration) const;
383 
385  Buffer
386  drawWindowKaiser(const float64 & duration, const float64 & beta=5.0) const;
387 
389  //
398  Buffer
399  drawWindowNuttall(const float64 & duration) const;
400 
402  Buffer
403  drawWindowParzen(const float64 & duration) const;
404 
406  Buffer
407  drawWindowRectangular(const float64 & duration) const;
408 
410  //
417  virtual
418  float64
419  generate(const float64 & frequency);
420 
422  //
438  virtual
439  float64
440  generate2(const float64 & frequency, const float64 & phase);
441 
443  //
458  virtual
459  Buffer
460  generate(
461  const float64 & duration,
462  const float64 & frequency);
463 
465  //
481  virtual
482  Buffer
483  generate2(
484  const float64 & duration,
485  const float64 & frequency,
486  const float64 & phase);
487 
489  //
497  virtual
498  Buffer
499  generate(
500  const float64 & duration,
501  const Buffer & frequencies);
502 
504  //
513  virtual
514  Buffer
515  generate2(
516  const float64 & duration,
517  const float64 & frequencies,
518  const Buffer & phase);
519 
521  //
530  virtual
531  Buffer
532  generate2(
533  const float64 & duration,
534  const Buffer & frequencies,
535  const float64 & phase);
536 
538  //
543  virtual
544  Buffer
545  generate2(
546  const float64 & duration,
547  const Buffer & frequencies,
548  const Buffer & phase);
549 
552  operator=(const Nsound::Generator & rhs);
553 
555  void
556  removeSlaveSync(Generator & slave);
557 
559  virtual
560  void reset();
561 
563 
565  void setSeed(const uint32 seed);
566 
568  //
572  Buffer
573  silence(const float64 & duration) const;
574 
576  float64
577  tell() const;
578 
580  Buffer
581  whiteNoise(const float64 & duration) const;
582 
584  Buffer
586  const float64 & duration,
587  const float64 & mu,
588  const float64 & sigma) const;
589 
591  Buffer
592  tanh(const float64 & duration) const;
593 
594  protected:
595 
597  Generator();
598 
600  virtual
601  void
602  ctor(const float64 & sample_rate);
603 
605  virtual
606  void
607  ctor(
608  const float64 & sample_rate,
609  const Buffer & wavetable);
610 
612 
620 
622 
623  // buzz() stuff
625  std::vector<float64> buzz_position_;
626 
627  // Chorus stuff
628  boolean chorus_is_on_;
630  std::vector<float64> chorus_position_;
631  std::vector<float64> chorus_factor_;
632 
633  // Sync stuff
634  boolean sync_is_master_;
635  boolean sync_is_slave_;
637  std::vector<uint32> sync_vector_;
638  std::set<Generator *> sync_slaves_;
639 
640 };//Generators
641 
642 }; // Nsound
643 
644 // :mode=c++: jEdit modeline
645 #endif
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
RandomNumberGenerator * rng_
The waveform to ossicialate.
Definition: Generator.h:621
Buffer drawSine2(const float64 &duration, const float64 &frequency, const float64 &phase)
This method draws a static sine wave.
Definition: Generator.cc:591
float64 tell() const
Returns the current position pointer, values are between 0.0 to 1.0.
Definition: Generator.cc:1317
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
Nsound::Generator & operator=(const Nsound::Generator &rhs)
Assignment operator.
Definition: Generator.cc:1210
Buffer drawWindowBlackman(const float64 &duration) const
Draws a Blackman window.
Definition: Generator.cc:785
virtual void ctor(const float64 &sample_rate)
DOXME.
Definition: Generator.cc:201
std::vector< float64 > chorus_position_
Definition: Generator.h:630
void setRealtime(bool flag)
Sets realtime mode, disables automatic reset() if set.
Definition: Generator.h:72
#define M_PI
Definition: Nsound.h:121
uint32 buzz_max_harmonics_
The random number generator.
Definition: Generator.h:624
Generator()
DOXME.
Definition: Generator.cc:52
std::vector< float64 > buzz_position_
Definition: Generator.h:625
Buffer tanh(const float64 &duration) const
This method draws the tanh function accross duration samples.
Definition: Generator.cc:1397
std::set< Generator * > sync_slaves_
Stores sample counts when synced.
Definition: Generator.h:638
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
float64 position_
Used for phase offset adjustment.
Definition: Generator.h:614
Buffer drawWindow(const float64 &duration, WindowType type) const
Draws a window of the specified type.
Definition: Generator.cc:702
Buffer drawWindowBartlett(const float64 &duration) const
Draws a Bartlett window.
Definition: Generator.cc:765
double float64
Definition: Nsound.h:146
float64 sync_pos_
The number of samples into the wavefrom.
Definition: Generator.h:615
Buffer drawSine(const float64 &duration, const float64 &frequency)
This method draws a static sine wave.
Definition: Generator.cc:544
Buffer drawWindowHanning(const float64 &duration) const
Draws a Hanning window.
Definition: Generator.cc:819
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. ...
Definition: Generator.cc:490
void addSlaveSync(Generator &slave)
Adds a generator as a slave to this instance for syncing.
Definition: Generator.cc:237
Buffer silence(const float64 &duration) const
This method generates silence.
Definition: Generator.cc:1310
float64 t_
The time step between samples in seconds.
Definition: Generator.h:618
boolean sync_is_slave_
Indicates if this generator is the master.
Definition: Generator.h:635
void buzzInit(const uint32 &max_harmonics)
Initaializes Buzz settings for real-time calls.
Definition: Generator.cc:252
boolean sync_is_master_
Definition: Generator.h:634
Buffer drawWindowParzen(const float64 &duration) const
Draws a Parzen window.
Definition: Generator.cc:932
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
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.
Definition: Generator.cc:388
void setChorus(const uint32 n_voices, const float64 &sigma=0.02)
Chorus or Unison.
Definition: Generator.cc:361
Buffer * waveform_
The current time (for real time draw functions.)
Definition: Generator.h:619
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
float64 last_frequency_
Definition: Generator.h:613
std::vector< float64 > chorus_factor_
Definition: Generator.h:631
virtual ~Generator()
Deletes the baseclass part of the object.
Definition: Generator.cc:192
virtual void reset()
Resets the position pointer back to the begging of the waveform.
Definition: Generator.cc:1272
Buffer gaussianNoise(const float64 &duration, const float64 &mu, const float64 &sigma) const
This method generates noise from a Gaussian distribution.
Definition: Generator.cc:1343
float64 sample_time_
The number of samples per second to generate.
Definition: Generator.h:617
Buffer drawWindowBlackmanHarris(const float64 &duration) const
Draws a Blackman-Harris window.
Definition: Generator.cc:796
A Buffer for storing audio samples.
Definition: Buffer.h:60
std::vector< uint32 > sync_vector_
Indicates the number of samples since reset.
Definition: Generator.h:637
Buffer whiteNoise(const float64 &duration) const
This method generates noise from a uniform distribution.
Definition: Generator.cc:1325
uint32 sync_count_
Indicates if this generator is a slave.
Definition: Generator.h:636
uint32 chorus_n_voices_
Definition: Generator.h:629
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 drawWindowNuttall(const float64 &duration) const
Draws a Nuttall window.
Definition: Generator.cc:921
void setSeed(const uint32 seed)
Sets the seed for the Generator's random number generator (rng).
Definition: Generator.cc:1303
RandomNumberGenerator & getRandomNumberGenerator()
Definition: Generator.h:562
Base class for random number generators.
WindowType
Definition: WindowType.h:39
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
void removeSlaveSync(Generator &slave)
Removes the generator from the sync list.
Definition: Generator.cc:1259
virtual float64 generate2(const float64 &frequency, const float64 &phase)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:979