-
Notifications
You must be signed in to change notification settings - Fork 2
/
waveflavour.cpp
213 lines (182 loc) · 4.6 KB
/
waveflavour.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "waveflavour.h"
#include "maximilian.h"
#include <math.h>
#include <stdio.h>
convert mtof;
// __PhaseCounter__________________________________________________________________________
//
void PhaseCounter::start(float d, float m) {
x=0;
oldX = 0;
dx = d;
max = m;
}
bool PhaseCounter::flipped() {
if ((int)oldX == (int)x) { return false; }
return true;
}
bool PhaseCounter::wrapped() {
if (oldX > x) { return true; }
return false;
}
int PhaseCounter::next() {
oldX = x;
x = x + dx;
if (x < 0) { x = 0; }
if (x >= max) { x = 0; }
return (int)x;
}
// __WaveTable_________________________________________________________________________
//
void WaveTable::startCommon() {
len=TABLE_LEN;
iRev = 0;
iInv = 0;
setReverseParams(0,1000);
setInvertParams(0,1000);
}
void WaveTable::setReverseParams(int dr, int mr) {
revTrigger.start(dr,mr);
}
void WaveTable::setInvertParams(int di, int mi) {
invTrigger.start(di,mi);
}
void WaveTable::startRamp() {
startCommon();
for (int i=0;i<len;i++) {
wave[i] = (float)i/(float)len;
}
}
void WaveTable::startSin(float a) {
startCommon();
float da;
da = (3.1415*2)/len;
for (int i=0;i<len;i++) {
wave[i] = sin(a);
a+=da;
}
}
void WaveTable::startSquare() {
startCommon();
for (int i=0;i<len;i++) {
wave[i] = i < len/2?-1:1;
}
}
void WaveTable::reverse() {
double tmp;
revTrigger.next();
if (revTrigger.wrapped()) {
tmp = wave[iRev];
wave[iRev] = wave[len-iRev];
wave[len-iRev]=tmp;
iRev++;
if (iRev>=len) { iRev=0; }
}
}
void WaveTable::invert() {
double tmp;
invTrigger.next();
if (invTrigger.wrapped()) {
tmp = wave[iInv]+1;
wave[iInv]=(2-tmp)-1;
iInv++;
if (iInv>=len) { iInv=0; }
}
}
void WaveTable::swap(WaveTable* other, int c) {
double tmp;
tmp = wave[c];
wave[c]=other->wave[c];
other->wave[c]=tmp;
}
// __Voice_____________________________________________________________________
//
void Voice::start(WaveTable* table, int nv, int* pOffs, float freq) {
this->table = table;
noVoices = nv;
for (int i=0;i<noVoices;i++) {
p[i].start(freq,TABLE_LEN);
pitchOffsets[i]=pOffs[i];
}
offsetter.start(0,TABLE_LEN);
volume=1;
globalPitchOffset = 0;
}
float calculatePitch(int n) {
float freq = mtof.mtof(n);
float x = 44100/freq;
return TABLE_LEN/x;
}
void Voice::setPitch(int n) {
note = n;
for (int i=0;i<noVoices;i++) {
p[i].dx=calculatePitch(n+pitchOffsets[i]+globalPitchOffset);
}
}
double Voice::next() {
int offset = offsetter.next();
double vTot=0;
for (int i=0;i<noVoices;i++) {
int c = p[i].next();
int oset = (c+offset)%TABLE_LEN;
vTot = vTot + ((table->wave[c]+table->wave[oset])/2);
}
return volume*vTot/noVoices;
}
// __Instrument_____________________________________________________________________________
//
void Instrument::start(int waveform1, int waveform2, int noVoices, int* pitchOffsets) {
switch (waveform1) {
case SIN:
table1.startSin(0);
break;
case RAMP:
table1.startRamp();
break;
case SQUARE:
table1.startSquare();
break;
}
switch (waveform2) {
case SIN:
table2.startSin(0);
break;
case RAMP:
table2.startRamp();
break;
case SQUARE:
table2.startSquare();
break;
}
voice.start(&table1, noVoices, pitchOffsets,0);
}
void Instrument::setParams(int revInc, int reverse, int invInc, int invert, int swapInc, int swap, float dPhase, int globalPitchOffset) {
table1.setReverseParams(revInc,reverse);
table1.setInvertParams(invInc,invert);
voice.globalPitchOffset = globalPitchOffset;
voice.offsetter.dx = dPhase;
swapIndex = 0;
swapTrigger.start(swapInc,swap);
}
void Instrument::setEnv(float attack, float release, float sustain) {
envAttack = attack;
envRelease = release;
envSustain = sustain;
}
double Instrument::next(int pitch, int trigger) {
if (trigger) {
voice.setPitch(pitch);
}
double o = env.ar(voice.next(),envAttack,envRelease,envSustain,trigger);
table1.reverse();
table1.invert();
swapTrigger.next();
if (swapTrigger.wrapped()) {
table1.swap(&table2,swapIndex);
}
swapIndex++;
if (swapIndex > table1.len) {
swapIndex = 1;
}
return o;
}