/[svn]/linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 517 by schoenebeck, Sun May 8 00:26:21 2005 UTC revision 970 by schoenebeck, Wed Dec 6 22:28:17 2006 UTC
# Line 3  Line 3 
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  *
# Line 34  Line 34 
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      // some data needed for the libgig callback function
45      struct progress_callback_arg_t {      struct progress_callback_arg_t {
46          InstrumentResourceManager* pManager;          InstrumentResourceManager*          pManager;
47          instrument_id_t*           pInstrumentKey;          InstrumentManager::instrument_id_t* pInstrumentKey;
48      };      };
49    
50      /**      /**
# Line 57  namespace LinuxSampler { namespace gig { Line 64  namespace LinuxSampler { namespace gig {
64          pArg->pManager->DispatchResourceProgressEvent(*pArg->pInstrumentKey, localProgress);          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            Lock();
82            ::gig::Instrument* pInstrument = Resource(ID, false);
83            String res = (pInstrument) ? pInstrument->pInfo->Name : "";
84            Unlock();
85            return res;
86        }
87    
88      ::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) {      ::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) {
89          // get gig file from inernal gig file manager          // get gig file from inernal gig file manager
90          ::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 :/
91    
92          // we pass this to the progress callback mechanism of libgig          // we pass this to the progress callback mechanism of libgig
93          progress_callback_arg_t callbackArg;          progress_callback_arg_t callbackArg;
# Line 70  namespace LinuxSampler { namespace gig { Line 98  namespace LinuxSampler { namespace gig {
98          progress.callback = OnInstrumentLoadingProgress;          progress.callback = OnInstrumentLoadingProgress;
99          progress.custom   = &callbackArg;          progress.custom   = &callbackArg;
100    
101          dmsg(1,("Loading gig instrument..."));          dmsg(1,("Loading gig instrument ('%s',%d)...",Key.FileName.c_str(),Key.Index));
102          ::gig::Instrument* pInstrument = pGig->GetInstrument(Key.iInstrument, &progress);          ::gig::Instrument* pInstrument = pGig->GetInstrument(Key.Index, &progress);
103          if (!pInstrument) {          if (!pInstrument) {
104              std::stringstream msg;              std::stringstream msg;
105              msg << "There's no instrument with index " << Key.iInstrument << ".";              msg << "There's no instrument with index " << Key.Index << ".";
106              throw InstrumentResourceManagerException(msg.str());              throw InstrumentResourceManagerException(msg.str());
107          }          }
108          pGig->GetFirstSample(); // just to force complete instrument loading          pGig->GetFirstSample(); // just to force complete instrument loading
# Line 87  namespace LinuxSampler { namespace gig { Line 115  namespace LinuxSampler { namespace gig {
115          while (pRgn) {          while (pRgn) {
116              // we randomly schedule 90% for the .gig file loading and the remaining 10% now for sample caching              // we randomly schedule 90% for the .gig file loading and the remaining 10% now for sample caching
117              const float localProgress = 0.9f + 0.1f * (float) iRegion / (float) pInstrument->Regions;              const float localProgress = 0.9f + 0.1f * (float) iRegion / (float) pInstrument->Regions;
118              DispatchResourceProgressEvent(Key, localProgress);                          DispatchResourceProgressEvent(Key, localProgress);
119                
120              if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) {              if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) {
121                  dmsg(2,("C"));                  dmsg(2,("C"));
122                  CacheInitialSamples(pRgn->GetSample(), dynamic_cast<gig::EngineChannel*>(pConsumer));                  CacheInitialSamples(pRgn->GetSample(), (gig::EngineChannel*) pConsumer);
123              }              }
124              for (uint i = 0; i < pRgn->DimensionRegions; i++) {              for (uint i = 0; i < pRgn->DimensionRegions; i++) {
125                  CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, dynamic_cast<gig::EngineChannel*>(pConsumer));                  CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (gig::EngineChannel*) pConsumer);
126              }              }
127    
128              pRgn = pInstrument->GetNextRegion();              pRgn = pInstrument->GetNextRegion();
# Line 105  namespace LinuxSampler { namespace gig { Line 133  namespace LinuxSampler { namespace gig {
133    
134          // we need the following for destruction later          // we need the following for destruction later
135          instr_entry_t* pEntry = new instr_entry_t;          instr_entry_t* pEntry = new instr_entry_t;
136          pEntry->iInstrument   = Key.iInstrument;          pEntry->ID.FileName   = Key.FileName;
137            pEntry->ID.Index      = Key.Index;
138          pEntry->pGig          = pGig;          pEntry->pGig          = pGig;
139    
140          gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(pConsumer);          gig::EngineChannel* pEngineChannel = (gig::EngineChannel*) pConsumer;
141          // 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'
142          pEntry->MaxSamplesPerCycle =          pEntry->MaxSamplesPerCycle =
143              (pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle()              (pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle()
144                                            : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE;                                            : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE;
145          pArg = pEntry;          pArg = pEntry;
146    
# Line 120  namespace LinuxSampler { namespace gig { Line 149  namespace LinuxSampler { namespace gig {
149    
150      void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) {      void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) {
151          instr_entry_t* pEntry = (instr_entry_t*) pArg;          instr_entry_t* pEntry = (instr_entry_t*) pArg;
152          Gigs.HandBack(pEntry->pGig, (GigConsumer*) pEntry->iInstrument); // conversion kinda hackish :/          // we don't need the .gig file here anymore
153            Gigs.HandBack(pEntry->pGig, (GigConsumer*) pEntry->ID.Index); // conversion kinda hackish :/
154          delete pEntry;          delete pEntry;
155      }      }
156    
# Line 151  namespace LinuxSampler { namespace gig { Line 181  namespace LinuxSampler { namespace gig {
181          }          }
182          if (!pSample->SamplesTotal) return; // skip zero size samples          if (!pSample->SamplesTotal) return; // skip zero size samples
183    
184          if (pSample->SamplesTotal <= NUM_RAM_PRELOAD_SAMPLES) {          if (pSample->SamplesTotal <= CONFIG_PRELOAD_SAMPLES) {
185              // Sample is too short for disk streaming, so we load the whole              // Sample is too short for disk streaming, so we load the whole
186              // sample into RAM and place 'pAudioIO->FragmentSize << MAX_PITCH'              // sample into RAM and place 'pAudioIO->FragmentSize << CONFIG_MAX_PITCH'
187              // number of '0' samples (silence samples) behind the official buffer              // number of '0' samples (silence samples) behind the official buffer
188              // 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
189              // the sample.              // the sample.
190              const uint maxSamplesPerCycle =              const uint maxSamplesPerCycle =
191                  (pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle()                  (pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle()
192                                                : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE;                                                : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE;
193              const uint neededSilenceSamples = (maxSamplesPerCycle << MAX_PITCH) + 3;              const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3;
194              const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize;              const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize;
195              if (currentlyCachedSilenceSamples < neededSilenceSamples) {              if (currentlyCachedSilenceSamples < neededSilenceSamples) {
196                  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: %d)\n", pSample->pInfo->Name.c_str(), pSample->SamplesTotal));
# Line 168  namespace LinuxSampler { namespace gig { Line 198  namespace LinuxSampler { namespace gig {
198                  dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize));                  dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize));
199              }              }
200          }          }
201          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
202              if (!pSample->GetCache().Size) pSample->LoadSampleData(NUM_RAM_PRELOAD_SAMPLES);              if (!pSample->GetCache().Size) pSample->LoadSampleData(CONFIG_PRELOAD_SAMPLES);
203          }          }
204    
205          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;

Legend:
Removed from v.517  
changed lines
  Added in v.970

  ViewVC Help
Powered by ViewVC