From 7adc6c638d91fdc46bca9597d27c8e47a573f0a5 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 18:19:17 -0600 Subject: [PATCH] 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; }