-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.cpp
More file actions
241 lines (217 loc) · 6.47 KB
/
patch.cpp
File metadata and controls
241 lines (217 loc) · 6.47 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
//
// patch.cpp
//
#include <circle/string.h>
#include "patch.h"
#include "sdload.h"
#include "voicehandler.h"
#define VOL_ATTACK 1
#define VOL_DECAY 2
#define VOL_SUSTAIN 3
#define VOL_RELEASE 4
#define FIL_ATTACK 5
#define FIL_DECAY 6
#define FIL_SUSTAIN 7
#define FIL_RELEASE 8
CPatch::CPatch(CVoiceHandler *pVoices)
: m_pVoices(pVoices),
m_pData(nullptr),
m_dataSize(0)
{
}
void CPatch::SetBuffer(const char* pBuffer, unsigned size)
{
m_pData = pBuffer;
m_dataSize = size;
}
void CPatch::ApplyParam(const char* key, float f)
{
if(!m_pVoices)
return;
// Dispatch parameters to voicehandler
if(strcmp(key, "Osc1-Coarse") == 0)
m_pVoices->SetCoarse(0, f);
else if (strcmp(key, "Osc1-Fine") == 0)
m_pVoices->SetFine(0, f);
else if (strcmp(key, "Osc1-Saw") == 0)
m_pVoices->SetWaveformSaw(0, f);
else if (strcmp(key, "Osc1-Square") == 0)
m_pVoices->SetWaveformSquare(0, f);
else if (strcmp(key, "Osc1-PulseWidth") == 0)
m_pVoices->SetPulseWidth(0, f);
else if (strcmp(key, "Osc1-Sync") == 0)
m_pVoices->SetOsc1Sync(f);
else if (strcmp(key, "Osc1-Vol") == 0)
m_pVoices->SetOscVol(0, f);
else if(strcmp(key, "Osc2-Coarse") == 0)
m_pVoices->SetCoarse(1, f);
else if (strcmp(key, "Osc2-Fine") == 0)
m_pVoices->SetFine(1, f);
else if (strcmp(key, "Osc2-Saw") == 0)
m_pVoices->SetWaveformSaw(1, f);
else if (strcmp(key, "Osc2-Square") == 0)
m_pVoices->SetWaveformSquare(1, f);
else if (strcmp(key, "Osc2-Tri") == 0)
m_pVoices->SetWaveformTri(f);
else if (strcmp(key, "Osc2-PulseWidth") == 0)
m_pVoices->SetPulseWidth(1, f);
else if (strcmp(key, "Osc2-KbdTrk") == 0)
m_pVoices->SetOsc2KbdTrk(f);
else if (strcmp(key, "Osc2-LowFreq") == 0)
m_pVoices->SetOsc2LowFreq(f);
else if (strcmp(key, "Osc2-Vol") == 0)
m_pVoices->SetOscVol(1, f);
else if (strcmp(key, "Filter-Cutoff") == 0)
m_pVoices->SetCutoff(f);
else if (strcmp(key, "Filter-Res") == 0)
m_pVoices->SetResonance(f);
else if (strcmp(key, "Filter-EnvAmt") == 0)
m_pVoices->SetEnvAmt(f);
else if (strcmp(key, "Filter-KbdTrk") == 0)
m_pVoices->SetKbdTrk(f);
else if (strcmp(key, "Lfo-Freq") == 0)
m_pVoices->SetLfoFreq(f);
else if (strcmp(key, "Lfo-Saw") == 0)
m_pVoices->SetLfoWaveSaw(f);
else if (strcmp(key, "Lfo-Square") == 0)
m_pVoices->SetLfoWaveSquare(f);
else if (strcmp(key, "Lfo-Tri") == 0)
m_pVoices->SetLfoWaveTri(f);
else if (strcmp(key, "Lfo-Mix") == 0)
m_pVoices->SetLfoMix(f);
else if (strcmp(key, "Lfo-Filter") == 0)
m_pVoices->SetLfoFilter(f);
else if (strcmp(key, "Lfo-Fm1") == 0)
m_pVoices->SetLfoFm(0, f);
else if (strcmp(key, "Lfo-Fm2") == 0)
m_pVoices->SetLfoFm(1, f);
else if (strcmp(key, "Lfo-Pwm1") == 0)
m_pVoices->SetPulseWidthModulation(0, f);
else if (strcmp(key, "Lfo-Pwm2") == 0)
m_pVoices->SetPulseWidthModulation(1, f);
else if (strcmp(key, "Polymod-FilEnv") == 0)
m_pVoices->SetPolyModFilEnv(f);
else if (strcmp(key, "Polymod-Osc2") == 0)
m_pVoices->SetPolyModOsc2(f);
else if (strcmp(key, "Polymod-Pw") == 0)
m_pVoices->SetPolyModPw(f);
else if (strcmp(key, "Polymod-Filter") == 0)
m_pVoices->SetPolyModFil(f);
else if (strcmp(key, "Polymod-Freq") == 0)
m_pVoices->SetPolyModFreq(f);
else if (strcmp(key, "VolEnv-A") == 0)
m_pVoices->SetEnv(VOL_ATTACK, f);
else if (strcmp(key, "VolEnv-D") == 0)
m_pVoices->SetEnv(VOL_DECAY, f);
else if (strcmp(key, "VolEnv-S") == 0)
m_pVoices->SetEnv(VOL_SUSTAIN, f);
else if (strcmp(key, "VolEnv-R") == 0)
m_pVoices->SetEnv(VOL_RELEASE, f);
else if (strcmp(key, "FilEnv-A") == 0)
m_pVoices->SetEnv(FIL_ATTACK, f);
else if (strcmp(key, "FilEnv-D") == 0)
m_pVoices->SetEnv(FIL_DECAY, f);
else if (strcmp(key, "FilEnv-S") == 0)
m_pVoices->SetEnv(FIL_SUSTAIN, f);
else if (strcmp(key, "FilEnv-R") == 0)
m_pVoices->SetEnv(FIL_RELEASE, f);
else if (strcmp(key, "NoiseVol") == 0)
m_pVoices->SetNoiseVol(f);
else if (strcmp(key, "Modwheel") == 0)
m_pVoices->SetModWheel(f);
else if (strcmp(key, "Unison") == 0)
m_pVoices->SetUnison(f);
else if (strcmp(key, "UnisonSpread") == 0)
m_pVoices->SetUnisonSpread(f);
else if (strcmp(key, "UnisonVoices") == 0)
m_pVoices->SetUnisonVoices(f);
else if (strcmp(key, "GlideTime") == 0)
m_pVoices->SetGlideTime(f);
else if (strcmp(key, "Gain") == 0)
m_pVoices->SetGain(f);
}
void CPatch::Parse()
{
if (!m_pData || !m_dataSize)
return;
CString key;
CString val;
bool readingValue = false;
for (unsigned i = 0; i < m_dataSize; ++i)
{
char c = m_pData[i];
if (c == '#' || c == '\n' || c == '\r')
{
if(key.GetLength() && val.GetLength())
{
float fVal = Atof(val.c_str());
ApplyParam(key.c_str(), fVal);
}
key = "";
val = "";
readingValue = false;
continue;
}
// Find '='
if (c == '=')
{
readingValue = true;
continue;
}
if (readingValue == false)
key += c;
else
val += c;
}
if (key.GetLength() && val.GetLength())
{
float fVal = Atof(val.c_str());
ApplyParam(key.c_str(), fVal);
}
}
// ---------------------------------------------------------
// Minimal atof() replacement — supports ± and 3 decimal precision
// Example inputs: "0.5", "-12.345", "127", "3.14"
// ---------------------------------------------------------
float CPatch::Atof(const char* str)
{
if (!str) return 0.0f;
float sign = 1.0f;
float value = 0.0f;
float frac = 0.0f;
int decimals = 0;
bool pastDot = false;
// Skip leading spaces
while (*str == ' ' || *str == '\t') str++;
// Handle sign
if (*str == '-') { sign = -1.0f; ++str; }
else if (*str == '+') { ++str; }
// Parse digits
for (; *str; ++str)
{
if (*str == '.' && !pastDot)
{
pastDot = true;
}
else if (*str >= '0' && *str <= '9')
{
int digit = *str - '0';
if (!pastDot)
{
value = value * 10.0f + (float)digit;
}
else if (decimals < 3) // limit to 3 decimal precision
{
frac = frac * 10.0f + (float)digit;
decimals++;
}
}
else
{
break;
}
}
while (decimals--)
frac *= 0.1f;
return sign * (value + frac);
}