3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
* Copyright (C) 2005 - 2009 Christian Schoenebeck * |
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 * |
67 |
|
|
68 |
// the instrument we borrowed on behalf of the editor |
// the instrument we borrowed on behalf of the editor |
69 |
::gig::Instrument* pInstrument; |
::gig::Instrument* pInstrument; |
70 |
|
// the instrument editor we work on behalf |
71 |
|
InstrumentEditor* pEditor; |
72 |
}; |
}; |
73 |
|
|
74 |
/** |
/** |
146 |
} |
} |
147 |
|
|
148 |
InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) { |
InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) { |
149 |
std::vector<instrument_id_t> result; |
Lock(); |
150 |
|
::gig::Instrument* pInstrument = Resource(ID, false); |
151 |
|
bool loaded = (pInstrument != NULL); |
152 |
|
if (!loaded) Unlock(); |
153 |
|
|
154 |
::RIFF::File* riff = NULL; |
::RIFF::File* riff = NULL; |
155 |
::gig::File* gig = NULL; |
::gig::File* gig = NULL; |
156 |
try { |
try { |
157 |
riff = new ::RIFF::File(ID.FileName); |
if(!loaded) { |
158 |
gig = new ::gig::File(riff); |
riff = new ::RIFF::File(ID.FileName); |
159 |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
gig = new ::gig::File(riff); |
160 |
::gig::Instrument* pInstrument = gig->GetInstrument(ID.Index); |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
161 |
|
pInstrument = gig->GetInstrument(ID.Index); |
162 |
|
} |
163 |
|
|
164 |
if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName); |
if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName); |
165 |
|
|
166 |
instrument_info_t info; |
instrument_info_t info; |
167 |
if (gig->pVersion) { |
for (int i = 0; i < 128; i++) { info.KeyBindings[i] = info.KeySwitchBindings[i] = 0; } |
168 |
info.FormatVersion = ToString(gig->pVersion->major); |
|
169 |
info.Product = gig->pInfo->Product; |
::gig::File* pFile = (::gig::File*) pInstrument->GetParent(); |
170 |
info.Artists = gig->pInfo->Artists; |
|
171 |
|
if (pFile->pVersion) { |
172 |
|
info.FormatVersion = ToString(pFile->pVersion->major); |
173 |
|
info.Product = pFile->pInfo->Product; |
174 |
|
info.Artists = pFile->pInfo->Artists; |
175 |
} |
} |
176 |
|
|
177 |
info.InstrumentName = pInstrument->pInfo->Name; |
info.InstrumentName = pInstrument->pInfo->Name; |
178 |
|
|
179 |
|
::gig::Region* pRegion = pInstrument->GetFirstRegion(); |
180 |
|
while (pRegion) { |
181 |
|
int low = pRegion->KeyRange.low; |
182 |
|
int high = pRegion->KeyRange.high; |
183 |
|
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
184 |
|
std::cerr << "Invalid key range: " << low << " - " << high << std::endl; |
185 |
|
} else { |
186 |
|
for (int i = low; i <= high; i++) info.KeyBindings[i] = 1; |
187 |
|
} |
188 |
|
|
189 |
|
pRegion = pInstrument->GetNextRegion(); |
190 |
|
} |
191 |
|
|
192 |
|
if (loaded) { // retrieve keyswitching only if the instrument is fully loaded. |
193 |
|
|
194 |
|
// only return keyswitch range if keyswitching is used |
195 |
|
bool hasKeyswitches = false; |
196 |
|
for (::gig::Region* pRegion = pInstrument->GetFirstRegion() ; |
197 |
|
pRegion && !hasKeyswitches ; |
198 |
|
pRegion = pInstrument->GetNextRegion()) { |
199 |
|
for (int i = 0 ; i < pRegion->Dimensions ; i++) { |
200 |
|
if (pRegion->pDimensionDefinitions[i].dimension == ::gig::dimension_keyboard) { |
201 |
|
hasKeyswitches = true; |
202 |
|
break; |
203 |
|
} |
204 |
|
} |
205 |
|
} |
206 |
|
|
207 |
|
if (hasKeyswitches) { |
208 |
|
int low = pInstrument->DimensionKeyRange.low; |
209 |
|
int high = pInstrument->DimensionKeyRange.high; |
210 |
|
if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { |
211 |
|
std::cerr << "Invalid keyswitch range: " << low << " - " << high << std::endl; |
212 |
|
} else { |
213 |
|
for (int i = low; i <= high; i++) info.KeySwitchBindings[i] = 1; |
214 |
|
} |
215 |
|
} |
216 |
|
} |
217 |
|
|
218 |
|
if (loaded) Unlock(); |
219 |
|
|
220 |
if (gig) delete gig; |
if (gig) delete gig; |
221 |
if (riff) delete riff; |
if (riff) delete riff; |
222 |
return info; |
return info; |
223 |
} catch (::RIFF::Exception e) { |
} catch (::RIFF::Exception e) { |
224 |
|
if (loaded) Unlock(); |
225 |
if (gig) delete gig; |
if (gig) delete gig; |
226 |
if (riff) delete riff; |
if (riff) delete riff; |
227 |
throw InstrumentManagerException(e.Message); |
throw InstrumentManagerException(e.Message); |
228 |
} catch (...) { |
} catch (...) { |
229 |
|
if (loaded) Unlock(); |
230 |
if (gig) delete gig; |
if (gig) delete gig; |
231 |
if (riff) delete riff; |
if (riff) delete riff; |
232 |
throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'"); |
throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'"); |
233 |
} |
} |
234 |
} |
} |
235 |
|
|
236 |
void InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException) { |
InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException) { |
237 |
const String sDataType = GetInstrumentDataStructureName(ID); |
const String sDataType = GetInstrumentDataStructureName(ID); |
238 |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
239 |
// find instrument editors capable to handle given instrument |
// find instrument editors capable to handle given instrument |
255 |
::gig::Instrument* pInstrument = Borrow(ID, pProxy); |
::gig::Instrument* pInstrument = Borrow(ID, pProxy); |
256 |
// remember the proxy and instrument for this instrument editor |
// remember the proxy and instrument for this instrument editor |
257 |
pProxy->pInstrument = pInstrument; |
pProxy->pInstrument = pInstrument; |
258 |
|
pProxy->pEditor = pEditor; |
259 |
InstrumentEditorProxiesMutex.Lock(); |
InstrumentEditorProxiesMutex.Lock(); |
260 |
InstrumentEditorProxies[pEditor] = pProxy; |
InstrumentEditorProxies.add(pProxy); |
261 |
InstrumentEditorProxiesMutex.Unlock(); |
InstrumentEditorProxiesMutex.Unlock(); |
262 |
// launch the instrument editor for the given instrument |
// launch the instrument editor for the given instrument |
263 |
pEditor->Launch(pInstrument, sDataType, sDataVersion); |
pEditor->Launch(pInstrument, sDataType, sDataVersion); |
264 |
|
|
265 |
|
// register the instrument editor as virtual MIDI device as well ... |
266 |
|
VirtualMidiDevice* pVirtualMidiDevice = |
267 |
|
dynamic_cast<VirtualMidiDevice*>(pEditor); |
268 |
|
if (!pVirtualMidiDevice) { |
269 |
|
std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush; |
270 |
|
return pEditor; |
271 |
|
} |
272 |
|
// NOTE: for now connect the virtual MIDI keyboard of the instrument editor (if any) with all engine channels that have the same instrument as the editor was opened for ( other ideas ? ) |
273 |
|
Lock(); |
274 |
|
std::set<gig::EngineChannel*> engineChannels = |
275 |
|
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
276 |
|
std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin(); |
277 |
|
std::set<gig::EngineChannel*>::iterator end = engineChannels.end(); |
278 |
|
for (; iter != end; ++iter) (*iter)->Connect(pVirtualMidiDevice); |
279 |
|
Unlock(); |
280 |
|
|
281 |
|
return pEditor; |
282 |
} |
} |
283 |
|
|
284 |
/** |
/** |
290 |
*/ |
*/ |
291 |
void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) { |
void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) { |
292 |
dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n")); |
dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n")); |
293 |
// hand back instrument and free proxy |
|
294 |
|
::gig::Instrument* pInstrument = NULL; |
295 |
|
InstrumentEditorProxy* pProxy = NULL; |
296 |
|
int iProxyIndex = -1; |
297 |
|
|
298 |
|
// first find the editor proxy entry for this editor |
299 |
InstrumentEditorProxiesMutex.Lock(); |
InstrumentEditorProxiesMutex.Lock(); |
300 |
if (InstrumentEditorProxies.count(pSender)) { |
for (int i = 0; i < InstrumentEditorProxies.size(); i++) { |
301 |
InstrumentEditorProxy* pProxy = |
InstrumentEditorProxy* pCurProxy = |
302 |
dynamic_cast<InstrumentEditorProxy*>( |
dynamic_cast<InstrumentEditorProxy*>( |
303 |
InstrumentEditorProxies[pSender] |
InstrumentEditorProxies[i] |
304 |
); |
); |
305 |
InstrumentEditorProxies.erase(pSender); |
if (pCurProxy->pEditor == pSender) { |
306 |
InstrumentEditorProxiesMutex.Unlock(); |
pProxy = pCurProxy; |
307 |
HandBack(pProxy->pInstrument, pProxy); |
iProxyIndex = i; |
308 |
if (pProxy) delete pProxy; |
pInstrument = pCurProxy->pInstrument; |
309 |
|
} |
310 |
|
} |
311 |
|
InstrumentEditorProxiesMutex.Unlock(); |
312 |
|
|
313 |
|
if (!pProxy) { |
314 |
|
std::cerr << "Eeeek, could not find instrument editor proxy, " |
315 |
|
"this is a bug!\n" << std::flush; |
316 |
|
return; |
317 |
|
} |
318 |
|
|
319 |
|
// now unregister editor as not being available as a virtual MIDI device anymore |
320 |
|
VirtualMidiDevice* pVirtualMidiDevice = |
321 |
|
dynamic_cast<VirtualMidiDevice*>(pSender); |
322 |
|
if (pVirtualMidiDevice) { |
323 |
|
Lock(); |
324 |
|
// NOTE: see note in LaunchInstrumentEditor() |
325 |
|
std::set<gig::EngineChannel*> engineChannels = |
326 |
|
GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); |
327 |
|
std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin(); |
328 |
|
std::set<gig::EngineChannel*>::iterator end = engineChannels.end(); |
329 |
|
for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice); |
330 |
|
Unlock(); |
331 |
} else { |
} else { |
332 |
|
std::cerr << "Could not unregister editor as not longer acting as " |
333 |
|
"virtual MIDI device. Wasn't it registered?\n" |
334 |
|
<< std::flush; |
335 |
|
} |
336 |
|
|
337 |
|
// finally delete proxy entry and hand back instrument |
338 |
|
if (pInstrument) { |
339 |
|
InstrumentEditorProxiesMutex.Lock(); |
340 |
|
InstrumentEditorProxies.remove(iProxyIndex); |
341 |
InstrumentEditorProxiesMutex.Unlock(); |
InstrumentEditorProxiesMutex.Unlock(); |
342 |
std::cerr << "Eeeek, could not find instrument editor proxy, this is a bug!\n" << std::flush; |
|
343 |
|
HandBack(pInstrument, pProxy); |
344 |
|
delete pProxy; |
345 |
} |
} |
346 |
|
|
347 |
// Note that we don't need to free the editor here. As it |
// Note that we don't need to free the editor here. As it |
349 |
// dies. |
// dies. |
350 |
} |
} |
351 |
|
|
352 |
|
#if 0 // currently unused : |
353 |
|
/** |
354 |
|
* Try to inform the respective instrument editor(s), that a note on |
355 |
|
* event just occured. This method is called by the MIDI thread. If any |
356 |
|
* obstacles are in the way (e.g. if a wait for an unlock would be |
357 |
|
* required) we give up immediately, since the RT safeness of the MIDI |
358 |
|
* thread has absolute priority. |
359 |
|
*/ |
360 |
|
void InstrumentResourceManager::TrySendNoteOnToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) { |
361 |
|
const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation |
362 |
|
if (!bGotLock) return; // hell, forget it, not worth the hassle |
363 |
|
for (int i = 0; i < InstrumentEditorProxies.size(); i++) { |
364 |
|
InstrumentEditorProxy* pProxy = |
365 |
|
dynamic_cast<InstrumentEditorProxy*>( |
366 |
|
InstrumentEditorProxies[i] |
367 |
|
); |
368 |
|
if (pProxy->pInstrument == pInstrument) |
369 |
|
pProxy->pEditor->SendNoteOnToDevice(Key, Velocity); |
370 |
|
} |
371 |
|
InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
|
* Try to inform the respective instrument editor(s), that a note off |
376 |
|
* event just occured. This method is called by the MIDI thread. If any |
377 |
|
* obstacles are in the way (e.g. if a wait for an unlock would be |
378 |
|
* required) we give up immediately, since the RT safeness of the MIDI |
379 |
|
* thread has absolute priority. |
380 |
|
*/ |
381 |
|
void InstrumentResourceManager::TrySendNoteOffToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) { |
382 |
|
const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation |
383 |
|
if (!bGotLock) return; // hell, forget it, not worth the hassle |
384 |
|
for (int i = 0; i < InstrumentEditorProxies.size(); i++) { |
385 |
|
InstrumentEditorProxy* pProxy = |
386 |
|
dynamic_cast<InstrumentEditorProxy*>( |
387 |
|
InstrumentEditorProxies[i] |
388 |
|
); |
389 |
|
if (pProxy->pInstrument == pInstrument) |
390 |
|
pProxy->pEditor->SendNoteOffToDevice(Key, Velocity); |
391 |
|
} |
392 |
|
InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation |
393 |
|
} |
394 |
|
#endif // unused |
395 |
|
|
396 |
void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) { |
void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) { |
397 |
if (Samples.empty()) { |
if (Samples.empty()) { |
398 |
std::cerr << "gig::InstrumentResourceManager: WARNING, " |
std::cerr << "gig::InstrumentResourceManager: WARNING, " |
472 |
} else if (sStructType == "gig::Instrument") { |
} else if (sStructType == "gig::Instrument") { |
473 |
// resume all previously suspended engines |
// resume all previously suspended engines |
474 |
ResumeAllEngines(); |
ResumeAllEngines(); |
475 |
|
} else if (sStructType == "gig::Sample") { |
476 |
|
// we're assuming here, that OnDataStructureToBeChanged() with |
477 |
|
// "gig::File" was called previously, so we won't resume anything |
478 |
|
// here, but just re-cache the given sample |
479 |
|
Lock(); |
480 |
|
::gig::Sample* pSample = (::gig::Sample*) pStruct; |
481 |
|
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
482 |
|
UncacheInitialSamples(pSample); |
483 |
|
// now re-cache ... |
484 |
|
std::vector< ::gig::Instrument*> instruments = |
485 |
|
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
486 |
|
for (int i = 0; i < instruments.size(); i++) { |
487 |
|
if (SampleReferencedByInstrument(pSample, instruments[i])) { |
488 |
|
std::set<gig::EngineChannel*> engineChannels = |
489 |
|
GetEngineChannelsUsing(instruments[i], false/*don't lock again*/); |
490 |
|
std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin(); |
491 |
|
std::set<gig::EngineChannel*>::iterator end = engineChannels.end(); |
492 |
|
for (; iter != end; ++iter) |
493 |
|
CacheInitialSamples(pSample, *iter); |
494 |
|
} |
495 |
|
} |
496 |
|
Unlock(); |
497 |
} else if (sStructType == "gig::Region") { |
} else if (sStructType == "gig::Region") { |
498 |
// advice the engines to resume the given region, that is to |
// advice the engines to resume the given region, that is to |
499 |
// using it for playback again |
// using it for playback again |
537 |
Lock(); |
Lock(); |
538 |
::gig::Sample* pSample = (::gig::Sample*) pOldSample; |
::gig::Sample* pSample = (::gig::Sample*) pOldSample; |
539 |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
::gig::File* pFile = (::gig::File*) pSample->GetParent(); |
540 |
|
bool bSampleStillInUse = false; |
541 |
std::vector< ::gig::Instrument*> instruments = |
std::vector< ::gig::Instrument*> instruments = |
542 |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); |
543 |
for (int i = 0; i < instruments.size(); i++) |
for (int i = 0; i < instruments.size(); i++) { |
544 |
if (!SampleReferencedByInstrument(pSample, instruments[i])) |
if (SampleReferencedByInstrument(pSample, instruments[i])) { |
545 |
UncacheInitialSamples(pSample); |
bSampleStillInUse = true; |
546 |
|
break; |
547 |
|
} |
548 |
|
} |
549 |
|
if (!bSampleStillInUse) UncacheInitialSamples(pSample); |
550 |
Unlock(); |
Unlock(); |
551 |
} |
} |
552 |
// make sure new sample reference is cached |
// make sure new sample reference is cached |
780 |
} |
} |
781 |
|
|
782 |
/** |
/** |
783 |
|
* Returns a list with all gig engine channels that are currently using |
784 |
|
* the given instrument. |
785 |
|
* |
786 |
|
* @param pInstrument - search criteria |
787 |
|
* @param bLock - whether we should lock (mutex) the instrument manager |
788 |
|
* during this call and unlock at the end of this call |
789 |
|
*/ |
790 |
|
std::set<gig::EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) { |
791 |
|
if (bLock) Lock(); |
792 |
|
std::set<gig::EngineChannel*> result; |
793 |
|
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
794 |
|
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
795 |
|
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
796 |
|
for (; iter != end; ++iter) { |
797 |
|
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter); |
798 |
|
if (!pEngineChannel) continue; |
799 |
|
result.insert(pEngineChannel); |
800 |
|
} |
801 |
|
if (bLock) Unlock(); |
802 |
|
return result; |
803 |
|
} |
804 |
|
|
805 |
|
/** |
806 |
* Returns a list with all gig Engines that are currently using the given |
* Returns a list with all gig Engines that are currently using the given |
807 |
* instrument. |
* instrument. |
808 |
* |
* |
812 |
*/ |
*/ |
813 |
std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) { |
std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) { |
814 |
if (bLock) Lock(); |
if (bLock) Lock(); |
815 |
std::set<gig::Engine*> result; |
std::set<gig::Engine*> result; |
816 |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument); |
817 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin(); |
818 |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |
std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end = consumers.end(); |