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 - 2012 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; |
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 |
562 |
pGig->GetFirstSample(); // just to force complete instrument loading |
pGig->GetFirstSample(); // just to force complete instrument loading |
563 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
564 |
|
|
565 |
|
uint maxSamplesPerCycle = GetMaxSamplesPerCycle(pConsumer); |
566 |
|
|
567 |
// cache initial samples points (for actually needed samples) |
// cache initial samples points (for actually needed samples) |
568 |
dmsg(1,("Caching initial samples...")); |
dmsg(1,("Caching initial samples...")); |
569 |
uint iRegion = 0; // just for progress calculation |
uint iRegion = 0; // just for progress calculation |
575 |
|
|
576 |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
577 |
dmsg(2,("C")); |
dmsg(2,("C")); |
578 |
CacheInitialSamples(pRgn->GetSample(), (EngineChannel*) pConsumer); |
CacheInitialSamples(pRgn->GetSample(), maxSamplesPerCycle); |
579 |
} |
} |
580 |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
581 |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (EngineChannel*) pConsumer); |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, maxSamplesPerCycle); |
582 |
} |
} |
583 |
|
|
584 |
pRgn = pInstrument->GetNextRegion(); |
pRgn = pInstrument->GetNextRegion(); |
591 |
instr_entry_t* pEntry = new instr_entry_t; |
instr_entry_t* pEntry = new instr_entry_t; |
592 |
pEntry->ID.FileName = Key.FileName; |
pEntry->ID.FileName = Key.FileName; |
593 |
pEntry->ID.Index = Key.Index; |
pEntry->ID.Index = Key.Index; |
594 |
pEntry->pGig = pGig; |
pEntry->pFile = pGig; |
595 |
|
|
596 |
// (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' |
597 |
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(); |
|
598 |
|
|
599 |
pArg = pEntry; |
pArg = pEntry; |
600 |
|
|
601 |
return pInstrument; |
return pInstrument; |
602 |
} |
} |
603 |
|
|
604 |
void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) { |
void InstrumentResourceManager::Destroy(::gig::Instrument* pResource, void* pArg) { |
605 |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
606 |
// we don't need the .gig file here anymore |
// we don't need the .gig file here anymore |
607 |
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 :/ |
608 |
delete pEntry; |
delete pEntry; |
609 |
} |
} |
610 |
|
|
|
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); |
|
|
} |
|
|
} |
|
|
|
|
611 |
void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) { |
void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) { |
612 |
// 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 |
613 |
} |
} |
652 |
* will be cached) |
* will be cached) |
653 |
*/ |
*/ |
654 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine) { |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine) { |
655 |
|
uint maxSamplesPerCycle = |
656 |
|
(pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() : |
657 |
|
DefaultMaxSamplesPerCycle(); |
658 |
|
CacheInitialSamples(pSample, maxSamplesPerCycle); |
659 |
|
} |
660 |
|
|
661 |
|
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, uint maxSamplesPerCycle) { |
662 |
if (!pSample) { |
if (!pSample) { |
663 |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
664 |
return; |
return; |
671 |
// number of '0' samples (silence samples) behind the official buffer |
// number of '0' samples (silence samples) behind the official buffer |
672 |
// 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 |
673 |
// the sample. |
// the sample. |
|
const uint maxSamplesPerCycle = |
|
|
(pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() |
|
|
: DefaultMaxSamplesPerCycle(); |
|
674 |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
675 |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
676 |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |