Skip to content

Commit

Permalink
Merge pull request electro-smith#103 from andrewikenberry/delayline_t…
Browse files Browse the repository at this point in the history
…emplate

Delayline template
  • Loading branch information
stephenhensley authored Dec 3, 2019
2 parents e73e01a + 5d02eb6 commit d910aed
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 85 deletions.
2 changes: 1 addition & 1 deletion daisysp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ adenv \
crossfade \
dcblock \
decimator \
delayline \
line \
nlfilt \
metro \
Expand All @@ -17,6 +16,7 @@ reverbsc \
svf \

# certain modules are only header files:
# delayline
# whitenoise

# TODO: Consider making this work for PCs as well?
Expand Down
2 changes: 1 addition & 1 deletion daisysp/daisysp.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ typedef struct {
#include "modules/crossfade.h"
#include "modules/dcblock.h"
#include "modules/decimator.h"
#include "modules/delayline.h"
#include "modules/line.h"
#include "modules/nlfilt.h"
#include "modules/metro.h"
Expand All @@ -44,6 +43,7 @@ typedef struct {
#ifdef __cplusplus
}

#include "modules/delayline.h"
#include "modules/reverbsc.h"
#include "modules/whitenoise.h"
#include "modules/svf.h"
Expand Down
1 change: 0 additions & 1 deletion daisysp/daisysp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
<ClCompile Include="modules\crossfade.c" />
<ClCompile Include="modules\dcblock.c" />
<ClCompile Include="modules\decimator.cpp" />
<ClCompile Include="modules\delayline.c" />
<ClCompile Include="modules\line.cpp" />
<ClCompile Include="modules\metro.c" />
<ClCompile Include="modules\nlfilt.c" />
Expand Down
56 changes: 0 additions & 56 deletions daisysp/modules/delayline.c

This file was deleted.

87 changes: 61 additions & 26 deletions daisysp/modules/delayline.h
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

0 comments on commit d910aed

Please sign in to comment.