/[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 2687 by schoenebeck, Sun Jan 4 17:16:05 2015 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 - 2013 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 219  namespace LinuxSampler { namespace gig { Line 208  namespace LinuxSampler { namespace gig {
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
# Line 246  namespace LinuxSampler { namespace gig { Line 235  namespace LinuxSampler { namespace gig {
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 =
# Line 282  namespace LinuxSampler { namespace gig { Line 271  namespace LinuxSampler { namespace gig {
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, "
# Line 322  namespace LinuxSampler { namespace gig { Line 312  namespace LinuxSampler { namespace gig {
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;
# Line 573  namespace LinuxSampler { namespace gig { Line 564  namespace LinuxSampler { namespace gig {
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
# Line 584  namespace LinuxSampler { namespace gig { Line 577  namespace LinuxSampler { namespace gig {
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();
# Line 600  namespace LinuxSampler { namespace gig { Line 593  namespace LinuxSampler { namespace gig {
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;
         Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine());  
         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();  
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);  
         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);  
         }  
     }  
   
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      }      }
# Line 683  namespace LinuxSampler { namespace gig { Line 654  namespace LinuxSampler { namespace gig {
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;
# Line 695  namespace LinuxSampler { namespace gig { Line 673  namespace LinuxSampler { namespace gig {
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) {

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

  ViewVC Help
Powered by ViewVC