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 - 2011 Christian Schoenebeck * |
* Copyright (C) 2005 - 2015 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 * |
32 |
|
|
33 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
34 |
|
|
|
// data stored as long as an instrument resource exists |
|
|
struct instr_entry_t { |
|
|
InstrumentManager::instrument_id_t ID; |
|
|
::gig::File* pGig; |
|
|
uint MaxSamplesPerCycle; ///< if some engine requests an already allocated instrument with a higher value, we have to reallocate the instrument |
|
|
}; |
|
|
|
|
35 |
// some data needed for the libgig callback function |
// some data needed for the libgig callback function |
36 |
struct progress_callback_arg_t { |
struct progress_callback_arg_t { |
37 |
InstrumentResourceManager* pManager; |
InstrumentResourceManager* pManager; |
69 |
* instrument ID |
* instrument ID |
70 |
*/ |
*/ |
71 |
void InstrumentResourceManager::OnInstrumentLoadingProgress(::gig::progress_t* pProgress) { |
void InstrumentResourceManager::OnInstrumentLoadingProgress(::gig::progress_t* pProgress) { |
72 |
dmsg(7,("gig::InstrumentResourceManager: progress %f%", pProgress->factor)); |
dmsg(7,("gig::InstrumentResourceManager: progress %f%%", pProgress->factor)); |
73 |
progress_callback_arg_t* pArg = static_cast<progress_callback_arg_t*>(pProgress->custom); |
progress_callback_arg_t* pArg = static_cast<progress_callback_arg_t*>(pProgress->custom); |
74 |
// we randomly schedule 90% for the .gig file loading and the remaining 10% later for sample caching |
// we randomly schedule 90% for the .gig file loading and the remaining 10% later for sample caching |
75 |
const float localProgress = 0.9f * pProgress->factor; |
const float localProgress = 0.9f * pProgress->factor; |
76 |
pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress); |
pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress); |
77 |
} |
} |
78 |
|
|
|
std::vector<InstrumentResourceManager::instrument_id_t> InstrumentResourceManager::Instruments() { |
|
|
return Entries(); |
|
|
} |
|
|
|
|
79 |
String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { |
String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { |
80 |
Lock(); |
Lock(); |
81 |
::gig::Instrument* pInstrument = Resource(ID, false); |
::gig::Instrument* pInstrument = Resource(ID, false); |
129 |
::RIFF::File* riff = NULL; |
::RIFF::File* riff = NULL; |
130 |
::gig::File* gig = NULL; |
::gig::File* gig = NULL; |
131 |
try { |
try { |
132 |
if(!loaded) { |
if (!loaded) { |
133 |
riff = new ::RIFF::File(ID.FileName); |
riff = new ::RIFF::File(ID.FileName); |
134 |
gig = new ::gig::File(riff); |
gig = new ::gig::File(riff); |
135 |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
208 |
} |
} |
209 |
} |
} |
210 |
|
|
211 |
InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID, void* pUserData) throw (InstrumentManagerException) { |
InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(LinuxSampler::EngineChannel* pEngineChannel, instrument_id_t ID, void* pUserData) throw (InstrumentManagerException) { |
212 |
const String sDataType = GetInstrumentDataStructureName(ID); |
const String sDataType = GetInstrumentDataStructureName(ID); |
213 |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
const String sDataVersion = GetInstrumentDataStructureVersion(ID); |
214 |
// find instrument editors capable to handle given instrument |
// find instrument editors capable to handle given instrument |
235 |
InstrumentEditorProxies.add(pProxy); |
InstrumentEditorProxies.add(pProxy); |
236 |
InstrumentEditorProxiesMutex.Unlock(); |
InstrumentEditorProxiesMutex.Unlock(); |
237 |
// launch the instrument editor for the given instrument |
// launch the instrument editor for the given instrument |
238 |
pEditor->Launch(pInstrument, sDataType, sDataVersion, pUserData); |
pEditor->Launch(pEngineChannel, pInstrument, sDataType, sDataVersion, pUserData); |
239 |
|
|
240 |
// register the instrument editor as virtual MIDI device as well ... |
// register the instrument editor as virtual MIDI device as well ... |
241 |
VirtualMidiDevice* pVirtualMidiDevice = |
VirtualMidiDevice* pVirtualMidiDevice = |
271 |
int iProxyIndex = -1; |
int iProxyIndex = -1; |
272 |
|
|
273 |
// first find the editor proxy entry for this editor |
// first find the editor proxy entry for this editor |
274 |
InstrumentEditorProxiesMutex.Lock(); |
{ |
275 |
for (int i = 0; i < InstrumentEditorProxies.size(); i++) { |
LockGuard lock(InstrumentEditorProxiesMutex); |
276 |
InstrumentEditorProxy* pCurProxy = |
for (int i = 0; i < InstrumentEditorProxies.size(); i++) { |
277 |
dynamic_cast<InstrumentEditorProxy*>( |
InstrumentEditorProxy* pCurProxy = |
278 |
InstrumentEditorProxies[i] |
dynamic_cast<InstrumentEditorProxy*>( |
279 |
); |
InstrumentEditorProxies[i] |
280 |
if (pCurProxy->pEditor == pSender) { |
); |
281 |
pProxy = pCurProxy; |
if (pCurProxy->pEditor == pSender) { |
282 |
iProxyIndex = i; |
pProxy = pCurProxy; |
283 |
pInstrument = pCurProxy->pInstrument; |
iProxyIndex = i; |
284 |
|
pInstrument = pCurProxy->pInstrument; |
285 |
|
} |
286 |
} |
} |
287 |
} |
} |
|
InstrumentEditorProxiesMutex.Unlock(); |
|
288 |
|
|
289 |
if (!pProxy) { |
if (!pProxy) { |
290 |
std::cerr << "Eeeek, could not find instrument editor proxy, " |
std::cerr << "Eeeek, could not find instrument editor proxy, " |
312 |
|
|
313 |
// finally delete proxy entry and hand back instrument |
// finally delete proxy entry and hand back instrument |
314 |
if (pInstrument) { |
if (pInstrument) { |
315 |
InstrumentEditorProxiesMutex.Lock(); |
{ |
316 |
InstrumentEditorProxies.remove(iProxyIndex); |
LockGuard lock(InstrumentEditorProxiesMutex); |
317 |
InstrumentEditorProxiesMutex.Unlock(); |
InstrumentEditorProxies.remove(iProxyIndex); |
318 |
|
} |
319 |
|
|
320 |
HandBack(pInstrument, pProxy); |
HandBack(pInstrument, pProxy); |
321 |
delete pProxy; |
delete pProxy; |
564 |
pGig->GetFirstSample(); // just to force complete instrument loading |
pGig->GetFirstSample(); // just to force complete instrument loading |
565 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
566 |
|
|
567 |
|
uint maxSamplesPerCycle = GetMaxSamplesPerCycle(pConsumer); |
568 |
|
|
569 |
// cache initial samples points (for actually needed samples) |
// cache initial samples points (for actually needed samples) |
570 |
dmsg(1,("Caching initial samples...")); |
dmsg(1,("Caching initial samples...")); |
571 |
uint iRegion = 0; // just for progress calculation |
uint iRegion = 0; // just for progress calculation |
577 |
|
|
578 |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
579 |
dmsg(2,("C")); |
dmsg(2,("C")); |
580 |
CacheInitialSamples(pRgn->GetSample(), (EngineChannel*) pConsumer); |
CacheInitialSamples(pRgn->GetSample(), maxSamplesPerCycle); |
581 |
} |
} |
582 |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
583 |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (EngineChannel*) pConsumer); |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, maxSamplesPerCycle); |
584 |
} |
} |
585 |
|
|
586 |
pRgn = pInstrument->GetNextRegion(); |
pRgn = pInstrument->GetNextRegion(); |
593 |
instr_entry_t* pEntry = new instr_entry_t; |
instr_entry_t* pEntry = new instr_entry_t; |
594 |
pEntry->ID.FileName = Key.FileName; |
pEntry->ID.FileName = Key.FileName; |
595 |
pEntry->ID.Index = Key.Index; |
pEntry->ID.Index = Key.Index; |
596 |
pEntry->pGig = pGig; |
pEntry->pFile = pGig; |
597 |
|
|
598 |
// (try to resolve the audio device context) |
// and we save this to check if we need to reallocate for an engine with higher value of 'MaxSamplesPerSecond' |
599 |
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(pConsumer); |
pEntry->MaxSamplesPerCycle = maxSamplesPerCycle; |
|
AudioOutputDevice* pDevice = |
|
|
(pEngineChannel) ? dynamic_cast<Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice : NULL; |
|
|
|
|
|
// and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond' |
|
|
pEntry->MaxSamplesPerCycle = |
|
|
(pDevice) ? pDevice->MaxSamplesPerCycle() : DefaultMaxSamplesPerCycle(); |
|
600 |
|
|
601 |
pArg = pEntry; |
pArg = pEntry; |
602 |
|
|
603 |
return pInstrument; |
return pInstrument; |
604 |
} |
} |
605 |
|
|
606 |
void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) { |
void InstrumentResourceManager::Destroy(::gig::Instrument* pResource, void* pArg) { |
607 |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
608 |
// we don't need the .gig file here anymore |
// we don't need the .gig file here anymore |
609 |
Gigs.HandBack(pEntry->pGig, reinterpret_cast<GigConsumer*>(pEntry->ID.Index)); // conversion kinda hackish :/ |
Gigs.HandBack(pEntry->pFile, reinterpret_cast<GigConsumer*>(pEntry->ID.Index)); // conversion kinda hackish :/ |
610 |
delete pEntry; |
delete pEntry; |
611 |
} |
} |
612 |
|
|
|
void InstrumentResourceManager::OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg) { |
|
|
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
|
|
|
|
|
// (try to resolve the audio device context) |
|
|
EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(pConsumer); |
|
|
AudioOutputDevice* pDevice = |
|
|
(pEngineChannel) ? dynamic_cast<Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice : NULL; |
|
|
|
|
|
uint maxSamplesPerCycle = |
|
|
(pDevice) ? pDevice->MaxSamplesPerCycle() : DefaultMaxSamplesPerCycle(); |
|
|
|
|
|
if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) { |
|
|
dmsg(1,("Completely reloading instrument due to insufficient precached samples ...\n")); |
|
|
Update(pResource, pConsumer); |
|
|
} |
|
|
} |
|
|
|
|
613 |
void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) { |
void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) { |
614 |
// TODO: we could delete Region and Instrument here if they have become unused |
// TODO: we could delete Region and Instrument here if they have become unused |
615 |
} |
} |
654 |
* will be cached) |
* will be cached) |
655 |
*/ |
*/ |
656 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine) { |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine) { |
657 |
|
uint maxSamplesPerCycle = |
658 |
|
(pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() : |
659 |
|
DefaultMaxSamplesPerCycle(); |
660 |
|
CacheInitialSamples(pSample, maxSamplesPerCycle); |
661 |
|
} |
662 |
|
|
663 |
|
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, uint maxSamplesPerCycle) { |
664 |
if (!pSample) { |
if (!pSample) { |
665 |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
666 |
return; |
return; |
673 |
// number of '0' samples (silence samples) behind the official buffer |
// number of '0' samples (silence samples) behind the official buffer |
674 |
// border, to allow the interpolator do it's work even at the end of |
// border, to allow the interpolator do it's work even at the end of |
675 |
// the sample. |
// the sample. |
|
const uint maxSamplesPerCycle = |
|
|
(pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() |
|
|
: DefaultMaxSamplesPerCycle(); |
|
676 |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
677 |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
678 |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
679 |
dmsg(3,("Caching whole sample (sample name: \"%s\", sample size: %d)\n", pSample->pInfo->Name.c_str(), pSample->SamplesTotal)); |
dmsg(3,("Caching whole sample (sample name: \"%s\", sample size: %lu)\n", pSample->pInfo->Name.c_str(), pSample->SamplesTotal)); |
680 |
::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(neededSilenceSamples); |
::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(neededSilenceSamples); |
681 |
dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize)); |
dmsg(4,("Cached %lu Bytes, %lu silence bytes.\n", buf.Size, buf.NullExtensionSize)); |
682 |
} |
} |
683 |
} |
} |
684 |
else { // we only cache CONFIG_PRELOAD_SAMPLES and stream the other sample points from disk |
else { // we only cache CONFIG_PRELOAD_SAMPLES and stream the other sample points from disk |
689 |
} |
} |
690 |
|
|
691 |
void InstrumentResourceManager::UncacheInitialSamples(::gig::Sample* pSample) { |
void InstrumentResourceManager::UncacheInitialSamples(::gig::Sample* pSample) { |
692 |
dmsg(1,("Uncaching sample %x\n",pSample)); |
dmsg(1,("Uncaching sample %p\n",(void*)pSample)); |
693 |
if (pSample->GetCache().Size) pSample->ReleaseSampleData(); |
if (pSample->GetCache().Size) pSample->ReleaseSampleData(); |
694 |
} |
} |
695 |
|
|