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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2106 by persson, Sun Jul 4 12:50:51 2010 UTC revision 2218 by iliev, Thu Jul 28 08:05:57 2011 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2008 Anders Dahnielson <anders@dahnielson.com>          *   *   Copyright (C) 2008 Anders Dahnielson <anders@dahnielson.com>          *
6   *   Copyright (C) 2009 - 2010 Anders Dahnielson and Grigor Iliev          *   *   Copyright (C) 2009 - 2011 Anders Dahnielson and Grigor Iliev          *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
# Line 36  Line 36 
36    
37  namespace sfz  namespace sfz
38  {  {
39        template <typename T> T check(std::string name, T min, T max, T val) {
40            if (val < min) {
41                std::cerr << "sfz: The value of opcode '" << name;
42                std::cerr << "' is below the minimum allowed value (min=" << min << "): " << val << std::endl;
43                val = min;
44            }
45            if (val > max) {
46                std::cerr << "sfz: The value of opcode '" << name;
47                std::cerr << "' is above the maximum allowed value (max=" << max << "): " << val << std::endl;
48                val = max;
49            }
50            
51            return val;
52        }
53    
54      Sample* SampleManager::FindSample(std::string samplePath) {      Sample* SampleManager::FindSample(std::string samplePath, int offset) {
55          std::map<Sample*, std::set<Region*> >::iterator it = sampleMap.begin();          std::map<Sample*, std::set<Region*> >::iterator it = sampleMap.begin();
56          for (; it != sampleMap.end(); it++) {          for (; it != sampleMap.end(); it++) {
57              if (it->first->GetFile() == samplePath) return it->first;              if (it->first->GetFile() == samplePath) {
58                    /* Because the start of the sample is cached in RAM we treat
59                     * same sample with different offset as different samples
60                     * // TODO: Ignore offset when the whole sample is cached in RAM?
61                     */
62                    int maxOffset = it->first->MaxOffset;
63                    if(it->first->Offset == offset || (it->first->Offset < maxOffset && offset < maxOffset)) {
64                        return it->first;
65                    }
66                }
67          }          }
68    
69          return NULL;          return NULL;
# Line 90  namespace sfz Line 113  namespace sfz
113      Sample* Region::GetSample(bool create)      Sample* Region::GetSample(bool create)
114      {      {
115          if(pSample == NULL && create) {          if(pSample == NULL && create) {
116              Sample* sf = GetInstrument()->GetSampleManager()->FindSample(sample);              int i = offset ? *offset : 0;
117                Sample* sf = GetInstrument()->GetSampleManager()->FindSample(sample, i);
118              if (sf != NULL) pSample = sf; // Reuse already created sample              if (sf != NULL) pSample = sf; // Reuse already created sample
119              else pSample = new Sample(sample);              else pSample = new Sample(sample, false, i);
120              GetInstrument()->GetSampleManager()->AddSampleConsumer(pSample, this);              GetInstrument()->GetSampleManager()->AddSampleConsumer(pSample, this);
121          }          }
122          return pSample;          return pSample;
# Line 142  namespace sfz Line 166  namespace sfz
166          return is_triggered;          return is_triggered;
167      }      }
168    
     bool  
     Region::OnControl(uint8_t chan, uint8_t cont, uint8_t val,  
         int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft,  
         uint8_t prog, float rand, trigger_t trig, uint8_t* cc,  
         float timer, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key)  
     {  
         // chan      (MIDI channel)  
         // cont      (MIDI controller)  
         // val       (MIDI controller value)  
   
         // bend      (MIDI pitch bend)  
         // bpm       (host BPM)  
         // chanaft   (MIDI channel pressure)  
         // polyaft   (MIDI polyphonic aftertouch)  
         // prog      (MIDI program change)  
         // rand      (generated random number)  
         // trigger   (how it was triggered)  
         // cc        (all CC values)  
   
         // timer       (time since previous region in the group was triggered)  
         // sw          (the state of region key switches, 128 possible values)  
         // last_sw_key (the last key pressed in the key switch range)  
         // prev_sw_key (the previous note value)  
   
         bool is_triggered = (  
             chan    >= lochan           &&  chan    <= hichan             &&  
             ((val   >= on_locc[cont]    &&  val     <= on_hicc[cont])     ||  
              (val   >= start_locc[cont] &&  val     <= start_hicc[cont])) &&  
             bend    >= lobend           &&  bend    <= hibend             &&  
             bpm     >= lobpm            &&  bpm     <  hibpm              &&  
             chanaft >= lochanaft        &&  chanaft <= hichanaft          &&  
             polyaft >= lopolyaft        &&  polyaft <= hipolyaft          &&  
             prog    >= loprog           &&  prog    <= hiprog             &&  
             rand    >= lorand           &&  rand    <  hirand             &&  
             timer   >= lotimer          &&  timer   <= hitimer            &&  
   
             ( sw_last == -1 ||  
               ((sw_last >= sw_lokey && sw_last <= sw_hikey) ? (last_sw_key == sw_last) : false) ) &&  
   
             ( sw_down == -1 ||  
               ((sw_down >= sw_lokey && (sw_hikey == -1 || sw_down <= sw_hikey)) ? (sw[sw_down]) : false) )  &&  
   
             ( sw_up   == -1 ||  
               ((sw_up   >= sw_lokey && (sw_hikey == -1 || sw_up   <= sw_hikey)) ? (!sw[sw_up]) : true) )  &&  
   
             ( sw_previous == -1 ||  
               prev_sw_key == sw_previous )  &&  
   
             ((trigger & trig) != 0)  
         );  
   
         if (!is_triggered)  
             return false;  
   
         for (int i = 0; i < 128; ++i)  
         {  
             if (locc[i] != -1 && hicc[i] != -1 && !(cc[i] >= locc[i] && cc[i] <= hicc[i]))  
                 return false;  
         }  
   
         return true;  
     }  
   
169      Articulation*      Articulation*
170      Region::GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc)      Region::GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc)
171      {      {
# Line 212  namespace sfz Line 173  namespace sfz
173      }      }
174    
175      bool Region::HasLoop() {      bool Region::HasLoop() {
176          bool b = loop_mode == ::sfz::LOOP_CONTINUOUS || loop_mode == ::sfz::LOOP_SUSTAIN; // TODO: ONE_SHOT mode          bool b = loop_mode == ::sfz::LOOP_UNSET ? pSample->GetLoops() :
177                (loop_mode == ::sfz::LOOP_CONTINUOUS || loop_mode == ::sfz::LOOP_SUSTAIN);
178          return b && GetLoopStart() && GetLoopEnd() && GetLoopEnd() > GetLoopStart();          return b && GetLoopStart() && GetLoopEnd() && GetLoopEnd() > GetLoopStart();
179      }      }
180    
181      uint Region::GetLoopStart() {      uint Region::GetLoopStart() {
182          return (!loop_start) ? 0 : *loop_start; // TODO: use sample loop when loop_start not defined          return (!loop_start) ? pSample->GetLoopStart() : *loop_start;
183      }      }
184    
185      uint Region::GetLoopEnd() {      uint Region::GetLoopEnd() {
186          return (!loop_end) ? 0 : *loop_end; // TODO: use sample loop when loop_end not defined          return (!loop_end) ? pSample->GetLoopEnd() : *loop_end;
187      }      }
188    
189      uint Region::GetLoopCount() {      uint Region::GetLoopCount() {
# Line 244  namespace sfz Line 206  namespace sfz
206              delete regions[i];              delete regions[i];
207          }          }
208          delete pLookupTable;          delete pLookupTable;
209            for (int i = 0 ; i < 128 ; i++) {
210                delete pLookupTableCC[i];
211            }
212      }      }
213    
214      void Query::search(const Instrument* pInstrument) {      void Query::search(const Instrument* pInstrument) {
# Line 251  namespace sfz Line 216  namespace sfz
216          regionIndex = 0;          regionIndex = 0;
217      }      }
218    
219        void Query::search(const Instrument* pInstrument, int triggercc) {
220            pRegionList = &pInstrument->pLookupTableCC[triggercc]->query(*this);
221            regionIndex = 0;
222        }
223    
224      Region* Query::next() {      Region* Query::next() {
225          for ( ; regionIndex < pRegionList->size() ; regionIndex++) {          for ( ; regionIndex < pRegionList->size() ; regionIndex++) {
226              if ((*pRegionList)[regionIndex]->OnKey(*this)) {              if ((*pRegionList)[regionIndex]->OnKey(*this)) {
# Line 328  namespace sfz Line 298  namespace sfz
298          trigger = TRIGGER_ATTACK;          trigger = TRIGGER_ATTACK;
299    
300          group = 0;          group = 0;
301          off_by.unset();          off_by = 0;
302          off_mode = OFF_FAST;          off_mode = OFF_FAST;
303    
304          // sample player          // sample player
# Line 339  namespace sfz Line 309  namespace sfz
309          end.unset();          end.unset();
310          loop_crossfade.unset();          loop_crossfade.unset();
311          offset.unset(); offset_random.unset();          offset.unset(); offset_random.unset();
312          loop_mode = NO_LOOP;          loop_mode = LOOP_UNSET;
313          loop_start.unset(); loop_end.unset();          loop_start.unset(); loop_end.unset();
314          sync_beats.unset(); sync_offset.unset();          sync_beats.unset(); sync_offset.unset();
315    
# Line 466  namespace sfz Line 436  namespace sfz
436              eq2_gain_oncc.set(i, 0);              eq2_gain_oncc.set(i, 0);
437              eq3_gain_oncc.set(i, 0);              eq3_gain_oncc.set(i, 0);
438          }          }
439            cutoff_cc = 0;
440    
441          eg.clear();          eg.clear();
442    
# Line 475  namespace sfz Line 446  namespace sfz
446          ampeg_attack   = 0;          ampeg_attack   = 0;
447          ampeg_hold     = 0;          ampeg_hold     = 0;
448          ampeg_decay    = 0;          ampeg_decay    = 0;
449          ampeg_sustain  = 100; // in percentage          ampeg_sustain  = -1; // in percentage
450          ampeg_release  = 0;          ampeg_release  = 0;
451    
452            ampeg_vel2delay   = 0;
453            ampeg_vel2attack  = 0;
454            ampeg_vel2hold    = 0;
455            ampeg_vel2decay   = 0;
456            ampeg_vel2sustain = 0;
457            ampeg_vel2release = 0;
458    
459          fileg_delay    = 0;          fileg_delay    = 0;
460          fileg_start    = 0; //in percentage          fileg_start    = 0; //in percentage
461          fileg_attack   = 0;          fileg_attack   = 0;
# Line 628  namespace sfz Line 606  namespace sfz
606          region->fil_type = fil_type;          region->fil_type = fil_type;
607          region->cutoff = cutoff;          region->cutoff = cutoff;
608          region->cutoff_oncc = cutoff_oncc;          region->cutoff_oncc = cutoff_oncc;
609            region->cutoff_cc = cutoff_cc;
610          region->cutoff_smoothcc = cutoff_smoothcc;          region->cutoff_smoothcc = cutoff_smoothcc;
611          region->cutoff_stepcc = cutoff_stepcc;          region->cutoff_stepcc = cutoff_stepcc;
612          region->cutoff_curvecc = cutoff_curvecc;          region->cutoff_curvecc = cutoff_curvecc;
# Line 699  namespace sfz Line 678  namespace sfz
678          region->ampeg_sustain  = ampeg_sustain;          region->ampeg_sustain  = ampeg_sustain;
679          region->ampeg_release  = ampeg_release;          region->ampeg_release  = ampeg_release;
680    
681            region->ampeg_vel2delay   = ampeg_vel2delay;
682            region->ampeg_vel2attack  = ampeg_vel2attack;
683            region->ampeg_vel2hold    = ampeg_vel2hold;
684            region->ampeg_vel2decay   = ampeg_vel2decay;
685            region->ampeg_vel2sustain = ampeg_vel2sustain;
686            region->ampeg_vel2release = ampeg_vel2release;
687    
688          region->fileg_delay    = fileg_delay;          region->fileg_delay    = fileg_delay;
689          region->fileg_start    = fileg_start;          region->fileg_start    = fileg_start;
690          region->fileg_attack   = fileg_attack;          region->fileg_attack   = fileg_attack;
# Line 832  namespace sfz Line 818  namespace sfz
818              ::sfz::Region* pRegion = _instrument->regions[i];              ::sfz::Region* pRegion = _instrument->regions[i];
819              int low = pRegion->lokey;              int low = pRegion->lokey;
820              int high = pRegion->hikey;              int high = pRegion->hikey;
821              if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) {              if (low != -1) { // lokey -1 means region doesn't play on note-on
822                  std::cerr << "Invalid key range: " << low << " - " << high << std::endl;                  // hikey -1 is the same as no limit, except that it
823              } else {                  // also enables on_locc/on_hicc
824                  for (int j = low; j <= high; j++) _instrument->KeyBindings[j] = true;                  if (high == -1) high = 127;
825                    if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) {
826                        std::cerr << "Invalid key range: " << low << " - " << high << std::endl;
827                    } else {
828                        for (int j = low; j <= high; j++) _instrument->KeyBindings[j] = true;
829                    }
830              }              }
831    
832              // get keyswitches              // get keyswitches
# Line 890  namespace sfz Line 881  namespace sfz
881          }          }
882    
883          _instrument->pLookupTable = new LookupTable(_instrument);          _instrument->pLookupTable = new LookupTable(_instrument);
884    
885            // create separate lookup tables for controller triggered
886            // regions, one for each CC
887            for (int i = 0 ; i < 128 ; i++) {
888                _instrument->pLookupTableCC[i] = new LookupTable(_instrument, i);
889            }
890      }      }
891    
892      File::~File()      File::~File()
# Line 1037  namespace sfz Line 1034  namespace sfz
1034          else if ("off_by" == key || "offby" == key) pCurDef->off_by = ToInt(value);          else if ("off_by" == key || "offby" == key) pCurDef->off_by = ToInt(value);
1035          else if ("off_mode" == key || "offmode" == key)          else if ("off_mode" == key || "offmode" == key)
1036          {          {
1037              if (value == "fast")  _current_group->off_mode = OFF_FAST;              if (value == "fast")  pCurDef->off_mode = OFF_FAST;
1038              else if (value == "normal") _current_group->off_mode = OFF_NORMAL;              else if (value == "normal") pCurDef->off_mode = OFF_NORMAL;
1039          }          }
1040    
1041          // sample player          // sample player
# Line 1062  namespace sfz Line 1059  namespace sfz
1059          else if ("loopstart" == key) pCurDef->loop_start = ToInt(value); // nonstandard          else if ("loopstart" == key) pCurDef->loop_start = ToInt(value); // nonstandard
1060          else if ("loop_end" == key) pCurDef->loop_end = ToInt(value);          else if ("loop_end" == key) pCurDef->loop_end = ToInt(value);
1061          else if ("loopend" == key) pCurDef->loop_end = ToInt(value); // nonstandard          else if ("loopend" == key) pCurDef->loop_end = ToInt(value); // nonstandard
1062            else if ("offset" == key) pCurDef->offset = ToInt(value);
1063          else if ("sync_beats" == key) pCurDef->sync_beats = ToInt(value);          else if ("sync_beats" == key) pCurDef->sync_beats = ToInt(value);
1064          else if ("sync_offset" == key) pCurDef->sync_offset = ToInt(value);          else if ("sync_offset" == key) pCurDef->sync_offset = ToInt(value);
1065    
# Line 1213  namespace sfz Line 1211  namespace sfz
1211              else if ("xfout_hi" == key_cc) pCurDef->xfout_hicc.set(num_cc, ToInt(value));              else if ("xfout_hi" == key_cc) pCurDef->xfout_hicc.set(num_cc, ToInt(value));
1212    
1213              // filter              // filter
1214              else if ("cutoff_on"  == key_cc || "cutoff_" == key_cc) pCurDef->cutoff_oncc.set(num_cc, ToInt(value));              else if ("cutoff_on"  == key_cc || "cutoff_" == key_cc) {
1215              else if ("cutoff2_on" == key_cc) pCurDef->cutoff2_oncc.set(num_cc, ToInt(value));                  pCurDef->cutoff_oncc.set(num_cc, ToInt(value));
1216                    pCurDef->cutoff_cc = num_cc;
1217                } else if ("cutoff2_on" == key_cc) pCurDef->cutoff2_oncc.set(num_cc, ToInt(value));
1218              else if ("cutoff_smooth"  == key_cc) pCurDef->cutoff_smoothcc.set(num_cc, ToInt(value));              else if ("cutoff_smooth"  == key_cc) pCurDef->cutoff_smoothcc.set(num_cc, ToInt(value));
1219              else if ("cutoff2_smooth" == key_cc) pCurDef->cutoff2_smoothcc.set(num_cc, ToInt(value));              else if ("cutoff2_smooth" == key_cc) pCurDef->cutoff2_smoothcc.set(num_cc, ToInt(value));
1220              else if ("cutoff_step"  == key_cc) pCurDef->cutoff_stepcc.set(num_cc, ToInt(value));              else if ("cutoff_step"  == key_cc) pCurDef->cutoff_stepcc.set(num_cc, ToInt(value));
# Line 1264  namespace sfz Line 1264  namespace sfz
1264          else if ("ampeg_hold"   == key) pCurDef->ampeg_hold = ToFloat(value);          else if ("ampeg_hold"   == key) pCurDef->ampeg_hold = ToFloat(value);
1265          else if ("ampeg_decay"   == key) pCurDef->ampeg_decay = ToFloat(value);          else if ("ampeg_decay"   == key) pCurDef->ampeg_decay = ToFloat(value);
1266          else if ("ampeg_sustain"   == key) pCurDef->ampeg_sustain = ToFloat(value);          else if ("ampeg_sustain"   == key) pCurDef->ampeg_sustain = ToFloat(value);
1267          else if ("ampeg_release"   == key) pCurDef->ampeg_release = ToFloat(value);          else if ("ampeg_release" == key) pCurDef->ampeg_release = ToFloat(value);
1268            else if ("ampeg_vel2delay" == key) pCurDef->ampeg_vel2delay = ToFloat(value);
1269            else if ("ampeg_vel2attack" == key) pCurDef->ampeg_vel2attack = ToFloat(value);
1270            else if ("ampeg_vel2hold" == key) pCurDef->ampeg_vel2hold = ToFloat(value);
1271            else if ("ampeg_vel2decay" == key) pCurDef->ampeg_vel2decay = ToFloat(value);
1272            else if ("ampeg_vel2sustain" == key) pCurDef->ampeg_vel2sustain = ToFloat(value);
1273            else if ("ampeg_vel2release" == key) pCurDef->ampeg_vel2release = ToFloat(value);
1274          else if ("fileg_delay"   == key) pCurDef->fileg_delay = ToFloat(value);          else if ("fileg_delay"   == key) pCurDef->fileg_delay = ToFloat(value);
1275          else if ("fileg_start"   == key) pCurDef->fileg_start = ToFloat(value);          else if ("fileg_start"   == key) pCurDef->fileg_start = ToFloat(value);
1276          else if ("fileg_attack"   == key) pCurDef->fileg_attack = ToFloat(value);          else if ("fileg_attack"   == key) pCurDef->fileg_attack = ToFloat(value);
# Line 1293  namespace sfz Line 1299  namespace sfz
1299          else if ("pitchlfo_fade" == key) pCurDef->pitchlfo_fade = ToFloat(value);          else if ("pitchlfo_fade" == key) pCurDef->pitchlfo_fade = ToFloat(value);
1300          else if ("pitchlfo_freq" == key) pCurDef->pitchlfo_freq = ToFloat(value);          else if ("pitchlfo_freq" == key) pCurDef->pitchlfo_freq = ToFloat(value);
1301          else if ("pitchlfo_depth" == key) pCurDef->pitchlfo_depth = ToInt(value);          else if ("pitchlfo_depth" == key) pCurDef->pitchlfo_depth = ToInt(value);
1302            
1303            // v2 LFO
1304            else if (sscanf(key.c_str(), "lfo%d%n", &x, &y)) {
1305                const char* s = key.c_str() + y;
1306                if (strcmp(s, "_freq") == 0) lfo(x).freq = check(key, 0.0f, 20.0f, ToFloat(value));
1307                else if (strcmp(s, "_wave") == 0) lfo(x).wave = ToInt(value);
1308                else if (strcmp(s, "_delay") == 0) lfo(x).delay = check(key, 0.0f, 100.0f, ToFloat(value));
1309                else if (strcmp(s, "_pitch") == 0) lfo(x).pitch = check(key, -9600, 9600, ToInt(value));
1310                else if (strcmp(s, "_cutoff") == 0) lfo(x).cutoff = check(key, -9600, 9600, ToInt(value));
1311                else if (strcmp(s, "_resonance") == 0) lfo(x).resonance = check(key, 0.0f, 40.0f, ToFloat(value));
1312                else if (strcmp(s, "_pan") == 0) lfo(x).pan = check(key, -100.0f, 100.0f, ToFloat(value));
1313                else std::cerr << "The opcode '" << key << "' is unsupported by libsfz!" << std::endl;
1314            }
1315    
1316          else {          else {
1317              std::cerr << "The opcode '" << key << "' is unsupported by libsfz!" << std::endl;              std::cerr << "The opcode '" << key << "' is unsupported by libsfz!" << std::endl;
# Line 1313  namespace sfz Line 1332  namespace sfz
1332              case 'g': i = 7; break;              case 'g': i = 7; break;
1333              case 'a': i = 9; break;              case 'a': i = 9; break;
1334              case 'b': i = 11; break;              case 'b': i = 11; break;
1335                case '-': if (s == "-1") return -1;
1336              default:              default:
1337                  std::cerr << "Not a note: " << s << std::endl;                  std::cerr << "Not a note: " << s << std::endl;
1338                  return 0;                  return 0;
# Line 1356  namespace sfz Line 1376  namespace sfz
1376          }          }
1377          return e.node[y];          return e.node[y];
1378      }      }
1379        
1380        LFO::LFO(): freq (-1),/* -1 is used to determine whether the LFO was initialized */
1381                    wave(0), delay(0), pitch(0), cutoff(0), resonance(0), pan(0) {
1382            
1383        }
1384    
1385        LFO& File::lfo(int x) {
1386            while (pCurDef->lfos.size() <= x) {
1387                pCurDef->lfos.add(LFO());
1388            }
1389            return pCurDef->lfos[x];
1390        }
1391    
1392  } // !namespace sfz  } // !namespace sfz

Legend:
Removed from v.2106  
changed lines
  Added in v.2218

  ViewVC Help
Powered by ViewVC