Compare commits

...

3 Commits

Author SHA1 Message Date
rkcf a7ce35791f Merge pull request 'Add CSV export' (#8) from export-csv into main
Reviewed-on: #8
2023-02-27 07:53:41 +00:00
rkcf 37c859c8dd Feat: add function to export full Wavetable to csv
Refs: #3
2023-02-27 01:34:49 -06:00
rkcf 7adc6c638d Feat: add basic Wave lookup table export to csv
Refs: #3
2023-02-26 18:19:17 -06:00
5 changed files with 92 additions and 6 deletions

View File

@ -1,10 +1,35 @@
#include "wave.h" #include "wave.h"
#include <cstdint>
#include <string>
#include <fstream>
#include <iostream>
// Initialize index at 0 for wave cycles and wave tables // Initialize index at 0 for wave cycles and wave tables
uint16_t Wave::m_nid = 0; uint16_t Wave::m_nid = 0;
void Wave::print_head() { void Wave::print_head()
{
for (uint8_t i = 0; i < 3; i++) { for (uint8_t i = 0; i < 3; i++) {
std::cout << m_table[i] << '\n'; 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;
}

View File

@ -17,7 +17,8 @@ using sample_table_t = std::array<float_t, DEFAULT_N_SAMPLES>;
/*! @class Wave wave.h "wave.h" /*! @class Wave wave.h "wave.h"
@brief Single cycle wave object with lookup table @brief Single cycle wave object with lookup table
*/ */
class Wave { class Wave
{
private: private:
static uint16_t m_nid; /*!< Unique ID number of NEXT wave */ static uint16_t m_nid; /*!< Unique ID number of NEXT wave */
sample_table_t m_table; /*!< Table of sample data */ sample_table_t m_table; /*!< Table of sample data */
@ -29,6 +30,12 @@ public:
/*! ID getter */ /*! ID getter */
uint16_t getId() { return m_cid; } 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 /*! Sample data table setter
@param table Sample data table @param table Sample data table
*/ */
@ -36,6 +43,12 @@ public:
/*! Prints the first 3 values of sample data */ /*! Prints the first 3 values of sample data */
void print_head(); 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_ #endif // WAVE_H_

View File

@ -1,11 +1,46 @@
#include "wavetable.h" #include "wavetable.h"
#include "wave.h"
#include <cstdint>
#include <string>
#include <fstream>
#include <iostream>
// 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;
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);
id_str.append(": "); id_str.append(": ");
id_str.append(m_name); id_str.append(m_name);
return id_str; 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;
}

View File

@ -18,14 +18,15 @@ using wave_table_t = std::array<Wave, DEFAULT_N_WAVES>;
/*! @class Wavetable wavetable.h "wavetable.h" /*! @class Wavetable wavetable.h "wavetable.h"
@brief Object containing a table of single cycle Wave objects @brief Object containing a table of single cycle Wave objects
*/ */
class Wavetable { class Wavetable
{
private: 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 */ std::string m_name; /*!< String name of wavetable */
wave_table_t m_wave_table; /*!< Table of Wave objects */ wave_table_t m_wave_table; /*!< Table of Wave objects */
public: 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) Wavetable(std::string name, wave_table_t wt)
: m_name{name}, m_wave_table{wt}, m_cid(m_nid++) {} : 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" @return a formatted string in the form "id: name"
*/ */
std::string 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_ #endif // WAVETABLE_H_

View File

@ -57,5 +57,11 @@ int main()
Wavetable wt_01{"Fundamental Waves", fundies}; Wavetable wt_01{"Fundamental Waves", fundies};
log(wt_01.name()); log(wt_01.name());
// 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; return 0;
} }