Skip to content

Commit

Permalink
Add calculate_decay_coefficient function to calculate the coefficient…
Browse files Browse the repository at this point in the history
… for a target exponential decay curve
  • Loading branch information
ideoforms committed Sep 12, 2024
1 parent 4387499 commit 9f6847c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions source/include/signalflow/core/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ void signalflow_save_block_to_wav_file(sample *block, int num_samples, std::stri
float signalflow_array_sum(float *array, size_t size);
float signalflow_array_mean(float *array, size_t size);

float signalflow_calculate_decay_coefficient(float decay_time,
unsigned int sample_rate,
float decay_level);

std::vector<int> signalflow_binary_sequence_to_vector(std::string binary);

}
18 changes: 17 additions & 1 deletion source/src/core/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,20 @@ float signalflow_array_mean(float *array, size_t size)
return sum / size;
}

} /* namespace signalflow */
float signalflow_calculate_decay_coefficient(float decay_time,
unsigned int sample_rate,
float decay_level)
{
// # linear to db = 20 x log10(lin)
// # db to linear = 10 ^ (db / 20)
// # for a floor of -60dB, want to find x such that x^y = 10^(-3)
// # and y is the required delay duration in samples
// # = 0.001^(1/y)
// decay_time_samples = int(decay_time * sample_rate)
// return np.power(envelope_floor, 1.0 / decay_time_samples)
float decay_time_samples = decay_time * sample_rate;
return powf(decay_level, 1.0f / decay_time_samples);
}

}
/* namespace signalflow */
6 changes: 6 additions & 0 deletions source/src/python/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ void init_python_util(py::module &m)
m.def(
"random_exponential", [](float from, float to) { return random_exponential(from, to); }, R"pbdoc(Return a random number, exponentially distributed across a fixed range)pbdoc");

m.def(
"calculate_decay_coefficient", [](float decay_time, unsigned int sample_rate, float decay_level) {
return signalflow_calculate_decay_coefficient(decay_time, sample_rate, decay_level);
},
R"pbdoc(Calculate the coefficient required for an exponential decay curve, with duration of decay_time and final value of decay_level (typically 0.001 or -60dB))pbdoc");

py::class_<KDTree>(m, "KDTree", "A KDTree structure")
.def(py::init<std::vector<std::vector<float>>>(), "data"_a = nullptr)
.def("get_nearest", &KDTree::get_nearest, "target"_a);
Expand Down

0 comments on commit 9f6847c

Please sign in to comment.