Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
instrument_envelope.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <span>
5
6namespace pulp::audio {
7
8enum class EnvelopeStage : std::uint8_t {
15};
16
18 double sample_rate = 48000.0;
19 double attack_seconds = 0.005;
20 double hold_seconds = 0.0;
21 double decay_seconds = 0.050;
22 double sustain_level = 1.0;
23 double release_seconds = 0.050;
24};
25
27public:
28 AhdsrEnvelope() = default;
29
30 // Call prepare() off the audio thread before realtime use. Invalid
31 // prepare() calls leave the previous configuration/state untouched.
32 bool prepare(const AhdsrEnvelopeConfig& config) noexcept;
33 void reset() noexcept;
34
35 // RT-safe after prepare(). note_on() restarts from zero for non-zero
36 // attacks; the first emitted attack sample is advanced by one frame.
37 void note_on() noexcept;
38 void note_off() noexcept;
39
40 float next_sample() noexcept;
41 // Writes envelope gain values into destination; does not multiply audio.
42 void render(std::span<float> destination) noexcept;
43
44 EnvelopeStage stage() const noexcept { return stage_; }
45 float value() const noexcept { return value_; }
46 bool active() const noexcept { return stage_ != EnvelopeStage::Idle; }
47
48private:
49 static bool config_valid(const AhdsrEnvelopeConfig& config) noexcept;
50 static bool duration_frames_valid(double seconds,
51 double sample_rate) noexcept;
52 static std::uint64_t seconds_to_frames(double seconds,
53 double sample_rate) noexcept;
54
55 void enter_stage(EnvelopeStage stage) noexcept;
56 void advance_from_peak_stage(bool preserve_current_value) noexcept;
57
58 AhdsrEnvelopeConfig config_{};
60 std::uint64_t stage_frame_ = 0;
61 std::uint64_t attack_frames_ = 0;
62 std::uint64_t hold_frames_ = 0;
63 std::uint64_t decay_frames_ = 0;
64 std::uint64_t release_frames_ = 0;
65 float value_ = 0.0f;
66 float release_start_ = 0.0f;
67};
68
69} // namespace pulp::audio
EnvelopeStage stage() const noexcept
Definition instrument_envelope.hpp:44
bool active() const noexcept
Definition instrument_envelope.hpp:46
bool prepare(const AhdsrEnvelopeConfig &config) noexcept
void note_off() noexcept
float value() const noexcept
Definition instrument_envelope.hpp:45
float next_sample() noexcept
void render(std::span< float > destination) noexcept
Definition analyzer_provider.hpp:12
EnvelopeStage
Definition instrument_envelope.hpp:8
@ Decay
Definition instrument_envelope.hpp:12
@ Release
Definition instrument_envelope.hpp:14
@ Hold
Definition instrument_envelope.hpp:11
@ Sustain
Definition instrument_envelope.hpp:13
@ Attack
Definition instrument_envelope.hpp:10
@ Idle
Definition instrument_envelope.hpp:9
STL namespace.
Definition instrument_envelope.hpp:17
double hold_seconds
Definition instrument_envelope.hpp:20
double release_seconds
Definition instrument_envelope.hpp:23
double attack_seconds
Definition instrument_envelope.hpp:19
double sample_rate
Definition instrument_envelope.hpp:18
double sustain_level
Definition instrument_envelope.hpp:22
double decay_seconds
Definition instrument_envelope.hpp:21