This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
morphedWavetable.cpp
129 lines (107 loc) · 2.83 KB
/
morphedWavetable.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
/***** morphedWavetable.cpp *****/
#include "morphedWavetable.h"
morphedWavetable::morphedWavetable(wavetable* a, wavetable* b) {
srand (time(NULL));
container[0] = a;
container[1] = b;
morphType = backAndForth;
phaser = 0.0;
numTables = 2;
currentTable = 0;
countUp = true;
randomInt = 1;
lastRandomInt = 0;
backAndForthIndex = 0;
modPhaser = 0;
}
morphedWavetable::morphedWavetable(wavetable* a, wavetable* b, wavetable* c) {
srand (time(NULL));
container[0] = a;
container[1] = b;
container[2] = c;
morphType = backAndForth;
phaser = 0.0;
numTables = 3;
currentTable = 0;
countUp = true;
randomInt = 1;
lastRandomInt = 0;
backAndForthIndex = 0;
modPhaser = 0;
}
morphedWavetable::morphedWavetable(wavetable* a, wavetable* b, wavetable* c, wavetable* d) {
srand (time(NULL));
container[0] = a;
container[1] = b;
container[2] = c;
container[3] = d;
morphType = backAndForth;
phaser = 0.0;
numTables = 4;
currentTable = 0;
countUp = true;
randomInt = 1;
lastRandomInt = 0;
backAndForthIndex = 0;
modPhaser = 0;
}
float morphedWavetable::outputMorph(int morphType) {
float numSamplesToCrossFade = morphSpeed * 44100;
phaserInterval = 1.0 / numSamplesToCrossFade;
float outA;
float outB;
switch(morphType) {
case backAndForth: {
if (countUp) {
outA = container[backAndForthIndex]->getTableOut() * powf(-phaser + 1, 2);
outB = container[backAndForthIndex + 1]->getTableOut() * powf(phaser, 2);
break;
}
outA = container[backAndForthIndex - 1]->getTableOut() * powf(phaser, 2);
outB = container[backAndForthIndex]->getTableOut() * powf(-phaser + 1, 2);
break;
}
case fullCircle: {
outA = container[currentTable]->getTableOut() * powf(-phaser + 1, 2);
outB = container[(currentTable + 1) % numTables]->getTableOut() * powf(phaser, 2);
break;
}
case random: {
outA = container[lastRandomInt]->getTableOut() * powf(-phaser + 1, 2);
outB = container[randomInt]->getTableOut() * powf(phaser, 2);
break;
}
case hard: {
outA = container[currentTable]->getTableOut();
outB = 0;
break;
}
}
phaser += phaserInterval;
if (phaser >= 1.0 ) {
phaser = 0.0;
currentTable += 1;
currentTable = currentTable % numTables;
lastRandomInt = randomInt;
getRandomInt();
if (countUp) backAndForthIndex += 1;
else backAndForthIndex -= 1;
if (backAndForthIndex == numTables - 1 || backAndForthIndex == 0)
countUp = !countUp;
}
return outA + outB;
}
void morphedWavetable::getRandomInt() {
randomInt = rand() % numTables;
if (randomInt == lastRandomInt)
getRandomInt();
}
void morphedWavetable::setMorphSpeed(float potInput) {
morphSpeed = powf(potInput, 2) * 5;
}
void morphedWavetable::setMorphMod(float potInput) {
modInterval = potInput;
modPhaser += modInterval;
if (modPhaser >= TWO_PI) modPhaser = modPhaser - TWO_PI;
mod = sinf(modPhaser);
}