-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidside.cpp
More file actions
220 lines (176 loc) · 5.89 KB
/
Copy pathmidside.cpp
File metadata and controls
220 lines (176 loc) · 5.89 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
#include "daisysp.h"
#include "daisy_patch.h"
#include <string>
#include "oscilloscope.hpp"
using namespace daisy;
using namespace daisysp;
constexpr int bufferSize = 4096;
DaisyPatch patch;
int samplesPerCallback = 0;
float midBuffer[bufferSize] = { 0 };
float returnBuffer[bufferSize] = { 0 };
float sideBuffer[bufferSize] = { 0 };
size_t bufferIndex = 0;
int bufferOffset = 0;
Parameter bufferOffsetParam;
Oscilloscope oscilloscopes[2];
size_t scopeIndex;
unsigned int encoderFunction = 0;
int pendingChangeParam = 0;
uint32_t lastScreenUpdate = 0;
void UpdateControls()
{
patch.UpdateAnalogControls();
patch.DebounceControls();
// //read ctrls and gates, then update sampleholds
// sampHolds[0].Process(patch.gate_input[0].State(), patch.controls[0].Process());
// sampHolds[1].Process(patch.gate_input[1].State(), patch.controls[1].Process());
//encoder
if (patch.encoder.Pressed())
{
int scopeChange = patch.encoder.Increment();
scopeIndex = (scopeIndex + scopeChange + 3) % 3;
if (scopeChange != 0)
{
pendingChangeParam = -1;
}
else if (pendingChangeParam == 0)
{
pendingChangeParam = 1;
}
}
else if (scopeIndex != 2)
{
if (pendingChangeParam == 1)
{
encoderFunction = (encoderFunction + 1) % 2;
}
pendingChangeParam = 0;
if (encoderFunction == 0)
{
// scale current oscilloscope
int scale = patch.encoder.Increment();
if (scale < 0)
{
oscilloscopes[scopeIndex].SetScale(oscilloscopes[scopeIndex].GetScale() + 1);
}
if (scale > 0 && oscilloscopes[scopeIndex].GetScale() > 1)
{
oscilloscopes[scopeIndex].SetScale(oscilloscopes[scopeIndex].GetScale() - 1);
}
}
else
{
// adjust gain of oscilloscope
int scale = patch.encoder.Increment();
if (scale != 0)
{
float gain = oscilloscopes[scopeIndex].GetGain() + (scale * 0.125f);
oscilloscopes[scopeIndex].SetGain(gain >= 0.f ? gain : 0.f);
}
}
}
bufferOffset = bufferOffsetParam.Process();
}
void AudioCallback(float** in, float** out, size_t size)
{
UpdateControls();
for (size_t i = 0; i < size; i++)
{
// Send the mid input via the first output, and store it in the buffer
float mid = midBuffer[bufferIndex] = out[0][i] = in[0][i];
// Retrieve the return signal
float returned = returnBuffer[bufferIndex] = in[1][i];
// Decide where to read from the buffer
int bufferReadIndex = (bufferIndex - bufferOffset + bufferSize) % bufferSize;
// Calculate the side signal by subtracting the return from the mid; allow monitoring from the second output
float side = sideBuffer[bufferIndex] = out[1][i] = midBuffer[bufferReadIndex] - returned;
// Get mid and side output levels based on controls
mid *= patch.controls[0].Value();
side *= patch.controls[1].Value();
// Output the left and right channels
out[2][i] = mid + side;
out[3][i] = mid - side;
bufferIndex = (bufferIndex + 1) % bufferSize;
}
samplesPerCallback = size;
}
//void UpdateOutputs()
//{
// dsy_dac_write(DSY_DAC_CHN1, sampHolds[0].output * 4095);
// dsy_dac_write(DSY_DAC_CHN2, sampHolds[1].output * 4095);
//}
void UpdateOled()
{
if (scopeIndex < 2)
{
patch.display.Fill(false);
size_t indices[] = { (bufferIndex - bufferOffset + bufferSize) % bufferSize, bufferIndex };
oscilloscopes[scopeIndex].Draw(indices);
patch.display.SetCursor(0, SSD1309_HEIGHT - 8);
std::string str = std::to_string(oscilloscopes[scopeIndex].GetScale());
char* s = &str[0];
patch.display.WriteString(s, Font_6x8, encoderFunction != 0);
// TODO: not working for some reason
patch.display.SetCursor(0, 0);
std::string str2 = std::to_string(oscilloscopes[scopeIndex].GetGain());
char* s2 = &str2[0];
patch.display.WriteString(s2, Font_6x8, encoderFunction != 1);
}
else
{
patch.display.Fill(true);
patch.display.SetCursor(0, SSD1309_HEIGHT - 22);
std::string str = "meow";
char* s = &str[0];
patch.display.WriteString(s, Font_11x18, false);
}
patch.display.Update();
}
int main()
{
//float sampleRate;
patch.Init();
//sampleRate = patch.AudioSampleRate();
patch.StartAdc();
patch.StartAudio(AudioCallback);
bufferOffsetParam.Init(patch.controls[2], 0, 1024, Parameter::LINEAR);
Window scope1windows[2];
scope1windows[0].buffer = midBuffer;
scope1windows[0].bufferLength = bufferSize;
scope1windows[0].x = 0;
scope1windows[0].y = 0;
scope1windows[0].width = SSD1309_WIDTH;
scope1windows[0].height = SSD1309_HEIGHT / 2;
scope1windows[1] = scope1windows[0];
scope1windows[1].buffer = returnBuffer;
scope1windows[1].y = SSD1309_HEIGHT / 2;
OscilloscopeParams params;
params.display = &patch.display;
params.windows = scope1windows;
params.windowCount = 2;
params.syncChannel = 0;
oscilloscopes[0].Init(params);
Window scope2window;
scope2window.buffer = sideBuffer;
scope2window.bufferLength = bufferSize;
scope2window.x = 0;
scope2window.y = 0;
scope2window.width = SSD1309_WIDTH;
scope2window.height = SSD1309_HEIGHT;
OscilloscopeParams params2;
params2.display = &patch.display;
params2.windows = &scope2window;
params2.windowCount = 1;
params2.syncChannel = 0;
oscilloscopes[1].Init(params2);
while(1)
{
// UpdateOutputs();
if (dsy_system_getnow() - lastScreenUpdate > 17)
{
UpdateOled();
lastScreenUpdate = dsy_system_getnow();
}
}
}