Refactor: rewrite classes and fix construction

pull/5/head
rkcf 2023-02-26 09:11:20 -06:00
parent 0c15bd2f72
commit 7cea04234f
5 changed files with 44 additions and 27 deletions

View File

@ -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<float_t, DEFAULT_N_SAMPLES> 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;
}

View File

@ -18,14 +18,15 @@ using sample_table_t = std::array<float_t, DEFAULT_N_SAMPLES>;
*/
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_

View File

@ -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;

View File

@ -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<Wave, DEFAULT_N_WAVES>;
@ -19,20 +20,26 @@ using wave_table_t = std::array<Wave, DEFAULT_N_WAVES>;
*/
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_

View File

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