Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Segfault1602 committed Nov 11, 2023
1 parent afee123 commit 7da68a9
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 154 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ set(CLANG_TIDY_COMMAND "${CLANG_TIDY_EXE}")
set(CMAKE_OSX_DEPLOYMENT_TARGET "13")

set(CLANG_COMPILER_OPTION ${CLANG_COMPILER_OPTION} -Wall -Wpedantic -Werror)
set(MSVC_COMPILER_OPTION /W4 /WX /analyze)
set(MSVC_COMPILER_OPTION /W4 /analyze)
set(GCC_COMPILER_OPTION ${CLANG_COMPILER_OPTION})

include_directories(include)

Expand Down
8 changes: 7 additions & 1 deletion include/bowed_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class BowedString
/// @brief Tick the string.
/// @param input Energy coming from the bridge. Optional.
/// @return The output sample at the bridge.
float Tick(float input = 0.f);
float Tick(float input);

/// @brief Set the finger pressure.
/// @param pressure 0 is no pressure, 1 is full pressure.
Expand All @@ -105,12 +105,18 @@ class BowedString
float samplerate_;
float freq_;
float velocity_ = 0.f;
float bow_force_ = 0.f;
bool note_on_ = false;

constexpr static float max_velocity_ = 0.2f;
constexpr static float velocity_offset_ = 0.03f;

OnePoleFilter decay_filter_;
OnePoleFilter noise_lp_filter_;

// Smoothing filters
OnePoleFilter velocity_filter_;
OnePoleFilter force_filter_;
OnePoleFilter bow_position_filter_;
};
} // namespace sfdsp
40 changes: 9 additions & 31 deletions include/string_ensemble.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,6 @@ class StringEnsemble

void TuneStrings(uint8_t string_num, float frequencies);

/// @brief Set the frequency of the string
/// @param string_number The string number to set the frequency of
/// @param f The frequency of the string in Hz
void SetFrequency(uint8_t string_number, float f);

/// @brief Returns the frequency of the string in Hz
/// @param string_number The string number to get the frequency of
/// @return The frequency of the string in Hz
float GetFrequency(uint8_t string_number) const;

/// @brief Set the velocity of the bow for a given string
/// @param string_number The string number to set the velocity of
/// @param v The velocity of the bow for the given string
void SetVelocity(uint8_t string_number, float v);

/// @brief Get the velocity of the bow for a given string
/// @param string_number The string number to get the velocity of
/// @return The velocity of the bow for the given string
float GetVelocity(uint8_t string_number) const;

/// @brief Set the force of the bow for a given string
/// @param string_number The string number to set the force of
/// @param f The force of the bow for the given string
void SetForce(uint8_t string_number, float f);

/// @brief Get the force of the bow for a given string
/// @param string_number The string number to get the force of
/// @return The force of the bow for the given string
float GetForce(uint8_t string_number) const;

/// @brief Lift the finger off the string. Pitch of the string goes back to the open string pitch.
/// @param string_number
void FingerOff(uint8_t string_number);
Expand All @@ -61,10 +31,18 @@ class StringEnsemble
/// @param t
void SetBridgeTransmission(float t);

float GetBridgeTransmission() const;

float Tick();

void ProcessBlock(float* out, size_t size);

const BowedString& operator[](uint8_t string_number) const;

BowedString& operator[](uint8_t string_number);

private:
std::array<sfdsp::BowedString, kStringCount> strings_;
std::array<BowedString, kStringCount> strings_;
std::array<float, kStringCount> openTuning_;
float bridgeTransmission_ = 0.1f;

Expand Down
7 changes: 5 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ add_library(dsp STATIC ${LIB_SOURCES})

target_include_directories(dsp PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

if(NOT MSVC)
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(dsp PRIVATE ${CLANG_COMPILER_OPTION})
else()
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
target_compile_options(dsp PRIVATE ${MSVC_COMPILER_OPTION})
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
MESSAGE("GNU")
target_compile_options(dsp PRIVATE ${GCC_COMPILER_OPTION})
endif()
4 changes: 4 additions & 0 deletions src/basic_oscillators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace sfdsp

float Sine(float phase)
{
while (phase < 0.f)
{
phase += 1.f;
}
phase = std::fmod(phase, 1.f);
float idx = phase * SIN_LUT_SIZE;
int idx0 = static_cast<int>(idx);
Expand Down
16 changes: 13 additions & 3 deletions src/bowed_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ void BowedString::Init(float samplerate, float tuning)
decay_filter_.SetGain(1.f);

noise_lp_filter_.SetPole(0.8f);

// Smoothing filter
constexpr float smoothingDb = -24.f;
constexpr float smoothingTimeMs = 10.f;
velocity_filter_.SetDecayFilter(smoothingDb, smoothingTimeMs, samplerate);
force_filter_.SetDecayFilter(smoothingDb, smoothingTimeMs, samplerate);
bow_position_filter_.SetDecayFilter(smoothingDb, smoothingTimeMs, samplerate);
}

void BowedString::SetFrequency(float f)
Expand Down Expand Up @@ -74,12 +81,12 @@ void BowedString::SetVelocity(float v)

void BowedString::SetForce(float f)
{
bow_table_.SetForce(f);
bow_force_ = f;
}

float BowedString::GetForce() const
{
return bow_table_.GetForce();
return bow_force_;
}

void BowedString::SetBowPosition(float pos)
Expand Down Expand Up @@ -136,6 +143,9 @@ float BowedString::NextOut()

float BowedString::Tick(float input)
{
float vel = velocity_filter_.Tick(velocity_);
bow_table_.SetForce(force_filter_.Tick(bow_force_));

float bridge, nut;
waveguide_.NextOut(nut, bridge);

Expand All @@ -145,7 +155,7 @@ float BowedString::Tick(float input)
float bow_output = 0.f;
if (note_on_)
{
float velocity_delta = velocity_ - (vsl_plus + vsr_plus);
float velocity_delta = vel - (vsl_plus + vsr_plus);
constexpr float noise_db = -30;
const float noise_gain = std::pow(10.f, noise_db / 20.f);

Expand Down
108 changes: 42 additions & 66 deletions src/string_ensemble.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include "string_ensemble.h"

#include <span>

using namespace sfdsp;

void StringEnsemble::Init(float samplerate, const std::array<float, kStringCount>& frequencies)
{
for (auto i = 0; i < kStringCount; ++i)
for (size_t i = 0; i < kStringCount; ++i)
{
strings_[i].Init(samplerate, frequencies[i]);
}
Expand All @@ -32,95 +30,73 @@ void StringEnsemble::TuneStrings(uint8_t string_number, float frequencies)
strings_[string_number].SetFrequency(frequencies);
}

void StringEnsemble::SetFrequency(uint8_t string_number, float f)
void StringEnsemble::FingerOff(uint8_t string_number)
{
assert(string_number < kStringCount);
assert(f > 0.f);
strings_[string_number].SetFrequency(f);

strings_[string_number].SetFrequency(openTuning_[string_number]);
}

float StringEnsemble::GetFrequency(uint8_t string_number) const
void StringEnsemble::SetBridgeTransmission(float t)
{
assert(string_number < kStringCount);
assert(t >= 0.f && t <= 1.f);

return strings_[string_number].GetFrequency();
bridgeTransmission_ = t;
}

void StringEnsemble::SetVelocity(uint8_t string_number, float v)
float StringEnsemble::GetBridgeTransmission() const
{
assert(string_number < kStringCount);

strings_[string_number].SetVelocity(v);
return bridgeTransmission_;
}

float StringEnsemble::GetVelocity(uint8_t string_number) const
float StringEnsemble::Tick()
{
assert(string_number < kStringCount);

return strings_[string_number].GetVelocity();
float out = 0;
ProcessBlock(&out, 1);
return out;
}

void StringEnsemble::SetForce(uint8_t string_number, float f)
void StringEnsemble::ProcessBlock(float* out, size_t size)
{
assert(string_number < kStringCount);

strings_[string_number].SetForce(f);
assert(out != nullptr);

if (f > 0.f)
for (size_t i = 0; i < size; ++i)
{
strings_[string_number].SetNoteOn(true);
}
else
{
strings_[string_number].SetNoteOn(false);
std::array<float, kStringCount> string_outs;

float transmission = 0.f;
float output = 0.f;
for (size_t j = 0; i < kStringCount; ++j)
{
string_outs[j] = strings_[j].NextOut();
output += string_outs[j];
transmission += string_outs[j] * bridgeTransmission_;
string_outs[j] *= (1.f - bridgeTransmission_);
}

// filter the bridge output
transmission = transmission_filter_.Tick(transmission) * 0.25f;

for (size_t j = 0; i < kStringCount; ++j)
{
strings_[j].Tick(string_outs[j] + transmission);
}

out[i] = 0.1248f * body_filters_[5].Tick(body_filters_[4].Tick(body_filters_[3].Tick(
body_filters_[2].Tick(body_filters_[1].Tick(body_filters_[0].Tick(output))))));
}
}

float StringEnsemble::GetForce(uint8_t string_number) const
const BowedString& StringEnsemble::operator[](uint8_t string_number) const
{
assert(string_number < kStringCount);

return strings_[string_number].GetForce();
return strings_[string_number];
}

void StringEnsemble::FingerOff(uint8_t string_number)
BowedString& StringEnsemble::operator[](uint8_t string_number)
{
assert(string_number < kStringCount);

strings_[string_number].SetFrequency(openTuning_[string_number]);
}

void StringEnsemble::SetBridgeTransmission(float t)
{
assert(t >= 0.f && t <= 1.f);

bridgeTransmission_ = t;
}

float StringEnsemble::Tick()
{
std::array<float, kStringCount> string_outs;

float transmission = 0.f;
float output = 0.f;
for (auto i = 0; i < kStringCount; ++i)
{
string_outs[i] = strings_[i].NextOut();
output += string_outs[i];
transmission += string_outs[i] * bridgeTransmission_;
string_outs[i] *= (1.f - bridgeTransmission_);
}

// filter the bridge output
transmission = transmission_filter_.Tick(transmission) * 0.25f;

for (size_t i = 0; i < kStringCount; ++i)
{
strings_[i].Tick(string_outs[i] + transmission);
}

float out = 0.1248f * body_filters_[5].Tick(body_filters_[4].Tick(body_filters_[3].Tick(
body_filters_[2].Tick(body_filters_[1].Tick(body_filters_[0].Tick(output))))));

return out;
return strings_[string_number];
}
10 changes: 0 additions & 10 deletions src/waveguide_gate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ void WaveguideGate::SetCoeff(float c)
coeff_ = c;
}

void PrintDelayline(const sfdsp::Delayline& line)
{
printf(" ");
for (auto i = 0; i < 4; ++i)
{
printf("%5.2f ", line[i + 1]);
}
printf("\n");
}

void WaveguideGate::Process(Delayline& left_traveling_line, Delayline& right_traveling_line)
{
if (delay_ == 0 || delay_ >= left_traveling_line.GetDelay() - 2)
Expand Down
2 changes: 1 addition & 1 deletion tests/basic_oscillators_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
TEST(BasicOscillatorsTest, Sine)
{
float inc = 1.f / 1024;
for (auto i = 0.f; i < TWO_PI; i += inc)
for (auto i = -TWO_PI; i < TWO_PI * 4; i += inc)
{
auto s = sfdsp::Sine(i);
auto t = std::sin(i * TWO_PI);
Expand Down
2 changes: 1 addition & 1 deletion tests/rms_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TEST_P(RMSTest, PureSine)

float phase = 0.f;

for (auto i = 0; i < kLoopLength; i++)
for (size_t i = 0; i < kLoopLength; i++)
{
auto s = kAmplitude * std::sin(phase * TWO_PI);
phase = std::fmod(phase + kPhaseInc, 1.f);
Expand Down
2 changes: 2 additions & 0 deletions tests/test_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "test_utils.h"

#include <cstdio>

void PrintWaveguide(sfdsp::Waveguide& wave, size_t delay_size)
{
std::vector<float> right_samples, left_samples;
Expand Down
Loading

0 comments on commit 7da68a9

Please sign in to comment.