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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2086 - (hide annotations) (download)
Sun Apr 25 12:51:30 2010 UTC (13 years, 11 months ago) by persson
File size: 56493 byte(s)
* sfz engine: added support for transpose
* sfz engine: fixed crash when using sw_down/up
* sfz engine: improved logic for sw_lokey/hikey/up/down/last
* sfz parser: added more v1 aliases
* fixed building with newer MinGW-w64

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC