forked from qiemem/eurorack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelements.cc
executable file
·147 lines (127 loc) · 4.3 KB
/
elements.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
// Copyright 2014 Emilie Gillet.
//
// Author: Emilie Gillet ([email protected])
//
// 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.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
#include "elements/drivers/cv_adc.h"
#include "elements/drivers/codec.h"
#include "elements/drivers/debug_pin.h"
#include "elements/drivers/debug_port.h"
#include "elements/drivers/pots_adc.h"
#include "elements/drivers/system.h"
#include "elements/dsp/part.h"
#include "elements/cv_scaler.h"
#include "elements/ui.h"
// #define PROFILE_INTERRUPT 1
using namespace elements;
using namespace stmlib;
Codec codec;
CvScaler cv_scaler;
DebugPort debug_port;
Part part;
Ui ui;
uint16_t reverb_buffer[32768] __attribute__ ((section (".ccmdata")));
// Default interrupt handlers.
extern "C" {
void NMI_Handler() { }
void HardFault_Handler() { while (1); }
void MemManage_Handler() { while (1); }
void BusFault_Handler() { while (1); }
void UsageFault_Handler() { while (1); }
void SVC_Handler() { }
void DebugMon_Handler() { }
void PendSV_Handler() { }
void SysTick_Handler() {
ui.Poll();
if (debug_port.readable()) {
uint8_t command = debug_port.Read();
uint8_t response = ui.HandleFactoryTestingRequest(command);
debug_port.Write(response);
}
}
}
float blow_in[kAudioChunkSize];
float strike_in[kAudioChunkSize];
float out[kAudioChunkSize];
float aux[kAudioChunkSize];
const float kNoiseGateThreshold = 0.0001f;
float strike_in_level = 0.0f;
float blow_in_level = 0.0f;
void FillBuffer(Codec::Frame* input, Codec::Frame* output, size_t n) {
#ifdef PROFILE_INTERRUPT
TIC
#endif // PROFILE_INTERRUPT
PerformanceState s;
cv_scaler.Read(part.mutable_patch(), &s);
s.gate |= ui.gate();
for (size_t i = 0; i < n; ++i) {
float blow_in_sample = static_cast<float>(input[i].r) / 32768.0f;
float strike_in_sample = static_cast<float>(input[i].l) / 32768.0f;
float error, gain;
error = strike_in_sample * strike_in_sample - strike_in_level;
strike_in_level += error * (error > 0.0f ? 0.1f : 0.0001f);
gain = strike_in_level <= kNoiseGateThreshold
? (1.0f / kNoiseGateThreshold) * strike_in_level : 1.0f;
strike_in[i] = gain * strike_in_sample;
error = blow_in_sample * blow_in_sample - blow_in_level;
blow_in_level += error * (error > 0.0f ? 0.1f : 0.0001f);
gain = blow_in_level <= kNoiseGateThreshold
? (1.0f / kNoiseGateThreshold) * blow_in_level : 1.0f;
blow_in[i] = gain * blow_in_sample;
}
part.Process(s, blow_in, strike_in, out, aux, n);
for (size_t i = 0; i < n; ++i) {
output[i].r = SoftConvert(out[i]);
output[i].l = SoftConvert(aux[i]);
}
#ifdef PROFILE_INTERRUPT
TOC
#endif // PROFILE_INTERRUPT
}
void Init() {
System sys;
sys.Init(true);
// Init and seed the random parameters and generators with the serial number.
part.Init(reverb_buffer);
part.Seed((uint32_t*)(0x1fff7a10), 3);
cv_scaler.Init();
ui.Init(&part, &cv_scaler);
if (!codec.Init(32000, CODEC_PROTOCOL_PHILIPS, CODEC_FORMAT_16_BIT)) {
ui.Panic();
}
if (!codec.Start(&FillBuffer)) {
ui.Panic();
}
if (cv_scaler.freshly_baked()) {
#ifdef PROFILE_INTERRUPT
DebugPin::Init();
#else
debug_port.Init();
#endif // PROFILE_INTERRUPT
}
sys.StartTimers();
}
int main(void) {
Init();
while (1) {
ui.DoEvents();
}
}