/[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 2058 - (hide annotations) (download)
Sun Feb 14 11:40:49 2010 UTC (14 years, 1 month ago) by persson
File size: 53064 byte(s)
* sfz/sf2 engines: fixed memory leak and memory handling errors
* sfz engine: added support for sw_trigger=first, sw_trigger=legato
  and sw_previous
* sfz parser: allow non-numerical key values ("C#4" for example)
* sfz engine: "key" opcode now sets pitch_keycenter too
* sfz engine: fixed error when unloading instrument with same sample
  used by multiple regions
* sfz parser: added some opcode aliases, like loopmode for loop_mode,
  to be more compatible

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC