/
audio.h
160 lines (146 loc) · 3.83 KB
/
audio.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
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
#include <MozziGuts.h>
#include <EventDelay.h>
#include <Oscil.h>
#include <tables/sin256_int8.h>
#include <tables/square256_int8.h>
#include <tables/saw256_int8.h>
#include <tables/triangle512_int8.h> //temp!
#define CONTROL_RATE 64
Oscil<SIN256_NUM_CELLS, AUDIO_RATE> sine1(SIN256_DATA), sine2(SIN256_DATA);
Oscil<SAW256_NUM_CELLS, AUDIO_RATE> saw1(SAW256_DATA), saw2(SAW256_DATA),
sqr1_saw1(SAW256_DATA), sqr1_saw2(SAW256_DATA), sqr2_saw1(SAW256_DATA), sqr2_saw2(SAW256_DATA);
Oscil<TRIANGLE512_NUM_CELLS, AUDIO_RATE> triangle1(TRIANGLE512_DATA), triangle2(TRIANGLE512_DATA);
void setupAudio() {
startMozzi(CONTROL_RATE);
unsigned int freq = 440u; //A4
sine1.setFreq(freq);
sqr1_saw1.setFreq(freq);
sqr1_saw2.setFreq(freq);
sqr2_saw1.setFreq(freq);
sqr2_saw2.setFreq(freq);
saw1.setFreq(freq);
triangle1.setFreq(freq);
sine2.setFreq(freq);
saw2.setFreq(freq);
triangle2.setFreq(freq);
}
void resetPhase(Oscillator osc, unsigned int channel) {
if(osc.waveForm == SINE) {
if(channel == 0) {
sine1.setPhase(0);
} else {
sine2.setPhase(0);
}
} else if(osc.waveForm == SQUARE) {
if(channel == 0) {
sqr1_saw1.setPhase(0);
sqr1_saw2.setPhase(osc.pw);
} else {
sqr2_saw1.setPhase(0);
sqr2_saw2.setPhase(osc.pw);
}
} else if(osc.waveForm == SAW) {
if(channel == 0) {
saw1.setPhase(0);
} else {
saw2.setPhase(0);
}
} else if(osc.waveForm == TRIANGLE) {
if(channel == 0) {
triangle1.setPhase(0);
} else {
triangle2.setPhase(0);
}
}
}
void changeFreq(Oscillator osc, unsigned int channel) {
unsigned int freq = (unsigned int)(440u * pow(2, (double)osc.octave));
float ratio;
if(osc.tune > 0) {
ratio = osc.tune * .059463 + 1;
freq *= ratio;
Serial.println(freq);
} else if (osc.tune < 0) {
ratio = (-osc.tune) * .059463 + 1;
freq /= ratio;
Serial.println(freq);
}
if(osc.waveForm == SINE) {
if(channel == 0) {
sine1.setFreq(freq);
} else {
sine2.setFreq(freq);
}
} else if(osc.waveForm == SQUARE) {
if(channel == 0) {
sqr1_saw1.setFreq(freq);
sqr1_saw2.setFreq(freq);
} else {
sqr2_saw1.setFreq(freq);
sqr2_saw2.setFreq(freq);
}
} else if(osc.waveForm == SAW) {
if(channel == 0) {
saw1.setFreq(freq);
} else {
saw2.setFreq(freq);
}
} else if(osc.waveForm == TRIANGLE) {
if(channel == 0) {
triangle1.setFreq(freq);
} else {
triangle2.setFreq(freq);
}
}
}
int nextSample(WaveForm wv, unsigned int channel) {
if(wv == SINE) {
return channel == 0 ? sine1.next() : sine2.next();
} else if(wv == SQUARE) {
return channel == 0 ? (sqr1_saw1.next() - sqr1_saw2.next())/2 : (sqr2_saw1.next() - sqr2_saw2.next())/2;
} else if(wv == SAW) {
return channel == 0 ? saw1.next() : saw2.next();
} else if(wv == TRIANGLE) {
return channel == 0 ? triangle1.next() : triangle2.next();
}
return 0;
}
WaveForm nextOscillator(Oscillator osc) {
if(osc.waveForm == SINE) {
return SQUARE;
}
else if(osc.waveForm == SQUARE) {
return SAW;
}
else if(osc.waveForm == SAW) {
return TRIANGLE;
}
else if(osc.waveForm == TRIANGLE) {
return SILENCE;
}
else if(osc.waveForm == SILENCE){
return SINE;
}
}
WaveForm prevOscillator(Oscillator osc) {
if(osc.waveForm == SINE) {
return SILENCE;
}
else if(osc.waveForm == SQUARE) {
return SINE;
}
else if(osc.waveForm == SAW) {
return SQUARE;
}
else if(osc.waveForm == TRIANGLE) {
return SAW;
}
else if(osc.waveForm == SILENCE){
return TRIANGLE;
}
}
int updateAudio(){
int osc1Sample = nextSample(oscStage.osc1.waveForm, 0);
int osc2Sample = nextSample(oscStage.osc2.waveForm, 1);
return (int)((osc1Sample * (1 - oscStage.mix)) + (osc2Sample * oscStage.mix));
}