-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnvelopeGenerator.h
65 lines (56 loc) · 1.56 KB
/
EnvelopeGenerator.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
#ifndef __ENVELOPEGENERATOR__
#define __ENVELOPEGENERATOR__
#include <cmath>
#pragma once
#include "GallantSignal.h"
using Gallant::Signal0;
class EnvelopeGenerator
{
public:
enum EnvelopeStage {
ENVELOPE_STAGE_OFF = 0,
ENVELOPE_STAGE_ATTACK,
ENVELOPE_STAGE_DECAY,
ENVELOPE_STAGE_SUSTAIN,
ENVELOPE_STAGE_RELEASE,
kNumEnvelopeStages
};
void enterStage(EnvelopeStage newStage);
double nextSample();
static void setSampleRate(double newSampleRate);
inline EnvelopeStage getCurrentStage() const { return currentStage; }
const double minimumLevel;
void setStageValue(EnvelopeStage stage, double value);
Signal0<> beganEnvelopeCycle;
Signal0<> finishedEnvelopeCycle;
EnvelopeGenerator() :
minimumLevel(0.0001),
currentStage(ENVELOPE_STAGE_OFF),
currentLevel(minimumLevel),
multiplier(1.0),
currentSampleIndex(0),
nextStageSampleIndex(0) {
stageValue[ENVELOPE_STAGE_OFF] = 0.0;
stageValue[ENVELOPE_STAGE_ATTACK] = 0.001;
stageValue[ENVELOPE_STAGE_DECAY] = 0.5;
stageValue[ENVELOPE_STAGE_SUSTAIN] = 0.1;
stageValue[ENVELOPE_STAGE_RELEASE] = 1.0;
};
void reset() {
currentStage = ENVELOPE_STAGE_OFF;
currentLevel = minimumLevel;
multiplier = 1.0;
currentSampleIndex = 0;
nextStageSampleIndex = 0;
}
private:
EnvelopeStage currentStage;
double currentLevel;
double multiplier;
static double sampleRate;
double stageValue[kNumEnvelopeStages];
void calculateMultiplier(double startLevel, double endLevel, unsigned long long lengthInSamples);
unsigned long long currentSampleIndex;
unsigned long long nextStageSampleIndex;
};
#endif