Skip to content

Commit

Permalink
Add logger and timer
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwellmattryan committed Oct 26, 2024
1 parent ea1f221 commit 00f1c0e
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 10 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ filter \

UTILITY_MOD_DIR = utilities
UTILITY_MODULES = \
logger

######################################
# source
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/audio/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cmath>

namespace cortex {

/**
* Contains information about the context in which DSP operations
* will be executed, useful for some components such as oscillators
Expand All @@ -19,4 +20,4 @@ struct Context {
* channel configuration, and a buffer size of 16 samples.
*/
static Context DEFAULT_CONTEXT = { 44100, 2, 16 };
} // namespace cortex
}
4 changes: 3 additions & 1 deletion src/audio/sample.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

namespace cortex {

/**
* The basic data type for DSP operations of
* an audio signal.
Expand All @@ -16,4 +17,5 @@ const Sample MIN = -1.0f;
* The maximum possible value for a sample.
*/
const Sample MAX = 1.0f;
} // namespace cortex

}
2 changes: 2 additions & 0 deletions src/audio/waveform.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "utilities/arithmetic.h"

namespace cortex {

/**
* The basic waveform variants, i.e. sine, triangle,
* sawtooth, and square.
Expand Down Expand Up @@ -36,4 +37,5 @@ inline Sample SineToWaveform(Sample sample, Waveform waveform)
return sample >= 0.0f ? 1.0f : -1.0f;
}
}

}
2 changes: 2 additions & 0 deletions src/cortex.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

// UTILITIES
#include "utilities/arithmetic.h"
#include "utilities/logger.h"
#include "utilities/midi.h"
#include "utilities/timer.h"

#endif
4 changes: 3 additions & 1 deletion src/generators/oscillator.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "utilities/arithmetic.h"

namespace cortex {

const size_t WAVETABLE_SIZE = 256;

/**
Expand Down Expand Up @@ -85,4 +86,5 @@ class Oscillator {

Oscillator* m_follower = nullptr;
};
} // namespace cortex

}
2 changes: 2 additions & 0 deletions src/modulators/adsr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -107,4 +108,5 @@ class AdsrEnvelopeModulator {
AdsrStage m_stage = AdsrStage::IDLE;
size_t m_samplesSinceLastStage = 0;
};

}
4 changes: 3 additions & 1 deletion src/processors/effects/saturator.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "utilities/arithmetic.h"

namespace cortex {

/**
* The Saturator class applies a tape saturation
* algorithm to audio signals.
Expand Down Expand Up @@ -54,4 +55,5 @@ class Saturator {
float m_saturation = 1.0f;
float m_symmetry = 1.0f;
};
} // namespace cortex

}
2 changes: 2 additions & 0 deletions src/processors/effects/wavefolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "utilities/arithmetic.h"

namespace cortex {

/**
* The Wavefolder class applies a wavefolding
* algorithm to audio signals.
Expand Down Expand Up @@ -58,4 +59,5 @@ class Wavefolder {
float m_threshold = 1.0f;
float m_symmetry = 1.0f;
};

}
3 changes: 2 additions & 1 deletion src/processors/filters/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ class Filter {
float m_alpha;
Sample m_previousOutput;
};
} // namespace cortex

}
4 changes: 3 additions & 1 deletion src/utilities/arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cmath>

namespace cortex {

/**
* A value for the absence of quantity; nothing.
*/
Expand Down Expand Up @@ -109,4 +110,5 @@ inline T exp(T n)
auto denominator = 1680 + n * (-840 + n * (180 + n * (-20 + n)));
return numerator / denominator;
}
} // namespace cortex

}
16 changes: 16 additions & 0 deletions src/utilities/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "logger.h"

#include <iostream>

using namespace cortex;

void Logger::Log(std::string strings...)
{
std::string result = "";

for (auto string : strings) {
result += string;
}

std::cout << result << std::endl;
}
16 changes: 16 additions & 0 deletions src/utilities/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <memory>

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__)
2 changes: 2 additions & 0 deletions src/utilities/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

}
34 changes: 34 additions & 0 deletions src/utilities/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <chrono>

#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<std::chrono::microseconds>(m_startTimepoint).time_since_epoch().count();
auto end = std::chrono::time_point_cast<std::chrono::microseconds>(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;
};

}
11 changes: 7 additions & 4 deletions tests/generators/oscillator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions vendor/spdlog
Submodule spdlog added at e593f6

0 comments on commit 00f1c0e

Please sign in to comment.