sequencer
envelope.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sequencer/assert.hpp>
5 
6 #include <cassert>
7 #include <limits>
8 
9 namespace sequencer::audio
10 {
11  class envelope_t
12  {
13  public:
14  double operator()( double t ) const noexcept
15  {
16  SEQUENCER_ASSERT( t >= 0 )
17  if ( t < attack() )
18  {
19  return t / attack();
20  }
21  const auto decay_start = attack() + sustain();
22  if ( t < decay_start )
23  {
24  return 1;
25  }
26  if ( t < decay_start + decay() )
27  {
28  return 1 - ( t - decay_start ) / decay();
29  }
30  return 0;
31  }
32 
33  void set_attack( double attack ) noexcept
34  {
35  attack_ = attack;
36  }
37 
38  double attack() const noexcept
39  {
40  return attack_;
41  }
42 
43  void set_sustain( double sustain ) noexcept
44  {
45  sustain_ = sustain;
46  }
47 
48  double sustain() const noexcept
49  {
50  return sustain_;
51  }
52 
53  void set_decay( double decay ) noexcept
54  {
55  decay_ = decay;
56  }
57 
58  double decay() const noexcept
59  {
60  return decay_;
61  }
62 
63  private:
64  copyable_atomic< double > attack_{0};
65  copyable_atomic< double > sustain_{std::numeric_limits< double >::max()};
66  copyable_atomic< double > decay_{0};
67  };
68 } // namespace sequencer::audio
double attack() const noexcept
Definition: envelope.hpp:38
double decay() const noexcept
Definition: envelope.hpp:58
#define SEQUENCER_ASSERT(cond)
Definition: assert.hpp:8
void set_decay(double decay) noexcept
Definition: envelope.hpp:53
Definition: envelope.hpp:11
void set_sustain(double sustain) noexcept
Definition: envelope.hpp:43
Definition: delay.hpp:12
void set_attack(double attack) noexcept
Definition: envelope.hpp:33
double operator()(double t) const noexcept
Definition: envelope.hpp:14
double sustain() const noexcept
Definition: envelope.hpp:48