/[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 2018 by iliev, Tue Oct 27 19:04:57 2009 UTC revision 2055 by persson, Sat Jan 30 10:30:02 2010 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2008-2009 Anders Dahnielson <anders@dahnielson.com>     *   *   Copyright (C) 2008 Anders Dahnielson <anders@dahnielson.com>          *
6   *   Copyright (C) 2009 Grigor Iliev                                       *   *   Copyright (C) 2009 - 2010 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 25  Line 25 
25    
26  #include <iostream>  #include <iostream>
27  #include <sstream>  #include <sstream>
28    #include <cstdio>
29    #include <cstring>
30    
31  #include "../../common/File.h"  #include "../../common/File.h"
32  #include "../../common/Path.h"  #include "../../common/Path.h"
# Line 33  Line 35 
35  namespace sfz  namespace sfz
36  {  {
37    
38      LinuxSampler::SampleFile* SampleManager::FindSample(std::string samplePath) {      Sample* SampleManager::FindSample(std::string samplePath) {
39          std::map<LinuxSampler::SampleFile*, std::set<Region*> >::iterator it = sampleMap.begin();          std::map<Sample*, std::set<Region*> >::iterator it = sampleMap.begin();
40          for (; it != sampleMap.end(); it++) {          for (; it != sampleMap.end(); it++) {
41              if (it->first->GetFile() == samplePath) return it->first;              if (it->first->GetFile() == samplePath) return it->first;
42          }          }
# Line 93  namespace sfz Line 95  namespace sfz
95          DestroySampleIfNotUsed();          DestroySampleIfNotUsed();
96      }      }
97    
98      LinuxSampler::SampleFile* Region::GetSample(bool create)      Sample* Region::GetSample(bool create)
99      {      {
100          if(pSample == NULL && create) {          if(pSample == NULL && create) {
101              LinuxSampler::SampleFile* sf = GetInstrument()->GetSampleManager()->FindSample(sample);              Sample* sf = GetInstrument()->GetSampleManager()->FindSample(sample);
102              if (sf != NULL) pSample = sf; // Reuse already created sample              if (sf != NULL) pSample = sf; // Reuse already created sample
103              else pSample = new LinuxSampler::SampleFile(sample);              else pSample = new Sample(sample);
104              GetInstrument()->GetSampleManager()->AddSampleConsumer(pSample, this);              GetInstrument()->GetSampleManager()->AddSampleConsumer(pSample, this);
105          }          }
106          return pSample;          return pSample;
# Line 246  namespace sfz Line 248  namespace sfz
248          return new Articulation(); //todo: implement GetArticulation()          return new Articulation(); //todo: implement GetArticulation()
249      }      }
250    
251        bool Region::HasLoop() {
252            bool b = loop_mode == ::sfz::LOOP_CONTINUOUS || loop_mode == ::sfz::LOOP_SUSTAIN; // TODO: ONE_SHOT mode
253            return b && GetLoopStart() && GetLoopEnd() && GetLoopEnd() > GetLoopStart();
254        }
255    
256        uint Region::GetLoopStart() {
257            return (!loop_start) ? 0 : *loop_start; // TODO: use sample loop when loop_start not defined
258        }
259    
260        uint Region::GetLoopEnd() {
261            return (!loop_end) ? 0 : *loop_end; // TODO: use sample loop when loop_end not defined
262        }
263    
264        uint Region::GetLoopCount() {
265            return (!count) ? 0 : *count;
266        }
267    
268      /////////////////////////////////////////////////////////////      /////////////////////////////////////////////////////////////
269      // class Instrument      // class Instrument
270    
# Line 347  namespace sfz Line 366  namespace sfz
366    
367          trigger = TRIGGER_ATTACK;          trigger = TRIGGER_ATTACK;
368    
369          group.unset();          group = 0;
370          off_by.unset();          off_by.unset();
371          off_mode = OFF_FAST;          off_mode = OFF_FAST;
372    
# Line 487  namespace sfz Line 506  namespace sfz
506              eq3_gain_oncc[i] = 0;              eq3_gain_oncc[i] = 0;
507          }          }
508    
509            eg.clear();
510    
511          // deprecated          // deprecated
512          ampeg_delay    = 0;          ampeg_delay    = 0;
513          ampeg_start    = 0; //in percentage          ampeg_start    = 0; //in percentage
# Line 690  namespace sfz Line 711  namespace sfz
711          region->eq2_vel2gain = eq2_vel2gain;          region->eq2_vel2gain = eq2_vel2gain;
712          region->eq3_vel2gain = eq3_vel2gain;          region->eq3_vel2gain = eq3_vel2gain;
713    
714            // envelope generator
715            region->eg = eg;
716    
717          // deprecated          // deprecated
718          region->ampeg_delay    = ampeg_delay;          region->ampeg_delay    = ampeg_delay;
719          region->ampeg_start    = ampeg_start;          region->ampeg_start    = ampeg_start;
# Line 729  namespace sfz Line 753  namespace sfz
753      {      {
754          _instrument = new Instrument(LinuxSampler::Path::getBaseName(file), pSampleManager);          _instrument = new Instrument(LinuxSampler::Path::getBaseName(file), pSampleManager);
755          _current_group = new Group();          _current_group = new Group();
756                  pCurDef = _current_group;          pCurDef = _current_group;
757          enum token_type_t { HEADER, OPCODE };          enum token_type_t { HEADER, OPCODE };
758          token_type_t token_type;          token_type_t token_type;
759          std::string token_string;          std::string token_string;
# Line 854  namespace sfz Line 878  namespace sfz
878          {          {
879              _current_section = GROUP;              _current_section = GROUP;
880              _current_group->Reset();              _current_group->Reset();
881                          pCurDef = _current_group;              pCurDef = _current_group;
882          }          }
883          else if (token == "<region>")          else if (token == "<region>")
884          {          {
885              _current_section = REGION;              _current_section = REGION;
886              _current_region = _current_group->RegionFactory();              _current_region = _current_group->RegionFactory();
887                          pCurDef = _current_region;              pCurDef = _current_region;
888              _instrument->regions.push_back(_current_region);              _instrument->regions.push_back(_current_region);
889                          _current_region->SetInstrument(_instrument);              _current_region->SetInstrument(_instrument);
890          }          }
891          else if (token == "<control>")          else if (token == "<control>")
892          {          {
# Line 887  namespace sfz Line 911  namespace sfz
911          std::string::size_type delimiter_index = token.find('=');          std::string::size_type delimiter_index = token.find('=');
912          std::string key = token.substr(0, delimiter_index);          std::string key = token.substr(0, delimiter_index);
913          std::string value = token.substr(delimiter_index + 1);          std::string value = token.substr(delimiter_index + 1);
914            int x, y;
915    
916          // sample definition          // sample definition
917          if ("sample" == key)          if ("sample" == key)
# Line 985  namespace sfz Line 1010  namespace sfz
1010          }          }
1011    
1012          // sample player          // sample player
1013          else if ("count" == key) pCurDef->count = ToInt(value);          else if ("count" == key) { pCurDef->count = ToInt(value); pCurDef->loop_mode = ONE_SHOT; }
1014          else if ("delay" == key) pCurDef->delay = ToFloat(value);          else if ("delay" == key) pCurDef->delay = ToFloat(value);
1015          else if ("delay_random"   == key) pCurDef->delay_random = ToFloat(value);          else if ("delay_random"   == key) pCurDef->delay_random = ToFloat(value);
1016          else if ("delay_beats"    == key) pCurDef->delay_beats = ToInt(value);          else if ("delay_beats"    == key) pCurDef->delay_beats = ToInt(value);
# Line 998  namespace sfz Line 1023  namespace sfz
1023          {          {
1024              if (value == "no_loop") pCurDef->loop_mode = NO_LOOP;              if (value == "no_loop") pCurDef->loop_mode = NO_LOOP;
1025              else if (value == "one_shot") pCurDef->loop_mode = ONE_SHOT;              else if (value == "one_shot") pCurDef->loop_mode = ONE_SHOT;
1026              else if (value == "loop_continous") pCurDef->loop_mode = LOOP_CONTINOUS;              else if (value == "loop_continuous") pCurDef->loop_mode = LOOP_CONTINUOUS;
1027              else if (value == "loop_sustain") pCurDef->loop_mode = LOOP_SUSTAIN;              else if (value == "loop_sustain") pCurDef->loop_mode = LOOP_SUSTAIN;
1028          }          }
1029          else if ("loop_start" == key) pCurDef->loop_start = ToInt(value);          else if ("loop_start" == key) pCurDef->loop_start = ToInt(value);
1030            else if ("loopstart" == key) pCurDef->loop_start = ToInt(value); // nonstandard
1031          else if ("loop_end" == key) pCurDef->loop_end = ToInt(value);          else if ("loop_end" == key) pCurDef->loop_end = ToInt(value);
1032            else if ("loopend" == key) pCurDef->loop_end = ToInt(value); // nonstandard
1033          else if ("sync_beats" == key) pCurDef->sync_beats = ToInt(value);          else if ("sync_beats" == key) pCurDef->sync_beats = ToInt(value);
1034          else if ("sync_offset" == key) pCurDef->sync_offset = ToInt(value);          else if ("sync_offset" == key) pCurDef->sync_offset = ToInt(value);
1035    
# Line 1181  namespace sfz Line 1208  namespace sfz
1208              else if ("eq3_gain_on" == key_cc) pCurDef->eq3_gain_oncc[num_cc] = ToInt(value);              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;              else std::cerr << "The opcode '" << key << "' is unsupported by libsfz!" << std::endl;
1210          }          }
1211          // Deprecated opcodes          // 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          else if ("ampeg_delay"   == key) pCurDef->ampeg_delay = ToFloat(value);          else if ("ampeg_delay"   == key) pCurDef->ampeg_delay = ToFloat(value);
1228          else if ("ampeg_start"   == key) pCurDef->ampeg_start = ToFloat(value);          else if ("ampeg_start"   == key) pCurDef->ampeg_start = ToFloat(value);
1229          else if ("ampeg_attack"   == key) pCurDef->ampeg_attack = ToFloat(value);          else if ("ampeg_attack"   == key) pCurDef->ampeg_attack = ToFloat(value);
# Line 1208  namespace sfz Line 1250  namespace sfz
1250          }          }
1251      }      }
1252    
1253        EGNode::EGNode() : time(0), level(0), shape(0), curve(0) {
1254        }
1255    
1256        EG::EG() :
1257            sustain(0), loop(0), loop_count(0),
1258            amplitude(0), cutoff(0) {
1259        }
1260    
1261        EG& File::eg(int x) {
1262            while (pCurDef->eg.size() <= x) {
1263                pCurDef->eg.add(EG());
1264            }
1265            return pCurDef->eg[x];
1266        }
1267    
1268        EGNode& File::egnode(int x, int y) {
1269            EG& e = eg(x);
1270            while (e.node.size() <= y) {
1271                e.node.add(EGNode());
1272            }
1273            return e.node[y];
1274        }
1275    
1276  } // !namespace sfz  } // !namespace sfz

Legend:
Removed from v.2018  
changed lines
  Added in v.2055

  ViewVC Help
Powered by ViewVC