-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
259 lines (238 loc) · 5.98 KB
/
main.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <stdbool.h>
#include <stdint.h>
#include <driverlib/pwm.h>
#include <driverlib/gpio.h>
#include <driverlib/adc.h>
#include <driverlib/sysctl.h>
#include <inc/hw_memmap.h>
#include <driverlib/interrupt.h>
#include <driverlib/timer.h>
#include <driverlib/rom_map.h>
#include <driverlib/debug.h>
#include "systeminit.h"
#include "input.h"
#include "oscillator.h"
#include "tables.h"
#define APP_PI 3.141592653589793f
/* System variables */
struct {
uint16_t readAInputs :1;
uint16_t readDInputs :1;
uint16_t modulate :1;
uint16_t outputNextSample :1;
} system_flags;
volatile uint32_t g_sampleCount;
uint32_t g_ui32SysClock;
/* Interrupts */
void Timer0AIntHandler(void);
void Timer1AIntHandler(void);
void Timer2AIntHandler(void);
void PWMGen2IntHandler(void);
void ADCInt0Handler(void);
/* Synth modules */
Osc mainOsc1,mainOsc2;
Osc pitchLFO;
Env volEnv;
Env pitchAmpEnv;
Env cutoffEnv;
Knob knobs[NUM_KNOBS];
q31_t SVFilter(q31_t sample, q31_t* buffer, q31_t F, q31_t Q){
q31_t LP=0;
q31_t HP=0;
q31_t BP=0;
F = getFilterFreq(F);
LP = __SSAT(((q63_t)F*buffer[1])>>31,31);
LP = __SSAT(buffer[0] + LP,31);
HP = __SSAT(((q63_t)buffer[1]*Q)>>31,31);
HP = __SSAT(sample-HP-LP,31);
BP = __SSAT(((q63_t)F*HP)>>31,31);
BP = __SSAT(BP + buffer[1],31);
buffer[1] = BP;
buffer[0] = LP;
return LP;
}
void setMainOscNote(uint16_t note) {
setOscNote(&mainOsc1, note);
setOscNote(&mainOsc2, note+12);
}
int16_t seq[] = {
0,12,24,36,48,60};
uint8_t seqLength = 6;
int16_t arpseq[] = {
0,3,7,12
};
uint8_t arpseqlength = 4;
volatile uint16_t seqind = 0;
volatile uint16_t seqCounter=0;
void triggerGate() {
/*
* Trigger envelopes here
*/
triggerEnv(&volEnv);
triggerEnv(&pitchAmpEnv);
triggerEnv(&cutoffEnv);
seqind = 0;
seqCounter = 0;
return;
}
void releaseGate() {
/*
* Release envelopes here
*/
releaseEnv(&volEnv);
releaseEnv(&pitchAmpEnv);
releaseEnv(&cutoffEnv);
return;
}
int main(void) {
g_ui32SysClock = MAP_SysCtlClockFreqSet(
(SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL
| SYSCTL_CFG_VCO_480), 120000000);
/*
* Set sound oscillators
*
*/
initOsc(&mainOsc1);
setOscType(&mainOsc1, SOUND_OSC, SawTable);
setOscNote(&mainOsc1, 0);
initOsc(&mainOsc2);
setOscType(&mainOsc2, SOUND_OSC, SawTable);
setOscNote(&mainOsc2, 0);
/*
* Set up LFOs
*
*/
initOsc(&pitchLFO);
setOscType(&pitchLFO, LFO_OSC, SoftTable);
setOscGain(&pitchLFO, 0x7FFFFFFF);
setOscNote(&pitchLFO, 0);
/*
* Set up envelopes
*
*/
initEnv(&volEnv,SoftTable,SoftTable);
setEnvAtkTime(&volEnv,0.01);
setEnvRelTime(&volEnv,0.01);
initEnv(&pitchAmpEnv,SoftTable,SoftTable);
setEnvAtkTime(&pitchAmpEnv,0.01);
setEnvRelTime(&pitchAmpEnv,0.01);
initEnv(&cutoffEnv,SoftTable,SoftTable);
setEnvAtkTime(&cutoffEnv,0.1);
setEnvRelTime(&cutoffEnv,0.1);
/*
* Set up analog controllers
*
*/
initKnob(&knobs[0], 1.0); // main pitch
initKnob(&knobs[1], 1.0);
initKnob(&knobs[2], 1.0); // pitch lfo gain
initKnob(&knobs[3], 1.0); // cutoff freq.
initKnob(&knobs[4], 0.5); // resonance
/* Initialize system hardware & peripherals */
setupDigitalInputs();
setupAnalogInputs();
setupPWM();
setupTimers();
/* Enable output to the onboard LEDs */
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_0 | GPIO_PIN_1);
MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0 | GPIO_PIN_1, 0x00);
MAP_IntMasterEnable();
setMainOscNote(24);
triggerGate();
q31_t OUTPUT_SCALE = (PWM_PERIOD-1)>>1;
q31_t cutoff = 0;
q31_t nextSample1,nextSample2;
q31_t buffer1[2] = {{0}};
q31_t buffer2[2] = {{0}};
while (1) {
if (system_flags.outputNextSample){
g_sampleCount++;
system_flags.outputNextSample = 0;
incrOscPhase(&mainOsc1);
incrOscPhase(&mainOsc2);
incrOscPhase(&pitchLFO);
cutoff=((q63_t)knobs[3].output*cutoffEnv.output)>>31;
nextSample1=mainOsc1.output;
nextSample1=SVFilter(nextSample1,&buffer1[0],cutoff,knobs[4].output);
nextSample1=__SSAT(((q63_t)nextSample1*OUTPUT_SCALE)>>31,31);
nextSample1+=OUTPUT_SCALE;
nextSample2=mainOsc2.output;
nextSample2=SVFilter(nextSample2,&buffer2[0],cutoff,knobs[4].output);
nextSample2=__SSAT(((q63_t)nextSample2*OUTPUT_SCALE)>>31,31);
nextSample2+=OUTPUT_SCALE;
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_5,nextSample1);
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_4,nextSample2);
if (!(g_sampleCount&0xFF)){
// seqCounter+=0x6d3;
// if(seqCounter<0x6d3){
// seqind+=1;
// if(seqind==arpseqlength){
// seqind=0;
// }
// }
//modifyOscNote(&mainOsc1,arpseq[seqind]);
//modifyOscNote(&mainOsc2,arpseq[seqind]);
// Tick modifiers
incrEnvPhase(&volEnv);
incrEnvPhase(&pitchAmpEnv);
incrEnvPhase(&cutoffEnv);
// Modify mainOsc1
modifyOscGain(&mainOsc1, volEnv.output);
modifyOscFreq(&mainOsc1, knobs[0].output);
modifyOscGain(&mainOsc2, volEnv.output);
modifyOscFreq(&mainOsc2, knobs[2].output);
// Modify pitchLFO
modifyOscGain(&pitchLFO, pitchAmpEnv.output);
modifyOscGain(&pitchLFO, knobs[1].output);
// Apply mods
applyMods(&mainOsc1);
applyMods(&mainOsc2);
applyMods(&pitchLFO);
}
}
if (system_flags.readDInputs){
system_flags.readDInputs = 0;
handleDigitalInputs();
}
if (system_flags.readAInputs){
system_flags.readAInputs = 0;
handleAnalogInputs(knobs);
}
}
}
/*
* Sample ouput interrupt.
*
* This timer is clocked at the sample frequency.
*
*/
void SysTickIntHandler(void) {
system_flags.outputNextSample = 1;
}
void Timer0AIntHandler(void) {
MAP_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
system_flags.outputNextSample = 1;
}
/*
* Modulation interrupt.
*
*/
void Timer1AIntHandler(void) {
MAP_TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
system_flags.modulate = 1;
}
/*
* Input interrupt.
*
* - Read and update values from hardware input peripherals.
*
*/
void Timer2AIntHandler(void) {
MAP_TimerIntClear(TIMER2_BASE, TIMER_TIMA_TIMEOUT);
system_flags.readDInputs = 1;
}
void ADCInt0Handler(void) {
ADCIntClear(ADC0_BASE, 0);
system_flags.readAInputs = 1;
}