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

#include <Nsound/Granulator.h>

Public Types

enum  EnvelopeType_e {
  CUSTOM, GAUSSIAN, GAUSSIAN_90, GAUSSIAN_70,
  GAUSSIAN_50, GAUSSIAN_30, GAUSSIAN_10, DECAY,
  REVERSE_DECAY
}
 

Public Member Functions

 Granulator (const float64 &sample_rate, EnvelopeType_e env_type, const float64 &envelope_noise=0.0, const Buffer *custom_envelope=NULL)
 Creates a generator with the specified sample rate. More...
 
 Granulator (const Nsound::Granulator &gran)
 
virtual ~Granulator ()
 
Nsound::Buffer generate (const float64 &duration, const float64 &grain_frequency, const float64 &waves_per_grain, const float64 &grains_per_second)
 
Nsound::Buffer generate (const float64 &duration, const Nsound::Buffer &grain_frequency, const Nsound::Buffer &waves_per_grain, const Nsound::Buffer &grains_per_second) const
 
Nsound::Granulatoroperator= (const Nsound::Granulator &rhs)
 Assignment operator. More...
 

Protected Member Functions

 Granulator ()
 

Protected Attributes

float64 sample_rate_
 
Generatorenvelope_generator_
 

Detailed Description

Definition at line 51 of file Granulator.h.

Member Enumeration Documentation

Enumerator
CUSTOM 
GAUSSIAN 
GAUSSIAN_90 
GAUSSIAN_70 
GAUSSIAN_50 
GAUSSIAN_30 
GAUSSIAN_10 
DECAY 
REVERSE_DECAY 

Definition at line 55 of file Granulator.h.

Constructor & Destructor Documentation

Granulator::Granulator ( const float64 sample_rate,
EnvelopeType_e  env_type,
const float64 envelope_noise = 0.0,
const Buffer custom_envelope = NULL 
)

Creates a generator with the specified sample rate.

Definition at line 49 of file Granulator.cc.

References CUSTOM, DECAY, Nsound::Generator::drawDecay(), Nsound::Generator::drawFatGaussian(), Nsound::Generator::drawGaussian(), envelope_generator_, GAUSSIAN, GAUSSIAN_10, GAUSSIAN_30, GAUSSIAN_50, GAUSSIAN_70, GAUSSIAN_90, Nsound::Buffer::getLength(), Nsound::Buffer::getReverse(), M_THROW, REVERSE_DECAY, sample_rate_, and Nsound::Generator::whiteNoise().

54  :
55  sample_rate_(sample_rate),
57 // sine_(new Sine(sample_rate_)),
58 // current_time_(0.0),
59 // next_grain_time_(0.0),
60 // last_grain_time_(0.0),
61 // time_step_(1.0 / sample_rate_)
62 {
64 
65  switch(env_type)
66  {
67  case Granulator::CUSTOM:
68  {
69  if(custom_envelope == NULL
70  || custom_envelope->getLength() != sample_rate_)
71  {
72  M_THROW("custom_envelope->getLength() must equal "
73  << sample_rate_
74  << ", length is "
75  << custom_envelope->getLength());
76 
78  new Generator(
80  gen.drawGaussian(1.0, 0.5, 0.33333)
81  * (1.0 + envelope_noise * gen.whiteNoise(1.0)));
82  }
83  else
84  {
85  envelope_generator_ =
86  new Generator(
88  (*custom_envelope)
89  * (1.0 + envelope_noise * gen.whiteNoise(1.0)));
90  }
91  break;
92  }
93 
95  {
96  envelope_generator_ = new Generator(
98  gen.drawGaussian(1.0, 0.5, 0.33333)
99  * (1.0 + envelope_noise * gen.whiteNoise(1.0)));
100  break;
101  }
102 
104  {
105  envelope_generator_ = new Generator(
106  sample_rate_,
107  gen.drawFatGaussian(1.0, 0.90)
108  * (1.0 + envelope_noise * gen.whiteNoise(1.0)));
109  break;
110  }
111 
113  {
114  envelope_generator_ = new Generator(
115  sample_rate_,
116  gen.drawFatGaussian(1.0, 0.70)
117  * (1.0 + envelope_noise * gen.whiteNoise(1.0)));
118  break;
119  }
120 
122  {
123  envelope_generator_ = new Generator(
124  sample_rate_,
125  gen.drawFatGaussian(1.0, 0.50)
126  * (1.0 + envelope_noise * gen.whiteNoise(1.0)));
127  break;
128  }
129 
131  {
132  envelope_generator_ = new Generator(
133  sample_rate_,
134  gen.drawFatGaussian(1.0, 0.30)
135  * (1.0 + envelope_noise * gen.whiteNoise(1.0)));
136  break;
137  }
138 
140  {
141  envelope_generator_ = new Generator(
142  sample_rate_,
143  gen.drawFatGaussian(1.0, 0.10)
144  * (1.0 + envelope_noise * gen.whiteNoise(1.0)));
145  break;
146  }
147 
148  case Granulator::DECAY:
149  {
150  Buffer waveform(static_cast<uint32>(sample_rate_));
151 
152  waveform << gen.drawDecay(1.0);
153 
154  Buffer noise = 1.0 + envelope_noise * gen.whiteNoise(1.0);
155 
156  envelope_generator_ = new Generator(
157  sample_rate_,
158  gen.drawDecay(1.0)
159  * (1.0 + envelope_noise * gen.whiteNoise(1.0)));
160  break;
161  }
162 
164  {
165  envelope_generator_ = new Generator(
166  sample_rate_,
167  gen.drawDecay(1.0).getReverse()
168  * (1.0 + envelope_noise * gen.whiteNoise(1.0)));
169  break;
170  }
171  }
172 }
Generator * envelope_generator_
Definition: Granulator.h:125
float64 sample_rate_
Definition: Granulator.h:123
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
#define M_THROW(message)
Definition: Macros.h:108
A Buffer for storing audio samples.
Definition: Buffer.h:60
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
Granulator::Granulator ( const Nsound::Granulator gran)

Definition at line 176 of file Granulator.cc.

177  :
179  envelope_generator_(NULL)
180 {
181  *this = gran;
182 }
Generator * envelope_generator_
Definition: Granulator.h:125
float64 sample_rate_
Definition: Granulator.h:123
Granulator::~Granulator ( )
virtual

Definition at line 186 of file Granulator.cc.

References envelope_generator_.

187 {
188  delete envelope_generator_;
189 }
Generator * envelope_generator_
Definition: Granulator.h:125
Nsound::Granulator::Granulator ( )
protected

Member Function Documentation

Buffer Granulator::generate ( const float64 duration,
const float64 grain_frequency,
const float64 waves_per_grain,
const float64 grains_per_second 
)

Definition at line 193 of file Granulator.cc.

References Nsound::Buffer::add(), envelope_generator_, Nsound::Generator::generate(), and sample_rate_.

Referenced by main(), and my_main().

198 {
199  Sine sin(sample_rate_);
200 
201  float64 time_step = 1.0 / sample_rate_;
202 
203  Buffer y(4096);
204 
205  for(float64 last_g_time = 0.0, current_time = 0.0, next_grain_time = 0.0;
206  current_time < duration;
207  current_time += time_step)
208  {
209  // Output a pulse.
210  if(next_grain_time - current_time < time_step)
211  {
212  float64 g_duration = waves_per_grain / grain_frequency;
213 
214  Buffer grain(4096);
215 
216  grain << sin.generate(g_duration, grain_frequency)
217  * envelope_generator_->generate(g_duration, 1.0 / g_duration);
218 
219  y.add(grain, static_cast<uint32>(current_time * sample_rate_));
220 
221  last_g_time = current_time;
222  }
223 
224  next_grain_time = last_g_time + (1.0 / grains_per_second);
225  }
226 
227  return y;
228 }
Generator * envelope_generator_
Definition: Granulator.h:125
float64 sample_rate_
Definition: Granulator.h:123
double float64
Definition: Nsound.h:146
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
A Buffer for storing audio samples.
Definition: Buffer.h:60
DOXME.
Definition: Sine.h:43
Buffer Granulator::generate ( const float64 duration,
const Nsound::Buffer grain_frequency,
const Nsound::Buffer waves_per_grain,
const Nsound::Buffer grains_per_second 
) const

Definition at line 232 of file Granulator.cc.

References Nsound::Buffer::add(), envelope_generator_, Nsound::Generator::generate(), Nsound::Buffer::getLength(), and sample_rate_.

237 {
238  Sine sin(sample_rate_);
239 
240  uint32 gf_length = grain_frequency.getLength();
241  uint32 wpg_length = waves_per_grain.getLength();
242  uint32 gps_length = grains_per_second.getLength();
243 
244  uint32 gf_index = 0;
245  uint32 wpg_index = 0;
246  uint32 gps_index = 0;
247 
248  float64 time_step = 1.0 / sample_rate_;
249 
250  Buffer y;
251 
252  for(float64 last_g_time = 0.0, current_time = 0.0, next_grain_time = 0.0;
253  current_time < duration;
254  current_time += time_step)
255  {
256 
257  float64 gps =
258  grains_per_second[gps_index++ % gps_length];
259 
260  // Output a pulse.
261  if(next_grain_time - current_time < time_step)
262  {
263  float64 g_frequency = grain_frequency[gf_index++ % gf_length];
264  float64 wpg = waves_per_grain[wpg_index++ % wpg_length];
265 
266  float64 g_duration = wpg / g_frequency;
267 
268  Buffer grain =
269  sin.generate(g_duration,g_frequency)
270  * envelope_generator_->generate(g_duration, 1.0 / g_duration);
271 
272  y.add(grain, static_cast<uint32>(current_time * sample_rate_));
273 
274  last_g_time = current_time;
275  }
276 
277  next_grain_time = last_g_time + (1.0 / gps);
278  }
279 
280  return y;
281 }
unsigned int uint32
Definition: Nsound.h:153
Generator * envelope_generator_
Definition: Granulator.h:125
float64 sample_rate_
Definition: Granulator.h:123
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
A Buffer for storing audio samples.
Definition: Buffer.h:60
void add(const Buffer &buffer, uint32 offset=0, uint32 n_samples=0)
This method adds buffer to *this.
Definition: Buffer.cc:225
DOXME.
Definition: Sine.h:43
Granulator & Granulator::operator= ( const Nsound::Granulator rhs)

Assignment operator.

Definition at line 287 of file Granulator.cc.

References envelope_generator_, and sample_rate_.

288 {
289  delete envelope_generator_;
290 
293 
294  return *this;
295 }
Generator * envelope_generator_
Definition: Granulator.h:125
float64 sample_rate_
Definition: Granulator.h:123
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50

Member Data Documentation

float64 Nsound::Granulator::sample_rate_
protected

Definition at line 123 of file Granulator.h.

Referenced by generate(), Granulator(), and operator=().

Generator* Nsound::Granulator::envelope_generator_
protected

Definition at line 125 of file Granulator.h.

Referenced by generate(), Granulator(), operator=(), and ~Granulator().


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