From c2dd910ed7efbe177e63a6188a5a94f8bf6fdbf8 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 06:16:40 -0600 Subject: [PATCH] Refactor: move wave functions to wave.cpp --- Makefile | 2 +- src/wave.cpp | 31 +++++++++++++++++++++++++++++++ src/wave.h | 19 ++++++------------- src/wtpp.cpp | 25 +------------------------ 4 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 src/wave.cpp diff --git a/Makefile b/Makefile index a37b48e..938246e 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ OLEVEL = -O2 CFLAGS = -Wall -std=c++17 $(OLEVEL) -o $(PROJ) -OBJECTS = src/wtpp.cpp +OBJECTS = src/wtpp.cpp src/wave.cpp src/wavetable.cpp build: $(CC) $(CFLAGS) $(OBJECTS) diff --git a/src/wave.cpp b/src/wave.cpp new file mode 100644 index 0000000..4e1c0d5 --- /dev/null +++ b/src/wave.cpp @@ -0,0 +1,31 @@ +#include + +#include "wave.h" + +void Wave::print_head() { + for (uint8_t i = 0; i < 10; i++) { + std::cout << m_table[i] << '\n'; + } +} + +/* Generates basic sine Wave object */ +Wave gen_sin() { + // Make the new Wavetable object + Wave wave = {0}; + // Make an array to hold our values + std::array 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 + wave.setTable(sin_vals); + + // Return the completed Wavetable object + return wave; +} diff --git a/src/wave.h b/src/wave.h index 8398157..f1f6b93 100644 --- a/src/wave.h +++ b/src/wave.h @@ -3,7 +3,6 @@ #include #include -#include #include @@ -12,24 +11,18 @@ const uint16_t DEFAULT_N_SAMPLES = 1024; /* Single cycle wave object with lookup table */ class Wave { private: - uint16_t m_id{}; // Unique ID number of wave + uint16_t m_id{}; // Unique ID number of wave std::array m_table; // table data public: - Wave(uint16_t id) : m_id{id}, m_table{} { - // Do nothing - } + Wave(uint16_t id) : m_id{id}, m_table{} { /* Do nothing */ } uint16_t id() { return m_id; } - void set_table(std::array table) { - m_table = table; - } + void setTable(std::array table) { m_table = table; } - void print_head() { - for (uint8_t i = 0; i < 10; i++) { - std::cout << m_table[i] << '\n'; - } - } + void print_head(); }; +Wave gen_sin(); + #endif // WAVE_H_ diff --git a/src/wtpp.cpp b/src/wtpp.cpp index 503bd5c..b399154 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -19,36 +19,13 @@ static void log(std::string message) std::cout << "wtpp: " << message << '\n'; } -/* Generates basic sine Wave object */ -static Wave gen_sine() -{ - // Make the new Wavetable object - Wave wave = {0}; - // Make an array to hold our values - std::array 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 - wave.set_table(sin_vals); - - // Return the completed Wavetable object - return wave; -} - int main() { // Generate wavetables log("Generating wavetables"); // Generate sine wave cycle object - Wave sin_wave = gen_sine(); + Wave sin_wave = gen_sin(); log(std::to_string(sin_wave.id())); log("Printing head of sine table");