-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlfoExtended.cpp
197 lines (143 loc) · 4.26 KB
/
lfoExtended.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* lfoExtended.cpp
*
* Created on: 30.01.2015
* Author: user
*/
#include "lfoExtended.h"
#include "random.h"
//#define VERBOSE
void lfoExtended::init(uint16_t bastlCycleFrequency) {
currentWaveform = SAW;
numbPhaseStepsToSkip = 0;
threshold = 255;
thresholdType = FOLDING;
currentStep = 0;
lastUnskippedPhase = 0;
currentSlope = 0;
xorBits = 0;
flopBits=0;
currentPhase = 0;
lastTimeStamp = 0;
this->bastlCycleFrequency = bastlCycleFrequency;
}
void lfoExtended::setBastlCyclesPerPeriod(uint16_t bastlCyclesPerPeriod) {
if (bastlCyclesPerPeriod==0) {
phaseIncrement = 0;
return;
}
phaseIncrement = ((uint32_t)65536 + (bastlCyclesPerPeriod>>1))/bastlCyclesPerPeriod;
maxAbsoluteSlope = ((uint16_t)phaseIncrement+100)>>6;
#ifdef VERBOSE
printf("Cycles per Period %u = phaseincrement of %u\n",bastlCyclesPerPeriod,phaseIncrement);
printf("Random max Steps betweene slope change: %u \n\n",maxStepsBetweenSlopeChange);
#endif
}
void lfoExtended::setWaveform(LFOBasicWaveform waveform) {
currentWaveform = waveform;
}
void lfoExtended::setXOR(uint8_t xorBits) {
this->xorBits = xorBits;
}
void lfoExtended::setFlop(uint8_t flopBits) {
this->flopBits = flopBits;
}
void lfoExtended::setResolution(uint8_t stepsPerPeriod) {
if (stepsPerPeriod == 0) stepsPerPeriod = 1; // preventing division by zero
this->numbPhaseStepsToSkip = ((uint32_t)65536/(stepsPerPeriod)-2);
#ifdef VERBOSE
printf("\nPhase steps to skip %u\n",numbPhaseStepsToSkip);
#endif
}
void lfoExtended::setThreshold(uint8_t thres,LFOThresholdType type) {
threshold = thres;
thresholdType = type;
}
void lfoExtended::setToStep(uint8_t step, uint16_t time) {
currentPhase = step << 8;
//lastTimeStamp = time;
}
uint8_t lfoExtended::getValue(uint16_t timestamp) {
while(lastTimeStamp!=timestamp) {
step();
lastTimeStamp++;
}
return getValue();
}
uint8_t lfoExtended::getPhase(){
return currentPhase >> 8;
}
uint8_t lfoExtended::getValue() {
#ifdef VERBOSE
printf("Getting output for %u\n",currentPhase);
#endif
// check if step will be skipped due to resolution
uint16_t phaseStepsSinceLast = currentPhase-lastUnskippedPhase;
#ifdef VERBOSE
printf("Phase Steps since last Call: %u\n",phaseStepsSinceLast);
#endif
// Break down current Phase to current Step //
currentStep = currentPhase >> 8;
// check if flopping is taking affect
if ((currentStep & flopBits)) {
return 0;
}
// check if resolution is taking affect
if (phaseStepsSinceLast < numbPhaseStepsToSkip) {
return currentOutput;
} else {
lastUnskippedPhase += numbPhaseStepsToSkip;
}
// check how many whole steps have passed to decide if new random slope must be chosen
uint8_t stepsSinceLast = currentStep - lastStep;
if (stepsSinceLast) lastStep = currentStep;
if (currentWaveform == RANDOM) {
// count down the steps that this slope is used
stepsUntilNextSlopePick -= stepsSinceLast;
// get new slope and its duration if it's time for it
if (stepsUntilNextSlopePick <= 0) {
currentSlope = bastlRandom::range(0,(2*maxAbsoluteSlope))-maxAbsoluteSlope;
stepsUntilNextSlopePick = bastlRandom::range(20/2,40);
#ifdef VERBOSE
printf("\nPick: %i for %i\n",currentSlope,stepsUntilNextSlopePick);
#endif
}
// check direction of slope if folding
if (thresholdType == FOLDING) {
int16_t tmpCurrentValue = currentOutput+currentSlope;//*stepsSinceLast;
if ((tmpCurrentValue > threshold) || (tmpCurrentValue <0)) {
currentSlope = -currentSlope;
}
}
// add new slope to current output value
currentOutput += currentSlope;//*stepsSinceLast;
} else {
// render Basic Waveform
switch(currentWaveform) {
case SAW: {
currentOutput = currentStep;
break;
}
case TRIANGLE: {
if (currentStep < 128) currentOutput = currentStep*2;
else currentOutput = 255-((currentStep*2)-255);
break;
}
default: break;
}
}
// Apply Overflowing
if (currentOutput > threshold) {
if (thresholdType == OVERFLOW) {
currentOutput = currentOutput % threshold;
} else {
// FOLDING
uint8_t sectorNumber = currentOutput/threshold;
if (sectorNumber & 1) currentOutput = threshold - (currentOutput % threshold);
else currentOutput = currentOutput % threshold;
}
}
// apply XOR Bits
currentOutput ^= xorBits;
return currentOutput;
}