forked from electro-smith/DaisyExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request electro-smith#103 from andrewikenberry/delayline_t…
…emplate Delayline template
- Loading branch information
Showing
5 changed files
with
63 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,71 @@ | ||
// Simple Delay Line | ||
// | ||
// November 2019 | ||
// By: shensley | ||
// | ||
// User manages their own buffer in order to provide | ||
// flexibility without dynamic memory. | ||
// Converted to Template December 2019 | ||
// | ||
// By: shensley | ||
// | ||
// TODO: add some sort of type flexibility -- for now its just floats. | ||
#pragma once | ||
#ifndef DSY_DELAY_H | ||
#define DSY_DELAY_H | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct | ||
#include <stdint.h> | ||
namespace daisysp | ||
{ | ||
uint8_t interp; | ||
size_t write_ptr, delay, size; | ||
float delay_frac, delay_sec, sr; | ||
float *line; | ||
}dsy_delayline; | ||
|
||
void dsy_delay_init(dsy_delayline *p, float* buff, size_t buff_size, float samplerate); | ||
void dsy_delay_reset(dsy_delayline *p); | ||
void dsy_delay_set_delay_samps(dsy_delayline *p, size_t size); | ||
void dsy_delay_set_delay_sec(dsy_delayline *p, float sec); | ||
void dsy_delay_line_write(dsy_delayline *p, float val); | ||
float dsy_delay_line_read(dsy_delayline *p); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
template<typename T, size_t max_size> | ||
class delayline | ||
{ | ||
public: | ||
delayline() { } | ||
~delayline() { } | ||
|
||
void init() | ||
{ | ||
reset(); | ||
} | ||
|
||
void reset() { | ||
for (size_t i = 0; i < max_size; i++) | ||
{ | ||
line_[i] = T(0); | ||
} | ||
write_ptr_ = 0; | ||
delay_ = 1; | ||
} | ||
|
||
inline void set_delay(size_t delay) | ||
{ | ||
frac_ = 0.0f; | ||
delay_ = delay < max_size ? delay : max_size - 1; | ||
} | ||
|
||
inline void set_delay(float delay) | ||
{ | ||
int32_t int_delay = static_cast<int32_t>(delay); | ||
frac_ = delay - static_cast<float>(int_delay); | ||
delay_ = int_delay < max_size ? int_delay : max_size - 1; | ||
} | ||
|
||
inline void write(const T sample) | ||
{ | ||
line_[write_ptr_] = sample; | ||
write_ptr_ = (write_ptr_ - 1 + max_size) % max_size; | ||
} | ||
|
||
inline const T read() const | ||
{ | ||
T a = line_[(write_ptr_ + delay_) % max_size]; | ||
T b = line_[(write_ptr_ + delay_ + 1) % max_size]; | ||
return a + (b - a) * frac_; | ||
} | ||
|
||
private: | ||
float frac_; | ||
size_t write_ptr_; | ||
size_t delay_; | ||
T line_[max_size]; | ||
|
||
}; | ||
} // namespace daisysp | ||
#endif |