Feat: add sine wavetable generation

Refs: #2
pull/5/head
rkcf 2023-02-25 14:54:33 -06:00
parent 3742e75309
commit 911f16b63c
2 changed files with 40 additions and 5 deletions

View File

@ -7,7 +7,7 @@ CFLAGS = -Wall -std=c++17 $(OLEVEL) -o $(PROJ)
OBJECTS = src/wtpp.cpp
build: clean
build:
$(CC) $(CFLAGS) $(OBJECTS)
clean:

View File

@ -1,19 +1,23 @@
#include <array>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <string>
#include <type_traits>
#include <math.h>
static const std::uint_fast32_t DEFAULT_SAMPLE_RATE = 44100; // 44.1 kHz sampling rate
static const std::uint_fast8_t DEFAULT_BIT_DEPTH = 12;
static const std::uint_fast16_t DEFAULT_N_SAMPLES = 1024;
class Wavetable
{
private:
std::uint_fast16_t m_id {};
std::string m_name {};
std::array<std::uint_fast32_t, 1024> m_table;
std::uint_fast16_t m_id {}; // Unique ID number of wavetable
std::string m_name {}; // String name of wavetable
std::array<std::float_t, DEFAULT_N_SAMPLES> m_table; // table data
public:
Wavetable(std::uint_fast16_t id, std::string name)
@ -29,6 +33,18 @@ public:
id_str.append(m_name);
return id_str;
}
void set_table(std::array<std::float_t, DEFAULT_N_SAMPLES> table)
{
m_table = table;
}
void print_head()
{
for(std::uint_fast8_t i = 0; i < 10; i++) {
std::cout << m_table[i] << '\n';
}
}
};
/* Logs a message */
@ -37,10 +53,26 @@ static void log(std::string message)
std::cout << "wtpp: " << message << '\n';
}
/* Generates basic sine wave table */
/* 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<std::float_t, DEFAULT_N_SAMPLES> sin_vals {};
// Make a place to store calculated value
std::float_t val = 0;
// Loop through number of samples and set table values
for (std::uint_fast16_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;
}
@ -53,5 +85,8 @@ int main()
Wavetable sin_tbl = gen_sin_tbl();
log(sin_tbl.name());
log("Printing head of sine table");
sin_tbl.print_head();
return 0;
}