1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2008 Anders Dahnielson <anders@dahnielson.com> * |
6 |
* Copyright (C) 2009 - 2010 Anders Dahnielson and Grigor Iliev * |
7 |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
12 |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
17 |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#include "sfz.h" |
25 |
|
26 |
#include <iostream> |
27 |
#include <sstream> |
28 |
#include <cctype> |
29 |
#include <cstdio> |
30 |
#include <cstring> |
31 |
|
32 |
#include "../../common/File.h" |
33 |
#include "../../common/Path.h" |
34 |
#include "../../common/global_private.h" |
35 |
|
36 |
namespace sfz |
37 |
{ |
38 |
|
39 |
Sample* SampleManager::FindSample(std::string samplePath) { |
40 |
std::map<Sample*, std::set<Region*> >::iterator it = sampleMap.begin(); |
41 |
for (; it != sampleMap.end(); it++) { |
42 |
if (it->first->GetFile() == samplePath) return it->first; |
43 |
} |
44 |
|
45 |
return NULL; |
46 |
} |
47 |
|
48 |
///////////////////////////////////////////////////////////// |
49 |
// class optional |
50 |
|
51 |
const optional_base::nothing_t optional_base::nothing; |
52 |
|
53 |
///////////////////////////////////////////////////////////// |
54 |
// class Articulation |
55 |
|
56 |
Articulation::Articulation() |
57 |
{ |
58 |
} |
59 |
|
60 |
Articulation::~Articulation() |
61 |
{ |
62 |
} |
63 |
|
64 |
///////////////////////////////////////////////////////////// |
65 |
// class Definition |
66 |
|
67 |
Definition::Definition() : |
68 |
locc(128), hicc(128), start_locc(128), start_hicc(128), stop_locc(128), |
69 |
stop_hicc(128), on_locc(128), on_hicc(128), delay_oncc(128), delay_samples_oncc(128), |
70 |
offset_oncc(128), amp_velcurve(128), gain_oncc(128), xfin_locc(128), xfin_hicc(128), |
71 |
xfout_locc(128), xfout_hicc(128), cutoff_oncc(128), cutoff2_oncc(128), cutoff_smoothcc(128), |
72 |
cutoff2_smoothcc(128), cutoff_stepcc(128), cutoff2_stepcc(128), cutoff_curvecc(128), |
73 |
cutoff2_curvecc(128), resonance_oncc(128), resonance2_oncc(128), resonance_smoothcc(128), |
74 |
resonance2_smoothcc(128), resonance_stepcc(128), resonance2_stepcc(128), |
75 |
resonance_curvecc(128), resonance2_curvecc(128), eq1_freq_oncc(128), eq2_freq_oncc(128), |
76 |
eq3_freq_oncc(128), eq1_bw_oncc(128), eq2_bw_oncc(128), eq3_bw_oncc(128), |
77 |
eq1_gain_oncc(128), eq2_gain_oncc(128), eq3_gain_oncc(128) |
78 |
|
79 |
{ |
80 |
} |
81 |
|
82 |
Definition::~Definition() |
83 |
{ |
84 |
} |
85 |
|
86 |
///////////////////////////////////////////////////////////// |
87 |
// class Region |
88 |
|
89 |
Region::Region() |
90 |
{ |
91 |
pSample = NULL; |
92 |
seq_counter = 1; |
93 |
} |
94 |
|
95 |
Region::~Region() |
96 |
{ |
97 |
DestroySampleIfNotUsed(); |
98 |
} |
99 |
|
100 |
Sample* Region::GetSample(bool create) |
101 |
{ |
102 |
if(pSample == NULL && create) { |
103 |
Sample* sf = GetInstrument()->GetSampleManager()->FindSample(sample); |
104 |
if (sf != NULL) pSample = sf; // Reuse already created sample |
105 |
else pSample = new Sample(sample); |
106 |
GetInstrument()->GetSampleManager()->AddSampleConsumer(pSample, this); |
107 |
} |
108 |
return pSample; |
109 |
} |
110 |
|
111 |
void Region::DestroySampleIfNotUsed() { |
112 |
if (pSample == NULL) return; |
113 |
GetInstrument()->GetSampleManager()->RemoveSampleConsumer(pSample, this); |
114 |
if (!GetInstrument()->GetSampleManager()->HasSampleConsumers(pSample)) { |
115 |
GetInstrument()->GetSampleManager()->RemoveSample(pSample); |
116 |
delete pSample; |
117 |
pSample = NULL; |
118 |
} |
119 |
} |
120 |
|
121 |
bool |
122 |
Region::OnKey(uint8_t chan, uint8_t key, uint8_t vel, |
123 |
int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, |
124 |
uint8_t prog, float rand, trigger_t trig, uint8_t* cc, |
125 |
float timer, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key) |
126 |
{ |
127 |
// chan (MIDI channel) |
128 |
// key (MIDI note) |
129 |
// vel (MIDI velocity) |
130 |
|
131 |
// bend (MIDI pitch bend) |
132 |
// bpm (host BPM) |
133 |
// chanaft (MIDI channel pressure) |
134 |
// polyaft (MIDI polyphonic aftertouch) |
135 |
// prog (MIDI program change) |
136 |
// rand (generated random number) |
137 |
// trigger (how it was triggered) |
138 |
// cc (all 128 CC values) |
139 |
|
140 |
// timer (time since previous region in the group was triggered) |
141 |
// sw (the state of region key switches, 128 possible values) |
142 |
// last_sw_key (the last key pressed in the key switch range) |
143 |
// prev_sw_key (the previous note value) |
144 |
|
145 |
bool is_triggered ( |
146 |
chan >= lochan && chan <= hichan && |
147 |
key >= lokey && key <= hikey && |
148 |
vel >= lovel && vel <= hivel && |
149 |
bend >= lobend && bend <= hibend && |
150 |
bpm >= lobpm && bpm < hibpm && |
151 |
chanaft >= lochanaft && chanaft <= hichanaft && |
152 |
polyaft >= lopolyaft && polyaft <= hipolyaft && |
153 |
prog >= loprog && prog <= hiprog && |
154 |
rand >= lorand && rand < hirand && |
155 |
timer >= lotimer && timer <= hitimer && |
156 |
|
157 |
( (sw_lokey == -1 || sw_hikey == -1 || sw_last == -1) || |
158 |
((sw_last >= sw_lokey && sw_last <= sw_hikey) ? (last_sw_key == sw_last) : true) ) && |
159 |
|
160 |
( (sw_down == -1 || sw_lokey == -1 || sw_hikey == -1) || |
161 |
((sw_down >= sw_lokey && sw_down <= sw_hikey) ? (sw[sw_down]) : true) ) && |
162 |
|
163 |
( (sw_up == -1 || sw_lokey == -1 || sw_hikey == -1) || |
164 |
((sw_up >= sw_lokey && sw_up <= sw_hikey) ? (!sw[sw_up]) : true) ) && |
165 |
|
166 |
((sw_previous != -1) ? (prev_sw_key == sw_previous) : true) && |
167 |
((trigger & trig) != 0) |
168 |
); |
169 |
|
170 |
if (!is_triggered) |
171 |
return false; |
172 |
|
173 |
for (int i = 0; i < 128; ++i) |
174 |
{ |
175 |
if (locc[i] != -1 && hicc[i] != -1 && !(cc[i] >= locc[i] && cc[i] <= hicc[i])) |
176 |
return false; |
177 |
} |
178 |
|
179 |
// seq_position has to be checked last, so we know that we |
180 |
// increment the right counter |
181 |
is_triggered = (seq_counter == seq_position); |
182 |
seq_counter = (seq_counter % seq_length) + 1; |
183 |
|
184 |
return is_triggered; |
185 |
} |
186 |
|
187 |
bool |
188 |
Region::OnControl(uint8_t chan, uint8_t cont, uint8_t val, |
189 |
int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, |
190 |
uint8_t prog, float rand, trigger_t trig, uint8_t* cc, |
191 |
float timer, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key) |
192 |
{ |
193 |
// chan (MIDI channel) |
194 |
// cont (MIDI controller) |
195 |
// val (MIDI controller value) |
196 |
|
197 |
// bend (MIDI pitch bend) |
198 |
// bpm (host BPM) |
199 |
// chanaft (MIDI channel pressure) |
200 |
// polyaft (MIDI polyphonic aftertouch) |
201 |
// prog (MIDI program change) |
202 |
// rand (generated random number) |
203 |
// trigger (how it was triggered) |
204 |
// cc (all CC values) |
205 |
|
206 |
// timer (time since previous region in the group was triggered) |
207 |
// sw (the state of region key switches, 128 possible values) |
208 |
// last_sw_key (the last key pressed in the key switch range) |
209 |
// prev_sw_key (the previous note value) |
210 |
|
211 |
bool is_triggered = ( |
212 |
chan >= lochan && chan <= hichan && |
213 |
((val >= on_locc[cont] && val <= on_hicc[cont]) || |
214 |
(val >= start_locc[cont] && val <= start_hicc[cont])) && |
215 |
bend >= lobend && bend <= hibend && |
216 |
bpm >= lobpm && bpm < hibpm && |
217 |
chanaft >= lochanaft && chanaft <= hichanaft && |
218 |
polyaft >= lopolyaft && polyaft <= hipolyaft && |
219 |
prog >= loprog && prog <= hiprog && |
220 |
rand >= lorand && rand < hirand && |
221 |
timer >= lotimer && timer <= hitimer && |
222 |
|
223 |
( (sw_lokey == -1 || sw_hikey == -1 || sw_last == -1) || |
224 |
((sw_last >= sw_lokey && sw_last <= sw_hikey) ? (last_sw_key == sw_last) : true) ) && |
225 |
|
226 |
( (sw_down == -1 || sw_lokey == -1 || sw_hikey == -1) || |
227 |
((sw_down >= sw_lokey && sw_down <= sw_hikey) ? (sw[sw_down]) : true) ) && |
228 |
|
229 |
( (sw_up == -1 || sw_lokey == -1 || sw_hikey == -1) || |
230 |
((sw_up >= sw_lokey && sw_up <= sw_hikey) ? (!sw[sw_up]) : true) ) && |
231 |
|
232 |
((sw_previous != -1) ? (prev_sw_key == sw_previous) : true) && |
233 |
((trigger & trig) != 0) |
234 |
); |
235 |
|
236 |
if (!is_triggered) |
237 |
return false; |
238 |
|
239 |
for (int i = 0; i < 128; ++i) |
240 |
{ |
241 |
if (locc[i] != -1 && hicc[i] != -1 && !(cc[i] >= locc[i] && cc[i] <= hicc[i])) |
242 |
return false; |
243 |
} |
244 |
|
245 |
return true; |
246 |
} |
247 |
|
248 |
Articulation* |
249 |
Region::GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc) |
250 |
{ |
251 |
return new Articulation(); //todo: implement GetArticulation() |
252 |
} |
253 |
|
254 |
bool Region::HasLoop() { |
255 |
bool b = loop_mode == ::sfz::LOOP_CONTINUOUS || loop_mode == ::sfz::LOOP_SUSTAIN; // TODO: ONE_SHOT mode |
256 |
return b && GetLoopStart() && GetLoopEnd() && GetLoopEnd() > GetLoopStart(); |
257 |
} |
258 |
|
259 |
uint Region::GetLoopStart() { |
260 |
return (!loop_start) ? 0 : *loop_start; // TODO: use sample loop when loop_start not defined |
261 |
} |
262 |
|
263 |
uint Region::GetLoopEnd() { |
264 |
return (!loop_end) ? 0 : *loop_end; // TODO: use sample loop when loop_end not defined |
265 |
} |
266 |
|
267 |
uint Region::GetLoopCount() { |
268 |
return (!count) ? 0 : *count; |
269 |
} |
270 |
|
271 |
///////////////////////////////////////////////////////////// |
272 |
// class Instrument |
273 |
|
274 |
Instrument::Instrument(std::string name, SampleManager* pSampleManager) : KeyBindings(128, false), KeySwitchBindings(128, false) |
275 |
{ |
276 |
this->name = name; |
277 |
this->pSampleManager = pSampleManager ? pSampleManager : this; |
278 |
} |
279 |
|
280 |
Instrument::~Instrument() |
281 |
{ |
282 |
for(int i = 0; i < regions.size(); i++) { |
283 |
delete (regions[i]); |
284 |
} |
285 |
regions.clear(); |
286 |
} |
287 |
|
288 |
std::vector<Region*> Instrument::GetRegionsOnKey ( |
289 |
uint8_t chan, uint8_t key, uint8_t vel, |
290 |
int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, |
291 |
uint8_t prog, float rand, trigger_t trig, uint8_t* cc, |
292 |
float timer, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key |
293 |
) { |
294 |
std::vector<Region*> v; |
295 |
for (int i = 0; i < regions.size(); i++) { |
296 |
if (regions[i]->OnKey ( |
297 |
chan, key, vel, bend, bpm, chanaft, polyaft, prog, |
298 |
rand, trig, cc, timer, sw, last_sw_key, prev_sw_key) |
299 |
) { v.push_back(regions[i]); } |
300 |
} |
301 |
|
302 |
return v; |
303 |
} |
304 |
|
305 |
bool Instrument::DestroyRegion(Region* pRegion) { |
306 |
for (std::vector<Region*>::iterator it = regions.begin(); it != regions.end(); it++) { |
307 |
if(*it == pRegion) { |
308 |
regions.erase(it); |
309 |
delete pRegion; |
310 |
return true; |
311 |
} |
312 |
} |
313 |
|
314 |
return false; |
315 |
} |
316 |
|
317 |
bool Instrument::HasKeyBinding(uint8_t key) { |
318 |
if (key > 127) return false; |
319 |
return KeyBindings[key]; |
320 |
} |
321 |
|
322 |
bool Instrument::HasKeySwitchBinding(uint8_t key) { |
323 |
if (key > 127) return false; |
324 |
return KeySwitchBindings[key]; |
325 |
} |
326 |
|
327 |
///////////////////////////////////////////////////////////// |
328 |
// class Group |
329 |
|
330 |
Group::Group() : |
331 |
id(0) |
332 |
{ |
333 |
Reset(); |
334 |
} |
335 |
|
336 |
Group::~Group() |
337 |
{ |
338 |
} |
339 |
|
340 |
void |
341 |
Group::Reset() |
342 |
{ |
343 |
// This is where all the default values are set. |
344 |
|
345 |
// sample definition default |
346 |
sample = ""; |
347 |
|
348 |
// input control |
349 |
lochan = 1; hichan = 16; |
350 |
lokey = 0; hikey = 127; |
351 |
lovel = 0; hivel = 127; |
352 |
lobend = -8192; hibend = 8192; |
353 |
lobpm = 0; hibpm = 500; |
354 |
lochanaft = 0; hichanaft = 127; |
355 |
lopolyaft = 0; hipolyaft = 127; |
356 |
loprog = 0; hiprog = 127; |
357 |
lorand = 0.0; hirand = 1.0; |
358 |
lotimer = 0.0; hitimer = 0.0; |
359 |
|
360 |
seq_length = 1; |
361 |
seq_position = 1; |
362 |
|
363 |
sw_lokey = -1; sw_hikey = -1; |
364 |
sw_last = -1; |
365 |
sw_down = -1; |
366 |
sw_up = -1; |
367 |
sw_previous = -1; |
368 |
sw_vel = VEL_CURRENT; |
369 |
|
370 |
trigger = TRIGGER_ATTACK; |
371 |
|
372 |
group = 0; |
373 |
off_by.unset(); |
374 |
off_mode = OFF_FAST; |
375 |
|
376 |
// sample player |
377 |
count.unset(); |
378 |
delay.unset(); delay_random.unset(); |
379 |
delay_beats.unset(); stop_beats.unset(); |
380 |
delay_samples.unset(); |
381 |
end.unset(); |
382 |
loop_crossfade.unset(); |
383 |
offset.unset(); offset_random.unset(); |
384 |
loop_mode = NO_LOOP; |
385 |
loop_start.unset(); loop_end.unset(); |
386 |
sync_beats.unset(); sync_offset.unset(); |
387 |
|
388 |
// amplifier |
389 |
volume = 0; |
390 |
pan = 0; |
391 |
width = 100; |
392 |
position = 0; |
393 |
amp_keytrack = 0; |
394 |
amp_keycenter = 60; |
395 |
amp_veltrack = 100; |
396 |
amp_random = 0; |
397 |
rt_decay = 0; |
398 |
xfin_lokey = 0; xfin_hikey = 0; |
399 |
xfout_lokey = 127; xfout_hikey = 127; |
400 |
xf_keycurve = POWER; |
401 |
xfin_lovel = 0; xfin_hivel = 0; |
402 |
xfout_lovel = 127; xfout_hivel = 127; |
403 |
xf_velcurve = POWER; |
404 |
xf_cccurve = POWER; |
405 |
|
406 |
// pitch |
407 |
transpose = 0; |
408 |
tune = 0; |
409 |
pitch_keycenter = 60; |
410 |
pitch_keytrack = 100; |
411 |
pitch_veltrack = 0; |
412 |
pitch_random = 0; |
413 |
bend_up = 200; |
414 |
bend_down = -200; |
415 |
bend_step = 1; |
416 |
|
417 |
// filter |
418 |
fil_type = LPF_2P; |
419 |
cutoff.unset(); |
420 |
cutoff_chanaft = 0; |
421 |
cutoff_polyaft = 0; |
422 |
resonance = 0; |
423 |
fil_keytrack = 0; |
424 |
fil_keycenter = 60; |
425 |
fil_veltrack = 0; |
426 |
fil_random = 0; |
427 |
|
428 |
fil2_type = LPF_2P; |
429 |
cutoff2.unset(); |
430 |
cutoff2_chanaft = 0; |
431 |
cutoff2_polyaft = 0; |
432 |
resonance2 = 0; |
433 |
fil2_keytrack = 0; |
434 |
fil2_keycenter = 60; |
435 |
fil2_veltrack = 0; |
436 |
fil2_random = 0; |
437 |
|
438 |
// per voice equalizer |
439 |
eq1_freq = 50; |
440 |
eq2_freq = 500; |
441 |
eq3_freq = 5000; |
442 |
eq1_vel2freq = 0; |
443 |
eq2_vel2freq = 0; |
444 |
eq3_vel2freq = 0; |
445 |
eq1_bw = 1; |
446 |
eq2_bw = 1; |
447 |
eq3_bw = 1; |
448 |
eq1_gain = 0; |
449 |
eq2_gain = 0; |
450 |
eq3_gain = 0; |
451 |
eq1_vel2gain = 0; |
452 |
eq2_vel2gain = 0; |
453 |
eq3_vel2gain = 0; |
454 |
|
455 |
// CCs |
456 |
for (int i = 0; i < 128; ++i) |
457 |
{ |
458 |
// input control |
459 |
locc[i] = 0; |
460 |
hicc[i] = 127; |
461 |
start_locc[i] = -1; |
462 |
start_hicc[i] = -1; |
463 |
stop_locc[i] = -1; |
464 |
stop_hicc[i] = -1; |
465 |
on_locc[i] = -1; |
466 |
on_hicc[i] = -1; |
467 |
|
468 |
// sample player |
469 |
delay_oncc[i].unset(); |
470 |
delay_samples_oncc[i].unset(); |
471 |
offset_oncc[i].unset(); |
472 |
|
473 |
// amplifier |
474 |
amp_velcurve[i] = -1; |
475 |
gain_oncc[i] = 0; |
476 |
xfin_locc[i] = 0; |
477 |
xfin_hicc[i] = 0; |
478 |
xfout_locc[i] = 127; |
479 |
xfout_hicc[i] = 127; |
480 |
|
481 |
// filter |
482 |
cutoff_oncc[i] = 0; |
483 |
cutoff_smoothcc[i] = 0; |
484 |
cutoff_stepcc[i] = 0; |
485 |
cutoff_curvecc[i] = 0; |
486 |
resonance_oncc[i] = 0; |
487 |
resonance_smoothcc[i] = 0; |
488 |
resonance_stepcc[i] = 0; |
489 |
resonance_curvecc[i] = 0; |
490 |
|
491 |
cutoff2_oncc[i] = 0; |
492 |
cutoff2_smoothcc[i] = 0; |
493 |
cutoff2_stepcc[i] = 0; |
494 |
cutoff2_curvecc[i] = 0; |
495 |
resonance2_oncc[i] = 0; |
496 |
resonance2_smoothcc[i] = 0; |
497 |
resonance2_stepcc[i] = 0; |
498 |
resonance2_curvecc[i] = 0; |
499 |
|
500 |
// per voice equalizer |
501 |
eq1_freq_oncc[i] = 0; |
502 |
eq2_freq_oncc[i] = 0; |
503 |
eq3_freq_oncc[i] = 0; |
504 |
eq1_bw_oncc[i] = 0; |
505 |
eq2_bw_oncc[i] = 0; |
506 |
eq3_bw_oncc[i] = 0; |
507 |
eq1_gain_oncc[i] = 0; |
508 |
eq2_gain_oncc[i] = 0; |
509 |
eq3_gain_oncc[i] = 0; |
510 |
} |
511 |
|
512 |
eg.clear(); |
513 |
|
514 |
// deprecated |
515 |
ampeg_delay = 0; |
516 |
ampeg_start = 0; //in percentage |
517 |
ampeg_attack = 0; |
518 |
ampeg_hold = 0; |
519 |
ampeg_decay = 0; |
520 |
ampeg_sustain = 100; // in percentage |
521 |
ampeg_release = 0; |
522 |
|
523 |
fileg_delay = 0; |
524 |
fileg_start = 0; //in percentage |
525 |
fileg_attack = 0; |
526 |
fileg_hold = 0; |
527 |
fileg_decay = 0; |
528 |
fileg_sustain = 100; // in percentage |
529 |
fileg_release = 0; |
530 |
|
531 |
pitcheg_delay = 0; |
532 |
pitcheg_start = 0; //in percentage |
533 |
pitcheg_attack = 0; |
534 |
pitcheg_hold = 0; |
535 |
pitcheg_decay = 0; |
536 |
pitcheg_sustain = 100; // in percentage |
537 |
pitcheg_release = 0; |
538 |
|
539 |
amplfo_delay = 0; |
540 |
amplfo_fade = 0; |
541 |
amplfo_freq = 0; |
542 |
amplfo_depth = 0; |
543 |
|
544 |
fillfo_delay = 0; |
545 |
fillfo_fade = 0; |
546 |
fillfo_freq = 0; |
547 |
fillfo_depth = 0; |
548 |
|
549 |
pitchlfo_delay = 0; |
550 |
pitchlfo_fade = 0; |
551 |
pitchlfo_freq = 0; |
552 |
pitchlfo_depth = 0; |
553 |
} |
554 |
|
555 |
Region* |
556 |
Group::RegionFactory() |
557 |
{ |
558 |
// This is where the current group setting are copied to the new region. |
559 |
|
560 |
Region* region = new Region(); |
561 |
|
562 |
region->id = id++; |
563 |
|
564 |
// sample definition |
565 |
region->sample = sample; |
566 |
|
567 |
// input control |
568 |
region->lochan = lochan; |
569 |
region->hichan = hichan; |
570 |
region->lokey = lokey; |
571 |
region->hikey = hikey; |
572 |
region->lovel = lovel; |
573 |
region->hivel = hivel; |
574 |
region->locc = locc; |
575 |
region->hicc = hicc; |
576 |
region->lobend = lobend; |
577 |
region->hibend = hibend; |
578 |
region->lobpm = lobpm; |
579 |
region->hibpm = hibpm; |
580 |
region->lochanaft = lochanaft; |
581 |
region->hichanaft = hichanaft; |
582 |
region->lopolyaft = lopolyaft; |
583 |
region->hipolyaft = hipolyaft; |
584 |
region->loprog = loprog; |
585 |
region->hiprog = hiprog; |
586 |
region->lorand = lorand; |
587 |
region->hirand = hirand; |
588 |
region->lotimer = lotimer; |
589 |
region->hitimer = hitimer; |
590 |
region->seq_length = seq_length; |
591 |
region->seq_position = seq_position; |
592 |
region->start_locc = start_locc; |
593 |
region->start_hicc = start_hicc; |
594 |
region->stop_locc = stop_locc; |
595 |
region->stop_hicc = stop_hicc; |
596 |
region->sw_lokey = sw_lokey; |
597 |
region->sw_hikey = sw_hikey; |
598 |
region->sw_last = sw_last; |
599 |
region->sw_down = sw_down; |
600 |
region->sw_up = sw_up; |
601 |
region->sw_previous = sw_previous; |
602 |
region->sw_vel = sw_vel; |
603 |
region->trigger = trigger; |
604 |
region->group = group; |
605 |
region->off_by = off_by; |
606 |
region->off_mode = off_mode; |
607 |
region->on_locc = on_locc; |
608 |
region->on_hicc = on_hicc; |
609 |
|
610 |
// sample player |
611 |
region->count = count; |
612 |
region->delay = delay; |
613 |
region->delay_random = delay_random; |
614 |
region->delay_oncc = delay_oncc; |
615 |
region->delay_beats = delay_beats; |
616 |
region->stop_beats = stop_beats; |
617 |
region->delay_samples = delay_samples; |
618 |
region->delay_samples_oncc = delay_samples_oncc; |
619 |
region->end = end; |
620 |
region->loop_crossfade = loop_crossfade; |
621 |
region->offset = offset; |
622 |
region->offset_random = offset_random; |
623 |
region->offset_oncc = offset_oncc; |
624 |
region->loop_mode = loop_mode; |
625 |
region->loop_start = loop_start; |
626 |
region->loop_end = loop_end; |
627 |
region->sync_beats = sync_beats; |
628 |
region->sync_offset = sync_offset; |
629 |
|
630 |
// amplifier |
631 |
region->volume = volume; |
632 |
region->pan = pan; |
633 |
region->width = width; |
634 |
region->position = position; |
635 |
region->amp_keytrack = amp_keytrack; |
636 |
region->amp_keycenter = amp_keycenter; |
637 |
region->amp_veltrack = amp_veltrack; |
638 |
region->amp_velcurve = amp_velcurve; |
639 |
region->amp_random = amp_random; |
640 |
region->rt_decay = rt_decay; |
641 |
region->gain_oncc = gain_oncc; |
642 |
region->xfin_lokey = xfin_lokey; |
643 |
region->xfin_hikey = xfin_hikey; |
644 |
region->xfout_lokey = xfout_lokey; |
645 |
region->xfout_hikey = xfout_hikey; |
646 |
region->xf_keycurve = xf_keycurve; |
647 |
region->xfin_lovel = xfin_lovel; |
648 |
region->xfin_hivel = xfin_lovel; |
649 |
region->xfout_lovel = xfout_lovel; |
650 |
region->xfout_hivel = xfout_hivel; |
651 |
region->xf_velcurve = xf_velcurve; |
652 |
region->xfin_locc = xfin_locc; |
653 |
region->xfin_hicc = xfin_hicc; |
654 |
region->xfout_locc = xfout_locc; |
655 |
region->xfout_hicc = xfout_hicc; |
656 |
region->xf_cccurve = xf_cccurve; |
657 |
|
658 |
// pitch |
659 |
region->transpose = transpose; |
660 |
region->tune = tune; |
661 |
region->pitch_keycenter = pitch_keycenter; |
662 |
region->pitch_keytrack = pitch_keytrack; |
663 |
region->pitch_veltrack = pitch_veltrack; |
664 |
region->pitch_random = pitch_random; |
665 |
region->bend_up = bend_up; |
666 |
region->bend_down = bend_down; |
667 |
region->bend_step = bend_step; |
668 |
|
669 |
// filter |
670 |
region->fil_type = fil_type; |
671 |
region->cutoff = cutoff; |
672 |
region->cutoff_oncc = cutoff_oncc; |
673 |
region->cutoff_smoothcc = cutoff_smoothcc; |
674 |
region->cutoff_stepcc = cutoff_stepcc; |
675 |
region->cutoff_curvecc = cutoff_curvecc; |
676 |
region->cutoff_chanaft = cutoff_chanaft; |
677 |
region->cutoff_polyaft = cutoff_polyaft; |
678 |
region->resonance = resonance; |
679 |
region->resonance_oncc = resonance_oncc; |
680 |
region->resonance_smoothcc = resonance_smoothcc; |
681 |
region->resonance_stepcc = resonance_stepcc; |
682 |
region->resonance_curvecc = resonance_curvecc; |
683 |
region->fil_keytrack = fil_keytrack; |
684 |
region->fil_keycenter = fil_keycenter; |
685 |
region->fil_veltrack = fil_veltrack; |
686 |
region->fil_random = fil_random; |
687 |
|
688 |
region->fil2_type = fil2_type; |
689 |
region->cutoff2 = cutoff2; |
690 |
region->cutoff2_oncc = cutoff2_oncc; |
691 |
region->cutoff2_smoothcc = cutoff2_smoothcc; |
692 |
region->cutoff2_stepcc = cutoff2_stepcc; |
693 |
region->cutoff2_curvecc = cutoff2_curvecc; |
694 |
region->cutoff2_chanaft = cutoff2_chanaft; |
695 |
region->cutoff2_polyaft = cutoff2_polyaft; |
696 |
region->resonance2 = resonance2; |
697 |
region->resonance2_oncc = resonance2_oncc; |
698 |
region->resonance2_smoothcc = resonance2_smoothcc; |
699 |
region->resonance2_stepcc = resonance2_stepcc; |
700 |
region->resonance2_curvecc = resonance2_curvecc; |
701 |
region->fil2_keytrack = fil2_keytrack; |
702 |
region->fil2_keycenter = fil2_keycenter; |
703 |
region->fil2_veltrack = fil2_veltrack; |
704 |
region->fil2_random = fil2_random; |
705 |
|
706 |
// per voice equalizer |
707 |
region->eq1_freq = eq1_freq; |
708 |
region->eq2_freq = eq2_freq; |
709 |
region->eq3_freq = eq3_freq; |
710 |
region->eq1_freq_oncc = eq1_freq_oncc; |
711 |
region->eq2_freq_oncc = eq2_freq_oncc; |
712 |
region->eq3_freq_oncc = eq3_freq_oncc; |
713 |
region->eq1_vel2freq = eq1_vel2freq; |
714 |
region->eq2_vel2freq = eq2_vel2freq; |
715 |
region->eq3_vel2freq = eq3_vel2freq; |
716 |
region->eq1_bw = eq1_bw; |
717 |
region->eq2_bw = eq2_bw; |
718 |
region->eq3_bw = eq3_bw; |
719 |
region->eq1_bw_oncc = eq1_bw_oncc; |
720 |
region->eq2_bw_oncc = eq2_bw_oncc; |
721 |
region->eq3_bw_oncc = eq3_bw_oncc; |
722 |
region->eq1_gain = eq1_gain; |
723 |
region->eq2_gain = eq2_gain; |
724 |
region->eq3_gain = eq3_gain; |
725 |
region->eq1_gain_oncc = eq1_gain_oncc; |
726 |
region->eq2_gain_oncc = eq2_gain_oncc; |
727 |
region->eq3_gain_oncc = eq3_gain_oncc; |
728 |
region->eq1_vel2gain = eq1_vel2gain; |
729 |
region->eq2_vel2gain = eq2_vel2gain; |
730 |
region->eq3_vel2gain = eq3_vel2gain; |
731 |
|
732 |
// envelope generator |
733 |
region->eg = eg; |
734 |
|
735 |
// deprecated |
736 |
region->ampeg_delay = ampeg_delay; |
737 |
region->ampeg_start = ampeg_start; |
738 |
region->ampeg_attack = ampeg_attack; |
739 |
region->ampeg_hold = ampeg_hold; |
740 |
region->ampeg_decay = ampeg_decay; |
741 |
region->ampeg_sustain = ampeg_sustain; |
742 |
region->ampeg_release = ampeg_release; |
743 |
|
744 |
region->fileg_delay = fileg_delay; |
745 |
region->fileg_start = fileg_start; |
746 |
region->fileg_attack = fileg_attack; |
747 |
region->fileg_hold = fileg_hold; |
748 |
region->fileg_decay = fileg_decay; |
749 |
region->fileg_sustain = fileg_sustain; |
750 |
region->fileg_release = fileg_release; |
751 |
|
752 |
region->pitcheg_delay = pitcheg_delay; |
753 |
region->pitcheg_start = pitcheg_start; |
754 |
region->pitcheg_attack = pitcheg_attack; |
755 |
region->pitcheg_hold = pitcheg_hold; |
756 |
region->pitcheg_decay = pitcheg_decay; |
757 |
region->pitcheg_sustain = pitcheg_sustain; |
758 |
region->pitcheg_release = pitcheg_release; |
759 |
|
760 |
region->amplfo_delay = amplfo_delay; |
761 |
region->amplfo_fade = amplfo_fade; |
762 |
region->amplfo_freq = amplfo_freq; |
763 |
region->amplfo_depth = amplfo_depth; |
764 |
|
765 |
region->fillfo_delay = fillfo_delay; |
766 |
region->fillfo_fade = fillfo_fade; |
767 |
region->fillfo_freq = fillfo_freq; |
768 |
region->fillfo_depth = fillfo_depth; |
769 |
|
770 |
region->pitchlfo_delay = pitchlfo_delay; |
771 |
region->pitchlfo_fade = pitchlfo_fade; |
772 |
region->pitchlfo_freq = pitchlfo_freq; |
773 |
region->pitchlfo_depth = pitchlfo_depth; |
774 |
|
775 |
return region; |
776 |
} |
777 |
|
778 |
///////////////////////////////////////////////////////////// |
779 |
// class File |
780 |
|
781 |
File::File(std::string file, SampleManager* pSampleManager) : |
782 |
_current_section(GROUP), |
783 |
default_path(""), |
784 |
octave_offset(0), |
785 |
note_offset(0) |
786 |
{ |
787 |
_instrument = new Instrument(LinuxSampler::Path::getBaseName(file), pSampleManager); |
788 |
_current_group = new Group(); |
789 |
pCurDef = _current_group; |
790 |
enum token_type_t { HEADER, OPCODE }; |
791 |
token_type_t token_type; |
792 |
std::string token_string; |
793 |
|
794 |
std::ifstream fs(file.c_str()); |
795 |
currentDir = LinuxSampler::Path::stripLastName(file); |
796 |
std::string token; |
797 |
std::string line; |
798 |
|
799 |
while (std::getline(fs, line)) |
800 |
{ |
801 |
// COMMENT |
802 |
std::string::size_type slash_index = line.find("//"); |
803 |
if (slash_index != std::string::npos) |
804 |
line.resize(slash_index); |
805 |
|
806 |
// DEFINITION |
807 |
std::stringstream linestream(line); |
808 |
while (linestream >> token) |
809 |
{ |
810 |
if (token[0] == '<' and token[token.size()-1] == '>') |
811 |
{ |
812 |
// HEAD |
813 |
if (!token_string.empty()) |
814 |
{ |
815 |
switch (token_type) |
816 |
{ |
817 |
case HEADER: |
818 |
push_header(token_string); |
819 |
break; |
820 |
case OPCODE: |
821 |
push_opcode(token_string); |
822 |
break; |
823 |
} |
824 |
token_string.erase(); |
825 |
} |
826 |
token_string.append(token); |
827 |
token_type = HEADER; |
828 |
} |
829 |
else if (token.find('=') != std::string::npos) |
830 |
{ |
831 |
// HEAD |
832 |
if (!token_string.empty()) |
833 |
{ |
834 |
switch (token_type) |
835 |
{ |
836 |
case HEADER: |
837 |
push_header(token_string); |
838 |
break; |
839 |
case OPCODE: |
840 |
push_opcode(token_string); |
841 |
break; |
842 |
} |
843 |
token_string.erase(); |
844 |
} |
845 |
token_string.append(token); |
846 |
token_type = OPCODE; |
847 |
} |
848 |
else |
849 |
{ |
850 |
// TAIL |
851 |
token_string.append(" "); |
852 |
token_string.append(token); |
853 |
} |
854 |
} |
855 |
|
856 |
// EOL |
857 |
if (!token_string.empty()) |
858 |
{ |
859 |
switch (token_type) |
860 |
{ |
861 |
case HEADER: |
862 |
push_header(token_string); |
863 |
break; |
864 |
case OPCODE: |
865 |
push_opcode(token_string); |
866 |
break; |
867 |
} |
868 |
token_string.erase(); |
869 |
} |
870 |
} |
871 |
|
872 |
for (int i = 0; i < _instrument->regions.size(); i++) { |
873 |
::sfz::Region* pRegion = _instrument->regions[i]; |
874 |
int low = pRegion->lokey; |
875 |
int high = pRegion->hikey; |
876 |
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
877 |
std::cerr << "Invalid key range: " << low << " - " << high << std::endl; |
878 |
} else { |
879 |
for (int j = low; j <= high; j++) _instrument->KeyBindings[j] = true; |
880 |
} |
881 |
|
882 |
// get keyswitches |
883 |
low = pRegion->sw_lokey; |
884 |
high = pRegion->sw_hikey; |
885 |
if(low == -1 && high == -1) { |
886 |
// Key switches not defined, so nothing to do |
887 |
} else if(low >= 0 && low <= 127 && high >= 0 && high <= 127 && high >= low) { |
888 |
for (int j = low; j <= high; j++) _instrument->KeySwitchBindings[j] = true; |
889 |
} else { |
890 |
std::cerr << "Invalid key switch range: " << low << " - " << high << std::endl; |
891 |
} |
892 |
|
893 |
// create velocity response curve |
894 |
int prev = 0; |
895 |
float prevvalue = 0; |
896 |
for (int v = 0 ; v < 128 ; v++) { |
897 |
if (pRegion->amp_velcurve[v] >= 0) { |
898 |
float step = (pRegion->amp_velcurve[v] - prevvalue) / (v - prev); |
899 |
for ( ; prev < v ; prev++) { |
900 |
pRegion->amp_velcurve[prev] = prevvalue; |
901 |
prevvalue += step; |
902 |
} |
903 |
} |
904 |
} |
905 |
if (prev) { |
906 |
float step = (1 - prevvalue) / (127 - prev); |
907 |
for ( ; prev < 128 ; prev++) { |
908 |
pRegion->amp_velcurve[prev] = prevvalue; |
909 |
prevvalue += step; |
910 |
} |
911 |
} else { |
912 |
// default curve |
913 |
for (int v = 0 ; v < 128 ; v++) { |
914 |
pRegion->amp_velcurve[v] = v * v / (127.0 * 127.0); |
915 |
} |
916 |
} |
917 |
// apply amp_veltrack |
918 |
float offset = -pRegion->amp_veltrack; |
919 |
if (offset <= 0) offset += 100; |
920 |
for (int v = 0 ; v < 128 ; v++) { |
921 |
pRegion->amp_velcurve[v] = |
922 |
(offset + pRegion->amp_veltrack * pRegion->amp_velcurve[v]) / 100; |
923 |
} |
924 |
} |
925 |
} |
926 |
|
927 |
File::~File() |
928 |
{ |
929 |
delete _current_group; |
930 |
delete _instrument; |
931 |
} |
932 |
|
933 |
Instrument* |
934 |
File::GetInstrument() |
935 |
{ |
936 |
return _instrument; |
937 |
} |
938 |
|
939 |
void |
940 |
File::push_header(std::string token) |
941 |
{ |
942 |
if (token == "<group>") |
943 |
{ |
944 |
_current_section = GROUP; |
945 |
_current_group->Reset(); |
946 |
pCurDef = _current_group; |
947 |
} |
948 |
else if (token == "<region>") |
949 |
{ |
950 |
_current_section = REGION; |
951 |
_current_region = _current_group->RegionFactory(); |
952 |
pCurDef = _current_region; |
953 |
_instrument->regions.push_back(_current_region); |
954 |
_current_region->SetInstrument(_instrument); |
955 |
} |
956 |
else if (token == "<control>") |
957 |
{ |
958 |
_current_section = CONTROL; |
959 |
default_path = ""; |
960 |
octave_offset = 0; |
961 |
note_offset = 0; |
962 |
} |
963 |
else |
964 |
{ |
965 |
_current_section = UNKNOWN; |
966 |
std::cerr << "The header '" << token << "' is unsupported by libsfz!" << std::endl; |
967 |
} |
968 |
} |
969 |
|
970 |
void |
971 |
File::push_opcode(std::string token) |
972 |
{ |
973 |
if (_current_section == UNKNOWN) |
974 |
return; |
975 |
|
976 |
std::string::size_type delimiter_index = token.find('='); |
977 |
std::string key = token.substr(0, delimiter_index); |
978 |
std::string value = token.substr(delimiter_index + 1); |
979 |
int x, y; |
980 |
|
981 |
// sample definition |
982 |
if ("sample" == key) |
983 |
{ |
984 |
std::string path = default_path + value; |
985 |
#ifndef WIN32 |
986 |
for (int i = 0; i < path.length(); i++) if( path[i] == '\\') path[i] = '/'; |
987 |
#endif |
988 |
path = currentDir + LinuxSampler::File::DirSeparator + path; // TODO: check for absolute path |
989 |
|
990 |
if(pCurDef) pCurDef->sample = path; |
991 |
return; |
992 |
} |
993 |
|
994 |
// control header directives |
995 |
else if ("default_path" == key) |
996 |
{ |
997 |
switch (_current_section) |
998 |
{ |
999 |
case CONTROL: |
1000 |
default_path = value; |
1001 |
} |
1002 |
return; |
1003 |
} |
1004 |
else if ("octave_offset" == key) |
1005 |
{ |
1006 |
switch (_current_section) |
1007 |
{ |
1008 |
case CONTROL: |
1009 |
octave_offset = ToInt(value); |
1010 |
} |
1011 |
return; |
1012 |
} |
1013 |
else if ("note_offset" == key) |
1014 |
{ |
1015 |
switch (_current_section) |
1016 |
{ |
1017 |
case CONTROL: |
1018 |
note_offset = ToInt(value); |
1019 |
} |
1020 |
return; |
1021 |
} |
1022 |
|
1023 |
// input controls |
1024 |
else if ("lochan" == key) pCurDef->lochan = ToInt(value); |
1025 |
else if ("hichan" == key) pCurDef->hichan = ToInt(value); |
1026 |
else if ("lokey" == key) pCurDef->lokey = parseKey(value); |
1027 |
else if ("hikey" == key) pCurDef->hikey = parseKey(value); |
1028 |
else if ("key" == key) |
1029 |
{ |
1030 |
pCurDef->lokey = pCurDef->hikey = pCurDef->pitch_keycenter = parseKey(value); |
1031 |
} |
1032 |
else if ("lovel" == key) pCurDef->lovel = ToInt(value); |
1033 |
else if ("hivel" == key) pCurDef->hivel = ToInt(value); |
1034 |
else if ("lobend" == key) pCurDef->lobend = ToInt(value); |
1035 |
else if ("hibend" == key) pCurDef->hibend = ToInt(value); |
1036 |
else if ("lobpm" == key) pCurDef->lobpm = ToInt(value); |
1037 |
else if ("hibpm" == key) pCurDef->hibpm = ToInt(value); |
1038 |
else if ("lochanaft" == key) pCurDef->lochanaft = ToInt(value); |
1039 |
else if ("hichanaft" == key) pCurDef->hichanaft = ToInt(value); |
1040 |
else if ("lopolyaft" == key) pCurDef->lopolyaft = ToInt(value); |
1041 |
else if ("hipolyaft" == key) pCurDef->hipolyaft = ToInt(value); |
1042 |
else if ("loprog" == key) pCurDef->loprog = ToInt(value); |
1043 |
else if ("hiprog" == key) pCurDef->hiprog = ToInt(value); |
1044 |
else if ("lorand" == key) pCurDef->lorand = ToFloat(value); |
1045 |
else if ("hirand" == key) pCurDef->hirand = ToFloat(value); |
1046 |
else if ("lotimer" == key) pCurDef->lotimer = ToFloat(value); |
1047 |
else if ("hitimer" == key) pCurDef->hitimer = ToFloat(value); |
1048 |
else if ("seq_length" == key) pCurDef->seq_length = ToInt(value); |
1049 |
else if ("seq_position" == key) pCurDef->seq_position = ToInt(value); |
1050 |
else if ("sw_lokey" == key) pCurDef->sw_lokey = parseKey(value); |
1051 |
else if ("sw_hikey" == key) pCurDef->sw_hikey = parseKey(value); |
1052 |
else if ("sw_last" == key) pCurDef->sw_last = parseKey(value); |
1053 |
else if ("sw_down" == key) pCurDef->sw_down = parseKey(value); |
1054 |
else if ("sw_up" == key) pCurDef->sw_up = parseKey(value); |
1055 |
else if ("sw_previous" == key) pCurDef->sw_previous = parseKey(value); |
1056 |
else if ("sw_vel" == key) |
1057 |
{ |
1058 |
if (value == "current") pCurDef->sw_vel = VEL_CURRENT; |
1059 |
else if (value == "previous") pCurDef->sw_vel = VEL_PREVIOUS; |
1060 |
} |
1061 |
else if ("trigger" == key) |
1062 |
{ |
1063 |
if (value == "attack") pCurDef->trigger = TRIGGER_ATTACK; |
1064 |
else if (value == "release") pCurDef->trigger = TRIGGER_RELEASE; |
1065 |
else if (value == "first") pCurDef->trigger = TRIGGER_FIRST; |
1066 |
else if (value == "legato") pCurDef->trigger = TRIGGER_LEGATO; |
1067 |
} |
1068 |
else if ("group" == key) pCurDef->group = ToInt(value); |
1069 |
else if ("off_by" == key || "offby" == key) pCurDef->off_by = ToInt(value); |
1070 |
else if ("off_mode" == key || "offmode" == key) |
1071 |
{ |
1072 |
if (value == "fast") _current_group->off_mode = OFF_FAST; |
1073 |
else if (value == "normal") _current_group->off_mode = OFF_NORMAL; |
1074 |
} |
1075 |
|
1076 |
// sample player |
1077 |
else if ("count" == key) { pCurDef->count = ToInt(value); pCurDef->loop_mode = ONE_SHOT; } |
1078 |
else if ("delay" == key) pCurDef->delay = ToFloat(value); |
1079 |
else if ("delay_random" == key) pCurDef->delay_random = ToFloat(value); |
1080 |
else if ("delay_beats" == key) pCurDef->delay_beats = ToInt(value); |
1081 |
else if ("stop_beats" == key) pCurDef->stop_beats = ToInt(value); |
1082 |
else if ("delay_samples" == key) pCurDef->delay_samples = ToInt(value); |
1083 |
else if ("end" == key) pCurDef->end = ToInt(value); |
1084 |
else if ("loop_crossfade" == key) pCurDef->loop_crossfade = ToFloat(value); |
1085 |
else if ("offset_random" == key) pCurDef->offset_random = ToInt(value); |
1086 |
else if ("loop_mode" == key || "loopmode" == key) |
1087 |
{ |
1088 |
if (value == "no_loop") pCurDef->loop_mode = NO_LOOP; |
1089 |
else if (value == "one_shot") pCurDef->loop_mode = ONE_SHOT; |
1090 |
else if (value == "loop_continuous") pCurDef->loop_mode = LOOP_CONTINUOUS; |
1091 |
else if (value == "loop_sustain") pCurDef->loop_mode = LOOP_SUSTAIN; |
1092 |
} |
1093 |
else if ("loop_start" == key) pCurDef->loop_start = ToInt(value); |
1094 |
else if ("loopstart" == key) pCurDef->loop_start = ToInt(value); // nonstandard |
1095 |
else if ("loop_end" == key) pCurDef->loop_end = ToInt(value); |
1096 |
else if ("loopend" == key) pCurDef->loop_end = ToInt(value); // nonstandard |
1097 |
else if ("sync_beats" == key) pCurDef->sync_beats = ToInt(value); |
1098 |
else if ("sync_offset" == key) pCurDef->sync_offset = ToInt(value); |
1099 |
|
1100 |
// amplifier |
1101 |
else if ("volume" == key) pCurDef->volume = ToFloat(value); |
1102 |
else if ("pan" == key) pCurDef->pan = ToFloat(value); |
1103 |
else if ("width" == key) pCurDef->width = ToFloat(value); |
1104 |
else if ("position" == key) pCurDef->position = ToFloat(value); |
1105 |
else if ("amp_keytrack" == key) pCurDef->amp_keytrack = ToFloat(value); |
1106 |
else if ("amp_keycenter" == key) pCurDef->amp_keycenter = parseKey(value); |
1107 |
else if ("amp_veltrack" == key) pCurDef->amp_veltrack = ToFloat(value); |
1108 |
else if ("amp_random" == key) pCurDef->amp_random = ToFloat(value); |
1109 |
else if ("rt_decay" == key) pCurDef->rt_decay = ToFloat(value); |
1110 |
else if ("xfin_lokey" == key) pCurDef->xfin_lokey = parseKey(value); |
1111 |
else if ("xfin_hikey" == key) pCurDef->xfin_hikey = parseKey(value); |
1112 |
else if ("xfout_lokey" == key) pCurDef->xfout_lokey = parseKey(value); |
1113 |
else if ("xfout_hikey" == key) pCurDef->xfout_hikey = parseKey(value); |
1114 |
else if ("xf_keycurve" == key) |
1115 |
{ |
1116 |
if (value == "gain") pCurDef->xf_keycurve = GAIN; |
1117 |
else if (value == "power") pCurDef->xf_keycurve = POWER; |
1118 |
} |
1119 |
else if ("xfin_lovel" == key) pCurDef->xfin_lovel = ToInt(value); |
1120 |
else if ("xfin_hivel" == key) pCurDef->xfin_hivel = ToInt(value); |
1121 |
else if ("xfout_lovel" == key) pCurDef->xfout_lovel = ToInt(value); |
1122 |
else if ("xfout_hivel" == key) pCurDef->xfout_hivel = ToInt(value); |
1123 |
else if ("xf_velcurve" == key) |
1124 |
{ |
1125 |
if (value == "gain") pCurDef->xf_velcurve = GAIN; |
1126 |
else if (value == "power") pCurDef->xf_velcurve = POWER; |
1127 |
} |
1128 |
else if ("xf_cccurve" == key) |
1129 |
{ |
1130 |
if (value == "gain") pCurDef->xf_cccurve = GAIN; |
1131 |
else if (value == "power") pCurDef->xf_cccurve = POWER; |
1132 |
} |
1133 |
|
1134 |
// pitch |
1135 |
else if ("transpose" == key) pCurDef->transpose = ToInt(value); |
1136 |
else if ("tune" == key) pCurDef->tune = ToInt(value); |
1137 |
else if ("pitch_keycenter" == key) pCurDef->pitch_keycenter = parseKey(value); |
1138 |
else if ("pitch_keytrack" == key) pCurDef->pitch_keytrack = ToInt(value); |
1139 |
else if ("pitch_veltrack" == key) pCurDef->pitch_veltrack = ToInt(value); |
1140 |
else if ("pitch_random" == key) pCurDef->pitch_random = ToInt(value); |
1141 |
else if ("bend_up" == key || "bendup" == key) pCurDef->bend_up = ToInt(value); |
1142 |
else if ("bend_down" == key || "benddown" == key) pCurDef->bend_down = ToInt(value); |
1143 |
else if ("bend_step" == key) pCurDef->bend_step = ToInt(value); |
1144 |
|
1145 |
// filter |
1146 |
else if ("fil_type" == key) |
1147 |
{ |
1148 |
if (value == "lpf_1p") pCurDef->fil_type = LPF_1P; |
1149 |
else if (value == "hpf_1p") pCurDef->fil_type = HPF_1P; |
1150 |
else if (value == "bpf_1p") pCurDef->fil_type = BPF_1P; |
1151 |
else if (value == "brf_1p") pCurDef->fil_type = BRF_1P; |
1152 |
else if (value == "apf_1p") pCurDef->fil_type = APF_1P; |
1153 |
else if (value == "lpf_2p") pCurDef->fil_type = LPF_2P; |
1154 |
else if (value == "hpf_2p") pCurDef->fil_type = HPF_2P; |
1155 |
else if (value == "bpf_2p") pCurDef->fil_type = BPF_2P; |
1156 |
else if (value == "brf_2p") pCurDef->fil_type = BRF_2P; |
1157 |
else if (value == "pkf_2p") pCurDef->fil_type = PKF_2P; |
1158 |
else if (value == "lpf_4p") pCurDef->fil_type = LPF_4P; |
1159 |
else if (value == "hpf_4p") pCurDef->fil_type = HPF_4P; |
1160 |
else if (value == "lpf_6p") pCurDef->fil_type = LPF_6P; |
1161 |
else if (value == "hpf_6p") pCurDef->fil_type = HPF_6P; |
1162 |
} |
1163 |
else if ("fil2_type" == key) |
1164 |
{ |
1165 |
if (value == "lpf_1p") pCurDef->fil2_type = LPF_1P; |
1166 |
else if (value == "hpf_1p") pCurDef->fil2_type = HPF_1P; |
1167 |
else if (value == "bpf_1p") pCurDef->fil2_type = BPF_1P; |
1168 |
else if (value == "brf_1p") pCurDef->fil2_type = BRF_1P; |
1169 |
else if (value == "apf_1p") pCurDef->fil2_type = APF_1P; |
1170 |
else if (value == "lpf_2p") pCurDef->fil2_type = LPF_2P; |
1171 |
else if (value == "hpf_2p") pCurDef->fil2_type = HPF_2P; |
1172 |
else if (value == "bpf_2p") pCurDef->fil2_type = BPF_2P; |
1173 |
else if (value == "brf_2p") pCurDef->fil2_type = BRF_2P; |
1174 |
else if (value == "pkf_2p") pCurDef->fil2_type = PKF_2P; |
1175 |
else if (value == "lpf_4p") pCurDef->fil2_type = LPF_4P; |
1176 |
else if (value == "hpf_4p") pCurDef->fil2_type = HPF_4P; |
1177 |
else if (value == "lpf_6p") pCurDef->fil2_type = LPF_6P; |
1178 |
else if (value == "hpf_6p") pCurDef->fil2_type = HPF_6P; |
1179 |
} |
1180 |
else if ("cutoff" == key) pCurDef->cutoff = ToFloat(value); |
1181 |
else if ("cutoff2" == key) pCurDef->cutoff2 = ToFloat(value); |
1182 |
else if ("cutoff_chanaft" == key) pCurDef->cutoff_chanaft = ToInt(value); |
1183 |
else if ("cutoff2_chanaft" == key) pCurDef->cutoff2_chanaft = ToInt(value); |
1184 |
else if ("cutoff_polyaft" == key) pCurDef->cutoff_polyaft = ToInt(value); |
1185 |
else if ("cutoff2_polyaft" == key) pCurDef->cutoff2_polyaft = ToInt(value); |
1186 |
else if ("resonance" == key) pCurDef->resonance = ToFloat(value); |
1187 |
else if ("resonance2" == key) pCurDef->resonance2 = ToFloat(value); |
1188 |
else if ("fil_keytrack" == key) pCurDef->fil_keytrack = ToInt(value); |
1189 |
else if ("fil2_keytrack" == key) pCurDef->fil2_keytrack = ToInt(value); |
1190 |
else if ("fil_keycenter" == key) pCurDef->fil_keycenter = parseKey(value); |
1191 |
else if ("fil2_keycenter" == key) pCurDef->fil2_keycenter = parseKey(value); |
1192 |
else if ("fil_veltrack" == key) pCurDef->fil_veltrack = ToInt(value); |
1193 |
else if ("fil2_veltrack" == key) pCurDef->fil2_veltrack = ToInt(value); |
1194 |
else if ("fil_random" == key) pCurDef->fil_random = ToInt(value); |
1195 |
else if ("fil2_random" == key) pCurDef->fil2_random = ToInt(value); |
1196 |
|
1197 |
// per voice equalizer |
1198 |
else if ("eq1_freq" == key) pCurDef->eq1_freq = ToFloat(value); |
1199 |
else if ("eq2_freq" == key) pCurDef->eq2_freq = ToFloat(value); |
1200 |
else if ("eq3_freq" == key) pCurDef->eq3_freq = ToFloat(value); |
1201 |
else if ("eq1_vel2freq" == key) pCurDef->eq1_vel2freq = ToFloat(value); |
1202 |
else if ("eq2_vel2freq" == key) pCurDef->eq2_vel2freq = ToFloat(value); |
1203 |
else if ("eq3_vel2freq" == key) pCurDef->eq3_vel2freq = ToFloat(value); |
1204 |
else if ("eq1_bw" == key) pCurDef->eq1_bw = ToFloat(value); |
1205 |
else if ("eq2_bw" == key) pCurDef->eq2_bw = ToFloat(value); |
1206 |
else if ("eq3_bw" == key) pCurDef->eq3_bw = ToFloat(value); |
1207 |
else if ("eq1_gain" == key) pCurDef->eq1_gain = ToFloat(value); |
1208 |
else if ("eq2_gain" == key) pCurDef->eq2_gain = ToFloat(value); |
1209 |
else if ("eq3_gain" == key) pCurDef->eq3_gain = ToFloat(value); |
1210 |
else if ("eq1_vel2gain" == key) pCurDef->eq1_vel2gain = ToFloat(value); |
1211 |
else if ("eq2_vel2gain" == key) pCurDef->eq2_vel2gain = ToFloat(value); |
1212 |
else if ("eq3_vel2gain" == key) pCurDef->eq3_vel2gain = ToFloat(value); |
1213 |
|
1214 |
else if (sscanf(key.c_str(), "amp_velcurve_%d", &x)) { |
1215 |
pCurDef->amp_velcurve[x] = ToFloat(value); |
1216 |
} |
1217 |
|
1218 |
// CCs |
1219 |
else if (key.find("cc") != std::string::npos) |
1220 |
{ |
1221 |
std::string::size_type delimiter_index = key.find("cc"); |
1222 |
std::string key_cc = key.substr(0, delimiter_index); |
1223 |
int num_cc = ToInt(key.substr(delimiter_index + 2)); |
1224 |
|
1225 |
// input controls |
1226 |
if ("lo" == key_cc) pCurDef->locc[num_cc] = ToInt(value); |
1227 |
else if ("hi" == key_cc) pCurDef->hicc[num_cc] = ToInt(value); |
1228 |
else if ("start_lo" == key_cc) pCurDef->start_locc[num_cc] = ToInt(value); |
1229 |
else if ("start_hi" == key_cc) pCurDef->start_hicc[num_cc] = ToInt(value); |
1230 |
else if ("stop_lo" == key_cc) pCurDef->stop_locc[num_cc] = ToInt(value); |
1231 |
else if ("stop_hi" == key_cc) pCurDef->stop_hicc[num_cc] = ToInt(value); |
1232 |
else if ("on_lo" == key_cc) pCurDef->on_locc[num_cc] = ToInt(value); |
1233 |
else if ("on_hi" == key_cc) pCurDef->on_hicc[num_cc] = ToInt(value); |
1234 |
|
1235 |
// sample player |
1236 |
else if ("delay_on" == key_cc) pCurDef->delay_oncc[num_cc] = ToFloat(value); |
1237 |
else if ("delay_samples_on" == key_cc) pCurDef->delay_samples_oncc[num_cc] = ToInt(value); |
1238 |
else if ("offset_on" == key_cc) pCurDef->offset_oncc[num_cc] = ToInt(value); |
1239 |
|
1240 |
// amplifier |
1241 |
else if ("gain_on" == key_cc) pCurDef->gain_oncc[num_cc] = ToFloat(value); |
1242 |
else if ("xfin_lo" == key_cc) pCurDef->xfin_locc[num_cc] = ToInt(value); |
1243 |
else if ("xfin_hi" == key_cc) pCurDef->xfin_hicc[num_cc] = ToInt(value); |
1244 |
else if ("xfout_lo" == key_cc) pCurDef->xfout_locc[num_cc] = ToInt(value); |
1245 |
else if ("xfout_hi" == key_cc) pCurDef->xfout_hicc[num_cc] = ToInt(value); |
1246 |
|
1247 |
// filter |
1248 |
else if ("cutoff_on" == key_cc) pCurDef->cutoff_oncc[num_cc] = ToInt(value); |
1249 |
else if ("cutoff2_on" == key_cc) pCurDef->cutoff2_oncc[num_cc] = ToInt(value); |
1250 |
else if ("cutoff_smooth" == key_cc) pCurDef->cutoff_smoothcc[num_cc] = ToInt(value); |
1251 |
else if ("cutoff2_smooth" == key_cc) pCurDef->cutoff2_smoothcc[num_cc] = ToInt(value); |
1252 |
else if ("cutoff_step" == key_cc) pCurDef->cutoff_stepcc[num_cc] = ToInt(value); |
1253 |
else if ("cutoff2_step" == key_cc) pCurDef->cutoff2_stepcc[num_cc] = ToInt(value); |
1254 |
else if ("cutoff_curve" == key_cc) pCurDef->cutoff_curvecc[num_cc] = ToInt(value); |
1255 |
else if ("cutoff2_curve" == key_cc) pCurDef->cutoff2_curvecc[num_cc] = ToInt(value); |
1256 |
else if ("resonance_on" == key_cc) pCurDef->resonance_oncc[num_cc] = ToInt(value); |
1257 |
else if ("resonance2_on" == key_cc) pCurDef->resonance2_oncc[num_cc] = ToInt(value); |
1258 |
else if ("resonance_smooth" == key_cc) pCurDef->resonance_smoothcc[num_cc] = ToInt(value); |
1259 |
else if ("resonance2_smooth" == key_cc) pCurDef->resonance2_smoothcc[num_cc] = ToInt(value); |
1260 |
else if ("resonance_step" == key_cc) pCurDef->resonance_stepcc[num_cc] = ToInt(value); |
1261 |
else if ("resonance2_step" == key_cc) pCurDef->resonance2_stepcc[num_cc] = ToInt(value); |
1262 |
else if ("resonance_curve" == key_cc) pCurDef->resonance_curvecc[num_cc] = ToInt(value); |
1263 |
else if ("resonance2_curve" == key_cc) pCurDef->resonance2_curvecc[num_cc] = ToInt(value); |
1264 |
|
1265 |
// per voice equalizer |
1266 |
else if ("eq1_freq_on" == key_cc) pCurDef->eq1_freq_oncc[num_cc] = ToInt(value); |
1267 |
else if ("eq2_freq_on" == key_cc) pCurDef->eq2_freq_oncc[num_cc] = ToInt(value); |
1268 |
else if ("eq3_freq_on" == key_cc) pCurDef->eq3_freq_oncc[num_cc] = ToInt(value); |
1269 |
else if ("eq1_bw_on" == key_cc) pCurDef->eq1_bw_oncc[num_cc] = ToInt(value); |
1270 |
else if ("eq2_bw_on" == key_cc) pCurDef->eq2_bw_oncc[num_cc] = ToInt(value); |
1271 |
else if ("eq3_bw_on" == key_cc) pCurDef->eq3_bw_oncc[num_cc] = ToInt(value); |
1272 |
else if ("eq1_gain_on" == key_cc) pCurDef->eq1_gain_oncc[num_cc] = ToInt(value); |
1273 |
else if ("eq2_gain_on" == key_cc) pCurDef->eq2_gain_oncc[num_cc] = ToInt(value); |
1274 |
else if ("eq3_gain_on" == key_cc) pCurDef->eq3_gain_oncc[num_cc] = ToInt(value); |
1275 |
else std::cerr << "The opcode '" << key << "' is unsupported by libsfz!" << std::endl; |
1276 |
} |
1277 |
// v2 envelope generators |
1278 |
else if (sscanf(key.c_str(), "eg%d%n", &x, &y)) { |
1279 |
const char* s = key.c_str() + y; |
1280 |
if (sscanf(s, "_time%d", &y)) egnode(x, y).time = ToFloat(value); |
1281 |
else if (sscanf(s, "_level%d", &y)) egnode(x, y).level = ToFloat(value); |
1282 |
else if (sscanf(s, "_shape%d", &y)) egnode(x, y).shape = ToFloat(value); |
1283 |
else if (sscanf(s, "_curve%d", &y)) egnode(x, y).curve = ToFloat(value); |
1284 |
else if (strcmp(s, "_sustain") == 0) eg(x).sustain = ToInt(value); |
1285 |
else if (strcmp(s, "_loop") == 0) eg(x).loop = ToInt(value); |
1286 |
else if (strcmp(s, "_loop_count") == 0) eg(x).loop_count = ToInt(value); |
1287 |
else if (strcmp(s, "_amplitude") == 0) eg(x).amplitude = ToFloat(value); |
1288 |
else if (strcmp(s, "_cutoff") == 0) eg(x).cutoff = ToFloat(value); |
1289 |
else std::cerr << "The opcode '" << key << "' is unsupported by libsfz!" << std::endl; |
1290 |
} |
1291 |
|
1292 |
// v1 envelope generators |
1293 |
else if ("ampeg_delay" == key) pCurDef->ampeg_delay = ToFloat(value); |
1294 |
else if ("ampeg_start" == key) pCurDef->ampeg_start = ToFloat(value); |
1295 |
else if ("ampeg_attack" == key) pCurDef->ampeg_attack = ToFloat(value); |
1296 |
else if ("ampeg_hold" == key) pCurDef->ampeg_hold = ToFloat(value); |
1297 |
else if ("ampeg_decay" == key) pCurDef->ampeg_decay = ToFloat(value); |
1298 |
else if ("ampeg_sustain" == key) pCurDef->ampeg_sustain = ToFloat(value); |
1299 |
else if ("ampeg_release" == key) pCurDef->ampeg_release = ToFloat(value); |
1300 |
else if ("fileg_delay" == key) pCurDef->fileg_delay = ToFloat(value); |
1301 |
else if ("fileg_start" == key) pCurDef->fileg_start = ToFloat(value); |
1302 |
else if ("fileg_attack" == key) pCurDef->fileg_attack = ToFloat(value); |
1303 |
else if ("fileg_hold" == key) pCurDef->fileg_hold = ToFloat(value); |
1304 |
else if ("fileg_decay" == key) pCurDef->fileg_decay = ToFloat(value); |
1305 |
else if ("fileg_sustain" == key) pCurDef->fileg_sustain = ToFloat(value); |
1306 |
else if ("fileg_release" == key) pCurDef->fileg_release = ToFloat(value); |
1307 |
else if ("pitcheg_delay" == key) pCurDef->pitcheg_delay = ToFloat(value); |
1308 |
else if ("pitcheg_start" == key) pCurDef->pitcheg_start = ToFloat(value); |
1309 |
else if ("pitcheg_attack" == key) pCurDef->pitcheg_attack = ToFloat(value); |
1310 |
else if ("pitcheg_hold" == key) pCurDef->pitcheg_hold = ToFloat(value); |
1311 |
else if ("pitcheg_decay" == key) pCurDef->pitcheg_decay = ToFloat(value); |
1312 |
else if ("pitcheg_sustain" == key) pCurDef->pitcheg_sustain = ToFloat(value); |
1313 |
else if ("pitcheg_release" == key) pCurDef->pitcheg_release = ToFloat(value); |
1314 |
|
1315 |
// v1 LFO |
1316 |
else if ("amplfo_delay" == key) pCurDef->amplfo_delay = ToFloat(value); |
1317 |
else if ("amplfo_fade" == key) pCurDef->amplfo_fade = ToFloat(value); |
1318 |
else if ("amplfo_freq" == key) pCurDef->amplfo_freq = ToFloat(value); |
1319 |
else if ("amplfo_depth" == key) pCurDef->amplfo_depth = ToFloat(value); |
1320 |
else if ("fillfo_delay" == key) pCurDef->fillfo_delay = ToFloat(value); |
1321 |
else if ("fillfo_fade" == key) pCurDef->fillfo_fade = ToFloat(value); |
1322 |
else if ("fillfo_freq" == key) pCurDef->fillfo_freq = ToFloat(value); |
1323 |
else if ("fillfo_depth" == key) pCurDef->fillfo_depth = ToFloat(value); |
1324 |
else if ("pitchlfo_delay" == key) pCurDef->pitchlfo_delay = ToFloat(value); |
1325 |
else if ("pitchlfo_fade" == key) pCurDef->pitchlfo_fade = ToFloat(value); |
1326 |
else if ("pitchlfo_freq" == key) pCurDef->pitchlfo_freq = ToFloat(value); |
1327 |
else if ("pitchlfo_depth" == key) pCurDef->pitchlfo_depth = ToInt(value); |
1328 |
|
1329 |
else { |
1330 |
std::cerr << "The opcode '" << key << "' is unsupported by libsfz!" << std::endl; |
1331 |
} |
1332 |
} |
1333 |
|
1334 |
int File::parseKey(const std::string& s) { |
1335 |
int i; |
1336 |
std::istringstream iss(s); |
1337 |
if (isdigit(iss.peek())) { |
1338 |
iss >> i; |
1339 |
} else { |
1340 |
switch (tolower(iss.get())) { |
1341 |
case 'c': i = 0; break; |
1342 |
case 'd': i = 2; break; |
1343 |
case 'e': i = 4; break; |
1344 |
case 'f': i = 5; break; |
1345 |
case 'g': i = 7; break; |
1346 |
case 'a': i = 9; break; |
1347 |
case 'b': i = 11; break; |
1348 |
default: |
1349 |
std::cerr << "Not a note: " << s << std::endl; |
1350 |
return 0; |
1351 |
} |
1352 |
if (iss.peek() == '#') { |
1353 |
i++; |
1354 |
iss.get(); |
1355 |
} else if (tolower(iss.peek()) == 'b') { |
1356 |
i--; |
1357 |
iss.get(); |
1358 |
} |
1359 |
int octave; |
1360 |
if (!(iss >> octave)) { |
1361 |
std::cerr << "Not a note: " << s << std::endl; |
1362 |
return 0; |
1363 |
} |
1364 |
i += (octave + 1) * 12; |
1365 |
} |
1366 |
return i + note_offset + 12 * octave_offset; |
1367 |
} |
1368 |
|
1369 |
EGNode::EGNode() : time(0), level(0), shape(0), curve(0) { |
1370 |
} |
1371 |
|
1372 |
EG::EG() : |
1373 |
sustain(0), loop(0), loop_count(0), |
1374 |
amplitude(0), cutoff(0) { |
1375 |
} |
1376 |
|
1377 |
EG& File::eg(int x) { |
1378 |
while (pCurDef->eg.size() <= x) { |
1379 |
pCurDef->eg.add(EG()); |
1380 |
} |
1381 |
return pCurDef->eg[x]; |
1382 |
} |
1383 |
|
1384 |
EGNode& File::egnode(int x, int y) { |
1385 |
EG& e = eg(x); |
1386 |
while (e.node.size() <= y) { |
1387 |
e.node.add(EGNode()); |
1388 |
} |
1389 |
return e.node[y]; |
1390 |
} |
1391 |
|
1392 |
} // !namespace sfz |