/[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 2309 by iliev, Mon Jan 30 10:40:19 2012 UTC revision 2326 by persson, Thu Mar 8 19:40:14 2012 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 - 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  *
# Line 32  Line 32 
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;
# Line 83  namespace LinuxSampler { namespace gig { Line 76  namespace LinuxSampler { namespace gig {
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);
# Line 140  namespace LinuxSampler { namespace gig { Line 129  namespace LinuxSampler { namespace gig {
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
# Line 573  namespace LinuxSampler { namespace gig { Line 562  namespace LinuxSampler { namespace gig {
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
# Line 584  namespace LinuxSampler { namespace gig { Line 575  namespace LinuxSampler { namespace gig {
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();
# Line 600  namespace LinuxSampler { namespace gig { Line 591  namespace LinuxSampler { namespace gig {
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)          // (try to resolve the audio device context)
597          EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(pConsumer);          // and we save this to check if we need to reallocate for an engine with higher value of 'MaxSamplesPerSecond'
598          Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine());          pEntry->MaxSamplesPerCycle = maxSamplesPerCycle;
         AudioOutputDevice* pDevice = (pEngine) ? pEngine->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();  
599                    
600          pArg = pEntry;          pArg = pEntry;
601    
602          return pInstrument;          return pInstrument;
603      }      }
604    
605      void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) {      void InstrumentResourceManager::Destroy(::gig::Instrument* pResource, void* pArg) {
606          instr_entry_t* pEntry = (instr_entry_t*) pArg;          instr_entry_t* pEntry = (instr_entry_t*) pArg;
607          // we don't need the .gig file here anymore          // we don't need the .gig file here anymore
608          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 :/
609          delete pEntry;          delete pEntry;
610      }      }
611    
     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);  
         Engine* pEngine = pEngineChannel ? dynamic_cast<Engine*>(pEngineChannel->GetEngine()) : NULL;  
         AudioOutputDevice* pDevice = (pEngine) ? pEngine->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);  
         }  
     }  
   
612      void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) {      void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) {
613          // 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
614      }      }
# Line 683  namespace LinuxSampler { namespace gig { Line 653  namespace LinuxSampler { namespace gig {
653       *                   will be cached)       *                   will be cached)
654       */       */
655      void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine) {      void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine) {
656            uint maxSamplesPerCycle =
657                (pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle() :
658                DefaultMaxSamplesPerCycle();
659            CacheInitialSamples(pSample, maxSamplesPerCycle);
660        }
661    
662        void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, uint maxSamplesPerCycle) {
663          if (!pSample) {          if (!pSample) {
664              dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n"));              dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n"));
665              return;              return;
# Line 695  namespace LinuxSampler { namespace gig { Line 672  namespace LinuxSampler { namespace gig {
672              // number of '0' samples (silence samples) behind the official buffer              // number of '0' samples (silence samples) behind the official buffer
673              // 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
674              // the sample.              // the sample.
             const uint maxSamplesPerCycle =  
                 (pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle()  
                           : DefaultMaxSamplesPerCycle();  
675              const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3;              const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3;
676              const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize;              const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize;
677              if (currentlyCachedSilenceSamples < neededSilenceSamples) {              if (currentlyCachedSilenceSamples < neededSilenceSamples) {

Legend:
Removed from v.2309  
changed lines
  Added in v.2326

  ViewVC Help
Powered by ViewVC