-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add wavetable oscillator #176
Open
brbrr
wants to merge
13
commits into
electro-smith:master
Choose a base branch
from
brbrr:add/wavetable-osc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
19d7ca6
Add wavetable oscillator
brbrr b136a2c
Fix circular dependency in wavetables.cpp
GuilhermeMoreno 5448cb9
Add header guards for WaveTable and WaveTableOsc
GuilhermeMoreno f5fbb1d
Add WaveTables to CMake file
GuilhermeMoreno d520803
Converts Tables struct into a template class
GuilhermeMoreno 259e291
Some tweaks to building and interface
beserge 6af5969
Fix wavetable style
beserge 50358fd
Merge branch 'add/wavetable-osc' into fix/cmake-build
brbrr 15fb142
Merge pull request #2 from GuilhermeMoreno/fix/cmake-build
brbrr bbca23c
Merge pull request #3 from GuilhermeMoreno/feature/wavetable-osc
brbrr 0204dcd
Add wavetables to CMake
beserge e0fb5a0
Merge branch 'add/wavetable-osc' of https://github.com/brbrr/DaisySP …
beserge f02772c
Fix style once more
beserge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,7 +93,8 @@ UTILITY_MODULES = \ | |
dcblock \ | ||
jitter \ | ||
metro \ | ||
port | ||
port \ | ||
wavetables | ||
#delayline | ||
#dsp | ||
#looper | ||
|
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,141 @@ | ||
#pragma once | ||
#ifndef DSY_WAVETABLEOSC_H | ||
#define DSY_WAVETABLEOSC_H | ||
|
||
#include "Utility/wavetables.h" | ||
|
||
|
||
namespace daisysp | ||
{ | ||
/** | ||
* @brief Wave table oscillator | ||
* | ||
* Based on implementation from STK (by Perry R. Cook and Gary P. Scavone, 1995--2021) | ||
*/ | ||
class WavetableOsc | ||
{ | ||
public: | ||
enum Waveform | ||
{ | ||
WAVE_SIN, | ||
WAVE_TRI, | ||
WAVE_SAW, | ||
WAVE_SQUARE, | ||
WAVE_LAST, | ||
}; | ||
|
||
void Init(float sample_rate_) | ||
{ | ||
sample_rate = sample_rate_; | ||
sr_resiprocal = 1 / sample_rate; | ||
SetWaveform(WAVE_SQUARE); | ||
SetFreq(440); | ||
SetAmp(0.7f); | ||
} | ||
|
||
void SetWaveform(const Waveform wf) | ||
{ | ||
switch(wf) | ||
{ | ||
case Waveform::WAVE_SIN: SetWaveTable(&Tables::Sine); break; | ||
case Waveform::WAVE_TRI: SetWaveTable(&Tables::Tri); break; | ||
case Waveform::WAVE_SAW: SetWaveTable(&Tables::Saw); break; | ||
case Waveform::WAVE_SQUARE: SetWaveTable(&Tables::Square); break; | ||
default: SetWaveTable(&Tables::Sine); | ||
} | ||
} | ||
|
||
void SetWaveTable(WaveTable *table) | ||
{ | ||
m_table = table; | ||
table_size = m_table->buff->wt_size; | ||
} | ||
|
||
/** | ||
* @brief Set the data interpolation rate based on a looping frequency. | ||
* | ||
* This function determines the interpolation rate based on the file | ||
* size and the current Stk::sampleRate. The \e frequency value | ||
* corresponds to file cycles per second. The frequency can be | ||
* negative, in which case the loop is read in reverse order. | ||
* | ||
* @param frequency Osc frequency in Hz. | ||
*/ | ||
void SetFreq(float frequency) | ||
{ | ||
auto norm_freq = frequency * sr_resiprocal; | ||
this->SetRate(table_size * norm_freq); | ||
|
||
// update the current wave table selector | ||
m_table->SetTopFreq(norm_freq); | ||
} | ||
|
||
/** | ||
* @brief Add a phase offset in cycles, where 1.0 = fileSize. | ||
* | ||
* | ||
* This function determines a time offset based on the file | ||
* size and the current sample sate. The \e angle value | ||
* is a multiple of file size. | ||
* | ||
* @param angle Phase offset in cycles. | ||
*/ | ||
void PhaseAdd(float angle) | ||
{ | ||
time_ += table_size * angle; | ||
|
||
if(time_ < 0.0f) | ||
time_ += table_size; | ||
else if(time_ >= table_size) | ||
time_ -= table_size; | ||
} | ||
|
||
void SetAmp(const float a) { amp = a; } | ||
|
||
/** | ||
* @brief Compute a sample frame and return the value. | ||
*/ | ||
float Process() | ||
{ | ||
while(time_ < 0.0f) | ||
time_ += table_size; | ||
while(time_ >= table_size) | ||
time_ -= table_size; | ||
|
||
float out = m_table->GetSample(time_); | ||
|
||
time_ += rate_; | ||
|
||
return out * amp; | ||
} | ||
|
||
private: | ||
/** | ||
* @brief Set the data read rate in samples. The rate can be negative. | ||
* | ||
* If the rate value is negative, the data is read in reverse order. | ||
* | ||
* @param rate | ||
*/ | ||
void SetRate(float rate) | ||
{ | ||
rate_ = rate; | ||
|
||
if(std::fmod(rate_, 1.0f) != 0.0f) | ||
interpolate_ = true; | ||
else | ||
interpolate_ = false; | ||
} | ||
|
||
WaveTable *m_table; | ||
float sample_rate; | ||
float sr_resiprocal; | ||
float time_ = 0.f; | ||
bool interpolate_ = false; | ||
float rate_; | ||
float amp = 1; | ||
uint16_t table_size = 1; | ||
}; | ||
|
||
} // namespace daisysp | ||
#endif |
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,27 @@ | ||
#include "wavetables.h" | ||
|
||
// hackish way to make it work for both mcu and computer | ||
#ifndef DSY_SDRAM_BSS | ||
#define DSY_SDRAM_BSS /* emtpy */ | ||
#endif | ||
|
||
namespace daisysp | ||
{ | ||
template <typename T, FFTFunction<T> fft> | ||
bool Tables<T, fft>::generated = false; | ||
|
||
template <typename T, FFTFunction<T> fft> | ||
WaveBuffer DSY_SDRAM_BSS Tables<T, fft>::buffer_pool[40]; | ||
template <typename T, FFTFunction<T> fft> | ||
uint8_t Tables<T, fft>::num_buffers = 0; | ||
|
||
template <typename T, FFTFunction<T> fft> | ||
WaveTable Tables<T, fft>::Sine; | ||
template <typename T, FFTFunction<T> fft> | ||
WaveTable Tables<T, fft>::Square; | ||
template <typename T, FFTFunction<T> fft> | ||
WaveTable Tables<T, fft>::Tri; | ||
template <typename T, FFTFunction<T> fft> | ||
WaveTable Tables<T, fft>::Saw; | ||
|
||
} // namespace daisysp |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These now throw the error(s)
'template<class T, void (* fft_func)(int, T*, T*)> class daisysp::Tables' used without template arguments
and so on.