DaisySP
Loading...
Searching...
No Matches
adsr.h
1/*
2Copyright (c) 2020 Electrosmith, Corp, Paul Batchelor
3
4Use of this source code is governed by an MIT-style
5license that can be found in the LICENSE file or at
6https://opensource.org/licenses/MIT.
7*/
8
9#pragma once
10#ifndef DSY_ADSR_H
11#define DSY_ADSR_H
12
13#include <stdint.h>
14#ifdef __cplusplus
15
16namespace daisysp
17{
24enum
25{
26 ADSR_SEG_IDLE = 0,
27 ADSR_SEG_ATTACK = 1,
28 ADSR_SEG_DECAY = 2,
29 ADSR_SEG_RELEASE = 4
30};
31
32
41class Adsr
42{
43 public:
44 Adsr() {}
45 ~Adsr() {}
49 void Init(float sample_rate, int blockSize = 1);
54 void Retrigger(bool hard);
58 float Process(bool gate);
62 void SetTime(int seg, float time);
63 void SetAttackTime(float timeInS, float shape = 0.0f);
64 void SetDecayTime(float timeInS);
65 void SetReleaseTime(float timeInS);
66
67 private:
68 void SetTimeConstant(float timeInS, float& time, float& coeff);
69
70 public:
74 inline void SetSustainLevel(float sus_level)
75 {
76 sus_level = (sus_level <= 0.f) ? -0.01f // forces envelope into idle
77 : (sus_level > 1.f) ? 1.f : sus_level;
78 sus_level_ = sus_level;
79 }
83 inline uint8_t GetCurrentSegment() { return mode_; }
87 inline bool IsRunning() const { return mode_ != ADSR_SEG_IDLE; }
88
89 private:
90 float sus_level_{0.f};
91 float x_{0.f};
92 float attackShape_{-1.f};
93 float attackTarget_{0.0f};
94 float attackTime_{-1.0f};
95 float decayTime_{-1.0f};
96 float releaseTime_{-1.0f};
97 float attackD0_{0.f};
98 float decayD0_{0.f};
99 float releaseD0_{0.f};
100 int sample_rate_;
101 uint8_t mode_{ADSR_SEG_IDLE};
102 bool gate_{false};
103};
104} // namespace daisysp
105#endif
106#endif
Definition adsr.h:42
void SetSustainLevel(float sus_level)
Definition adsr.h:74
float Process(bool gate)
Definition adsr.cpp:95
uint8_t GetCurrentSegment()
Definition adsr.h:83
void Init(float sample_rate, int blockSize=1)
Definition adsr.cpp:7
bool IsRunning() const
Definition adsr.h:87
void Retrigger(bool hard)
Definition adsr.cpp:25
void SetTime(int seg, float time)
Definition adsr.cpp:32
FIR Filter implementation, generic and ARM CMSIS DSP based.
Definition adenv.h:16