From 00f1c0e0c1cd3b3f5e13fa480e839e9b81685ea4 Mon Sep 17 00:00:00 2001 From: Matthew Maxwell Date: Sat, 26 Oct 2024 14:40:03 -0500 Subject: [PATCH] Add logger and timer --- CMakeLists.txt | 1 + Makefile | 2 ++ src/audio/context.h | 3 ++- src/audio/sample.h | 4 +++- src/audio/waveform.h | 2 ++ src/cortex.h | 2 ++ src/generators/oscillator.h | 4 +++- src/modulators/adsr.h | 2 ++ src/processors/effects/saturator.h | 4 +++- src/processors/effects/wavefolder.h | 2 ++ src/processors/filters/filter.h | 3 ++- src/utilities/arithmetic.h | 4 +++- src/utilities/logger.cpp | 16 +++++++++++++ src/utilities/logger.h | 16 +++++++++++++ src/utilities/midi.h | 2 ++ src/utilities/timer.h | 34 ++++++++++++++++++++++++++++ tests/generators/oscillator_test.cpp | 11 +++++---- vendor/spdlog | 1 + 18 files changed, 103 insertions(+), 10 deletions(-) create mode 100644 src/utilities/logger.cpp create mode 100644 src/utilities/logger.h create mode 100644 src/utilities/timer.h create mode 160000 vendor/spdlog diff --git a/CMakeLists.txt b/CMakeLists.txt index d03f9bc..4325888 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ list(APPEND SOURCE src/processors/effects/saturator.cpp src/processors/effects/wavefolder.cpp src/processors/filters/filter.cpp + src/utilities/logger.cpp ) list(APPEND INCLUDE_DIR diff --git a/Makefile b/Makefile index a82ecb6..0668150 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ filter \ UTILITY_MOD_DIR = utilities UTILITY_MODULES = \ +logger ###################################### # source @@ -37,6 +38,7 @@ CPP_SOURCES += $(addsuffix .cpp, $(MODULE_DIR)/$(GENERATOR_MOD_DIR)/$(GENERATOR_ CPP_SOURCES += $(addsuffix .cpp, $(MODULE_DIR)/$(MODULATOR_MOD_DIR)/$(MODULATOR_MODULES)) CPP_SOURCES += $(addsuffix .cpp, $(MODULE_DIR)/$(PROCESSOR_EFFECTS_MOD_DIR)/$(PROCESSOR_EFFECTS_MODULES)) CPP_SOURCES += $(addsuffix .cpp, $(MODULE_DIR)/$(PROCESSOR_FILTERS_MOD_DIR)/$(PROCESS_FILTERS_MODULES)) +CPP_SOURCES += $(addsuffix .cpp, $(MODULE_DIR)/$(UTILITY_MOD_DIR)/$(UTILITY_MODULES)) ###################################### # building variables diff --git a/src/audio/context.h b/src/audio/context.h index 8508c47..ff789b5 100644 --- a/src/audio/context.h +++ b/src/audio/context.h @@ -3,6 +3,7 @@ #include namespace cortex { + /** * Contains information about the context in which DSP operations * will be executed, useful for some components such as oscillators @@ -19,4 +20,4 @@ struct Context { * channel configuration, and a buffer size of 16 samples. */ static Context DEFAULT_CONTEXT = { 44100, 2, 16 }; -} // namespace cortex +} diff --git a/src/audio/sample.h b/src/audio/sample.h index 74b8940..8a238ae 100644 --- a/src/audio/sample.h +++ b/src/audio/sample.h @@ -1,6 +1,7 @@ #pragma once namespace cortex { + /** * The basic data type for DSP operations of * an audio signal. @@ -16,4 +17,5 @@ const Sample MIN = -1.0f; * The maximum possible value for a sample. */ const Sample MAX = 1.0f; -} // namespace cortex + +} diff --git a/src/audio/waveform.h b/src/audio/waveform.h index e0b4c04..fda13cd 100644 --- a/src/audio/waveform.h +++ b/src/audio/waveform.h @@ -4,6 +4,7 @@ #include "utilities/arithmetic.h" namespace cortex { + /** * The basic waveform variants, i.e. sine, triangle, * sawtooth, and square. @@ -36,4 +37,5 @@ inline Sample SineToWaveform(Sample sample, Waveform waveform) return sample >= 0.0f ? 1.0f : -1.0f; } } + } \ No newline at end of file diff --git a/src/cortex.h b/src/cortex.h index f6460ca..8c0f9eb 100644 --- a/src/cortex.h +++ b/src/cortex.h @@ -26,6 +26,8 @@ // UTILITIES #include "utilities/arithmetic.h" +#include "utilities/logger.h" #include "utilities/midi.h" +#include "utilities/timer.h" #endif diff --git a/src/generators/oscillator.h b/src/generators/oscillator.h index 2ab4242..eef2d62 100644 --- a/src/generators/oscillator.h +++ b/src/generators/oscillator.h @@ -8,6 +8,7 @@ #include "utilities/arithmetic.h" namespace cortex { + const size_t WAVETABLE_SIZE = 256; /** @@ -85,4 +86,5 @@ class Oscillator { Oscillator* m_follower = nullptr; }; -} // namespace cortex + +} diff --git a/src/modulators/adsr.h b/src/modulators/adsr.h index 04e8967..00499f1 100644 --- a/src/modulators/adsr.h +++ b/src/modulators/adsr.h @@ -3,6 +3,7 @@ #include "audio/context.h" namespace cortex { + /** * The configuration of an ADSR envelope curve including durations * for the attack, decay, and release stages as well as a sustain level. @@ -107,4 +108,5 @@ class AdsrEnvelopeModulator { AdsrStage m_stage = AdsrStage::IDLE; size_t m_samplesSinceLastStage = 0; }; + } diff --git a/src/processors/effects/saturator.h b/src/processors/effects/saturator.h index 4d17e59..64228dd 100644 --- a/src/processors/effects/saturator.h +++ b/src/processors/effects/saturator.h @@ -4,6 +4,7 @@ #include "utilities/arithmetic.h" namespace cortex { + /** * The Saturator class applies a tape saturation * algorithm to audio signals. @@ -54,4 +55,5 @@ class Saturator { float m_saturation = 1.0f; float m_symmetry = 1.0f; }; -} // namespace cortex \ No newline at end of file + +} \ No newline at end of file diff --git a/src/processors/effects/wavefolder.h b/src/processors/effects/wavefolder.h index c66f82a..5d61f78 100644 --- a/src/processors/effects/wavefolder.h +++ b/src/processors/effects/wavefolder.h @@ -4,6 +4,7 @@ #include "utilities/arithmetic.h" namespace cortex { + /** * The Wavefolder class applies a wavefolding * algorithm to audio signals. @@ -58,4 +59,5 @@ class Wavefolder { float m_threshold = 1.0f; float m_symmetry = 1.0f; }; + } \ No newline at end of file diff --git a/src/processors/filters/filter.h b/src/processors/filters/filter.h index bd80d49..535a574 100644 --- a/src/processors/filters/filter.h +++ b/src/processors/filters/filter.h @@ -53,4 +53,5 @@ class Filter { float m_alpha; Sample m_previousOutput; }; -} // namespace cortex + +} diff --git a/src/utilities/arithmetic.h b/src/utilities/arithmetic.h index 165f9a2..0af867a 100644 --- a/src/utilities/arithmetic.h +++ b/src/utilities/arithmetic.h @@ -4,6 +4,7 @@ #include namespace cortex { + /** * A value for the absence of quantity; nothing. */ @@ -109,4 +110,5 @@ inline T exp(T n) auto denominator = 1680 + n * (-840 + n * (180 + n * (-20 + n))); return numerator / denominator; } -} // namespace cortex + +} diff --git a/src/utilities/logger.cpp b/src/utilities/logger.cpp new file mode 100644 index 0000000..a544031 --- /dev/null +++ b/src/utilities/logger.cpp @@ -0,0 +1,16 @@ +#include "logger.h" + +#include + +using namespace cortex; + +void Logger::Log(std::string strings...) +{ + std::string result = ""; + + for (auto string : strings) { + result += string; + } + + std::cout << result << std::endl; +} diff --git a/src/utilities/logger.h b/src/utilities/logger.h new file mode 100644 index 0000000..8c1d10f --- /dev/null +++ b/src/utilities/logger.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +namespace cortex { + +class Logger { +public: + static void Log(std::string strings...); +}; + +} + +#define CX_INFO(...) ::cortex::Logger::Log(__VA_ARGS__) +#define CX_WARN(...) ::cortex::Logger::Log(__VA_ARGS__) +#define CX_ERROR(...) ::cortex::Logger::Log(__VA_ARGS__) diff --git a/src/utilities/midi.h b/src/utilities/midi.h index 3e4e2cb..0211729 100644 --- a/src/utilities/midi.h +++ b/src/utilities/midi.h @@ -5,6 +5,7 @@ #include "utilities/arithmetic.h" namespace cortex { + /** * Converts a MIDI note number to a (floating-point) frequency. Any value outside * of the proper note range [0, 127] will be clamped. @@ -17,4 +18,5 @@ inline T midi_to_frequency(T n) { return powf(2, (clamp(n, 0.0f, 127.0f) - 69.0f) / 12.0f) * 440.0f; } + } diff --git a/src/utilities/timer.h b/src/utilities/timer.h new file mode 100644 index 0000000..64cf02e --- /dev/null +++ b/src/utilities/timer.h @@ -0,0 +1,34 @@ +#include + +#include "utilities/logger.h" + +namespace cortex { + +class Timer { +public: + Timer() + { + m_startTimepoint = std::chrono::high_resolution_clock::now(); + } + + ~Timer() + { + Stop(); + } + + void Stop() + { + auto endTimepoint = std::chrono::high_resolution_clock::now(); + + auto start = std::chrono::time_point_cast(m_startTimepoint).time_since_epoch().count(); + auto end = std::chrono::time_point_cast(endTimepoint).time_since_epoch().count(); + + auto durationMs = (end - start) * 0.001f; + CX_INFO(std::to_string(durationMs) + "ms"); + } + +private: + std::chrono::high_resolution_clock::time_point m_startTimepoint; +}; + +} \ No newline at end of file diff --git a/tests/generators/oscillator_test.cpp b/tests/generators/oscillator_test.cpp index 0c6aa8d..ecf9703 100644 --- a/tests/generators/oscillator_test.cpp +++ b/tests/generators/oscillator_test.cpp @@ -55,10 +55,13 @@ TEST(oscillator_suite, oscillator_sync) EXPECT_NEAR(leader.Generate(), 0.00783538f, 1e-5f); EXPECT_NEAR(follower.Generate(), 0.01174026f, 1e-5f); - int numSamples = context.sampleRate; - while (numSamples--) { - leader.Generate(); - follower.Generate(); + { + Timer t; + int numSamples = context.sampleRate; + while (numSamples--) { + leader.Generate(); + follower.Generate(); + } } EXPECT_NE(leader.Generate(), follower.Generate()); diff --git a/vendor/spdlog b/vendor/spdlog new file mode 160000 index 0000000..e593f66 --- /dev/null +++ b/vendor/spdlog @@ -0,0 +1 @@ +Subproject commit e593f6695c6065e6b345fe2862f04a519ed484e0