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 * |
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" |
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 |
} |
} |
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; |
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 |
|
|
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 |
|
|
505 |
eq2_gain_oncc[i] = 0; |
eq2_gain_oncc[i] = 0; |
506 |
eq3_gain_oncc[i] = 0; |
eq3_gain_oncc[i] = 0; |
507 |
} |
} |
508 |
|
|
509 |
|
eg.clear(); |
510 |
|
|
511 |
|
// deprecated |
512 |
|
ampeg_delay = 0; |
513 |
|
ampeg_start = 0; //in percentage |
514 |
|
ampeg_attack = 0; |
515 |
|
ampeg_hold = 0; |
516 |
|
ampeg_decay = 0; |
517 |
|
ampeg_sustain = 100; // in percentage |
518 |
|
ampeg_release = 0; |
519 |
|
|
520 |
|
fileg_delay = 0; |
521 |
|
fileg_start = 0; //in percentage |
522 |
|
fileg_attack = 0; |
523 |
|
fileg_hold = 0; |
524 |
|
fileg_decay = 0; |
525 |
|
fileg_sustain = 100; // in percentage |
526 |
|
fileg_release = 0; |
527 |
|
|
528 |
|
pitcheg_delay = 0; |
529 |
|
pitcheg_start = 0; //in percentage |
530 |
|
pitcheg_attack = 0; |
531 |
|
pitcheg_hold = 0; |
532 |
|
pitcheg_decay = 0; |
533 |
|
pitcheg_sustain = 100; // in percentage |
534 |
|
pitcheg_release = 0; |
535 |
} |
} |
536 |
|
|
537 |
Region* |
Region* |
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 |
718 |
|
region->ampeg_delay = ampeg_delay; |
719 |
|
region->ampeg_start = ampeg_start; |
720 |
|
region->ampeg_attack = ampeg_attack; |
721 |
|
region->ampeg_hold = ampeg_hold; |
722 |
|
region->ampeg_decay = ampeg_decay; |
723 |
|
region->ampeg_sustain = ampeg_sustain; |
724 |
|
region->ampeg_release = ampeg_release; |
725 |
|
|
726 |
|
region->fileg_delay = fileg_delay; |
727 |
|
region->fileg_start = fileg_start; |
728 |
|
region->fileg_attack = fileg_attack; |
729 |
|
region->fileg_hold = fileg_hold; |
730 |
|
region->fileg_decay = fileg_decay; |
731 |
|
region->fileg_sustain = fileg_sustain; |
732 |
|
region->fileg_release = fileg_release; |
733 |
|
|
734 |
|
region->pitcheg_delay = pitcheg_delay; |
735 |
|
region->pitcheg_start = pitcheg_start; |
736 |
|
region->pitcheg_attack = pitcheg_attack; |
737 |
|
region->pitcheg_hold = pitcheg_hold; |
738 |
|
region->pitcheg_decay = pitcheg_decay; |
739 |
|
region->pitcheg_sustain = pitcheg_sustain; |
740 |
|
region->pitcheg_release = pitcheg_release; |
741 |
|
|
742 |
return region; |
return region; |
743 |
} |
} |
744 |
|
|
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; |
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 |
{ |
{ |
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) |
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); |
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 |
|
|
1207 |
else if ("eq2_gain_on" == key_cc) pCurDef->eq2_gain_oncc[num_cc] = ToInt(value); |
else if ("eq2_gain_on" == key_cc) pCurDef->eq2_gain_oncc[num_cc] = ToInt(value); |
1208 |
else if ("eq3_gain_on" == key_cc) pCurDef->eq3_gain_oncc[num_cc] = ToInt(value); |
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 |
} else { |
} |
1211 |
|
// 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); |
1228 |
|
else if ("ampeg_start" == key) pCurDef->ampeg_start = ToFloat(value); |
1229 |
|
else if ("ampeg_attack" == key) pCurDef->ampeg_attack = ToFloat(value); |
1230 |
|
else if ("ampeg_hold" == key) pCurDef->ampeg_hold = ToFloat(value); |
1231 |
|
else if ("ampeg_decay" == key) pCurDef->ampeg_decay = ToFloat(value); |
1232 |
|
else if ("ampeg_sustain" == key) pCurDef->ampeg_sustain = ToFloat(value); |
1233 |
|
else if ("ampeg_release" == key) pCurDef->ampeg_release = ToFloat(value); |
1234 |
|
else if ("fileg_delay" == key) pCurDef->fileg_delay = ToFloat(value); |
1235 |
|
else if ("fileg_start" == key) pCurDef->fileg_start = ToFloat(value); |
1236 |
|
else if ("fileg_attack" == key) pCurDef->fileg_attack = ToFloat(value); |
1237 |
|
else if ("fileg_hold" == key) pCurDef->fileg_hold = ToFloat(value); |
1238 |
|
else if ("fileg_decay" == key) pCurDef->fileg_decay = ToFloat(value); |
1239 |
|
else if ("fileg_sustain" == key) pCurDef->fileg_sustain = ToFloat(value); |
1240 |
|
else if ("fileg_release" == key) pCurDef->fileg_release = ToFloat(value); |
1241 |
|
else if ("pitcheg_delay" == key) pCurDef->pitcheg_delay = ToFloat(value); |
1242 |
|
else if ("pitcheg_start" == key) pCurDef->pitcheg_start = ToFloat(value); |
1243 |
|
else if ("pitcheg_attack" == key) pCurDef->pitcheg_attack = ToFloat(value); |
1244 |
|
else if ("pitcheg_hold" == key) pCurDef->pitcheg_hold = ToFloat(value); |
1245 |
|
else if ("pitcheg_decay" == key) pCurDef->pitcheg_decay = ToFloat(value); |
1246 |
|
else if ("pitcheg_sustain" == key) pCurDef->pitcheg_sustain = ToFloat(value); |
1247 |
|
else if ("pitcheg_release" == key) pCurDef->pitcheg_release = ToFloat(value); |
1248 |
|
else { |
1249 |
std::cerr << "The opcode '" << key << "' is unsupported by libsfz!" << std::endl; |
std::cerr << "The opcode '" << key << "' is unsupported by libsfz!" << std::endl; |
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 |