From 541af6a70155decfbd0019480de53f68c7d3006c Mon Sep 17 00:00:00 2001 From: rkcf Date: Sat, 25 Feb 2023 14:01:32 -0600 Subject: [PATCH 01/18] Fix: change C++ version in makefile --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 1d7fb1e..1c659d6 100644 --- a/Makefile +++ b/Makefile @@ -3,11 +3,11 @@ CC = /usr/bin/clang++ GDB = /usr/bin/gdb OLEVEL = -O2 -CFLAGS = -Wall -std=c++20 $(OLEVEL) -o $(PROJ) +CFLAGS = -Wall -std=c++17 $(OLEVEL) -o $(PROJ) OBJECTS = src/wtpp.cpp -build: +build: clean $(CC) $(CFLAGS) $(OBJECTS) clean: -- 2.40.1 From 3742e75309dd2ebb24beb8d2a87334df485740be Mon Sep 17 00:00:00 2001 From: rkcf Date: Sat, 25 Feb 2023 14:02:21 -0600 Subject: [PATCH 02/18] Add: skeleton wtpp.cpp with Wavetable class Refs: #2 --- src/wtpp.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/wtpp.cpp diff --git a/src/wtpp.cpp b/src/wtpp.cpp new file mode 100644 index 0000000..53c209d --- /dev/null +++ b/src/wtpp.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +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; + + +class Wavetable +{ +private: + std::uint_fast16_t m_id {}; + std::string m_name {}; + std::array m_table; + +public: + Wavetable(std::uint_fast16_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; + } +}; + +/* Logs a message */ +static void log(std::string message) +{ + std::cout << "wtpp: " << message << '\n'; +} + +/* Generates basic sine wave table */ +static Wavetable gen_sin_tbl() +{ + Wavetable table = {0, "sine"}; + return table; +} + +int main() +{ + // Generate wavetables + log("Generating wavetables"); + + // Generate sine table + Wavetable sin_tbl = gen_sin_tbl(); + log(sin_tbl.name()); + + return 0; +} -- 2.40.1 From 911f16b63c7e90b32ed740daa1530e09c8520a29 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sat, 25 Feb 2023 14:54:33 -0600 Subject: [PATCH 03/18] Feat: add sine wavetable generation Refs: #2 --- Makefile | 2 +- src/wtpp.cpp | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 1c659d6..a37b48e 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ CFLAGS = -Wall -std=c++17 $(OLEVEL) -o $(PROJ) OBJECTS = src/wtpp.cpp -build: clean +build: $(CC) $(CFLAGS) $(OBJECTS) clean: diff --git a/src/wtpp.cpp b/src/wtpp.cpp index 53c209d..3c73dfb 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -1,19 +1,23 @@ #include +#include #include #include #include #include +#include + 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 m_table; + std::uint_fast16_t m_id {}; // Unique ID number of wavetable + std::string m_name {}; // String name of wavetable + std::array 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 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 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; } -- 2.40.1 From 3cc6970596d943bafbb05aca613c29388b42c7ca Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 03:53:50 -0600 Subject: [PATCH 04/18] Refactor: simplify number typedefs --- src/wtpp.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/wtpp.cpp b/src/wtpp.cpp index 3c73dfb..c17efbd 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -7,20 +7,20 @@ #include -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; +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: - std::uint_fast16_t m_id {}; // Unique ID number of wavetable + uint_fast16_t m_id {}; // Unique ID number of wavetable std::string m_name {}; // String name of wavetable - std::array m_table; // table data + std::array m_table; // table data public: - Wavetable(std::uint_fast16_t id, std::string name) + Wavetable(uint16_t id, std::string name) : m_id{ id }, m_name{ name }, m_table {} { // Do nothing @@ -34,14 +34,14 @@ public: return id_str; } - void set_table(std::array table) + void set_table(std::array table) { m_table = table; } void print_head() { - for(std::uint_fast8_t i = 0; i < 10; i++) { + for(uint8_t i = 0; i < 10; i++) { std::cout << m_table[i] << '\n'; } } @@ -59,12 +59,12 @@ static Wavetable gen_sin_tbl() // Make the new Wavetable object Wavetable table = {0, "sine"}; // Make an array to hold our values - std::array sin_vals {}; + std::array sin_vals {}; // Make a place to store calculated value - std::float_t val = 0; + 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++) { + 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; -- 2.40.1 From 4a274f461838a0d1bb7d62c4fbe16236ed9c9ffb Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 04:27:46 -0600 Subject: [PATCH 05/18] Feat: add Wave class for single cycle waveform and make Wavetable proper Refs: #2 --- src/wtpp.cpp | 62 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/src/wtpp.cpp b/src/wtpp.cpp index c17efbd..379f290 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -11,21 +11,42 @@ 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 +/* Single cycle wave object with lookup table */ +class Wave { private: - uint_fast16_t m_id {}; // Unique ID number of wavetable - std::string m_name {}; // String name of wavetable + uint16_t m_id{}; // Unique ID number of wave std::array m_table; // table data - public: - Wavetable(uint16_t id, std::string name) - : m_id{ id }, m_name{ name }, m_table {} + 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 print_head() { + for (uint8_t i = 0; i < 10; i++) { + std::cout << m_table[i] << '\n'; + } + } +}; + +/* Table of single cycle wave lookup tables */ +class Wavetable +{ +private: + uint16_t m_id {}; // Unique ID number of wavetable + std::string m_name {}; // String name of wavetable + std::array m_table; // table data + +public: std::string name() { std::string id_str = std::to_string(m_id); @@ -34,17 +55,10 @@ public: return id_str; } - void set_table(std::array table) + void set_table(std::array table) { m_table = table; } - - void print_head() - { - for(uint8_t i = 0; i < 10; i++) { - std::cout << m_table[i] << '\n'; - } - } }; /* Logs a message */ @@ -53,11 +67,11 @@ static void log(std::string message) std::cout << "wtpp: " << message << '\n'; } -/* Generates basic sine Wavetable object */ -static Wavetable gen_sin_tbl() +/* Generates basic sine Wave object */ +static Wave gen_sine() { // Make the new Wavetable object - Wavetable table = {0, "sine"}; + Wave wave = {0}; // Make an array to hold our values std::array sin_vals {}; // Make a place to store calculated value @@ -70,10 +84,10 @@ static Wavetable gen_sin_tbl() sin_vals[i] = val; } // Store values in new Wavetable.table - table.set_table(sin_vals); + wave.set_table(sin_vals); // Return the completed Wavetable object - return table; + return wave; } int main() @@ -81,12 +95,12 @@ int main() // Generate wavetables log("Generating wavetables"); - // Generate sine table - Wavetable sin_tbl = gen_sin_tbl(); - log(sin_tbl.name()); + // Generate sine wave cycle object + Wave sin_wave = gen_sine(); + log(std::to_string(sin_wave.id())); log("Printing head of sine table"); - sin_tbl.print_head(); + sin_wave.print_head(); return 0; } -- 2.40.1 From e2736eb2769e43482d7e35d0d2e72772199cb13c Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 05:08:31 -0600 Subject: [PATCH 06/18] Refactor: move Wave class to own header Refs: #2 --- src/wave.h | 35 +++++++++++++++++++++++++++++++++++ src/wtpp.cpp | 30 ++---------------------------- 2 files changed, 37 insertions(+), 28 deletions(-) create mode 100644 src/wave.h diff --git a/src/wave.h b/src/wave.h new file mode 100644 index 0000000..8398157 --- /dev/null +++ b/src/wave.h @@ -0,0 +1,35 @@ +#ifndef WAVE_H_ +#define WAVE_H_ + +#include +#include +#include + +#include + +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 + std::array m_table; // table data +public: + 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 print_head() { + for (uint8_t i = 0; i < 10; i++) { + std::cout << m_table[i] << '\n'; + } + } +}; + +#endif // WAVE_H_ diff --git a/src/wtpp.cpp b/src/wtpp.cpp index 379f290..94bbdf7 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -7,36 +7,10 @@ #include +#include "wave.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; - -/* Single cycle wave object with lookup table */ -class Wave -{ -private: - 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 - } - - uint16_t id() { - return m_id; - } - - void set_table(std::array table) { - m_table = table; - } - - void print_head() { - for (uint8_t i = 0; i < 10; i++) { - std::cout << m_table[i] << '\n'; - } - } -}; /* Table of single cycle wave lookup tables */ class Wavetable -- 2.40.1 From 145677f0f26787ed4c0822de4809f3f009dcdad6 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 05:12:15 -0600 Subject: [PATCH 07/18] Refactor: move Wavetable class to own header --- src/wavetable.h | 28 ++++++++++++++++++++++++++++ src/wtpp.cpp | 24 +----------------------- 2 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 src/wavetable.h diff --git a/src/wavetable.h b/src/wavetable.h new file mode 100644 index 0000000..d3490f6 --- /dev/null +++ b/src/wavetable.h @@ -0,0 +1,28 @@ +#ifndef WAVETABLE_H_ +#define WAVETABLE_H_ + +#include +#include +#include + +#include "wave.h" + +/* Table of single cycle wave lookup tables */ +class Wavetable { +private: + uint16_t m_id{}; // Unique ID number of wavetable + std::string m_name{}; // String name of wavetable + std::array m_table; // table data + +public: + 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 table) { m_table = table; } +}; + +#endif // WAVETABLE_H_ diff --git a/src/wtpp.cpp b/src/wtpp.cpp index 94bbdf7..503bd5c 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -8,33 +8,11 @@ #include #include "wave.h" +#include "wavetable.h" static const uint32_t DEFAULT_SAMPLE_RATE = 44100; // 44.1 kHz sampling rate static const uint8_t DEFAULT_BIT_DEPTH = 12; -/* Table of single cycle wave lookup tables */ -class Wavetable -{ -private: - uint16_t m_id {}; // Unique ID number of wavetable - std::string m_name {}; // String name of wavetable - std::array m_table; // table data - -public: - 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 table) - { - m_table = table; - } -}; - /* Logs a message */ static void log(std::string message) { -- 2.40.1 From 6abdf8900928e81ec72a296316ded3567669d7e4 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 05:49:13 -0600 Subject: [PATCH 08/18] Refactor: breakout function definition to cpp file --- src/wavetable.cpp | 8 ++++++++ src/wavetable.h | 9 ++------- 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 src/wavetable.cpp diff --git a/src/wavetable.cpp b/src/wavetable.cpp new file mode 100644 index 0000000..a52393d --- /dev/null +++ b/src/wavetable.cpp @@ -0,0 +1,8 @@ +#include "wavetable.h" + +std::string Wavetable::name() { + std::string id_str = std::to_string(m_id); + id_str.append(": "); + id_str.append(m_name); + return id_str; +} diff --git a/src/wavetable.h b/src/wavetable.h index d3490f6..2e75ba9 100644 --- a/src/wavetable.h +++ b/src/wavetable.h @@ -9,18 +9,13 @@ /* Table of single cycle wave lookup tables */ class Wavetable { -private: + private: uint16_t m_id{}; // Unique ID number of wavetable std::string m_name{}; // String name of wavetable std::array m_table; // table data public: - std::string name() { - std::string id_str = std::to_string(m_id); - id_str.append(": "); - id_str.append(m_name); - return id_str; - } + std::string name(); void set_table(std::array table) { m_table = table; } }; -- 2.40.1 From c2dd910ed7efbe177e63a6188a5a94f8bf6fdbf8 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 06:16:40 -0600 Subject: [PATCH 09/18] 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"); -- 2.40.1 From 0c15bd2f721464d3de7758225eeb5bde5e084569 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 07:22:25 -0600 Subject: [PATCH 10/18] Docs: add doxygen compatable documentation to files --- src/wave.h | 24 ++++++++++++++++++++---- src/wavetable.h | 27 +++++++++++++++++++++------ src/wtpp.cpp | 4 +++- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/wave.h b/src/wave.h index f1f6b93..d6d75a1 100644 --- a/src/wave.h +++ b/src/wave.h @@ -1,3 +1,6 @@ +/*! @file wave.h + @brief Header for Wave class and associated Wave functions + */ #ifndef WAVE_H_ #define WAVE_H_ @@ -8,21 +11,34 @@ const uint16_t DEFAULT_N_SAMPLES = 1024; -/* Single cycle wave object with lookup table */ +using sample_table_t = std::array; + +/*! @class Wave wave.h "wave.h" + @brief Single cycle wave object with lookup table + */ class Wave { private: - uint16_t m_id{}; // Unique ID number of wave - std::array m_table; // table data + uint16_t m_id{}; /*!< Unique ID number of wave */ + sample_table_t m_table; /*!< Table of sample data */ + public: Wave(uint16_t id) : m_id{id}, m_table{} { /* Do nothing */ } + /*! ID getter */ uint16_t id() { return m_id; } - void setTable(std::array table) { m_table = table; } + /*! Sample data table setter + @param table Sample data table + */ + void setTable(sample_table_t table) { m_table = table; } + /*! Prints the first 10 values of sample data */ void print_head(); }; +/*! Generate sine wave data + @return a table of values + */ Wave gen_sin(); #endif // WAVE_H_ diff --git a/src/wavetable.h b/src/wavetable.h index 2e75ba9..f44cdc9 100644 --- a/src/wavetable.h +++ b/src/wavetable.h @@ -1,3 +1,6 @@ +/*! @file wavetable.h + @brief Header for Wavetable class and associated table functions + */ #ifndef WAVETABLE_H_ #define WAVETABLE_H_ @@ -7,17 +10,29 @@ #include "wave.h" -/* Table of single cycle wave lookup tables */ +const uint8_t DEFAULT_N_WAVES = 256; + +using wave_table_t = std::array; + +/*! @class Wavetable wavetable.h "wavetable.h" + @brief Object containing a table of single cycle Wave objects + */ class Wavetable { - private: - uint16_t m_id{}; // Unique ID number of wavetable - std::string m_name{}; // String name of wavetable - std::array m_table; // table data +private: + uint16_t m_id{}; /*!< Unique ID number of wavetable */ + std::string m_name{}; /*!< String name of wavetable */ + wave_table_t m_table; /*!< Table of Wave objects */ public: + /*! Makes formatted name and id string + @return a formatted string in the form "id: name" + */ std::string name(); - void set_table(std::array table) { m_table = table; } + /*! setter for wave table + @param table Table of Wave objects + */ + void set_table(wave_table_t table) { m_table = table; } }; #endif // WAVETABLE_H_ diff --git a/src/wtpp.cpp b/src/wtpp.cpp index b399154..bb72a54 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -13,7 +13,9 @@ static const uint32_t DEFAULT_SAMPLE_RATE = 44100; // 44.1 kHz sampling rate static const uint8_t DEFAULT_BIT_DEPTH = 12; -/* Logs a message */ +/*! Logs a message + @param message The string to be logged + */ static void log(std::string message) { std::cout << "wtpp: " << message << '\n'; -- 2.40.1 From 7cea04234f2bff9968097a66712430d4b28f36d9 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 09:11:20 -0600 Subject: [PATCH 11/18] Refactor: rewrite classes and fix construction --- src/wave.cpp | 15 ++++++--------- src/wave.h | 9 +++++---- src/wavetable.cpp | 5 ++++- src/wavetable.h | 25 ++++++++++++++++--------- src/wtpp.cpp | 17 +++++++++++++---- 5 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/wave.cpp b/src/wave.cpp index 4e1c0d5..43cbb5c 100644 --- a/src/wave.cpp +++ b/src/wave.cpp @@ -2,18 +2,18 @@ #include "wave.h" +// Initialize index at 0 for wave cycles and wave tables +uint16_t Wave::m_nid = 0; + 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() { +sample_table_t gen_sin() { // Make the new Wavetable object - Wave wave = {0}; - // Make an array to hold our values - std::array sin_vals{}; + sample_table_t sin_vals = {0}; // Make a place to store calculated value float_t val = 0; @@ -23,9 +23,6 @@ Wave gen_sin() { 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; + return sin_vals; } diff --git a/src/wave.h b/src/wave.h index d6d75a1..2198862 100644 --- a/src/wave.h +++ b/src/wave.h @@ -18,14 +18,15 @@ using sample_table_t = std::array; */ class Wave { private: - uint16_t m_id{}; /*!< Unique ID number of wave */ + static uint16_t m_nid; /*!< Unique ID number of NEXT wave */ sample_table_t m_table; /*!< Table of sample data */ public: - Wave(uint16_t id) : m_id{id}, m_table{} { /* Do nothing */ } + const uint16_t m_cid; /*!< Unique ID of current wavetable */ + Wave() : m_cid(m_nid++) {} /*! ID getter */ - uint16_t id() { return m_id; } + uint16_t getId() { return m_cid; } /*! Sample data table setter @param table Sample data table @@ -39,6 +40,6 @@ public: /*! Generate sine wave data @return a table of values */ -Wave gen_sin(); +sample_table_t gen_sin(); #endif // WAVE_H_ diff --git a/src/wavetable.cpp b/src/wavetable.cpp index a52393d..b59149d 100644 --- a/src/wavetable.cpp +++ b/src/wavetable.cpp @@ -1,7 +1,10 @@ #include "wavetable.h" +// Initialize index at 0 for wave cycles and wave tables +uint16_t Wavetable::m_nid = 0; + std::string Wavetable::name() { - std::string id_str = std::to_string(m_id); + std::string id_str = std::to_string(m_cid); id_str.append(": "); id_str.append(m_name); return id_str; diff --git a/src/wavetable.h b/src/wavetable.h index f44cdc9..4e48506 100644 --- a/src/wavetable.h +++ b/src/wavetable.h @@ -10,7 +10,8 @@ #include "wave.h" -const uint8_t DEFAULT_N_WAVES = 256; +/* Number of Wave cycle objects in table */ +const uint16_t DEFAULT_N_WAVES = 256; using wave_table_t = std::array; @@ -19,20 +20,26 @@ using wave_table_t = std::array; */ class Wavetable { private: - uint16_t m_id{}; /*!< Unique ID number of wavetable */ - std::string m_name{}; /*!< String name of wavetable */ - wave_table_t m_table; /*!< Table of Wave objects */ + static uint16_t m_nid; /*!< Unique ID number of wavetable */ + std::string m_name; /*!< String name of wavetable */ + wave_table_t m_wave_table; /*!< Table of Wave objects */ public: + const uint16_t m_cid; /*!< Unique ID of NEXT wavetable */ + Wavetable(std::string name) : m_cid(m_nid++) + { + m_name = name; + /* Initialize table of Waves */ + wave_table_t wt; + } + + /*! ID getter */ + uint16_t getId() { return m_cid; } + /*! Makes formatted name and id string @return a formatted string in the form "id: name" */ std::string name(); - - /*! setter for wave table - @param table Table of Wave objects - */ - void set_table(wave_table_t table) { m_table = table; } }; #endif // WAVETABLE_H_ diff --git a/src/wtpp.cpp b/src/wtpp.cpp index bb72a54..be4cfc3 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -23,12 +23,21 @@ static void log(std::string message) int main() { - // Generate wavetables - log("Generating wavetables"); + // Generate basic wave lookup tables + log("Generating wave cycle lookup tables"); // Generate sine wave cycle object - Wave sin_wave = gen_sin(); - log(std::to_string(sin_wave.id())); + Wave sin_wave; + + // Calculate sine values + sample_table_t sin_vals = gen_sin(); + + // Set wave lookup table values + sin_wave.setTable(sin_vals); + + // Create Wavetable object + Wavetable wt_01{"Basic Sine"}; + log(wt_01.name()); log("Printing head of sine table"); sin_wave.print_head(); -- 2.40.1 From 6d40c3876c5bcd35101cd2478ebf113ff8ad1a99 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 16:02:15 -0600 Subject: [PATCH 12/18] Refactor: move gen_sin to new generators file --- Makefile | 2 +- src/generators.cpp | 17 +++++++++++++++++ src/generators.h | 14 ++++++++++++++ src/wave.cpp | 16 ---------------- src/wave.h | 5 ----- src/wtpp.cpp | 1 + 6 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 src/generators.cpp create mode 100644 src/generators.h diff --git a/Makefile b/Makefile index 938246e..7b57192 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ OLEVEL = -O2 CFLAGS = -Wall -std=c++17 $(OLEVEL) -o $(PROJ) -OBJECTS = src/wtpp.cpp src/wave.cpp src/wavetable.cpp +OBJECTS = src/wtpp.cpp src/wave.cpp src/wavetable.cpp src/generators.cpp build: $(CC) $(CFLAGS) $(OBJECTS) diff --git a/src/generators.cpp b/src/generators.cpp new file mode 100644 index 0000000..d226edd --- /dev/null +++ b/src/generators.cpp @@ -0,0 +1,17 @@ +#include "generators.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; +} diff --git a/src/generators.h b/src/generators.h new file mode 100644 index 0000000..d1fb5d1 --- /dev/null +++ b/src/generators.h @@ -0,0 +1,14 @@ +/*! @file generators.h + @brief Header for wave cycle data generators + */ +#ifndef GENERATORS_H_ +#define GENERATORS_H_ + +#include "wave.h" + +/*! Generate sine wave data + @return a table of values + */ +sample_table_t gen_sin(); + +#endif // GENERATORS_H_ diff --git a/src/wave.cpp b/src/wave.cpp index 43cbb5c..642e59c 100644 --- a/src/wave.cpp +++ b/src/wave.cpp @@ -10,19 +10,3 @@ void Wave::print_head() { std::cout << m_table[i] << '\n'; } } - -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; -} diff --git a/src/wave.h b/src/wave.h index 2198862..1c14139 100644 --- a/src/wave.h +++ b/src/wave.h @@ -37,9 +37,4 @@ public: void print_head(); }; -/*! Generate sine wave data - @return a table of values - */ -sample_table_t gen_sin(); - #endif // WAVE_H_ diff --git a/src/wtpp.cpp b/src/wtpp.cpp index be4cfc3..cb8df51 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -8,6 +8,7 @@ #include #include "wave.h" +#include "generators.h" #include "wavetable.h" static const uint32_t DEFAULT_SAMPLE_RATE = 44100; // 44.1 kHz sampling rate -- 2.40.1 From 6b36df77e162f7048cfb70f56b44431ff68be295 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 16:13:25 -0600 Subject: [PATCH 13/18] Fix: missing header includes and add generator declarations --- src/generators.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/generators.h b/src/generators.h index d1fb5d1..08220e0 100644 --- a/src/generators.h +++ b/src/generators.h @@ -4,6 +4,9 @@ #ifndef GENERATORS_H_ #define GENERATORS_H_ +#include "math.h" +#include + #include "wave.h" /*! Generate sine wave data @@ -11,4 +14,29 @@ */ sample_table_t gen_sin(); +/*! Generate square wave data + @return a table of values + */ +sample_table_t gen_squ(); + +/*! Generate triangle wave data + @return a table of values + */ +sample_table_t gen_tri(); + +/*! Generate triangle wave data + @return a table of values + */ +sample_table_t gen_tri(); + +/*! Generate saw wave data + @return a table of values + */ +sample_table_t gen_saw(); + +/*! Generate ramp wave data + @return a table of values + */ +sample_table_t gen_ramp(); + #endif // GENERATORS_H_ -- 2.40.1 From 32f14458625b4cbcdf24ed68209a5e8a500cd940 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 16:28:16 -0600 Subject: [PATCH 14/18] Feat: add square wave lookup table generation Refs: #2 --- src/generators.cpp | 20 +++++++++++++++++++- src/wave.cpp | 4 +--- src/wave.h | 3 ++- src/wtpp.cpp | 19 ++++++++++--------- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/generators.cpp b/src/generators.cpp index d226edd..393dd84 100644 --- a/src/generators.cpp +++ b/src/generators.cpp @@ -1,6 +1,8 @@ #include "generators.h" +#include "wave.h" -sample_table_t gen_sin() { +sample_table_t gen_sin() +{ // Make the new Wavetable object sample_table_t sin_vals = {0}; // Make a place to store calculated value @@ -15,3 +17,19 @@ sample_table_t gen_sin() { // 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; +} diff --git a/src/wave.cpp b/src/wave.cpp index 642e59c..f0e5938 100644 --- a/src/wave.cpp +++ b/src/wave.cpp @@ -1,12 +1,10 @@ -#include - #include "wave.h" // Initialize index at 0 for wave cycles and wave tables uint16_t Wave::m_nid = 0; void Wave::print_head() { - for (uint8_t i = 0; i < 10; i++) { + for (uint8_t i = 0; i < 3; i++) { std::cout << m_table[i] << '\n'; } } diff --git a/src/wave.h b/src/wave.h index 1c14139..de12395 100644 --- a/src/wave.h +++ b/src/wave.h @@ -4,6 +4,7 @@ #ifndef WAVE_H_ #define WAVE_H_ +#include #include #include @@ -33,7 +34,7 @@ public: */ void setTable(sample_table_t table) { m_table = table; } - /*! Prints the first 10 values of sample data */ + /*! Prints the first 3 values of sample data */ void print_head(); }; diff --git a/src/wtpp.cpp b/src/wtpp.cpp index cb8df51..f439150 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -24,24 +24,25 @@ static void log(std::string message) int main() { - // Generate basic wave lookup tables log("Generating wave cycle lookup tables"); + Wave sin_wave, squ_wave; - // Generate sine wave cycle object - Wave sin_wave; - - // Calculate sine values + // Calculate fundamental wave values sample_table_t sin_vals = gen_sin(); + sample_table_t squ_vals = gen_squ(); // Set wave lookup table values sin_wave.setTable(sin_vals); - - // Create Wavetable object - Wavetable wt_01{"Basic Sine"}; - log(wt_01.name()); + squ_wave.setTable(squ_vals); log("Printing head of sine table"); sin_wave.print_head(); + log("Printing head of square table"); + squ_wave.print_head(); + + // Create Wavetable object containing fundamental Wave objects + Wavetable wt_01{"Fundamental Waves"}; + log(wt_01.name()); return 0; } -- 2.40.1 From 6a2f2911e7ca3e3655dbe419482ecd95f2ed317c Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 16:38:53 -0600 Subject: [PATCH 15/18] Feat: add saw wave lookup table generation Refs: #2 --- src/generators.cpp | 15 +++++++++++++++ src/generators.h | 19 +++++++------------ src/wtpp.cpp | 6 +++++- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/generators.cpp b/src/generators.cpp index 393dd84..a377710 100644 --- a/src/generators.cpp +++ b/src/generators.cpp @@ -33,3 +33,18 @@ sample_table_t gen_squ() 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 = 0; // place to store calculate value + + // Start low go high [-1, 1] + for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) { + saw_vals[i] = val; + val += step; + } + + return saw_vals; +} diff --git a/src/generators.h b/src/generators.h index 08220e0..9e84223 100644 --- a/src/generators.h +++ b/src/generators.h @@ -19,24 +19,19 @@ sample_table_t gen_sin(); */ sample_table_t gen_squ(); -/*! Generate triangle wave data - @return a table of values - */ -sample_table_t gen_tri(); - -/*! Generate triangle wave data - @return a table of values - */ -sample_table_t gen_tri(); - /*! Generate saw wave data @return a table of values */ sample_table_t gen_saw(); -/*! Generate ramp wave data +/*! Generate inverse saw wave data @return a table of values */ -sample_table_t gen_ramp(); +sample_table_t gen_isaw(); + +/*! Generate triangle wave data + @return a table of values + */ +sample_table_t gen_tri(); #endif // GENERATORS_H_ diff --git a/src/wtpp.cpp b/src/wtpp.cpp index f439150..f06458c 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -25,20 +25,24 @@ static void log(std::string message) int main() { log("Generating wave cycle lookup tables"); - Wave sin_wave, squ_wave; + Wave sin_wave, squ_wave, saw_wave; // Calculate fundamental wave values sample_table_t sin_vals = gen_sin(); sample_table_t squ_vals = gen_squ(); + sample_table_t saw_vals = gen_saw(); // Set wave lookup table values sin_wave.setTable(sin_vals); squ_wave.setTable(squ_vals); + saw_wave.setTable(saw_vals); log("Printing head of sine table"); sin_wave.print_head(); log("Printing head of square table"); squ_wave.print_head(); + log("Printing head of saw table"); + saw_wave.print_head(); // Create Wavetable object containing fundamental Wave objects Wavetable wt_01{"Fundamental Waves"}; -- 2.40.1 From e886b4cac2ae6c15b511f54447f168d9228334d0 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 16:45:02 -0600 Subject: [PATCH 16/18] Feat: add inverse saw lookup table generation Refs: #2 --- src/generators.cpp | 18 ++++++++++++++++-- src/wtpp.cpp | 6 +++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/generators.cpp b/src/generators.cpp index a377710..3e91586 100644 --- a/src/generators.cpp +++ b/src/generators.cpp @@ -27,7 +27,7 @@ sample_table_t gen_squ() if (i < (DEFAULT_N_SAMPLES / 2)) { squ_vals[i] = 1; } else { - squ_vals[i] = -1; + squ_vals[i] = 0; } } @@ -40,7 +40,7 @@ sample_table_t gen_saw() float_t step = 2.0 / DEFAULT_N_SAMPLES; // calculate our incremental step size float_t val = 0; // place to store calculate value - // Start low go high [-1, 1] + // Start low go high [0, 1] for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) { saw_vals[i] = val; val += step; @@ -48,3 +48,17 @@ sample_table_t gen_saw() return saw_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; +} diff --git a/src/wtpp.cpp b/src/wtpp.cpp index f06458c..547e57e 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -25,17 +25,19 @@ static void log(std::string message) int main() { log("Generating wave cycle lookup tables"); - Wave sin_wave, squ_wave, saw_wave; + Wave sin_wave, squ_wave, saw_wave, isaw_wave; // Calculate fundamental wave values sample_table_t sin_vals = gen_sin(); sample_table_t squ_vals = gen_squ(); sample_table_t saw_vals = gen_saw(); + sample_table_t isaw_vals = gen_isaw(); // Set wave lookup table values sin_wave.setTable(sin_vals); squ_wave.setTable(squ_vals); saw_wave.setTable(saw_vals); + isaw_wave.setTable(isaw_vals); log("Printing head of sine table"); sin_wave.print_head(); @@ -43,6 +45,8 @@ int main() squ_wave.print_head(); log("Printing head of saw table"); saw_wave.print_head(); + log("Printing head of inverse saw table"); + isaw_wave.print_head(); // Create Wavetable object containing fundamental Wave objects Wavetable wt_01{"Fundamental Waves"}; -- 2.40.1 From 916f874a8ff7207c3b4d8892f119378e18bf3cd7 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 16:55:55 -0600 Subject: [PATCH 17/18] Feat: add triangle lookup table generator Refs: #2 --- src/generators.cpp | 23 ++++++++++++++++++++++- src/wtpp.cpp | 6 +++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/generators.cpp b/src/generators.cpp index 3e91586..4933980 100644 --- a/src/generators.cpp +++ b/src/generators.cpp @@ -49,7 +49,28 @@ sample_table_t gen_saw() return saw_vals; } -sample_table_t gen_isaw() { +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 = 0; // place to store calculate value + + // Start low go high then low [0, 1, 0] + 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 diff --git a/src/wtpp.cpp b/src/wtpp.cpp index 547e57e..e6150ab 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -25,18 +25,20 @@ static void log(std::string message) int main() { log("Generating wave cycle lookup tables"); - Wave sin_wave, squ_wave, saw_wave, isaw_wave; + Wave sin_wave, squ_wave, saw_wave, tri_wave, isaw_wave; // Calculate fundamental wave values sample_table_t sin_vals = gen_sin(); sample_table_t squ_vals = gen_squ(); sample_table_t saw_vals = gen_saw(); + sample_table_t tri_vals = gen_tri(); sample_table_t isaw_vals = gen_isaw(); // Set wave lookup table values sin_wave.setTable(sin_vals); squ_wave.setTable(squ_vals); saw_wave.setTable(saw_vals); + tri_wave.setTable(tri_vals); isaw_wave.setTable(isaw_vals); log("Printing head of sine table"); @@ -45,6 +47,8 @@ int main() squ_wave.print_head(); log("Printing head of saw table"); saw_wave.print_head(); + log("Printing head of triangle table"); + tri_wave.print_head(); log("Printing head of inverse saw table"); isaw_wave.print_head(); -- 2.40.1 From 23cfca8ccf1b8cf344cee3b2dd19661e106b7e68 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 17:48:42 -0600 Subject: [PATCH 18/18] Fix: Wavetable wave_table initialization --- src/wavetable.h | 8 ++------ src/wtpp.cpp | 5 +++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/wavetable.h b/src/wavetable.h index 4e48506..6b462bf 100644 --- a/src/wavetable.h +++ b/src/wavetable.h @@ -26,12 +26,8 @@ private: public: const uint16_t m_cid; /*!< Unique ID of NEXT wavetable */ - Wavetable(std::string name) : m_cid(m_nid++) - { - m_name = name; - /* Initialize table of Waves */ - wave_table_t wt; - } + Wavetable(std::string name, wave_table_t wt) + : m_name{name}, m_wave_table{wt}, m_cid(m_nid++) {} /*! ID getter */ uint16_t getId() { return m_cid; } diff --git a/src/wtpp.cpp b/src/wtpp.cpp index e6150ab..bc65bfb 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -52,8 +52,9 @@ int main() log("Printing head of inverse saw table"); isaw_wave.print_head(); - // Create Wavetable object containing fundamental Wave objects - Wavetable wt_01{"Fundamental Waves"}; + // Create wave table of all fundamental Wave objects + wave_table_t fundies = {sin_wave, squ_wave, saw_wave, tri_wave, isaw_wave}; + Wavetable wt_01{"Fundamental Waves", fundies}; log(wt_01.name()); return 0; -- 2.40.1