Refactor: move constants to common and fix logging

pull/13/head
rkcf 2023-02-27 04:45:37 -06:00
parent 52a22d53c4
commit 8d7bc51d6b
7 changed files with 32 additions and 33 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 src/wave.cpp src/wavetable.cpp src/generators.cpp OBJECTS = src/wtpp.cpp src/wave.cpp src/wavetable.cpp src/generators.cpp src/common.cpp
build: build:
$(CC) $(CFLAGS) $(OBJECTS) $(CC) $(CFLAGS) $(OBJECTS)

View File

@ -1,17 +1,27 @@
/*! @file common.h /*! @file common.h
@brief Header for shared utilities and constants @brief Header for constants, types, utilities
*/ */
#ifndef COMMON_H_ #ifndef COMMON_H_
#define COMMON_H_ #define COMMON_H_
#include <cstdint>
#include <string> #include <string>
#include <iostream>
/*! Logs a message /*!
Constants
*/
const uint32_t DEFAULT_SAMPLE_RATE = 22050; /*!< 22.05 kHz sampling rate */
const uint8_t DEFAULT_BIT_DEPTH = 12; /*!< 12bit samples */
const uint16_t DEFAULT_N_SAMPLES = 1024; /*!< 1024 samples per wave cycle */
const uint16_t DEFAULT_N_WAVES = 64; /*!< Number of Wave cycle objects in Wavetable */
/*!
TYPES
*/
/*! Logs a message to console
@param message The string to be logged @param message The string to be logged
*/ */
static void log(std::string message) { void clog(std::string message);
std::cout << "wtpp: " << message << '\n';
}
#endif // COMMON_H_ #endif // COMMON_H_

View File

@ -1,3 +1,4 @@
#include <cmath>
#include "generators.h" #include "generators.h"
#include "wave.h" #include "wave.h"

View File

@ -4,9 +4,6 @@
#ifndef GENERATORS_H_ #ifndef GENERATORS_H_
#define GENERATORS_H_ #define GENERATORS_H_
#include "math.h"
#include <cstdint>
#include "wave.h" #include "wave.h"
/*! Generate sine wave data /*! Generate sine wave data

View File

@ -4,18 +4,17 @@
#ifndef WAVE_H_ #ifndef WAVE_H_
#define WAVE_H_ #define WAVE_H_
#include <iostream> #include "common.h"
#include <array> #include <array>
#include <cmath>
#include <cstdint> #include <cstdint>
#include <iostream>
#include <string>
#include <math.h> using sample_table_t = std::array<float_t, DEFAULT_N_SAMPLES>; /*!< Array of waveform sample data*/
const uint16_t DEFAULT_N_SAMPLES = 1024;
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
{ {

View File

@ -10,10 +10,7 @@
#include "wave.h" #include "wave.h"
/* Number of Wave cycle objects in table */ using wave_table_t = std::array<Wave, DEFAULT_N_WAVES>; /*!< Array of Wave objects */
const uint16_t DEFAULT_N_WAVES = 256;
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

View File

@ -1,7 +1,5 @@
#include <array> #include <array>
#include <cmath>
#include <cstdint> #include <cstdint>
#include <string>
#include <type_traits> #include <type_traits>
#include <math.h> #include <math.h>
@ -13,12 +11,9 @@
#include "generators.h" #include "generators.h"
#include "wavetable.h" #include "wavetable.h"
static const uint32_t DEFAULT_SAMPLE_RATE = 44100; // 44.1 kHz sampling rate
static const uint8_t DEFAULT_BIT_DEPTH = 12;
int main() int main()
{ {
log("Generating wave cycle lookup tables"); clog("Generating wave cycle lookup tables");
Wave sin_wave, squ_wave, saw_wave, tri_wave, isaw_wave; Wave sin_wave, squ_wave, saw_wave, tri_wave, isaw_wave;
// Calculate fundamental wave values // Calculate fundamental wave values
@ -35,21 +30,21 @@ int main()
tri_wave.setTable(tri_vals); tri_wave.setTable(tri_vals);
isaw_wave.setTable(isaw_vals); isaw_wave.setTable(isaw_vals);
log("Printing head of sine table"); clog("Printing head of sine table");
sin_wave.print_head(); sin_wave.print_head();
log("Printing head of square table"); clog("Printing head of square table");
squ_wave.print_head(); squ_wave.print_head();
log("Printing head of saw table"); clog("Printing head of saw table");
saw_wave.print_head(); saw_wave.print_head();
log("Printing head of triangle table"); clog("Printing head of triangle table");
tri_wave.print_head(); tri_wave.print_head();
log("Printing head of inverse saw table"); clog("Printing head of inverse saw table");
isaw_wave.print_head(); isaw_wave.print_head();
// Create wave table of all fundamental Wave objects // Create wave table of all fundamental Wave objects
wave_table_t fundies = {sin_wave, squ_wave, saw_wave, tri_wave, isaw_wave}; wave_table_t fundies = {sin_wave, squ_wave, saw_wave, tri_wave, isaw_wave};
Wavetable wt_01{"Fundamental Waves", fundies}; Wavetable wt_01{"Fundamental Waves", fundies};
log(wt_01.name()); clog(wt_01.name());
// Write sine wave table to file // Write sine wave table to file
sin_wave.export_csv("data/sine.csv"); sin_wave.export_csv("data/sine.csv");