mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 17:52:26 -05:00
Piano: Make decay more accurate
1. Make decay sample-granular rather than buffer-granular You only have ~43 buffers per second which can make a jagged signal. 2. Calculate decay in milliseconds Decay is supposed to be a time value.
This commit is contained in:
parent
44c81ee9db
commit
421a340572
Notes:
sideshowbarker
2024-07-19 09:39:49 +09:00
Author: https://github.com/willmcpherson2 Commit: https://github.com/SerenityOS/serenity/commit/421a3405729 Pull-request: https://github.com/SerenityOS/serenity/pull/1178
3 changed files with 21 additions and 12 deletions
|
@ -31,6 +31,7 @@
|
|||
|
||||
AudioEngine::AudioEngine()
|
||||
{
|
||||
set_decay(0);
|
||||
}
|
||||
|
||||
AudioEngine::~AudioEngine()
|
||||
|
@ -45,6 +46,11 @@ void AudioEngine::fill_buffer(FixedArray<Sample>& buffer)
|
|||
for (size_t note = 0; note < note_count; ++note) {
|
||||
if (!m_note_on[note])
|
||||
continue;
|
||||
|
||||
m_power[note] -= m_decay_step;
|
||||
if (m_power[note] < 0)
|
||||
m_power[note] = 0;
|
||||
|
||||
double val = 0;
|
||||
switch (m_wave) {
|
||||
case Wave::Sine:
|
||||
|
@ -70,16 +76,6 @@ void AudioEngine::fill_buffer(FixedArray<Sample>& buffer)
|
|||
buffer[i].right = buffer[i].left;
|
||||
}
|
||||
|
||||
if (m_decay) {
|
||||
for (size_t note = 0; note < note_count; ++note) {
|
||||
if (m_note_on[note]) {
|
||||
m_power[note] -= m_decay / 100.0;
|
||||
if (m_power[note] < 0)
|
||||
m_power[note] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (m_delay) {
|
||||
if (m_delay_buffers.size() >= m_delay) {
|
||||
auto to_blend = m_delay_buffers.dequeue();
|
||||
|
@ -201,10 +197,22 @@ void AudioEngine::set_wave(Direction direction)
|
|||
}
|
||||
}
|
||||
|
||||
static inline double calculate_step(double distance, int milliseconds)
|
||||
{
|
||||
if (milliseconds == 0)
|
||||
return distance;
|
||||
|
||||
constexpr double samples_per_millisecond = sample_rate / 1000.0;
|
||||
double samples = milliseconds * samples_per_millisecond;
|
||||
double step = distance / samples;
|
||||
return step;
|
||||
}
|
||||
|
||||
void AudioEngine::set_decay(int decay)
|
||||
{
|
||||
ASSERT(decay >= 0);
|
||||
m_decay = decay;
|
||||
m_decay_step = calculate_step(1, m_decay);
|
||||
}
|
||||
|
||||
void AudioEngine::set_delay(int delay)
|
||||
|
|
|
@ -77,7 +77,8 @@ private:
|
|||
|
||||
int m_octave { 4 };
|
||||
int m_wave { first_wave };
|
||||
int m_decay { 0 };
|
||||
int m_decay;
|
||||
double m_decay_step;
|
||||
int m_delay { 0 };
|
||||
|
||||
int m_time { 0 };
|
||||
|
|
|
@ -91,7 +91,7 @@ KnobsWidget::KnobsWidget(GUI::Widget* parent, AudioEngine& audio_engine, MainWid
|
|||
m_wave_value->set_text(wave_strings[new_wave]);
|
||||
};
|
||||
|
||||
constexpr int max_decay = 20;
|
||||
constexpr int max_decay = 1000;
|
||||
m_decay_knob = GUI::Slider::construct(Orientation::Vertical, m_knobs_container);
|
||||
m_decay_knob->set_range(0, max_decay);
|
||||
m_decay_knob->set_value(max_decay - m_audio_engine.decay());
|
||||
|
|
Loading…
Add table
Reference in a new issue