/[svn]/linuxsampler/trunk/src/engines/sfz/sfz.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/sfz/sfz.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2091 - (show annotations) (download)
Sat May 15 09:02:31 2010 UTC (13 years, 10 months ago) by persson
File size: 56201 byte(s)
* sfz engine: reduced memory usage for sfz data

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC