forked from pichenettes/eurorack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedges.cc
executable file
·246 lines (206 loc) · 6.89 KB
/
edges.cc
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
// Copyright 2012 Emilie Gillet.
//
// Author: Emilie Gillet ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "avrlibx/system/init.h"
#include "avrlibx/system/time.h"
#include "avrlibx/system/timer.h"
#include "avrlibx/utils/op.h"
#include "edges/adc_acquisition.h"
#include "edges/audio_buffer.h"
#include "edges/digital_oscillator.h"
#include "edges/hardware_config.h"
#include "edges/midi.h"
#include "edges/midi_handler.h"
#include "edges/settings.h"
#include "edges/timer_oscillator.h"
#include "edges/ui.h"
using namespace avrlibx;
using namespace edges;
MidiIO midi_io;
MidiBuffer midi_in_buffer;
midi::MidiStreamParser<MidiHandler> midi_parser;
GateInputs gate_inputs;
DACSS dac_ss;
AudioDACInterface audio_dac;
ADCSS adc_ss;
ADCInterface adc;
AudioRateTimer audio_rate_timer;
SyncSwitch sync_switch;
TimerOscillator channel_0;
TimerOscillator channel_1;
TimerOscillator channel_2;
TimerOscillator channel_3;
DigitalOscillator channel_4;
// Channel 1 overflow. If sync is enabled, reset the phase of the channel 2.
ISR(TCD0_OVF_vect) {
Channel2Timer::Restart();
}
volatile uint16_t audio_interrupt_count = 0;
uint8_t channel_remapping[] = { 0, 1, 4, 5, 2, 3, 6, 7};
// Audio rate interrupt (48.048kHz).
ISR(TCE0_OVF_vect) {
// Shove a sample to the DAC.
uint16_t sample = audio_buffer.ImmediateRead();
audio_dac.Strobe();
Word sample_12bits;
sample_12bits.value = sample | 0x3000;
audio_dac.Overwrite(sample_12bits.bytes[1]);
audio_dac.Overwrite(sample_12bits.bytes[0]);
// Step through the ADC pipeline.
int8_t result = adc_acquisition.Cycle();
if (result != -1) {
uint16_t value = adc_acquisition.channel(result);
#ifdef OCTAL_ADC
value = 4095 - value;
result = channel_remapping[result];
#endif // OCTAL_ADC
ui.set_cv(result, value);
}
// When a conversion is finished, update the corresponding channel
// Note that the UI can take over the value read from the DAC, for example
// When programming an arpeggio
switch (result) {
case 0:
channel_1.UpdatePitch<Channel1Timer>(
midi_handler.shift_pitch(0,
settings.dac_to_pitch(0, ui.cv(0))) +
settings.dac_to_fm(0, ui.cv(4)),
settings.pw(0));
channel_0.SubFollow<Channel0Timer>(channel_1);
break;
case 1:
channel_2.UpdatePitch<Channel2Timer>(
midi_handler.shift_pitch(1,
settings.dac_to_pitch(1, ui.cv(1))) +
settings.dac_to_fm(1, ui.cv(5)),
settings.pw(1));
break;
case 2:
channel_3.UpdatePitch<Channel3Timer>(
midi_handler.shift_pitch(2,
settings.dac_to_pitch(2, ui.cv(2))) +
settings.dac_to_fm(2, ui.cv(6)),
settings.pw(2));
break;
case 3:
{
channel_4.UpdatePitch(
midi_handler.shift_pitch(3,
settings.dac_to_pitch(3, ui.cv(3))) +
settings.dac_to_fm(3, ui.cv(7)),
settings.shape(3));
#ifndef OCTAL_ADC
uint8_t cv_controlled_pw_8bit = U16ShiftRight4(ui.cv(3));
channel_1.set_cv_pw(cv_controlled_pw_8bit);
channel_2.set_cv_pw(cv_controlled_pw_8bit);
channel_3.set_cv_pw(cv_controlled_pw_8bit);
#endif // #ifndef OCTAL_ADC
}
break;
#ifdef OCTAL_ADC
case 4:
channel_1.set_cv_pw(U16ShiftRight4(ui.cv(4)));
break;
case 5:
channel_2.set_cv_pw(U16ShiftRight4(ui.cv(5)));
break;
case 6:
channel_3.set_cv_pw(U16ShiftRight4(ui.cv(6)));
break;
case 7:
channel_4.set_cv_pw(U16ShiftRight4(ui.cv(7)));
break;
#endif // #ifdef OCTAL_ADC
default:
{
// Otherwise, scan the gate.
uint8_t gate = ~gate_inputs.value();
uint8_t midi_gate = midi_handler.gate();
bool g_1 = (gate & 1) || (midi_gate & 1) || settings.arpeggio(0);
channel_0.Gate<Channel0>(g_1);
channel_1.Gate<Channel1>(g_1);
bool g_2 = (gate & 2) || (midi_gate & 2) || settings.arpeggio(1);
channel_2.Gate<Channel2>(g_2);
bool g_3 = (gate & 4) || (midi_gate & 4) || settings.arpeggio(2);
channel_3.Gate<Channel3>(g_3);
bool g_4 = (gate & 8) || (midi_gate & 8) || settings.arpeggio(3);
channel_4.Gate(g_4);
// Read some MIDI bytes, but process them later.
if (midi_io.readable()) {
midi_in_buffer.NonBlockingWrite(midi_io.ImmediateRead());
}
}
break;
}
++audio_interrupt_count;
}
inline void Init() {
SysInit();
sync_switch.set_direction(INPUT);
sync_switch.set_mode(PORT_MODE_PULL_UP);
gate_inputs.set_direction(INPUT);
gate_inputs.set_mode(PORT_MODE_PULL_UP);
ui.Init();
// Reset to factory default if any switch is pressed during boot.
settings.Init(!(Switches::Read() & 8));
midi_io.Init();
midi_handler.Init();
audio_dac.Init();
audio_dac.Strobe();
audio_dac.Overwrite(0x1f);
audio_dac.Overwrite(0xff);
channel_0.Init<Channel0, Channel0Timer>();
channel_1.Init<Channel1, Channel1Timer>();
channel_2.Init<Channel2, Channel2Timer>();
channel_3.Init<Channel3, Channel3Timer>();
channel_4.Init();
adc_acquisition.Init();
audio_rate_timer.set_prescaler(TIMER_PRESCALER_CLK);
audio_rate_timer.set_period(665); // 48kHz
audio_rate_timer.set_mode(TIMER_MODE_NORMAL);
audio_rate_timer.EnableOverflowInterrupt(2);
}
int main(void) {
Init();
while (1) {
// Render audio.
channel_4.Render();
// Scan MIDI in.
while (midi_in_buffer.readable()) {
midi_parser.PushByte(midi_in_buffer.ImmediateRead());
}
// Do some UI stuff.
if (audio_interrupt_count >= 48) {
audio_interrupt_count -= 48;
uint8_t gate = ~gate_inputs.value();
// Detect new notes and forward to UI subsystem for calibration functions.
uint8_t mask = 1;
for (uint8_t channel = 0; channel < kNumChannels; ++channel) {
if ((gate & mask) && !(ui.gate() & mask)) {
settings.StepArpeggio(channel);
}
mask <<= 1;
}
uint8_t midi_gate = midi_handler.gate();
ui.set_gate(gate|midi_gate);
ui.Poll();
}
// Check the status of the sync switch
if (sync_switch.value()) {
Channel1Timer::EnableOverflowInterrupt(3);
} else {
Channel1Timer::DisableOverflowInterrupt();
}
}
}