93 lines
2.0 KiB
C++
93 lines
2.0 KiB
C++
#include <array>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <type_traits>
|
|
|
|
#include <math.h>
|
|
|
|
static const uint32_t DEFAULT_SAMPLE_RATE = 44100; // 44.1 kHz sampling rate
|
|
static const uint8_t DEFAULT_BIT_DEPTH = 12;
|
|
static const uint16_t DEFAULT_N_SAMPLES = 1024;
|
|
|
|
|
|
class Wavetable
|
|
{
|
|
private:
|
|
uint_fast16_t m_id {}; // Unique ID number of wavetable
|
|
std::string m_name {}; // String name of wavetable
|
|
std::array<float_t, DEFAULT_N_SAMPLES> m_table; // table data
|
|
|
|
public:
|
|
Wavetable(uint16_t id, std::string name)
|
|
: m_id{ id }, m_name{ name }, m_table {}
|
|
{
|
|
// Do nothing
|
|
}
|
|
|
|
std::string name()
|
|
{
|
|
std::string id_str = std::to_string(m_id);
|
|
id_str.append(": ");
|
|
id_str.append(m_name);
|
|
return id_str;
|
|
}
|
|
|
|
void set_table(std::array<float_t, DEFAULT_N_SAMPLES> table)
|
|
{
|
|
m_table = table;
|
|
}
|
|
|
|
void print_head()
|
|
{
|
|
for(uint8_t i = 0; i < 10; i++) {
|
|
std::cout << m_table[i] << '\n';
|
|
}
|
|
}
|
|
};
|
|
|
|
/* Logs a message */
|
|
static void log(std::string message)
|
|
{
|
|
std::cout << "wtpp: " << message << '\n';
|
|
}
|
|
|
|
/* Generates basic sine Wavetable object */
|
|
static Wavetable gen_sin_tbl()
|
|
{
|
|
// Make the new Wavetable object
|
|
Wavetable table = {0, "sine"};
|
|
// Make an array to hold our values
|
|
std::array<float_t, DEFAULT_N_SAMPLES> sin_vals {};
|
|
// 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;
|
|
}
|
|
// Store values in new Wavetable.table
|
|
table.set_table(sin_vals);
|
|
|
|
// Return the completed Wavetable object
|
|
return table;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
// Generate wavetables
|
|
log("Generating wavetables");
|
|
|
|
// Generate sine table
|
|
Wavetable sin_tbl = gen_sin_tbl();
|
|
log(sin_tbl.name());
|
|
|
|
log("Printing head of sine table");
|
|
sin_tbl.print_head();
|
|
|
|
return 0;
|
|
}
|