-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
291 additions
and
42 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
|
||
/**------------------------------------------------------------------------- | ||
* @file wavetable-buffer.h | ||
* @brief WavetableBuffer stores an amplitude envelope from -1..1. | ||
* | ||
*-----------------------------------------------------------------------*/ | ||
|
||
#include "signalflow/buffer/buffer.h" | ||
#include "signalflow/core/constants.h" | ||
#include "signalflow/core/util.h" | ||
|
||
#define SIGNALFLOW_DEFAULT_WAVETABLE_BUFFER_LENGTH 2048 | ||
|
||
namespace signalflow | ||
{ | ||
/**------------------------------------------------------------------------- | ||
* A WavetableBuffer is a mono buffer with a fixed number of samples, | ||
* which can be sampled at a position [0,1] to give a bipolar [-1, 1] | ||
* amplitude value, intended for use with the Wavetable node. | ||
*-----------------------------------------------------------------------*/ | ||
class WavetableBuffer : public Buffer | ||
{ | ||
public: | ||
WavetableBuffer(int length = SIGNALFLOW_DEFAULT_WAVETABLE_BUFFER_LENGTH); | ||
|
||
/**------------------------------------------------------------------------ | ||
* Initialise a buffer with the envelope `shape`. | ||
* | ||
* Supported buffer shapes: | ||
* | ||
* sine: Sine wave | ||
* triangle: Triangle wave | ||
* | ||
* @param name One of the recognised shapes above | ||
* @param num_frames Length of buffer | ||
* | ||
*------------------------------------------------------------------------*/ | ||
WavetableBuffer(std::string shape, int num_frames = SIGNALFLOW_DEFAULT_WAVETABLE_BUFFER_LENGTH); | ||
WavetableBuffer(const std::function<float(float)> f); | ||
WavetableBuffer(std::vector<float> samples); | ||
|
||
/**------------------------------------------------------------------------ | ||
* @param position An envelope position between [0, 1]. | ||
* @return A frame position | ||
*------------------------------------------------------------------------*/ | ||
virtual double offset_to_frame(double offset) override; | ||
virtual double frame_to_offset(double frame) override; | ||
}; | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "signalflow/buffer/wavetable-buffer.h" | ||
|
||
namespace signalflow | ||
{ | ||
|
||
WavetableBuffer::WavetableBuffer(int length) | ||
: Buffer(1, length) | ||
{ | ||
/*------------------------------------------------------------------------- | ||
* Initialise to a flat envelope at maximum amplitude. | ||
*-----------------------------------------------------------------------*/ | ||
this->fill(1.0); | ||
} | ||
|
||
WavetableBuffer::WavetableBuffer(std::string shape, int num_frames) | ||
: WavetableBuffer(num_frames) | ||
{ | ||
if (shape == "sine") | ||
{ | ||
for (int x = 0; x < num_frames; x++) | ||
{ | ||
this->data[0][x] = sinf(2 * M_PI * x / num_frames); | ||
} | ||
} | ||
else if (shape == "triangle") | ||
{ | ||
for (int x = 0; x < num_frames / 2; x++) | ||
this->data[0][x] = (float) 4 * x / num_frames - 1; | ||
for (int x = 0; x < num_frames / 2; x++) | ||
this->data[0][(num_frames / 2) + x] = 1 - 4 * (float) x / (num_frames); | ||
} | ||
else if (shape == "saw") | ||
{ | ||
for (int x = 0; x < num_frames; x++) | ||
this->data[0][x] = 2.0 * x / num_frames - 1.0; | ||
} | ||
else if (shape == "square") | ||
{ | ||
for (int x = 0; x < num_frames; x++) | ||
{ | ||
if (x < num_frames / 2) | ||
this->data[0][x] = -1.0; | ||
else | ||
this->data[0][x] = 1.0; | ||
} | ||
} | ||
else | ||
{ | ||
throw std::runtime_error("Invalid wavetable buffer shape: " + shape); | ||
} | ||
} | ||
|
||
WavetableBuffer::WavetableBuffer(const std::function<float(float)> f) | ||
: Buffer(1, 1024) | ||
{ | ||
this->fill(f); | ||
} | ||
|
||
WavetableBuffer::WavetableBuffer(std::vector<float> samples) | ||
: Buffer(samples) {} | ||
|
||
double WavetableBuffer::offset_to_frame(double offset) | ||
{ | ||
return signalflow_scale_lin_lin(offset, 0, 1, 0, this->num_frames - 1); | ||
} | ||
|
||
double WavetableBuffer::frame_to_offset(double frame) | ||
{ | ||
return signalflow_scale_lin_lin(frame, 0, this->num_frames - 1, 0, 1); | ||
} | ||
|
||
} |
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