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

#include <Nsound/EnvelopeAdsr.h>

Public Member Functions

 EnvelopeAdsr (const float64 &sample_rate, const float64 &attack_time, const float64 &delay_time, const float64 &sustain_amplitude, const float64 &release_time)
 Constructor. More...
 
void reset ()
 
void setAttackTime (const float64 &time)
 
void setDelayTime (const float64 &time)
 
void setSustainAmplitude (const float64 &amp)
 
void setReleaseTime (const float64 &time)
 
AudioStream shape (const AudioStream &as)
 Shapes the AudioStream inplace. More...
 
Buffer shape (const Buffer &buf)
 Shapes the Buffer inplace. More...
 
float64 shape (float64 sample, bool key_on)
 Realtime interface. More...
 
bool is_done () const
 

Protected Types

enum  Mode {
  attacking, delaying, sustaining, releasing,
  done
}
 

Protected Attributes

float64 sample_rate_
 
float64 attack_slope_
 
float64 attack_time_
 
float64 delay_slope_
 
float64 delay_time_
 
float64 sustain_amp_
 
float64 release_slope_
 
float64 release_time_
 
float64 scale_
 
Mode mode_
 

Detailed Description

Definition at line 69 of file EnvelopeAdsr.h.

Member Enumeration Documentation

Constructor & Destructor Documentation

EnvelopeAdsr::EnvelopeAdsr ( const float64 sample_rate,
const float64 attack_time,
const float64 delay_time,
const float64 sustain_amplitude,
const float64 release_time 
)

Constructor.

Definition at line 39 of file EnvelopeAdsr.cc.

References M_ASSERT_VALUE, setAttackTime(), setDelayTime(), and setReleaseTime().

45  :
46  sample_rate_(sample_rate),
47  attack_slope_(0.0),
48  attack_time_(attack_time),
49  delay_slope_(0.0),
50  delay_time_(delay_time),
51  sustain_amp_(sustain_amplitude),
52  release_slope_(0.0),
53  release_time_(release_time),
54  scale_(0.0),
56 {
57  M_ASSERT_VALUE(sample_rate, >, 0.0);
58  M_ASSERT_VALUE(attack_time, >=, 0.0);
59  M_ASSERT_VALUE(delay_time, >=, 0.0);
60  M_ASSERT_VALUE(sustain_amplitude, >, 0.0);
61  M_ASSERT_VALUE(sustain_amplitude, <=, 1.0);
62  M_ASSERT_VALUE(release_time, >=, 0.0);
63 
64  M_ASSERT_VALUE(attack_time + delay_time + release_time, >, 0.0);
65 
66  setAttackTime(attack_time);
67  setDelayTime(delay_time);
68  setReleaseTime(release_time);
69 }
void setReleaseTime(const float64 &time)
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
void setAttackTime(const float64 &time)
Definition: EnvelopeAdsr.cc:81
void setDelayTime(const float64 &time)
Definition: EnvelopeAdsr.cc:97

Member Function Documentation

void EnvelopeAdsr::reset ( )

Definition at line 73 of file EnvelopeAdsr.cc.

References attacking, mode_, and scale_.

void EnvelopeAdsr::setAttackTime ( const float64 time)

Definition at line 81 of file EnvelopeAdsr.cc.

References attack_slope_, attack_time_, M_ASSERT_VALUE, and sample_rate_.

Referenced by EnvelopeAdsr().

82 {
83  M_ASSERT_VALUE(time, >=, 0.0);
84 
85  uint32 n_samples = static_cast<uint32>(time * sample_rate_ + 0.5);
86 
87  n_samples -= 1;
88 
89  M_ASSERT_VALUE(n_samples, >, 0);
90 
91  attack_slope_ = 1.0 / static_cast<float64>(n_samples);
92  attack_time_ = time;
93 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
double float64
Definition: Nsound.h:146
void EnvelopeAdsr::setDelayTime ( const float64 time)

Definition at line 97 of file EnvelopeAdsr.cc.

References delay_slope_, delay_time_, M_ASSERT_VALUE, and sample_rate_.

Referenced by EnvelopeAdsr().

98 {
99  M_ASSERT_VALUE(time, >=, 0.0);
100 
101  uint32 n_samples = static_cast<uint32>(time * sample_rate_ + 0.5);
102 
103  n_samples -= 1;
104 
105  M_ASSERT_VALUE(n_samples, >, 0);
106 
107  delay_slope_ = -1.0 / static_cast<float64>(n_samples);
108  delay_time_ = time;
109 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
double float64
Definition: Nsound.h:146
void EnvelopeAdsr::setSustainAmplitude ( const float64 amp)

Definition at line 113 of file EnvelopeAdsr.cc.

References M_ASSERT_VALUE, and sustain_amp_.

114 {
115  M_ASSERT_VALUE(amp, >, 0.0);
116 
117  sustain_amp_ = amp;
118 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
void EnvelopeAdsr::setReleaseTime ( const float64 time)

Definition at line 122 of file EnvelopeAdsr.cc.

References M_ASSERT_VALUE, release_slope_, release_time_, and sample_rate_.

Referenced by EnvelopeAdsr().

123 {
124  M_ASSERT_VALUE(time, >=, 0.0);
125 
126  uint32 n_samples = static_cast<uint32>(time * sample_rate_ + 0.5);
127 
128  n_samples -= 1;
129 
130  M_ASSERT_VALUE(n_samples, >, 0);
131 
132  release_slope_ = -1.0 / static_cast<float64>(n_samples);
133  release_time_ = time;
134 }
unsigned int uint32
Definition: Nsound.h:153
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
double float64
Definition: Nsound.h:146
AudioStream EnvelopeAdsr::shape ( const AudioStream as)

Shapes the AudioStream inplace.

Definition at line 138 of file EnvelopeAdsr.cc.

References Nsound::AudioStream::getNChannels(), and Nsound::AudioStream::getSampleRate().

Referenced by shape().

139 {
140  AudioStream out(as.getSampleRate(), as.getNChannels());
141 
142  for(uint32 i = 0; i < as.getNChannels(); ++i)
143  {
144  out[i] = shape(as[i]);
145  }
146 
147  return out;
148 }
unsigned int uint32
Definition: Nsound.h:153
float64 getSampleRate() const
Returns the sample rate of the stream.
Definition: AudioStream.h:217
uint32 getNChannels(void) const
Returns the number of audio channels in the stream.
Definition: AudioStream.h:212
AudioStream shape(const AudioStream &as)
Shapes the AudioStream inplace.
Buffer EnvelopeAdsr::shape ( const Buffer buf)

Shapes the Buffer inplace.

Definition at line 152 of file EnvelopeAdsr.cc.

References attack_time_, attacking, Nsound::Buffer::begin(), delay_time_, Nsound::Buffer::end(), Nsound::Buffer::getLength(), is_done(), mode_, release_time_, sample_rate_, shape(), and sustaining.

153 {
154  float64 duration = static_cast<float64>(buf.getLength()) / sample_rate_;
155 
156  float64 sustain_time =
157  duration - attack_time_ - delay_time_ - release_time_;
158 
159  if(sustain_time < 0) sustain_time = 0;
160 
161  uint32 sustain_samples = static_cast<uint32>(sustain_time * sample_rate_);
162 
163  mode_ = attacking;
164 
165  Buffer::const_iterator itor = buf.begin();
166  Buffer::const_iterator end = buf.end();
167 
168  Buffer out;
169 
170  // Attack
171 
172  while(mode_ != sustaining && itor != end)
173  {
174  out << shape(*itor, true);
175  ++itor;
176  }
177 
178  // Sustain
179 
180  uint32 count = 0;
181 
182  while(count < sustain_samples && itor != end)
183  {
184  out << shape(*itor, true);
185  ++itor;
186  ++count;
187  }
188 
189  // Relase
190 
191  while(!is_done() && itor != end)
192  {
193  out << shape(*itor, false);
194  ++itor;
195  }
196 
197  return out;
198 }
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
bool is_done() const
Definition: EnvelopeAdsr.h:96
AudioStream shape(const AudioStream &as)
Shapes the AudioStream inplace.
A Buffer for storing audio samples.
Definition: Buffer.h:60
float64 EnvelopeAdsr::shape ( float64  sample,
bool  key_on 
)

Realtime interface.

Definition at line 202 of file EnvelopeAdsr.cc.

References attack_slope_, attacking, delay_slope_, delaying, done, M_ASSERT_VALUE, mode_, release_slope_, releasing, scale_, sustain_amp_, and sustaining.

203 {
204  switch(mode_)
205  {
206  case attacking:
207  {
209  if(scale_ >= 1.0)
210  {
211  scale_ = 0.999;
212  mode_ = delaying;
213  }
214  if(!key_on) mode_ = releasing;
215 
216  break;
217  }
218 
219  case delaying:
220  {
221  scale_ += delay_slope_;
223  if(!key_on) mode_ = releasing;
224 
225  break;
226  }
227 
228  case sustaining:
229  {
231  if(!key_on) mode_ = releasing;
232 
233  break;
234  }
235 
236  case releasing:
237  {
239 
240  if(scale_ <= 0.0)
241  {
242  scale_ = 0.0;
243  mode_ = done;
244  }
245 
246  break;
247  }
248 
249  case done:
250  {
251  scale_ = 0.0;
252  }
253  }
254 
255  M_ASSERT_VALUE(scale_, <, 1.0);
256 
257  return sample * scale_;
258 }
#define M_ASSERT_VALUE(a, op, value)
Definition: Macros.h:76
bool Nsound::EnvelopeAdsr::is_done ( ) const
inline

Definition at line 96 of file EnvelopeAdsr.h.

References done, and mode_.

Referenced by shape().

Member Data Documentation

float64 Nsound::EnvelopeAdsr::sample_rate_
protected

Definition at line 100 of file EnvelopeAdsr.h.

Referenced by setAttackTime(), setDelayTime(), setReleaseTime(), and shape().

float64 Nsound::EnvelopeAdsr::attack_slope_
protected

Definition at line 101 of file EnvelopeAdsr.h.

Referenced by setAttackTime(), and shape().

float64 Nsound::EnvelopeAdsr::attack_time_
protected

Definition at line 102 of file EnvelopeAdsr.h.

Referenced by setAttackTime(), and shape().

float64 Nsound::EnvelopeAdsr::delay_slope_
protected

Definition at line 103 of file EnvelopeAdsr.h.

Referenced by setDelayTime(), and shape().

float64 Nsound::EnvelopeAdsr::delay_time_
protected

Definition at line 104 of file EnvelopeAdsr.h.

Referenced by setDelayTime(), and shape().

float64 Nsound::EnvelopeAdsr::sustain_amp_
protected

Definition at line 105 of file EnvelopeAdsr.h.

Referenced by setSustainAmplitude(), and shape().

float64 Nsound::EnvelopeAdsr::release_slope_
protected

Definition at line 106 of file EnvelopeAdsr.h.

Referenced by setReleaseTime(), and shape().

float64 Nsound::EnvelopeAdsr::release_time_
protected

Definition at line 107 of file EnvelopeAdsr.h.

Referenced by setReleaseTime(), and shape().

float64 Nsound::EnvelopeAdsr::scale_
protected

Definition at line 108 of file EnvelopeAdsr.h.

Referenced by reset(), and shape().

Mode Nsound::EnvelopeAdsr::mode_
protected

Definition at line 112 of file EnvelopeAdsr.h.

Referenced by is_done(), reset(), and shape().


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