templatized delayline.
// 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>
+namespace daisysp
+{
+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;
+ }
-typedef struct
+ inline void set_delay(float delay)
{
- uint8_t interp;
- size_t write_ptr, delay, size;
- float delay_frac, delay_sec, sr;
- float *line;
-}dsy_delayline;
+ int32_t int_delay = static_cast<int32_t>(delay);
+ frac_ = delay - static_cast<float>(int_delay);
+ delay_ = int_delay < max_size ? int_int_delay : max_size - 1;
+ }
-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);
+ inline void write(const T sample)
+ {
+ line_[write_ptr_] = sample;
+ write_ptr_ = (write_ptr_ - 1 + max_delay) % max_delay;
+ }
-#ifdef __cplusplus
+ inline const T read() const
+ {
+ T a = line_[(write_ptr_ + delay_) % max_delay];
+ T b = line_[(write_ptr_ + delay_ + 1) % max_delay];
+ return a + (b - a) * frac_;
}
-#endif
+
+ private:
+ float frac_;
+ size_t write_ptr_;
+ size_t delay_;
+ T line[max_size];
+
+};
+} // namespace daisysp
#endif
