Refactor: move gen_sin to new generators file

pull/5/head
rkcf 2023-02-26 16:02:15 -06:00
parent 7cea04234f
commit 6d40c3876c
6 changed files with 33 additions and 22 deletions

View File

@ -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)

17
src/generators.cpp Normal file
View File

@ -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;
}

14
src/generators.h Normal file
View File

@ -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_

View File

@ -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;
}

View File

@ -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_

View File

@ -8,6 +8,7 @@
#include <math.h>
#include "wave.h"
#include "generators.h"
#include "wavetable.h"
static const uint32_t DEFAULT_SAMPLE_RATE = 44100; // 44.1 kHz sampling rate