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 * |
32 |
#include "../../common/File.h" |
#include "../../common/File.h" |
33 |
#include "../../common/Path.h" |
#include "../../common/Path.h" |
34 |
#include "../../common/global_private.h" |
#include "../../common/global_private.h" |
35 |
|
#include "LookupTable.h" |
36 |
|
|
37 |
namespace sfz |
namespace sfz |
38 |
{ |
{ |
109 |
} |
} |
110 |
|
|
111 |
bool Region::OnKey(const Query& q) { |
bool Region::OnKey(const Query& q) { |
112 |
|
// As the region comes from a LookupTable search on the query, |
113 |
|
// the following parameters are not checked here: chan, key, |
114 |
|
// vel, chanaft, polyaft, prog, sw_previous, cc. They are all |
115 |
|
// handled by the lookup table. |
116 |
bool is_triggered( |
bool is_triggered( |
|
q.chan >= lochan && q.chan <= hichan && |
|
|
q.key >= lokey && q.key <= hikey && |
|
|
q.vel >= lovel && q.vel <= hivel && |
|
117 |
q.bend >= lobend && q.bend <= hibend && |
q.bend >= lobend && q.bend <= hibend && |
118 |
q.bpm >= lobpm && q.bpm < hibpm && |
q.bpm >= lobpm && q.bpm < hibpm && |
|
q.chanaft >= lochanaft && q.chanaft <= hichanaft && |
|
|
q.polyaft >= lopolyaft && q.polyaft <= hipolyaft && |
|
|
q.prog >= loprog && q.prog <= hiprog && |
|
119 |
q.rand >= lorand && q.rand < hirand && |
q.rand >= lorand && q.rand < hirand && |
120 |
q.timer >= lotimer && q.timer <= hitimer && |
q.timer >= lotimer && q.timer <= hitimer && |
121 |
|
|
128 |
( sw_up == -1 || |
( sw_up == -1 || |
129 |
((sw_up >= sw_lokey && (sw_hikey == -1 || sw_up <= sw_hikey)) ? (!q.sw[sw_up]) : true) ) && |
((sw_up >= sw_lokey && (sw_hikey == -1 || sw_up <= sw_hikey)) ? (!q.sw[sw_up]) : true) ) && |
130 |
|
|
|
( sw_previous == -1 || |
|
|
q.prev_sw_key == sw_previous ) && |
|
|
|
|
131 |
((trigger & q.trig) != 0) |
((trigger & q.trig) != 0) |
132 |
); |
); |
133 |
|
|
134 |
if (!is_triggered) |
if (!is_triggered) |
135 |
return false; |
return false; |
136 |
|
|
|
for (int i = 0; i < 128; ++i) { |
|
|
if (locc[i] != -1 && hicc[i] != -1 && !(q.cc[i] >= locc[i] && q.cc[i] <= hicc[i])) |
|
|
return false; |
|
|
} |
|
|
|
|
137 |
// seq_position has to be checked last, so we know that we |
// seq_position has to be checked last, so we know that we |
138 |
// increment the right counter |
// increment the right counter |
139 |
is_triggered = (seq_counter == seq_position); |
is_triggered = (seq_counter == seq_position); |
142 |
return is_triggered; |
return is_triggered; |
143 |
} |
} |
144 |
|
|
|
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; |
|
|
} |
|
|
|
|
145 |
Articulation* |
Articulation* |
146 |
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) |
147 |
{ |
{ |
149 |
} |
} |
150 |
|
|
151 |
bool Region::HasLoop() { |
bool Region::HasLoop() { |
152 |
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() : |
153 |
|
(loop_mode == ::sfz::LOOP_CONTINUOUS || loop_mode == ::sfz::LOOP_SUSTAIN); |
154 |
return b && GetLoopStart() && GetLoopEnd() && GetLoopEnd() > GetLoopStart(); |
return b && GetLoopStart() && GetLoopEnd() && GetLoopEnd() > GetLoopStart(); |
155 |
} |
} |
156 |
|
|
157 |
uint Region::GetLoopStart() { |
uint Region::GetLoopStart() { |
158 |
return (!loop_start) ? 0 : *loop_start; // TODO: use sample loop when loop_start not defined |
return (!loop_start) ? pSample->GetLoopStart() : *loop_start; |
159 |
} |
} |
160 |
|
|
161 |
uint Region::GetLoopEnd() { |
uint Region::GetLoopEnd() { |
162 |
return (!loop_end) ? 0 : *loop_end; // TODO: use sample loop when loop_end not defined |
return (!loop_end) ? pSample->GetLoopEnd() : *loop_end; |
163 |
} |
} |
164 |
|
|
165 |
uint Region::GetLoopCount() { |
uint Region::GetLoopCount() { |
173 |
{ |
{ |
174 |
this->name = name; |
this->name = name; |
175 |
this->pSampleManager = pSampleManager ? pSampleManager : this; |
this->pSampleManager = pSampleManager ? pSampleManager : this; |
176 |
|
pLookupTable = 0; |
177 |
} |
} |
178 |
|
|
179 |
Instrument::~Instrument() |
Instrument::~Instrument() |
180 |
{ |
{ |
181 |
for(int i = 0; i < regions.size(); i++) { |
for (int i = 0; i < regions.size(); i++) { |
182 |
delete (regions[i]); |
delete regions[i]; |
183 |
} |
} |
184 |
regions.clear(); |
delete pLookupTable; |
185 |
|
for (int i = 0 ; i < 128 ; i++) { |
186 |
|
delete pLookupTableCC[i]; |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
void Query::search(const Instrument* pInstrument) { |
191 |
|
pRegionList = &pInstrument->pLookupTable->query(*this); |
192 |
|
regionIndex = 0; |
193 |
} |
} |
194 |
|
|
195 |
Query::Query(const Instrument& instrument) { |
void Query::search(const Instrument* pInstrument, int triggercc) { |
196 |
i = instrument.regions.begin(); |
pRegionList = &pInstrument->pLookupTableCC[triggercc]->query(*this); |
197 |
regions_end = instrument.regions.end(); |
regionIndex = 0; |
198 |
} |
} |
199 |
|
|
200 |
Region* Query::next() { |
Region* Query::next() { |
201 |
while (i != regions_end) { |
for ( ; regionIndex < pRegionList->size() ; regionIndex++) { |
202 |
if ((*i++)->OnKey(*this)) return *i; |
if ((*pRegionList)[regionIndex]->OnKey(*this)) { |
203 |
|
return (*pRegionList)[regionIndex++]; |
204 |
|
} |
205 |
} |
} |
206 |
return 0; |
return 0; |
207 |
} |
} |
274 |
trigger = TRIGGER_ATTACK; |
trigger = TRIGGER_ATTACK; |
275 |
|
|
276 |
group = 0; |
group = 0; |
277 |
off_by.unset(); |
off_by = 0; |
278 |
off_mode = OFF_FAST; |
off_mode = OFF_FAST; |
279 |
|
|
280 |
// sample player |
// sample player |
285 |
end.unset(); |
end.unset(); |
286 |
loop_crossfade.unset(); |
loop_crossfade.unset(); |
287 |
offset.unset(); offset_random.unset(); |
offset.unset(); offset_random.unset(); |
288 |
loop_mode = NO_LOOP; |
loop_mode = LOOP_UNSET; |
289 |
loop_start.unset(); loop_end.unset(); |
loop_start.unset(); loop_end.unset(); |
290 |
sync_beats.unset(); sync_offset.unset(); |
sync_beats.unset(); sync_offset.unset(); |
291 |
|
|
778 |
::sfz::Region* pRegion = _instrument->regions[i]; |
::sfz::Region* pRegion = _instrument->regions[i]; |
779 |
int low = pRegion->lokey; |
int low = pRegion->lokey; |
780 |
int high = pRegion->hikey; |
int high = pRegion->hikey; |
781 |
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
if (low != -1) { // lokey -1 means region doesn't play on note-on |
782 |
std::cerr << "Invalid key range: " << low << " - " << high << std::endl; |
// hikey -1 is the same as no limit, except that it |
783 |
} else { |
// also enables on_locc/on_hicc |
784 |
for (int j = low; j <= high; j++) _instrument->KeyBindings[j] = true; |
if (high == -1) high = 127; |
785 |
|
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
786 |
|
std::cerr << "Invalid key range: " << low << " - " << high << std::endl; |
787 |
|
} else { |
788 |
|
for (int j = low; j <= high; j++) _instrument->KeyBindings[j] = true; |
789 |
|
} |
790 |
} |
} |
791 |
|
|
792 |
// get keyswitches |
// get keyswitches |
839 |
} |
} |
840 |
} |
} |
841 |
} |
} |
842 |
|
|
843 |
|
_instrument->pLookupTable = new LookupTable(_instrument); |
844 |
|
|
845 |
|
// create separate lookup tables for controller triggered |
846 |
|
// regions, one for each CC |
847 |
|
for (int i = 0 ; i < 128 ; i++) { |
848 |
|
_instrument->pLookupTableCC[i] = new LookupTable(_instrument, i); |
849 |
|
} |
850 |
} |
} |
851 |
|
|
852 |
File::~File() |
File::~File() |
958 |
else if ("hivel" == key) pCurDef->hivel = ToInt(value); |
else if ("hivel" == key) pCurDef->hivel = ToInt(value); |
959 |
else if ("lobend" == key) pCurDef->lobend = ToInt(value); |
else if ("lobend" == key) pCurDef->lobend = ToInt(value); |
960 |
else if ("hibend" == key) pCurDef->hibend = ToInt(value); |
else if ("hibend" == key) pCurDef->hibend = ToInt(value); |
961 |
else if ("lobpm" == key) pCurDef->lobpm = ToInt(value); |
else if ("lobpm" == key) pCurDef->lobpm = ToFloat(value); |
962 |
else if ("hibpm" == key) pCurDef->hibpm = ToInt(value); |
else if ("hibpm" == key) pCurDef->hibpm = ToFloat(value); |
963 |
else if ("lochanaft" == key) pCurDef->lochanaft = ToInt(value); |
else if ("lochanaft" == key) pCurDef->lochanaft = ToInt(value); |
964 |
else if ("hichanaft" == key) pCurDef->hichanaft = ToInt(value); |
else if ("hichanaft" == key) pCurDef->hichanaft = ToInt(value); |
965 |
else if ("lopolyaft" == key) pCurDef->lopolyaft = ToInt(value); |
else if ("lopolyaft" == key) pCurDef->lopolyaft = ToInt(value); |
994 |
else if ("off_by" == key || "offby" == key) pCurDef->off_by = ToInt(value); |
else if ("off_by" == key || "offby" == key) pCurDef->off_by = ToInt(value); |
995 |
else if ("off_mode" == key || "offmode" == key) |
else if ("off_mode" == key || "offmode" == key) |
996 |
{ |
{ |
997 |
if (value == "fast") _current_group->off_mode = OFF_FAST; |
if (value == "fast") pCurDef->off_mode = OFF_FAST; |
998 |
else if (value == "normal") _current_group->off_mode = OFF_NORMAL; |
else if (value == "normal") pCurDef->off_mode = OFF_NORMAL; |
999 |
} |
} |
1000 |
|
|
1001 |
// sample player |
// sample player |
1270 |
case 'g': i = 7; break; |
case 'g': i = 7; break; |
1271 |
case 'a': i = 9; break; |
case 'a': i = 9; break; |
1272 |
case 'b': i = 11; break; |
case 'b': i = 11; break; |
1273 |
|
case '-': if (s == "-1") return -1; |
1274 |
default: |
default: |
1275 |
std::cerr << "Not a note: " << s << std::endl; |
std::cerr << "Not a note: " << s << std::endl; |
1276 |
return 0; |
return 0; |