From 7adc6c638d91fdc46bca9597d27c8e47a573f0a5 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 18:19:17 -0600 Subject: [PATCH 1/2] Feat: add basic Wave lookup table export to csv Refs: #3 --- src/wave.cpp | 27 ++++++++++++++++++++++++++- src/wave.h | 6 ++++++ src/wtpp.cpp | 3 +++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/wave.cpp b/src/wave.cpp index f0e5938..c1fe70d 100644 --- a/src/wave.cpp +++ b/src/wave.cpp @@ -1,10 +1,35 @@ #include "wave.h" +#include +#include +#include +#include // Initialize index at 0 for wave cycles and wave tables uint16_t Wave::m_nid = 0; -void Wave::print_head() { +void Wave::print_head() +{ for (uint8_t i = 0; i < 3; i++) { std::cout << m_table[i] << '\n'; } } + +uint8_t Wave::export_csv(std::string path) +{ + // Open file for writing + std::ofstream outf{path}; + + // Check if we failed to make file + if (!outf) { + std::cerr << "Failed to write to file\n"; + return 1; + } + + // put values into file + for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) { + outf << m_table[i] << "\n"; + } + + // Clean exit + return 0; +} diff --git a/src/wave.h b/src/wave.h index de12395..fcbb9db 100644 --- a/src/wave.h +++ b/src/wave.h @@ -36,6 +36,12 @@ public: /*! Prints the first 3 values of sample data */ void print_head(); + + /*! Exports sample table data to csv + @param path File path to save to + @return uint8_t integer return value for error checking + */ + uint8_t export_csv(std::string path); }; #endif // WAVE_H_ diff --git a/src/wtpp.cpp b/src/wtpp.cpp index bc65bfb..2bef3a8 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -57,5 +57,8 @@ int main() Wavetable wt_01{"Fundamental Waves", fundies}; log(wt_01.name()); + // Write sine wave table to file + sin_wave.export_csv("data/sine.csv"); + return 0; } From 37c859c8dda703b76e25ad236b8a32be3dcf4aa0 Mon Sep 17 00:00:00 2001 From: rkcf Date: Mon, 27 Feb 2023 01:34:49 -0600 Subject: [PATCH 2/2] Feat: add function to export full Wavetable to csv Refs: #3 --- src/wave.h | 9 ++++++++- src/wavetable.cpp | 37 ++++++++++++++++++++++++++++++++++++- src/wavetable.h | 13 ++++++++++--- src/wtpp.cpp | 3 +++ 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/wave.h b/src/wave.h index fcbb9db..ddcba66 100644 --- a/src/wave.h +++ b/src/wave.h @@ -17,7 +17,8 @@ using sample_table_t = std::array; /*! @class Wave wave.h "wave.h" @brief Single cycle wave object with lookup table */ -class Wave { +class Wave +{ private: static uint16_t m_nid; /*!< Unique ID number of NEXT wave */ sample_table_t m_table; /*!< Table of sample data */ @@ -29,6 +30,12 @@ public: /*! ID getter */ uint16_t getId() { return m_cid; } + /*! Get single value at index + @param i index of target + @return float_t value at index + */ + float_t getVal(uint16_t i) { return m_table[i]; } + /*! Sample data table setter @param table Sample data table */ diff --git a/src/wavetable.cpp b/src/wavetable.cpp index b59149d..f98ef2d 100644 --- a/src/wavetable.cpp +++ b/src/wavetable.cpp @@ -1,11 +1,46 @@ #include "wavetable.h" +#include "wave.h" +#include +#include +#include +#include // Initialize index at 0 for wave cycles and wave tables uint16_t Wavetable::m_nid = 0; -std::string Wavetable::name() { +std::string Wavetable::name() +{ std::string id_str = std::to_string(m_cid); id_str.append(": "); id_str.append(m_name); return id_str; } + +uint8_t Wavetable::export_csv(std::string data_path) +{ + // Make the full name path + std::string fname = data_path.append("/"); + fname.append(std::to_string(m_cid)); + fname.append(".csv"); + + // Open file for writing + std::ofstream outf{fname}; + + // Check if we failed to make file + if (!outf) { + std::cerr << "Failed to write to file\n"; + return 1; + } + + // For each value in the Wave lookup table + for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) { + // For each Wave in table + for (uint16_t j = 0; j < DEFAULT_N_WAVES; j++) { + outf << m_wave_table[j].getVal(i) << ","; + } + outf << "\n"; + } + + // Clean exit + return 0; +} diff --git a/src/wavetable.h b/src/wavetable.h index 6b462bf..6acd1f8 100644 --- a/src/wavetable.h +++ b/src/wavetable.h @@ -18,14 +18,15 @@ using wave_table_t = std::array; /*! @class Wavetable wavetable.h "wavetable.h" @brief Object containing a table of single cycle Wave objects */ -class Wavetable { +class Wavetable +{ private: - static uint16_t m_nid; /*!< Unique ID number of wavetable */ + static uint16_t m_nid; /*!< Unique ID of NEXT 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 */ + const uint16_t m_cid; /*!< Unique ID of wavetable */ Wavetable(std::string name, wave_table_t wt) : m_name{name}, m_wave_table{wt}, m_cid(m_nid++) {} @@ -36,6 +37,12 @@ public: @return a formatted string in the form "id: name" */ std::string name(); + + /*! exports a complete info csv of the Wavetable named after wavetable + @param data_path directory where wavetable is exported + @result uint8_t integer return value for error checking + */ + uint8_t export_csv(std::string data_path); }; #endif // WAVETABLE_H_ diff --git a/src/wtpp.cpp b/src/wtpp.cpp index 2bef3a8..e00d1b2 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -60,5 +60,8 @@ int main() // Write sine wave table to file sin_wave.export_csv("data/sine.csv"); + // Write full Wavetable data to file + wt_01.export_csv("data"); + return 0; }