1 // Copyright 2015 Emilie Gillet.
2 //
3 // Author: Emilie Gillet (emilie.o.gillet@gmail.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 // See http://creativecommons.org/licenses/MIT/ for more information.
24
25 #include "rings/drivers/adc.h"
26 #include "rings/drivers/codec.h"
27 #include "rings/drivers/debug_pin.h"
28 #include "rings/drivers/debug_port.h"
29 #include "rings/drivers/system.h"
30 #include "rings/drivers/version.h"
31 #include "rings/dsp/part.h"
32 #include "rings/dsp/strummer.h"
33 #include "rings/dsp/string_synth_part.h"
34 #include "rings/cv_scaler.h"
35 #include "rings/settings.h"
36 #include "rings/ui.h"
37
38 // #define PROFILE_INTERRUPT 1
39
40 using namespace rings;
41 using namespace stmlib;
42
43 uint16_t reverb_buffer[32768] __attribute__ ((section (".ccmdata")));
44
45 Codec codec;
46 CvScaler cv_scaler;
47 DebugPort debug_port;
48 Part part;
49 Settings settings;
50 StringSynthPart string_synth;
51 Strummer strummer;
52 Ui ui;
53
54 // Default interrupt handlers.
55 extern "C" {
56
57 int __errno;
58
NMI_Handler()59 void NMI_Handler() { }
HardFault_Handler()60 void HardFault_Handler() { while (1); }
MemManage_Handler()61 void MemManage_Handler() { while (1); }
BusFault_Handler()62 void BusFault_Handler() { while (1); }
UsageFault_Handler()63 void UsageFault_Handler() { while (1); }
SVC_Handler()64 void SVC_Handler() { }
DebugMon_Handler()65 void DebugMon_Handler() { }
PendSV_Handler()66 void PendSV_Handler() { }
67
SysTick_Handler()68 void SysTick_Handler() {
69 ui.Poll();
70 if (settings.freshly_baked()) {
71 if (debug_port.readable()) {
72 uint8_t command = debug_port.Read();
73 uint8_t response = ui.HandleFactoryTestingRequest(command);
74 debug_port.Write(response);
75 }
76 }
77 }
78
79 }
80
81 float in[kMaxBlockSize];
82 float out[kMaxBlockSize];
83 float aux[kMaxBlockSize];
84
85 const float kNoiseGateThreshold = 0.00003f;
86 float in_level = 0.0f;
87
FillBuffer(Codec::Frame * input,Codec::Frame * output,size_t size)88 void FillBuffer(Codec::Frame* input, Codec::Frame* output, size_t size) {
89 #ifdef PROFILE_INTERRUPT
90 TIC
91 #endif // PROFILE_INTERRUPT
92 PerformanceState performance_state;
93 Patch patch;
94
95 cv_scaler.DetectAudioNormalization(input, size);
96 cv_scaler.Read(&patch, &performance_state);
97
98 if (settings.state().easter_egg) {
99 for (size_t i = 0; i < size; ++i) {
100 in[i] = static_cast<float>(input[i].r) / 32768.0f;
101 }
102 strummer.Process(NULL, size, &performance_state);
103 string_synth.Process(performance_state, patch, in, out, aux, size);
104 } else {
105 // Apply noise gate.
106 for (size_t i = 0; i < size; ++i) {
107 float in_sample = static_cast<float>(input[i].r) / 32768.0f;
108 float error, gain;
109 error = in_sample * in_sample - in_level;
110 in_level += error * (error > 0.0f ? 0.1f : 0.0001f);
111 gain = in_level <= kNoiseGateThreshold
112 ? (1.0f / kNoiseGateThreshold) * in_level : 1.0f;
113 in[i] = gain * in_sample;
114 }
115 strummer.Process(in, size, &performance_state);
116 part.Process(performance_state, patch, in, out, aux, size);
117 }
118
119 for (size_t i = 0; i < size; ++i) {
120 output[i].l = Clip16(static_cast<int32_t>(out[i] * 32768.0f));
121 output[i].r = Clip16(static_cast<int32_t>(aux[i] * 32768.0f));
122 }
123 ui.set_strumming_flag(performance_state.strum);
124 #ifdef PROFILE_INTERRUPT
125 TOC
126 #endif // PROFILE_INTERRUPT
127 }
128
Init()129 void Init() {
130 System sys;
131 Version version;
132
133 sys.Init(true);
134 version.Init();
135
136 strummer.Init(0.01f, kSampleRate / kMaxBlockSize);
137 part.Init(reverb_buffer);
138 string_synth.Init(reverb_buffer);
139
140 settings.Init();
141 cv_scaler.Init(settings.mutable_calibration_data());
142 ui.Init(&settings, &cv_scaler, &part, &string_synth);
143
144 if (!codec.Init(!version.revised(), kSampleRate)) {
145 ui.Panic();
146 }
147 if (!codec.Start(kMaxBlockSize, &FillBuffer)) {
148 ui.Panic();
149 }
150 codec.set_line_input_gain(22);
151
152 if (settings.freshly_baked()) {
153 #ifdef PROFILE_INTERRUPT
154 DebugPin::Init();
155 #else
156 debug_port.Init();
157 #endif // PROFILE_INTERRUPT
158 }
159 sys.StartTimers();
160 }
161
main(void)162 int main(void) {
163 Init();
164 while (1) {
165 ui.DoEvents();
166 }
167 }
168