/[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 2072 - (hide annotations) (download)
Sat Mar 20 11:37:52 2010 UTC (14 years, 1 month ago) by persson
File size: 55000 byte(s)
* sfz engine: added support for random, seq_position, seq_length and
  volume
* sfz parser: added v1 LFO opcodes (no support in engine yet)

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC