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 Christian Schoenebeck * |
* Copyright (C) 2005, 2006 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 * |
34 |
|
|
35 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
36 |
|
|
37 |
|
// data stored as long as an instrument resource exists |
38 |
|
struct instr_entry_t { |
39 |
|
InstrumentManager::instrument_id_t ID; |
40 |
|
::gig::File* pGig; |
41 |
|
uint MaxSamplesPerCycle; ///< if some engine requests an already allocated instrument with a higher value, we have to reallocate the instrument |
42 |
|
}; |
43 |
|
|
44 |
|
// some data needed for the libgig callback function |
45 |
|
struct progress_callback_arg_t { |
46 |
|
InstrumentResourceManager* pManager; |
47 |
|
InstrumentManager::instrument_id_t* pInstrumentKey; |
48 |
|
}; |
49 |
|
|
50 |
|
/** |
51 |
|
* Callback function which will be called by libgig during loading of |
52 |
|
* instruments to inform about the current progress. Or to be more |
53 |
|
* specific; it will be called during the GetInstrument() call. |
54 |
|
* |
55 |
|
* @param pProgress - contains current progress value, pointer to the |
56 |
|
* InstrumentResourceManager instance and |
57 |
|
* instrument ID |
58 |
|
*/ |
59 |
|
void InstrumentResourceManager::OnInstrumentLoadingProgress(::gig::progress_t* pProgress) { |
60 |
|
dmsg(7,("gig::InstrumentResourceManager: progress %f%", pProgress->factor)); |
61 |
|
progress_callback_arg_t* pArg = static_cast<progress_callback_arg_t*>(pProgress->custom); |
62 |
|
// we randomly schedule 90% for the .gig file loading and the remaining 10% later for sample caching |
63 |
|
const float localProgress = 0.9f * pProgress->factor; |
64 |
|
pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress); |
65 |
|
} |
66 |
|
|
67 |
|
std::vector<InstrumentResourceManager::instrument_id_t> InstrumentResourceManager::Instruments() { |
68 |
|
return Entries(); |
69 |
|
} |
70 |
|
|
71 |
|
InstrumentManager::mode_t InstrumentResourceManager::GetMode(const instrument_id_t& ID) { |
72 |
|
return static_cast<InstrumentManager::mode_t>(AvailabilityMode(ID)); |
73 |
|
} |
74 |
|
|
75 |
|
void InstrumentResourceManager::SetMode(const instrument_id_t& ID, InstrumentManager::mode_t Mode) { |
76 |
|
dmsg(2,("gig::InstrumentResourceManager: setting mode for %s (Index=%d) to %d\n",ID.FileName.c_str(),ID.Index,Mode)); |
77 |
|
SetAvailabilityMode(ID, static_cast<ResourceManager<InstrumentManager::instrument_id_t, ::gig::Instrument>::mode_t>(Mode)); |
78 |
|
} |
79 |
|
|
80 |
|
String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) { |
81 |
|
::gig::Instrument* pInstrument = Resource(ID); |
82 |
|
return (pInstrument) ? pInstrument->pInfo->Name : ""; |
83 |
|
} |
84 |
|
|
85 |
::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { |
::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) { |
86 |
// get gig file from inernal gig file manager |
// get gig file from inernal gig file manager |
87 |
::gig::File* pGig = Gigs.Borrow(Key.FileName, (GigConsumer*) Key.iInstrument); // conversion kinda hackish :/ |
::gig::File* pGig = Gigs.Borrow(Key.FileName, (GigConsumer*) Key.Index); // conversion kinda hackish :/ |
88 |
|
|
89 |
dmsg(1,("Loading gig instrument...")); |
// we pass this to the progress callback mechanism of libgig |
90 |
::gig::Instrument* pInstrument = pGig->GetInstrument(Key.iInstrument); |
progress_callback_arg_t callbackArg; |
91 |
|
callbackArg.pManager = this; |
92 |
|
callbackArg.pInstrumentKey = &Key; |
93 |
|
|
94 |
|
::gig::progress_t progress; |
95 |
|
progress.callback = OnInstrumentLoadingProgress; |
96 |
|
progress.custom = &callbackArg; |
97 |
|
|
98 |
|
dmsg(1,("Loading gig instrument ('%s',%d)...",Key.FileName.c_str(),Key.Index)); |
99 |
|
::gig::Instrument* pInstrument = pGig->GetInstrument(Key.Index, &progress); |
100 |
if (!pInstrument) { |
if (!pInstrument) { |
101 |
std::stringstream msg; |
std::stringstream msg; |
102 |
msg << "There's no instrument with index " << Key.iInstrument << "."; |
msg << "There's no instrument with index " << Key.Index << "."; |
103 |
throw InstrumentResourceManagerException(msg.str()); |
throw InstrumentResourceManagerException(msg.str()); |
104 |
} |
} |
105 |
pGig->GetFirstSample(); // just to force complete instrument loading |
pGig->GetFirstSample(); // just to force complete instrument loading |
107 |
|
|
108 |
// cache initial samples points (for actually needed samples) |
// cache initial samples points (for actually needed samples) |
109 |
dmsg(1,("Caching initial samples...")); |
dmsg(1,("Caching initial samples...")); |
110 |
|
uint iRegion = 0; // just for progress calculation |
111 |
::gig::Region* pRgn = pInstrument->GetFirstRegion(); |
::gig::Region* pRgn = pInstrument->GetFirstRegion(); |
112 |
while (pRgn) { |
while (pRgn) { |
113 |
|
// we randomly schedule 90% for the .gig file loading and the remaining 10% now for sample caching |
114 |
|
const float localProgress = 0.9f + 0.1f * (float) iRegion / (float) pInstrument->Regions; |
115 |
|
DispatchResourceProgressEvent(Key, localProgress); |
116 |
|
|
117 |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) { |
118 |
dmsg(2,("C")); |
dmsg(2,("C")); |
119 |
CacheInitialSamples(pRgn->GetSample(), dynamic_cast<gig::EngineChannel*>(pConsumer)); |
CacheInitialSamples(pRgn->GetSample(), (gig::EngineChannel*) pConsumer); |
120 |
} |
} |
121 |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
for (uint i = 0; i < pRgn->DimensionRegions; i++) { |
122 |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, dynamic_cast<gig::EngineChannel*>(pConsumer)); |
CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (gig::EngineChannel*) pConsumer); |
123 |
} |
} |
124 |
|
|
125 |
pRgn = pInstrument->GetNextRegion(); |
pRgn = pInstrument->GetNextRegion(); |
126 |
|
iRegion++; |
127 |
} |
} |
128 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
129 |
|
DispatchResourceProgressEvent(Key, 1.0f); // done; notify all consumers about progress 100% |
130 |
|
|
131 |
// we need the following for destruction later |
// we need the following for destruction later |
132 |
instr_entry_t* pEntry = new instr_entry_t; |
instr_entry_t* pEntry = new instr_entry_t; |
133 |
pEntry->iInstrument = Key.iInstrument; |
pEntry->ID.FileName = Key.FileName; |
134 |
|
pEntry->ID.Index = Key.Index; |
135 |
pEntry->pGig = pGig; |
pEntry->pGig = pGig; |
136 |
|
|
137 |
gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(pConsumer); |
gig::EngineChannel* pEngineChannel = (gig::EngineChannel*) pConsumer; |
138 |
// and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond' |
// and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond' |
139 |
pEntry->MaxSamplesPerCycle = |
pEntry->MaxSamplesPerCycle = |
140 |
(pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() |
(pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() |
141 |
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
142 |
pArg = pEntry; |
pArg = pEntry; |
143 |
|
|
146 |
|
|
147 |
void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) { |
void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) { |
148 |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
instr_entry_t* pEntry = (instr_entry_t*) pArg; |
149 |
Gigs.HandBack(pEntry->pGig, (GigConsumer*) pEntry->iInstrument); // conversion kinda hackish :/ |
// we don't need the .gig file here anymore |
150 |
|
Gigs.HandBack(pEntry->pGig, (GigConsumer*) pEntry->ID.Index); // conversion kinda hackish :/ |
151 |
delete pEntry; |
delete pEntry; |
152 |
} |
} |
153 |
|
|
172 |
* @param pEngineChannel - pointer to Gig Engine Channel which caused this call |
* @param pEngineChannel - pointer to Gig Engine Channel which caused this call |
173 |
*/ |
*/ |
174 |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel) { |
void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel) { |
175 |
if (!pSample || pSample->GetCache().Size || !pSample->SamplesTotal) return; |
if (!pSample) { |
176 |
if (pSample->SamplesTotal <= NUM_RAM_PRELOAD_SAMPLES) { |
dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n")); |
177 |
|
return; |
178 |
|
} |
179 |
|
if (!pSample->SamplesTotal) return; // skip zero size samples |
180 |
|
|
181 |
|
if (pSample->SamplesTotal <= CONFIG_PRELOAD_SAMPLES) { |
182 |
// Sample is too short for disk streaming, so we load the whole |
// Sample is too short for disk streaming, so we load the whole |
183 |
// sample into RAM and place 'pAudioIO->FragmentSize << MAX_PITCH' |
// sample into RAM and place 'pAudioIO->FragmentSize << CONFIG_MAX_PITCH' |
184 |
// number of '0' samples (silence samples) behind the official buffer |
// number of '0' samples (silence samples) behind the official buffer |
185 |
// 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 |
186 |
// the sample. |
// the sample. |
|
dmsg(3,("Caching whole sample (sample name: \"%s\", sample size: %d)\n", pSample->pInfo->Name.c_str(), pSample->SamplesTotal)); |
|
187 |
const uint maxSamplesPerCycle = |
const uint maxSamplesPerCycle = |
188 |
(pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() |
(pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle() |
189 |
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
: GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE; |
190 |
const uint silenceSamples = (maxSamplesPerCycle << MAX_PITCH) + 3; |
const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3; |
191 |
::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(silenceSamples); |
const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize; |
192 |
dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize)); |
if (currentlyCachedSilenceSamples < neededSilenceSamples) { |
193 |
|
dmsg(3,("Caching whole sample (sample name: \"%s\", sample size: %d)\n", pSample->pInfo->Name.c_str(), pSample->SamplesTotal)); |
194 |
|
::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(neededSilenceSamples); |
195 |
|
dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize)); |
196 |
|
} |
197 |
} |
} |
198 |
else { // we only cache NUM_RAM_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 |
199 |
pSample->LoadSampleData(NUM_RAM_PRELOAD_SAMPLES); |
if (!pSample->GetCache().Size) pSample->LoadSampleData(CONFIG_PRELOAD_SAMPLES); |
200 |
} |
} |
201 |
|
|
202 |
if (!pSample->GetCache().Size) std::cerr << "Unable to cache sample - maybe memory full!" << std::endl << std::flush; |
if (!pSample->GetCache().Size) std::cerr << "Unable to cache sample - maybe memory full!" << std::endl << std::flush; |