From 6d40c3876c5bcd35101cd2478ebf113ff8ad1a99 Mon Sep 17 00:00:00 2001 From: rkcf Date: Sun, 26 Feb 2023 16:02:15 -0600 Subject: [PATCH] Refactor: move gen_sin to new generators file --- Makefile | 2 +- src/generators.cpp | 17 +++++++++++++++++ src/generators.h | 14 ++++++++++++++ src/wave.cpp | 16 ---------------- src/wave.h | 5 ----- src/wtpp.cpp | 1 + 6 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 src/generators.cpp create mode 100644 src/generators.h diff --git a/Makefile b/Makefile index 938246e..7b57192 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ OLEVEL = -O2 CFLAGS = -Wall -std=c++17 $(OLEVEL) -o $(PROJ) -OBJECTS = src/wtpp.cpp src/wave.cpp src/wavetable.cpp +OBJECTS = src/wtpp.cpp src/wave.cpp src/wavetable.cpp src/generators.cpp build: $(CC) $(CFLAGS) $(OBJECTS) diff --git a/src/generators.cpp b/src/generators.cpp new file mode 100644 index 0000000..d226edd --- /dev/null +++ b/src/generators.cpp @@ -0,0 +1,17 @@ +#include "generators.h" + +sample_table_t gen_sin() { + // Make the new Wavetable object + sample_table_t sin_vals = {0}; + // 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; + } + // Return the completed Wavetable object + return sin_vals; +} diff --git a/src/generators.h b/src/generators.h new file mode 100644 index 0000000..d1fb5d1 --- /dev/null +++ b/src/generators.h @@ -0,0 +1,14 @@ +/*! @file generators.h + @brief Header for wave cycle data generators + */ +#ifndef GENERATORS_H_ +#define GENERATORS_H_ + +#include "wave.h" + +/*! Generate sine wave data + @return a table of values + */ +sample_table_t gen_sin(); + +#endif // GENERATORS_H_ diff --git a/src/wave.cpp b/src/wave.cpp index 43cbb5c..642e59c 100644 --- a/src/wave.cpp +++ b/src/wave.cpp @@ -10,19 +10,3 @@ void Wave::print_head() { std::cout << m_table[i] << '\n'; } } - -sample_table_t gen_sin() { - // Make the new Wavetable object - sample_table_t sin_vals = {0}; - // 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; - } - // Return the completed Wavetable object - return sin_vals; -} diff --git a/src/wave.h b/src/wave.h index 2198862..1c14139 100644 --- a/src/wave.h +++ b/src/wave.h @@ -37,9 +37,4 @@ public: void print_head(); }; -/*! Generate sine wave data - @return a table of values - */ -sample_table_t gen_sin(); - #endif // WAVE_H_ diff --git a/src/wtpp.cpp b/src/wtpp.cpp index be4cfc3..cb8df51 100644 --- a/src/wtpp.cpp +++ b/src/wtpp.cpp @@ -8,6 +8,7 @@ #include #include "wave.h" +#include "generators.h" #include "wavetable.h" static const uint32_t DEFAULT_SAMPLE_RATE = 44100; // 44.1 kHz sampling rate