-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAHDSR.h
107 lines (103 loc) · 2.89 KB
/
DAHDSR.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
==============================================================================
DAHDSR.h
Created: 6 Oct 2020 1:07:11pm
Author: Hayden Setlik
==============================================================================
*/
#pragma once
#include <JuceHeader.h>
#include <math.h>
#include "ParameterStructure.h"
class DAHDSR
{
public:
enum envPhase
{
delayPhase,
attackPhase,
holdPhase,
decayPhase,
sustainPhase,
releasePhase,
noteOff
};
juce::AudioProcessorValueTreeState* tree;
//functions
DAHDSR(int ind, juce::AudioProcessorValueTreeState* t) : tree(t), factor(1.0f), sampleRate(44100), index(ind)
{
trigger = false;
samplesIntoPhase = 0;
currentPhase = noteOff;
auto iStr = juce::String(ind);
delayId = "delayParam" + iStr;
attackId = "attackParam" + iStr;
holdId = "holdParam" + iStr;
decayId = "decayParam" + iStr;
sustainId = "sustainParam" + iStr;
releaseId = "releaseParam" + iStr;
}
~DAHDSR() {}
void triggerOn()
{
trigger = true;
samplesInPhase = floor(delayTime * (sampleRate / 1000));
samplesIntoPhase = 0;
currentPhase = delayPhase;
}
float valueOf(juce::String& str)
{
return *tree->getRawParameterValue(str);
}
void updateParams()
{
delayTime = valueOf(delayId);
attackTime = valueOf(attackId);
holdTime = valueOf(holdId);
decayTime = valueOf(decayId);
sustainLevel = valueOf(sustainId);
releaseTime = valueOf(releaseId);
}
void triggerOff()
{
trigger = false;
currentPhase = releasePhase;
samplesIntoPhase = 0;
samplesInPhase = releaseTime * (sampleRate / 1000);
factor = exp((log(minLevel) - log(sustainLevel)) /samplesInPhase);
}
void setSampleRate(double value) {sampleRate = value;}
float process(float input);
envPhase getPhase() {return currentPhase;}
double output;
private:
//data
static unsigned long long phaseSafe(unsigned long long input) //to avoid divide-by-zero errors
{
if(input > 0)
return input;
else
return 1;
}
envPhase currentPhase;
unsigned long long samplesIntoPhase;
unsigned long long samplesInPhase;
double factor;
float minLevel = 0.00001f;
double sampleRate;
int index;
bool trigger;
juce::String delayId;
juce::String attackId;
juce::String holdId;
juce::String decayId;
juce::String sustainId;
juce::String releaseId;
private:
float delayTime = 0.0f;
float attackTime = 20.0f;
float holdTime = 0.0f;
float decayTime = 100.0f;
float sustainLevel = 0.6f;
float releaseTime = 40.0f;
};