Skip to content

Commit 273e9b2

Browse files
authored
Merge pull request #345 from andysheen/dev
Delay is now accessible through AudioParameters
2 parents 6a259cb + 6062bed commit 273e9b2

File tree

2 files changed

+98
-31
lines changed

2 files changed

+98
-31
lines changed

src/AudioEffects/AudioEffect.h

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -282,51 +282,64 @@ class Tremolo : public AudioEffect {
282282
class Delay : public AudioEffect {
283283
public:
284284
/// e.g. depthPercent=50, ms=1000, sampleRate=44100
285-
Delay(uint16_t duration_ms=1000, float depthPercent=50, float feedbackAmount=0.3, uint32_t sampleRate=44100) {
286-
this->sampleRate = sampleRate;
287-
p_feedback = feedbackAmount;
288-
p_percent = depthPercent;
289-
p_ms = duration_ms;
285+
Delay(uint16_t duration_ms=1000, float depthPercent=0.3, float feedbackAmount=0.3, uint32_t sampleRate=44100) {
286+
delayLine = new DelayLine(duration_ms,depthPercent,feedbackAmount,sampleRate);
290287
}
291288

292-
Delay(const Delay &copy) = default;
289+
Delay(const Delay &ref) {
290+
delayLine = new DelayLine(*(ref.delayLine));
291+
copyParent((AudioEffect *)&ref);
292+
};
293293

294-
void setDuration(int16_t ms){
295-
p_ms = ms;
294+
virtual ~Delay(){
295+
delete delayLine;
296296
}
297297

298-
int16_t duration(){
299-
return p_ms;
298+
void setDuration(int16_t m){
299+
delayLine->setDuration(m);
300300
}
301301

302-
void setDepth(float percent){
303-
p_percent = percent;
302+
int16_t getDuration(){
303+
return delayLine->getDuration();
304304
}
305305

306-
uint8_t depth() {
307-
return p_percent;
306+
void setDepth(float p){
307+
delayLine->setDepth(p);
308308
}
309309

310-
void setFeedback(float feedback){
311-
p_feedback = feedback;
310+
float getDepth() {
311+
return delayLine->getDepth();
312312
}
313313

314-
uint8_t feedback() {
315-
return p_feedback;
314+
void setFeedback(float f){
315+
delayLine->setFeedback(f);
316+
}
317+
318+
float getFeedback() {
319+
return delayLine->getFeedback();
320+
}
321+
322+
void setSampleRate(int32_t s){
323+
delayLine->setSampleRate(s);
324+
}
325+
326+
float getSampleRate() {
327+
return delayLine->getSampleRate();
316328
}
317329

318330
effect_t process(effect_t input) {
319331
if (!active()) return input;
320332

333+
delayLine->tick();
321334
updateBufferSize();
322335
// get value from buffer
323336
int32_t value = (p_history->available()<sampleCount) ? input : p_history->read();
324337
// add feedback decay
325-
int32_t delayValue = (value*p_feedback);
338+
int32_t delayValue = (value*delayLine->getFeedback());
326339
// add input and delay to the buffer
327340
p_history->write(input+delayValue);
328341
// mix input with result
329-
return (delayValue * p_percent) + (input * (1.0 - p_percent));
342+
return (delayValue * delayLine->getDepth()) + (input * (1.0 - delayLine->getDepth()));
330343
}
331344

332345
Delay *clone() {
@@ -335,14 +348,11 @@ class Delay : public AudioEffect {
335348

336349
protected:
337350
RingBuffer<effect_t>* p_history=nullptr;
338-
float p_percent;
339-
float p_feedback;
340-
uint16_t p_ms;
351+
DelayLine *delayLine;
341352
uint16_t sampleCount=0;
342-
uint32_t sampleRate;
343353

344354
void updateBufferSize(){
345-
uint16_t newSampleCount = sampleRate * p_ms / 1000;
355+
uint16_t newSampleCount = delayLine->getSampleRate() * delayLine->getDuration() / 1000;
346356
if (newSampleCount!=sampleCount){
347357
if (p_history!=nullptr) delete p_history;
348358
sampleCount = newSampleCount;

src/AudioEffects/AudioParameters.h

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace audio_tools {
55

66
/**
77
* @brief Base class for all parameters
8-
*
8+
*
99
*/
1010
class AbstractParameter {
1111
public:
@@ -36,7 +36,7 @@ class AbstractParameter {
3636

3737
/**
3838
* @brief A constant value
39-
*
39+
*
4040
*/
4141
class Parameter : public AbstractParameter {
4242
public:
@@ -48,7 +48,7 @@ class Parameter : public AbstractParameter {
4848

4949
/**
5050
* @brief Generates ADSR values between 0.0 and 1.0
51-
*
51+
*
5252
*/
5353
class ADSR : public AbstractParameter {
5454
public:
@@ -85,7 +85,7 @@ class ADSR : public AbstractParameter {
8585
float sustainLevel(){
8686
return sustain;
8787
}
88-
88+
8989
void setReleaseRate(float r){
9090
release = r;
9191
}
@@ -167,9 +167,66 @@ class ADSR : public AbstractParameter {
167167

168168
};
169169

170+
/**
171+
* @brief Generates DelayLines
172+
*
173+
*/
174+
class DelayLine : public AbstractParameter {
175+
public:
176+
177+
DelayLine(uint16_t duration_ms=1000, float depthPercent=0.3, float feedbackAmount=0.3, uint32_t sampleRate=44100) {
178+
this->sampleRate = sampleRate;
179+
this->duration = duration_ms;
180+
this->feedback = feedbackAmount;
181+
this->depth = depthPercent;
182+
}
183+
184+
DelayLine(DelayLine &copy) = default;
185+
186+
void setDuration(int16_t dur){
187+
duration = dur;
188+
}
189+
190+
int16_t getDuration(){
191+
return duration;
192+
}
193+
194+
void setDepth(float per){
195+
depth = per;
196+
}
197+
198+
float getDepth() {
199+
return depth;
200+
}
201+
202+
void setFeedback(float feed){
203+
feedback = feed;
204+
}
205+
206+
float getFeedback() {
207+
return feedback;
208+
}
209+
210+
void setSampleRate(int32_t sample){
211+
sampleRate = sample;
212+
}
213+
214+
float getSampleRate() {
215+
return sampleRate;
216+
}
217+
218+
protected:
219+
float depth,feedback,duration,sampleRate;
220+
221+
inline float update( ) {
222+
return 0;
223+
}
224+
};
225+
226+
170227
/**
171228
* @brief Scales a dynamic parameter to the indicated range
172-
*
229+
*
173230
*/
174231
class ScaledParameter : public AbstractParameter {
175232
public:
@@ -190,4 +247,4 @@ class ScaledParameter : public AbstractParameter {
190247
};
191248

192249

193-
}
250+
}

0 commit comments

Comments
 (0)