-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspring.cpp
More file actions
272 lines (217 loc) · 5.75 KB
/
spring.cpp
File metadata and controls
272 lines (217 loc) · 5.75 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
This file is part of Virtual Robot's Pine Synthesizer.
Copyright <2022> <Brian Gunnison>
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include "daisy_pod.h"
#include "daisysp.h"
#include "utilities.h"
#include "voice.h"
#include "filter.h"
#include "midimap.h"
#include "controlmap.h"
using namespace daisy;
using namespace daisysp;
#define MIDI_CHANNEL 0
#define AUDIO_BLOCK_SIZE 48
DaisyPod hw;
CpuLoadMeter loadMeter;
Voices voice;
Filters filt;
CCMIDIMap ccmap;
PCMIDIMap pcmap;
CCMIDINoteMap noteMap;
ControlMap standAloneController;
float sampleRate;
float finalGainLeft;
float finalGainRight;
uint32_t noteCounter = 0;
uint32_t outClipIndicator = 0;
#define LOG_CPU_LOAD 0
#if LOG_CPU_LOAD
uint8_t currentCpuLoad = 0;
#endif
void logMidiEvent(MidiEvent *m)
{
if (m->type == NoteOn)
{
NoteOnEvent p = m->AsNoteOn();
log("%d - Note on:\t%d\t%d\t%d", noteCounter, p.channel, p.note, p.velocity);
noteCounter++;
}
if (m->type == NoteOff)
{
NoteOffEvent p = m->AsNoteOff();
log("%d - Note off:\t%d\t%d\t%d", noteCounter, p.channel, p.note, p.velocity);
noteCounter--;
}
if (m->type == ControlChange)
{
ControlChangeEvent p = m->AsControlChange();
log("%d - CC:\t%d\t%d\t%d", noteCounter, p.channel, p.control_number, p.value);
}
if (m->type == ProgramChange)
{
ProgramChangeEvent p = m->AsProgramChange();
log("%d - PG:\t%d\t%d\t%d", noteCounter, p.channel, p.program);
}
noteCounter++;
}
void UpdateControls()
{
hw.ProcessAnalogControls();
hw.ProcessDigitalControls();
standAloneController.Control();
}
void AudioCallback(AudioHandle::InterleavingInputBuffer in, AudioHandle::InterleavingOutputBuffer out, size_t size)
{
float sig = 0;
#if LOG_CPU_LOAD
loadMeter.OnBlockStart();
#endif
UpdateControls();
for (size_t i = 0; i < size; i += 2)
{
sig = filt.Process(voice.Process());
out[i] = sig * finalGainLeft;
out[i + 1] = sig * finalGainRight;
if (out[i] > 1.0 || out[i+1] > 1.0)
{
outClipIndicator++;
}
}
#if LOG_CPU_LOAD
loadMeter.OnBlockEnd();
currentCpuLoad = (uint8_t)(loadMeter.GetAvgCpuLoad() * 100);
#endif
}
void HandleMidiMessage(MidiEvent m)
{
//logMidiEvent(&m);
//if (m.channel != MIDI_CHANNEL)
//{
// return;
//}
switch (m.type)
{
case NoteOn:
{
NoteOnEvent p = m.AsNoteOn();
BlinkLEDs(50, LED_OFF, LED_OFF);
p = m.AsNoteOn();
//log("Note in: %s", GetMidiNoteName(p.note));
p.note = noteMap.Process(p.note, true);
log("Note out: %s", GetMidiNoteName(p.note));
if (p.note > 127)
{
break; // so we can use notes as control
}
voice.NoteOn(&p);
}
break;
case NoteOff:
{
NoteOffEvent p = m.AsNoteOff();
p.note = noteMap.Process(p.note, false);
if (p.note > 127)
{
break; // so we can use notes as control
}
voice.NoteOff(&p);
}
break;
case ControlChange:
{
ControlChangeEvent p = m.AsControlChange();
ccmap.Change(p.control_number, p.value);
noteMap.Change(p.control_number, p.value);
}
break;
default:
break;
}
}
void SetCCFinalGain(uint8_t value)
{
finalGainLeft = finalGainRight = float(value) / 127.0f;
}
// Main -- Init, and Midi Handling
int main(void)
{
// Init
hw.Init();
hw.SetAudioBlockSize(AUDIO_BLOCK_SIZE);
ComInit(&hw);
//log("Init start");
sampleRate = hw.AudioSampleRate();
voice.Init(&hw, sampleRate);
filt.Init(&hw, sampleRate);
loadMeter.Init(sampleRate, AUDIO_BLOCK_SIZE);
ccmap.Init();
pcmap.Init();
//ccmap.Add(7, SetCCFinalGain);
// make up your mind
//SetAlesisV125MIDIMap(&ccmap, ¬eMap, &voice, &filt);
// This setup uses the pod controls to select voice, filter and change parameters via the pots
// the midi map is selectable via the FCB1010 switches to be major or minor pentatonic scales.
// A alelesis samplepad (drumpad) is setupt o play the pentatonic scale with its two upper pads
// set to the octave up and down select notes (which are not played)
noteMap.Init();
noteMap.SetOctaveUpDownNotes(40, 41);
standAloneController.Init(&hw, &voice, &filt, &finalGainRight, &finalGainLeft);
SetFCB1010MIDIMap(¬eMap);
log("Init end");
// Start stuff.
hw.StartAdc();
hw.StartAudio(AudioCallback);
hw.midi.StartReceive();
#if LOG_CPU_LOAD
uint8_t cpuLoad = 0;
#endif
uint32_t now = 0;
for (;;)
{
#if LOG_CPU_LOAD
uint32_t tp = System::GetNow() - now;
if (tp > 5000)
{
now = System::GetNow();
loadMeter.Reset();
cpuLoad = currentCpuLoad;
log("Ave CPU load Peak: %d", cpuLoad);
}
#endif
hw.midi.Listen();
// Handle MIDI Events
while (hw.midi.HasEvents())
{
HandleMidiMessage(hw.midi.PopEvent());
}
//voice.UpdateBackGround();
if (outClipIndicator > 0)
{
hw.seed.SetLed(true);
outClipIndicator--;
}
else
{
hw.seed.SetLed(false);
}
ResetLEDs();
}
}