Refactor: move wave functions to wave.cpp

pull/5/head
rkcf 2023-02-26 06:16:40 -06:00
parent 6abdf89009
commit c2dd910ed7
4 changed files with 39 additions and 38 deletions

View File

@ -5,7 +5,7 @@ OLEVEL = -O2
CFLAGS = -Wall -std=c++17 $(OLEVEL) -o $(PROJ) CFLAGS = -Wall -std=c++17 $(OLEVEL) -o $(PROJ)
OBJECTS = src/wtpp.cpp OBJECTS = src/wtpp.cpp src/wave.cpp src/wavetable.cpp
build: build:
$(CC) $(CFLAGS) $(OBJECTS) $(CC) $(CFLAGS) $(OBJECTS)

31
src/wave.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <iostream>
#include "wave.h"
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() {
// Make the new Wavetable object
Wave wave = {0};
// Make an array to hold our values
std::array<float_t, DEFAULT_N_SAMPLES> sin_vals{};
// Make a place to store calculated value
float_t val = 0;
// Loop through number of samples and set table values
for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) {
// Calculate the value of sin at position
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;
}

View File

@ -3,7 +3,6 @@
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <iostream>
#include <math.h> #include <math.h>
@ -12,24 +11,18 @@ const uint16_t DEFAULT_N_SAMPLES = 1024;
/* Single cycle wave object with lookup table */ /* Single cycle wave object with lookup table */
class Wave { class Wave {
private: private:
uint16_t m_id{}; // Unique ID number of wave uint16_t m_id{}; // Unique ID number of wave
std::array<float_t, DEFAULT_N_SAMPLES> m_table; // table data std::array<float_t, DEFAULT_N_SAMPLES> m_table; // table data
public: public:
Wave(uint16_t id) : m_id{id}, m_table{} { Wave(uint16_t id) : m_id{id}, m_table{} { /* Do nothing */ }
// Do nothing
}
uint16_t id() { return m_id; } uint16_t id() { return m_id; }
void set_table(std::array<float_t, DEFAULT_N_SAMPLES> table) { void setTable(std::array<float_t, DEFAULT_N_SAMPLES> table) { m_table = table; }
m_table = table;
}
void print_head() { void print_head();
for (uint8_t i = 0; i < 10; i++) {
std::cout << m_table[i] << '\n';
}
}
}; };
Wave gen_sin();
#endif // WAVE_H_ #endif // WAVE_H_

View File

@ -19,36 +19,13 @@ static void log(std::string message)
std::cout << "wtpp: " << message << '\n'; std::cout << "wtpp: " << message << '\n';
} }
/* Generates basic sine Wave object */
static Wave gen_sine()
{
// Make the new Wavetable object
Wave wave = {0};
// Make an array to hold our values
std::array<float_t, DEFAULT_N_SAMPLES> sin_vals {};
// Make a place to store calculated value
float_t val = 0;
// Loop through number of samples and set table values
for (uint16_t i = 0; i < DEFAULT_N_SAMPLES; i++) {
// Calculate the value of sin at position
val = std::sin((2 * M_PI * i) / DEFAULT_N_SAMPLES);
sin_vals[i] = val;
}
// Store values in new Wavetable.table
wave.set_table(sin_vals);
// Return the completed Wavetable object
return wave;
}
int main() int main()
{ {
// Generate wavetables // Generate wavetables
log("Generating wavetables"); log("Generating wavetables");
// Generate sine wave cycle object // Generate sine wave cycle object
Wave sin_wave = gen_sine(); Wave sin_wave = gen_sin();
log(std::to_string(sin_wave.id())); log(std::to_string(sin_wave.id()));
log("Printing head of sine table"); log("Printing head of sine table");