1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 15:07:45 +00:00

Piano: Add track Volume and improve QOL

This patch implements a couple of enhancements to the synthesizer
engine:

* Each track has a volume control.
* The input and tooltips for all controls are improved.
* The noise channel is pitched, which allows for basic drum synthesis.
This commit is contained in:
kleines Filmröllchen 2021-04-21 20:32:21 +02:00 committed by Andreas Kling
parent 418bc484e4
commit d77e7e99e4
5 changed files with 87 additions and 39 deletions

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2019-2020, William McPherson <willmcpherson2@gmail.com>
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -13,6 +14,7 @@
Track::Track(const u32& time)
: m_time(time)
{
set_volume(volume_max);
set_sustain_impl(1000);
set_attack(5);
set_decay(1000);
@ -81,7 +83,7 @@ void Track::fill_sample(Sample& sample)
note_sample = triangle(note);
break;
case Wave::Noise:
note_sample = noise();
note_sample = noise(note);
break;
case Wave::RecordedSample:
note_sample = recorded_sample(note);
@ -89,8 +91,8 @@ void Track::fill_sample(Sample& sample)
default:
VERIFY_NOT_REACHED();
}
new_sample.left += note_sample.left * m_power[note] * volume;
new_sample.right += note_sample.right * m_power[note] * volume;
new_sample.left += note_sample.left * m_power[note] * volume_factor * (static_cast<double>(volume()) / volume_max);
new_sample.right += note_sample.right * m_power[note] * volume_factor * (static_cast<double>(volume()) / volume_max);
}
if (m_delay) {
@ -188,11 +190,17 @@ Audio::Frame Track::triangle(size_t note)
return w;
}
Audio::Frame Track::noise() const
Audio::Frame Track::noise(size_t note)
{
double random_percentage = static_cast<double>(rand()) / RAND_MAX;
double w = (random_percentage * 2) - 1;
return w;
double step = note_frequencies[note] / sample_rate;
// m_pos keeps track of the time since the last random sample
m_pos[note] += step;
if (m_pos[note] > 0.05) {
double random_percentage = static_cast<double>(rand()) / RAND_MAX;
m_last_w[note] = (random_percentage * 2) - 1;
m_pos[note] = 0;
}
return m_last_w[note];
}
Audio::Frame Track::recorded_sample(size_t note)
@ -308,6 +316,12 @@ void Track::set_wave(Direction direction)
}
}
void Track::set_volume(int volume)
{
VERIFY(volume >= 0);
m_volume = volume;
}
void Track::set_attack(int attack)
{
VERIFY(attack >= 0);