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();