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 * |
37 |
namespace sfz |
namespace sfz |
38 |
{ |
{ |
39 |
|
|
40 |
Sample* SampleManager::FindSample(std::string samplePath) { |
Sample* SampleManager::FindSample(std::string samplePath, int offset) { |
41 |
std::map<Sample*, std::set<Region*> >::iterator it = sampleMap.begin(); |
std::map<Sample*, std::set<Region*> >::iterator it = sampleMap.begin(); |
42 |
for (; it != sampleMap.end(); it++) { |
for (; it != sampleMap.end(); it++) { |
43 |
if (it->first->GetFile() == samplePath) return it->first; |
if (it->first->GetFile() == samplePath) { |
44 |
|
/* Because the start of the sample is cached in RAM we treat |
45 |
|
* same sample with different offset as different samples |
46 |
|
* // TODO: Ignore offset when the whole sample is cached in RAM? |
47 |
|
*/ |
48 |
|
int maxOffset = it->first->MaxOffset; |
49 |
|
if(it->first->Offset == offset || (it->first->Offset < maxOffset && offset < maxOffset)) { |
50 |
|
return it->first; |
51 |
|
} |
52 |
|
} |
53 |
} |
} |
54 |
|
|
55 |
return NULL; |
return NULL; |
99 |
Sample* Region::GetSample(bool create) |
Sample* Region::GetSample(bool create) |
100 |
{ |
{ |
101 |
if(pSample == NULL && create) { |
if(pSample == NULL && create) { |
102 |
Sample* sf = GetInstrument()->GetSampleManager()->FindSample(sample); |
int i = offset ? *offset : 0; |
103 |
|
Sample* sf = GetInstrument()->GetSampleManager()->FindSample(sample, i); |
104 |
if (sf != NULL) pSample = sf; // Reuse already created sample |
if (sf != NULL) pSample = sf; // Reuse already created sample |
105 |
else pSample = new Sample(sample); |
else pSample = new Sample(sample, false, i); |
106 |
GetInstrument()->GetSampleManager()->AddSampleConsumer(pSample, this); |
GetInstrument()->GetSampleManager()->AddSampleConsumer(pSample, this); |
107 |
} |
} |
108 |
return pSample; |
return pSample; |
152 |
return is_triggered; |
return is_triggered; |
153 |
} |
} |
154 |
|
|
|
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; |
|
|
} |
|
|
|
|
155 |
Articulation* |
Articulation* |
156 |
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) |
157 |
{ |
{ |
159 |
} |
} |
160 |
|
|
161 |
bool Region::HasLoop() { |
bool Region::HasLoop() { |
162 |
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() : |
163 |
|
(loop_mode == ::sfz::LOOP_CONTINUOUS || loop_mode == ::sfz::LOOP_SUSTAIN); |
164 |
return b && GetLoopStart() && GetLoopEnd() && GetLoopEnd() > GetLoopStart(); |
return b && GetLoopStart() && GetLoopEnd() && GetLoopEnd() > GetLoopStart(); |
165 |
} |
} |
166 |
|
|
167 |
uint Region::GetLoopStart() { |
uint Region::GetLoopStart() { |
168 |
return (!loop_start) ? 0 : *loop_start; // TODO: use sample loop when loop_start not defined |
return (!loop_start) ? pSample->GetLoopStart() : *loop_start; |
169 |
} |
} |
170 |
|
|
171 |
uint Region::GetLoopEnd() { |
uint Region::GetLoopEnd() { |
172 |
return (!loop_end) ? 0 : *loop_end; // TODO: use sample loop when loop_end not defined |
return (!loop_end) ? pSample->GetLoopEnd() : *loop_end; |
173 |
} |
} |
174 |
|
|
175 |
uint Region::GetLoopCount() { |
uint Region::GetLoopCount() { |
192 |
delete regions[i]; |
delete regions[i]; |
193 |
} |
} |
194 |
delete pLookupTable; |
delete pLookupTable; |
195 |
|
for (int i = 0 ; i < 128 ; i++) { |
196 |
|
delete pLookupTableCC[i]; |
197 |
|
} |
198 |
} |
} |
199 |
|
|
200 |
void Query::search(const Instrument* pInstrument) { |
void Query::search(const Instrument* pInstrument) { |
202 |
regionIndex = 0; |
regionIndex = 0; |
203 |
} |
} |
204 |
|
|
205 |
|
void Query::search(const Instrument* pInstrument, int triggercc) { |
206 |
|
pRegionList = &pInstrument->pLookupTableCC[triggercc]->query(*this); |
207 |
|
regionIndex = 0; |
208 |
|
} |
209 |
|
|
210 |
Region* Query::next() { |
Region* Query::next() { |
211 |
for ( ; regionIndex < pRegionList->size() ; regionIndex++) { |
for ( ; regionIndex < pRegionList->size() ; regionIndex++) { |
212 |
if ((*pRegionList)[regionIndex]->OnKey(*this)) { |
if ((*pRegionList)[regionIndex]->OnKey(*this)) { |
295 |
end.unset(); |
end.unset(); |
296 |
loop_crossfade.unset(); |
loop_crossfade.unset(); |
297 |
offset.unset(); offset_random.unset(); |
offset.unset(); offset_random.unset(); |
298 |
loop_mode = NO_LOOP; |
loop_mode = LOOP_UNSET; |
299 |
loop_start.unset(); loop_end.unset(); |
loop_start.unset(); loop_end.unset(); |
300 |
sync_beats.unset(); sync_offset.unset(); |
sync_beats.unset(); sync_offset.unset(); |
301 |
|
|
422 |
eq2_gain_oncc.set(i, 0); |
eq2_gain_oncc.set(i, 0); |
423 |
eq3_gain_oncc.set(i, 0); |
eq3_gain_oncc.set(i, 0); |
424 |
} |
} |
425 |
|
cutoff_cc = 0; |
426 |
|
|
427 |
eg.clear(); |
eg.clear(); |
428 |
|
|
435 |
ampeg_sustain = 100; // in percentage |
ampeg_sustain = 100; // in percentage |
436 |
ampeg_release = 0; |
ampeg_release = 0; |
437 |
|
|
438 |
|
ampeg_vel2delay = 0; |
439 |
|
ampeg_vel2attack = 0; |
440 |
|
ampeg_vel2hold = 0; |
441 |
|
ampeg_vel2decay = 0; |
442 |
|
ampeg_vel2sustain = 0; |
443 |
|
ampeg_vel2release = 0; |
444 |
|
|
445 |
fileg_delay = 0; |
fileg_delay = 0; |
446 |
fileg_start = 0; //in percentage |
fileg_start = 0; //in percentage |
447 |
fileg_attack = 0; |
fileg_attack = 0; |
592 |
region->fil_type = fil_type; |
region->fil_type = fil_type; |
593 |
region->cutoff = cutoff; |
region->cutoff = cutoff; |
594 |
region->cutoff_oncc = cutoff_oncc; |
region->cutoff_oncc = cutoff_oncc; |
595 |
|
region->cutoff_cc = cutoff_cc; |
596 |
region->cutoff_smoothcc = cutoff_smoothcc; |
region->cutoff_smoothcc = cutoff_smoothcc; |
597 |
region->cutoff_stepcc = cutoff_stepcc; |
region->cutoff_stepcc = cutoff_stepcc; |
598 |
region->cutoff_curvecc = cutoff_curvecc; |
region->cutoff_curvecc = cutoff_curvecc; |
664 |
region->ampeg_sustain = ampeg_sustain; |
region->ampeg_sustain = ampeg_sustain; |
665 |
region->ampeg_release = ampeg_release; |
region->ampeg_release = ampeg_release; |
666 |
|
|
667 |
|
region->ampeg_vel2delay = ampeg_vel2delay; |
668 |
|
region->ampeg_vel2attack = ampeg_vel2attack; |
669 |
|
region->ampeg_vel2hold = ampeg_vel2hold; |
670 |
|
region->ampeg_vel2decay = ampeg_vel2decay; |
671 |
|
region->ampeg_vel2sustain = ampeg_vel2sustain; |
672 |
|
region->ampeg_vel2release = ampeg_vel2release; |
673 |
|
|
674 |
region->fileg_delay = fileg_delay; |
region->fileg_delay = fileg_delay; |
675 |
region->fileg_start = fileg_start; |
region->fileg_start = fileg_start; |
676 |
region->fileg_attack = fileg_attack; |
region->fileg_attack = fileg_attack; |
804 |
::sfz::Region* pRegion = _instrument->regions[i]; |
::sfz::Region* pRegion = _instrument->regions[i]; |
805 |
int low = pRegion->lokey; |
int low = pRegion->lokey; |
806 |
int high = pRegion->hikey; |
int high = pRegion->hikey; |
807 |
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
if (low != -1) { // lokey -1 means region doesn't play on note-on |
808 |
std::cerr << "Invalid key range: " << low << " - " << high << std::endl; |
// hikey -1 is the same as no limit, except that it |
809 |
} else { |
// also enables on_locc/on_hicc |
810 |
for (int j = low; j <= high; j++) _instrument->KeyBindings[j] = true; |
if (high == -1) high = 127; |
811 |
|
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
812 |
|
std::cerr << "Invalid key range: " << low << " - " << high << std::endl; |
813 |
|
} else { |
814 |
|
for (int j = low; j <= high; j++) _instrument->KeyBindings[j] = true; |
815 |
|
} |
816 |
} |
} |
817 |
|
|
818 |
// get keyswitches |
// get keyswitches |
867 |
} |
} |
868 |
|
|
869 |
_instrument->pLookupTable = new LookupTable(_instrument); |
_instrument->pLookupTable = new LookupTable(_instrument); |
870 |
|
|
871 |
|
// create separate lookup tables for controller triggered |
872 |
|
// regions, one for each CC |
873 |
|
for (int i = 0 ; i < 128 ; i++) { |
874 |
|
_instrument->pLookupTableCC[i] = new LookupTable(_instrument, i); |
875 |
|
} |
876 |
} |
} |
877 |
|
|
878 |
File::~File() |
File::~File() |
1045 |
else if ("loopstart" == key) pCurDef->loop_start = ToInt(value); // nonstandard |
else if ("loopstart" == key) pCurDef->loop_start = ToInt(value); // nonstandard |
1046 |
else if ("loop_end" == key) pCurDef->loop_end = ToInt(value); |
else if ("loop_end" == key) pCurDef->loop_end = ToInt(value); |
1047 |
else if ("loopend" == key) pCurDef->loop_end = ToInt(value); // nonstandard |
else if ("loopend" == key) pCurDef->loop_end = ToInt(value); // nonstandard |
1048 |
|
else if ("offset" == key) pCurDef->offset = ToInt(value); |
1049 |
else if ("sync_beats" == key) pCurDef->sync_beats = ToInt(value); |
else if ("sync_beats" == key) pCurDef->sync_beats = ToInt(value); |
1050 |
else if ("sync_offset" == key) pCurDef->sync_offset = ToInt(value); |
else if ("sync_offset" == key) pCurDef->sync_offset = ToInt(value); |
1051 |
|
|
1197 |
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)); |
1198 |
|
|
1199 |
// filter |
// filter |
1200 |
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) { |
1201 |
else if ("cutoff2_on" == key_cc) pCurDef->cutoff2_oncc.set(num_cc, ToInt(value)); |
pCurDef->cutoff_oncc.set(num_cc, ToInt(value)); |
1202 |
|
pCurDef->cutoff_cc = num_cc; |
1203 |
|
} else if ("cutoff2_on" == key_cc) pCurDef->cutoff2_oncc.set(num_cc, ToInt(value)); |
1204 |
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)); |
1205 |
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)); |
1206 |
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)); |
1250 |
else if ("ampeg_hold" == key) pCurDef->ampeg_hold = ToFloat(value); |
else if ("ampeg_hold" == key) pCurDef->ampeg_hold = ToFloat(value); |
1251 |
else if ("ampeg_decay" == key) pCurDef->ampeg_decay = ToFloat(value); |
else if ("ampeg_decay" == key) pCurDef->ampeg_decay = ToFloat(value); |
1252 |
else if ("ampeg_sustain" == key) pCurDef->ampeg_sustain = ToFloat(value); |
else if ("ampeg_sustain" == key) pCurDef->ampeg_sustain = ToFloat(value); |
1253 |
else if ("ampeg_release" == key) pCurDef->ampeg_release = ToFloat(value); |
else if ("ampeg_release" == key) pCurDef->ampeg_release = ToFloat(value); |
1254 |
|
else if ("ampeg_vel2delay" == key) pCurDef->ampeg_vel2delay = ToFloat(value); |
1255 |
|
else if ("ampeg_vel2attack" == key) pCurDef->ampeg_vel2attack = ToFloat(value); |
1256 |
|
else if ("ampeg_vel2hold" == key) pCurDef->ampeg_vel2hold = ToFloat(value); |
1257 |
|
else if ("ampeg_vel2decay" == key) pCurDef->ampeg_vel2decay = ToFloat(value); |
1258 |
|
else if ("ampeg_vel2sustain" == key) pCurDef->ampeg_vel2sustain = ToFloat(value); |
1259 |
|
else if ("ampeg_vel2release" == key) pCurDef->ampeg_vel2release = ToFloat(value); |
1260 |
else if ("fileg_delay" == key) pCurDef->fileg_delay = ToFloat(value); |
else if ("fileg_delay" == key) pCurDef->fileg_delay = ToFloat(value); |
1261 |
else if ("fileg_start" == key) pCurDef->fileg_start = ToFloat(value); |
else if ("fileg_start" == key) pCurDef->fileg_start = ToFloat(value); |
1262 |
else if ("fileg_attack" == key) pCurDef->fileg_attack = ToFloat(value); |
else if ("fileg_attack" == key) pCurDef->fileg_attack = ToFloat(value); |
1305 |
case 'g': i = 7; break; |
case 'g': i = 7; break; |
1306 |
case 'a': i = 9; break; |
case 'a': i = 9; break; |
1307 |
case 'b': i = 11; break; |
case 'b': i = 11; break; |
1308 |
|
case '-': if (s == "-1") return -1; |
1309 |
default: |
default: |
1310 |
std::cerr << "Not a note: " << s << std::endl; |
std::cerr << "Not a note: " << s << std::endl; |
1311 |
return 0; |
return 0; |