wtpp/src/generators.cpp

87 lines
1.9 KiB
C++

#include <cmath>
#include "generators.h"
#include "wave.h"
sample_table_t gen_sin()
{
// Make the new Wavetable object
sample_table_t sin_vals = {0};
// Make a place to store calculated value
float_t val = 0;
// Loop through number of samples and set table values
for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) {
// Calculate the value of sin at position
val = std::sin((2 * M_PI * i) / DEFAULT_N_SAMPLES);
sin_vals[i] = val;
}
// Return the completed Wavetable object
return sin_vals;
}
sample_table_t gen_squ()
{
sample_table_t squ_vals = {0};
// Start high and go low at halfway
for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) {
if (i < (DEFAULT_N_SAMPLES / 2)) {
squ_vals[i] = 1;
} else {
squ_vals[i] = -1;
}
}
return squ_vals;
}
sample_table_t gen_saw()
{
sample_table_t saw_vals = {0};
float_t step = 2.0 / DEFAULT_N_SAMPLES; // calculate our incremental step size
float_t val = -1; // place to store calculate value
// Start low go high [0, 1]
for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) {
saw_vals[i] = val;
val += step;
}
return saw_vals;
}
sample_table_t gen_tri()
{
sample_table_t tri_vals = {0};
uint16_t half_samples = DEFAULT_N_SAMPLES / 2;
float_t step = 2.0 / half_samples; // calculate our incremental step size
float_t val = -1; // place to store calculate value
// Start low go high then low [-1, 1, -1]
for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) {
tri_vals[i] = val;
if (i < half_samples) {
val += step;
} else {
val -= step;
}
}
return tri_vals;
}
sample_table_t gen_isaw()
{
sample_table_t isaw_vals = {0};
float_t step = 2.0 / DEFAULT_N_SAMPLES; // calculate our incremental step size
float_t val = 1; // place to store calculate value
// Start high go low [1, 0]
for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) {
isaw_vals[i] = val;
val -= step;
}
return isaw_vals;
}