diff options
author | kleines Filmröllchen <malu.bertsch@gmail.com> | 2021-09-28 17:58:21 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-22 22:26:17 +0100 |
commit | cc9aab746257eb2c667df1d43b48917aae040f0d (patch) | |
tree | 0e0ac6a9fc7b8d353a8a0f0e137b6b428d5017d7 /Userland/Libraries/LibDSP | |
parent | a1093abe2613c4fe8f75d4296d32b5aee0e8b380 (diff) | |
download | serenity-cc9aab746257eb2c667df1d43b48917aae040f0d.zip |
LibDSP: Add Envelope abstraction
For the upcoming synthesizer, having an abstracted ADSR envelope concept
is highly desirable. Additionally, Envelope is mostly constexpr and
therefore super fast :^)
Diffstat (limited to 'Userland/Libraries/LibDSP')
-rw-r--r-- | Userland/Libraries/LibDSP/Envelope.h | 70 | ||||
-rw-r--r-- | Userland/Libraries/LibDSP/Music.h | 33 |
2 files changed, 103 insertions, 0 deletions
diff --git a/Userland/Libraries/LibDSP/Envelope.h b/Userland/Libraries/LibDSP/Envelope.h new file mode 100644 index 0000000000..a47543b008 --- /dev/null +++ b/Userland/Libraries/LibDSP/Envelope.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/StdLibExtras.h> + +namespace LibDSP { + +enum class EnvelopeState : u8 { + Off, + Attack, + Decay, + Sustain, + Release, +}; + +struct Envelope { + constexpr Envelope() = default; + constexpr Envelope(double envelope) + : envelope(envelope) + { + } + + constexpr bool is_attack() const { return 0 <= envelope && envelope < 1; } + constexpr double attack() const { return clamp(envelope, 0, 1); } + constexpr void set_attack(double offset) { envelope = offset; } + static constexpr Envelope from_attack(double attack) { return Envelope(attack); } + + constexpr bool is_decay() const { return 1 <= envelope && envelope < 2; } + constexpr double decay() const { return clamp(envelope, 1, 2) - 1; } + constexpr void set_decay(double offset) { envelope = 1 + offset; } + static constexpr Envelope from_decay(double decay) { return Envelope(decay + 1); } + + constexpr bool is_sustain() const { return 2 <= envelope && envelope < 3; } + constexpr double sustain() const { return clamp(envelope, 2, 3) - 2; } + constexpr void set_sustain(double offset) { envelope = 2 + offset; } + static constexpr Envelope from_sustain(double decay) { return Envelope(decay + 2); } + + constexpr bool is_release() const { return 3 <= envelope && envelope < 4; } + constexpr double release() const { return clamp(envelope, 3, 4) - 3; } + constexpr void set_release(double offset) { envelope = 3 + offset; } + static constexpr Envelope from_release(double decay) { return Envelope(decay + 3); } + + constexpr bool is_active() const { return 0 <= envelope && envelope < 4; } + + constexpr void reset() { envelope = -1; } + + constexpr operator EnvelopeState() + { + if (!is_active()) + return EnvelopeState::Off; + if (is_attack()) + return EnvelopeState::Attack; + if (is_decay()) + return EnvelopeState::Decay; + if (is_sustain()) + return EnvelopeState::Sustain; + if (is_release()) + return EnvelopeState::Release; + VERIFY_NOT_REACHED(); + } + + double envelope { -1 }; +}; + +} diff --git a/Userland/Libraries/LibDSP/Music.h b/Userland/Libraries/LibDSP/Music.h index fed5112571..65f3c9c92a 100644 --- a/Userland/Libraries/LibDSP/Music.h +++ b/Userland/Libraries/LibDSP/Music.h @@ -11,6 +11,7 @@ #include <AK/Variant.h> #include <AK/Vector.h> #include <LibAudio/Buffer.h> +#include <LibDSP/Envelope.h> namespace LibDSP { @@ -26,6 +27,38 @@ struct RollNote { u32 off_sample; u8 pitch; i8 velocity; + + Envelope to_envelope(u32 time, u32 attack_samples, u32 decay_samples, u32 release_samples) + { + i64 time_since_end = static_cast<i64>(time) - static_cast<i64>(off_sample); + // We're before the end of this note. + if (time_since_end < 0) { + i64 time_since_start = static_cast<i64>(time) - static_cast<i64>(on_sample); + if (time_since_start < 0) + return {}; + + if (time_since_start < attack_samples) { + if (attack_samples == 0) + return Envelope::from_attack(0); + return Envelope::from_attack(static_cast<double>(time_since_start) / static_cast<double>(attack_samples)); + } + if (time_since_start < attack_samples + decay_samples) { + if (decay_samples == 0) + return Envelope::from_decay(0); + return Envelope::from_decay(static_cast<double>(time_since_start - attack_samples) / static_cast<double>(decay_samples)); + } + // This is a note-dependent value! + u32 sustain_samples = length() - attack_samples - decay_samples; + return Envelope::from_sustain(static_cast<double>(time_since_start - attack_samples - decay_samples) / static_cast<double>(sustain_samples)); + } + + // Overshot the release time + if (time_since_end > release_samples) + return {}; + return Envelope::from_release(static_cast<double>(time_since_end) / static_cast<double>(release_samples)); + } + + constexpr bool is_playing(u32 time) { return on_sample <= time && time <= off_sample; } }; enum class SignalType : u8 { |