Refactor: change Wavetable to use pointer for Wave table

Refs: #14
main
rkcf 2023-02-27 08:52:49 -06:00
parent b0c352674d
commit 6ba6d6b09e
5 changed files with 30 additions and 10 deletions

View File

@ -10,9 +10,9 @@
/*! /*!
Constants Constants
*/ */
const uint32_t DEFAULT_SAMPLE_RATE = 22050; /*!< 22.05 kHz sampling rate */ const uint32_t DEFAULT_SAMPLE_RATE = 22050; /*!< Sampling rate */
const uint8_t DEFAULT_BIT_DEPTH = 12; /*!< 12bit samples */ const uint8_t DEFAULT_BIT_DEPTH = 12; /*!< Bit depth of samples */
const uint16_t DEFAULT_N_SAMPLES = 1024; /*!< 1024 samples per wave cycle */ const uint16_t DEFAULT_N_SAMPLES = 256; /*!< Number of samples per wave cycle */
const uint16_t DEFAULT_N_WAVES = 64; /*!< Number of Wave cycle objects in Wavetable */ const uint16_t DEFAULT_N_WAVES = 64; /*!< Number of Wave cycle objects in Wavetable */
/*! /*!

View File

@ -23,13 +23,20 @@ private:
float_t *m_tp; /*!< pointer to first element of sample array */ float_t *m_tp; /*!< pointer to first element of sample array */
public: public:
const uint16_t m_cid; /*!< Unique ID of current wavetable */ uint16_t m_cid; /*!< Unique ID of current wavetable */
Wave() : m_cid(m_nid++) Wave() : m_cid(m_nid++)
{ {
// Allocate sample memory // Allocate sample memory
m_tp = new float_t[DEFAULT_N_SAMPLES]; m_tp = new float_t[DEFAULT_N_SAMPLES];
} }
Wave(Wave& newWave)
{
m_nid = newWave.m_nid;
m_cid = newWave.m_cid;
m_tp = newWave.m_tp;
}
/*! ID getter */ /*! ID getter */
uint16_t getId() { return m_cid; } uint16_t getId() { return m_cid; }

View File

@ -8,6 +8,11 @@
// Initialize index at 0 for wave cycles and wave tables // Initialize index at 0 for wave cycles and wave tables
uint16_t Wavetable::m_nid = 0; uint16_t Wavetable::m_nid = 0;
void Wavetable::setWave(uint16_t i, Wave wave)
{
*(m_wtp+i) = wave;
}
std::string Wavetable::name() std::string Wavetable::name()
{ {
std::string id_str = std::to_string(m_cid); std::string id_str = std::to_string(m_cid);
@ -36,7 +41,8 @@ uint8_t Wavetable::export_csv(std::string data_path)
for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) { for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) {
// For each Wave in table // For each Wave in table
for (uint16_t j = 0; j < DEFAULT_N_WAVES; j++) { for (uint16_t j = 0; j < DEFAULT_N_WAVES; j++) {
outf << m_wave_table[j].getVal(i) << ","; Wave w = *(m_wtp+j);
outf << w.getVal(i) << ",";
} }
outf << "\n"; outf << "\n";
} }

View File

@ -8,6 +8,7 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include "common.h"
#include "wave.h" #include "wave.h"
using wave_table_t = std::array<Wave, DEFAULT_N_WAVES>; /*!< Array of Wave objects */ using wave_table_t = std::array<Wave, DEFAULT_N_WAVES>; /*!< Array of Wave objects */
@ -20,16 +21,22 @@ class Wavetable
private: private:
static uint16_t m_nid; /*!< Unique ID of NEXT wavetable */ static uint16_t m_nid; /*!< Unique ID of NEXT wavetable */
std::string m_name; /*!< String name of wavetable */ std::string m_name; /*!< String name of wavetable */
wave_table_t m_wave_table; /*!< Table of Wave objects */ Wave *m_wtp; /*!< Pointer to table of Waves */
public: public:
const uint16_t m_cid; /*!< Unique ID of wavetable */ const uint16_t m_cid; /*!< Unique ID of wavetable */
Wavetable(std::string name, wave_table_t wt) Wavetable(std::string name) : m_name{name}, m_cid(m_nid++)
: m_name{name}, m_wave_table{wt}, m_cid(m_nid++) {} {
// Allocate Wavetable memory
m_wtp = new Wave[DEFAULT_N_WAVES];
}
/*! ID getter */ /*! ID getter */
uint16_t getId() { return m_cid; } uint16_t getId() { return m_cid; }
/*! Set index i to provided Wave */
void setWave(uint16_t i, Wave wave);
/*! Makes formatted name and id string /*! Makes formatted name and id string
@return a formatted string in the form "id: name" @return a formatted string in the form "id: name"
*/ */

View File

@ -42,8 +42,8 @@ int main()
isaw_wave.print_head(); isaw_wave.print_head();
// Create wave table of all fundamental Wave objects // Create wave table of all fundamental Wave objects
wave_table_t fundies = {sin_wave, squ_wave, saw_wave, tri_wave, isaw_wave}; std::array<Wave, 5> fundies = {sin_wave, squ_wave, saw_wave, tri_wave, isaw_wave};
Wavetable wt_01{"Fundamental Waves", fundies}; Wavetable wt_01{"Fundamental Waves"};
clog(wt_01.name()); clog(wt_01.name());
// Write sine wave table to file // Write sine wave table to file