/[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 1797 by persson, Thu Dec 4 19:33:13 2008 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 - 2008 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 24  Line 24 
24  #include <sstream>  #include <sstream>
25    
26  #include "InstrumentResourceManager.h"  #include "InstrumentResourceManager.h"
27    #include "EngineChannel.h"
28    #include "Engine.h"
29    
30  #include "../../common/global_private.h"  #include "../../common/global_private.h"
31  #include "../../plugins/InstrumentEditorFactory.h"  #include "../../plugins/InstrumentEditorFactory.h"
32    
 // We need to know the maximum number of sample points which are going to  
 // be processed for each render cycle of the audio output driver, to know  
 // how much initial sample points we need to cache into RAM. If the given  
 // sampler channel does not have an audio output device assigned yet  
 // though, we simply use this default value.  
 #define GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE     128  
   
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 88  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();  
     }  
   
     InstrumentManager::mode_t InstrumentResourceManager::GetMode(const instrument_id_t& ID) {  
         return static_cast<InstrumentManager::mode_t>(AvailabilityMode(ID));  
     }  
   
     void InstrumentResourceManager::SetMode(const instrument_id_t& ID, InstrumentManager::mode_t Mode) {  
         dmsg(2,("gig::InstrumentResourceManager: setting mode for %s (Index=%d) to %d\n",ID.FileName.c_str(),ID.Index,Mode));  
         SetAvailabilityMode(ID, static_cast<ResourceManager<InstrumentManager::instrument_id_t, ::gig::Instrument>::mode_t>(Mode));  
     }  
   
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 154  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
136                  pInstrument = gig->GetInstrument(ID.Index);                  pInstrument = gig->GetInstrument(ID.Index);
137              }              }
138                
139              if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName);              if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName);
140    
141              instrument_info_t info;              instrument_info_t info;
# Line 233  namespace LinuxSampler { namespace gig { Line 208  namespace LinuxSampler { namespace gig {
208          }          }
209      }      }
210    
211      void InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException) {      InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(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 260  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);          pEditor->Launch(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 =
242              dynamic_cast<VirtualMidiDevice*>(pEditor);              dynamic_cast<VirtualMidiDevice*>(pEditor);
243          if (!pVirtualMidiDevice) {          if (!pVirtualMidiDevice) {
244              std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush;              std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush;
245              return;              return pEditor;
246          }          }
247          // NOTE: for now connect the virtual MIDI keyboard of the instrument editor (if any) with all engine channels that have the same instrument as the editor was opened for ( other ideas ? )          // NOTE: for now connect the virtual MIDI keyboard of the instrument editor (if any) with all engine channels that have the same instrument as the editor was opened for ( other ideas ? )
248          Lock();          Lock();
249          std::set<gig::EngineChannel*> engineChannels =          std::set<EngineChannel*> engineChannels =
250              GetEngineChannelsUsing(pInstrument, false/*don't lock again*/);              GetEngineChannelsUsing(pInstrument, false/*don't lock again*/);
251          std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin();          std::set<EngineChannel*>::iterator iter = engineChannels.begin();
252          std::set<gig::EngineChannel*>::iterator end  = engineChannels.end();          std::set<EngineChannel*>::iterator end  = engineChannels.end();
253          for (; iter != end; ++iter) (*iter)->Connect(pVirtualMidiDevice);          for (; iter != end; ++iter) (static_cast<AbstractEngineChannel*>(*iter))->Connect(pVirtualMidiDevice);
254          Unlock();          Unlock();
255    
256            return pEditor;
257      }      }
258    
259      /**      /**
# Line 320  namespace LinuxSampler { namespace gig { Line 297  namespace LinuxSampler { namespace gig {
297          if (pVirtualMidiDevice) {          if (pVirtualMidiDevice) {
298              Lock();              Lock();
299              // NOTE: see note in LaunchInstrumentEditor()              // NOTE: see note in LaunchInstrumentEditor()
300              std::set<gig::EngineChannel*> engineChannels =              std::set<EngineChannel*> engineChannels =
301                  GetEngineChannelsUsing(pInstrument, false/*don't lock again*/);                  GetEngineChannelsUsing(pInstrument, false/*don't lock again*/);
302              std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin();              std::set<EngineChannel*>::iterator iter = engineChannels.begin();
303              std::set<gig::EngineChannel*>::iterator end  = engineChannels.end();              std::set<EngineChannel*>::iterator end  = engineChannels.end();
304              for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice);              for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice);
305              Unlock();              Unlock();
306          } else {          } else {
# Line 430  namespace LinuxSampler { namespace gig { Line 407  namespace LinuxSampler { namespace gig {
407              ::gig::Instrument* pInstrument =              ::gig::Instrument* pInstrument =
408                  (::gig::Instrument*) pRegion->GetParent();                  (::gig::Instrument*) pRegion->GetParent();
409              Lock();              Lock();
410              std::set<gig::Engine*> engines =              std::set<Engine*> engines =
411                  GetEnginesUsing(pInstrument, false/*don't lock again*/);                  GetEnginesUsing(pInstrument, false/*don't lock again*/);
412              std::set<gig::Engine*>::iterator iter = engines.begin();              std::set<Engine*>::iterator iter = engines.begin();
413              std::set<gig::Engine*>::iterator end  = engines.end();              std::set<Engine*>::iterator end  = engines.end();
414              for (; iter != end; ++iter) (*iter)->Suspend(pRegion);              for (; iter != end; ++iter) (*iter)->Suspend(pRegion);
415              Unlock();              Unlock();
416          } else if (sStructType == "gig::DimensionRegion") {          } else if (sStructType == "gig::DimensionRegion") {
# Line 447  namespace LinuxSampler { namespace gig { Line 424  namespace LinuxSampler { namespace gig {
424              ::gig::Instrument* pInstrument =              ::gig::Instrument* pInstrument =
425                  (::gig::Instrument*) pRegion->GetParent();                  (::gig::Instrument*) pRegion->GetParent();
426              Lock();              Lock();
427              std::set<gig::Engine*> engines =              std::set<Engine*> engines =
428                  GetEnginesUsing(pInstrument, false/*don't lock again*/);                  GetEnginesUsing(pInstrument, false/*don't lock again*/);
429              std::set<gig::Engine*>::iterator iter = engines.begin();              std::set<Engine*>::iterator iter = engines.begin();
430              std::set<gig::Engine*>::iterator end  = engines.end();              std::set<Engine*>::iterator end  = engines.end();
431              for (; iter != end; ++iter) (*iter)->Suspend(pRegion);              for (; iter != end; ++iter) (*iter)->Suspend(pRegion);
432              Unlock();              Unlock();
433          } else {          } else {
# Line 470  namespace LinuxSampler { namespace gig { Line 447  namespace LinuxSampler { namespace gig {
447          } else if (sStructType == "gig::Instrument") {          } else if (sStructType == "gig::Instrument") {
448              // resume all previously suspended engines              // resume all previously suspended engines
449              ResumeAllEngines();              ResumeAllEngines();
450            } else if (sStructType == "gig::Sample") {
451                // we're assuming here, that OnDataStructureToBeChanged() with
452                // "gig::File" was called previously, so we won't resume anything
453                // here, but just re-cache the given sample
454                Lock();
455                ::gig::Sample* pSample = (::gig::Sample*) pStruct;
456                ::gig::File* pFile = (::gig::File*) pSample->GetParent();
457                UncacheInitialSamples(pSample);
458                // now re-cache ...
459                std::vector< ::gig::Instrument*> instruments =
460                    GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/);
461                for (int i = 0; i < instruments.size(); i++) {
462                    if (SampleReferencedByInstrument(pSample, instruments[i])) {
463                        std::set<EngineChannel*> engineChannels =
464                            GetEngineChannelsUsing(instruments[i], false/*don't lock again*/);
465                        std::set<EngineChannel*>::iterator iter = engineChannels.begin();
466                        std::set<EngineChannel*>::iterator end  = engineChannels.end();
467                        for (; iter != end; ++iter)
468                            CacheInitialSamples(pSample, *iter);
469                    }
470                }
471                Unlock();
472          } else if (sStructType == "gig::Region") {          } else if (sStructType == "gig::Region") {
473              // advice the engines to resume the given region, that is to              // advice the engines to resume the given region, that is to
474              // using it for playback again              // using it for playback again
# Line 477  namespace LinuxSampler { namespace gig { Line 476  namespace LinuxSampler { namespace gig {
476              ::gig::Instrument* pInstrument =              ::gig::Instrument* pInstrument =
477                  (::gig::Instrument*) pRegion->GetParent();                  (::gig::Instrument*) pRegion->GetParent();
478              Lock();              Lock();
479              std::set<gig::Engine*> engines =              std::set<Engine*> engines =
480                  GetEnginesUsing(pInstrument, false/*don't lock again*/);                  GetEnginesUsing(pInstrument, false/*don't lock again*/);
481              std::set<gig::Engine*>::iterator iter = engines.begin();              std::set<Engine*>::iterator iter = engines.begin();
482              std::set<gig::Engine*>::iterator end  = engines.end();              std::set<Engine*>::iterator end  = engines.end();
483              for (; iter != end; ++iter) (*iter)->Resume(pRegion);              for (; iter != end; ++iter) (*iter)->Resume(pRegion);
484              Unlock();              Unlock();
485          } else if (sStructType == "gig::DimensionRegion") {          } else if (sStructType == "gig::DimensionRegion") {
# Line 492  namespace LinuxSampler { namespace gig { Line 491  namespace LinuxSampler { namespace gig {
491              ::gig::Instrument* pInstrument =              ::gig::Instrument* pInstrument =
492                  (::gig::Instrument*) pRegion->GetParent();                  (::gig::Instrument*) pRegion->GetParent();
493              Lock();              Lock();
494              std::set<gig::Engine*> engines =              std::set<Engine*> engines =
495                  GetEnginesUsing(pInstrument, false/*don't lock again*/);                  GetEnginesUsing(pInstrument, false/*don't lock again*/);
496              std::set<gig::Engine*>::iterator iter = engines.begin();              std::set<Engine*>::iterator iter = engines.begin();
497              std::set<gig::Engine*>::iterator end  = engines.end();              std::set<Engine*>::iterator end  = engines.end();
498              for (; iter != end; ++iter) (*iter)->Resume(pRegion);              for (; iter != end; ++iter) (*iter)->Resume(pRegion);
499              Unlock();              Unlock();
500          } else {          } else {
# Line 513  namespace LinuxSampler { namespace gig { Line 512  namespace LinuxSampler { namespace gig {
512              Lock();              Lock();
513              ::gig::Sample* pSample = (::gig::Sample*) pOldSample;              ::gig::Sample* pSample = (::gig::Sample*) pOldSample;
514              ::gig::File* pFile = (::gig::File*) pSample->GetParent();              ::gig::File* pFile = (::gig::File*) pSample->GetParent();
515                bool bSampleStillInUse = false;
516              std::vector< ::gig::Instrument*> instruments =              std::vector< ::gig::Instrument*> instruments =
517                  GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/);                  GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/);
518              for (int i = 0; i < instruments.size(); i++)              for (int i = 0; i < instruments.size(); i++) {
519                  if (!SampleReferencedByInstrument(pSample, instruments[i]))                  if (SampleReferencedByInstrument(pSample, instruments[i])) {
520                      UncacheInitialSamples(pSample);                      bSampleStillInUse = true;
521                        break;
522                    }
523                }
524                if (!bSampleStillInUse) UncacheInitialSamples(pSample);
525              Unlock();              Unlock();
526          }          }
527          // make sure new sample reference is cached          // make sure new sample reference is cached
# Line 526  namespace LinuxSampler { namespace gig { Line 530  namespace LinuxSampler { namespace gig {
530              ::gig::Sample* pSample = (::gig::Sample*) pNewSample;              ::gig::Sample* pSample = (::gig::Sample*) pNewSample;
531              ::gig::File* pFile = (::gig::File*) pSample->GetParent();              ::gig::File* pFile = (::gig::File*) pSample->GetParent();
532              // get all engines that use that same gig::File              // get all engines that use that same gig::File
533              std::set<gig::Engine*> engines = GetEnginesUsing(pFile, false/*don't lock again*/);              std::set<Engine*> engines = GetEnginesUsing(pFile, false/*don't lock again*/);
534              std::set<gig::Engine*>::iterator iter = engines.begin();              std::set<Engine*>::iterator iter = engines.begin();
535              std::set<gig::Engine*>::iterator end  = engines.end();              std::set<Engine*>::iterator end  = engines.end();
536              for (; iter != end; ++iter)              for (; iter != end; ++iter)
537                  CacheInitialSamples(pSample, *iter);                  CacheInitialSamples(pSample, *iter);
538              Unlock();              Unlock();
# Line 537  namespace LinuxSampler { namespace gig { Line 541  namespace LinuxSampler { namespace gig {
541    
542      ::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) {      ::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) {
543          // get gig file from internal gig file manager          // get gig file from internal gig file manager
544          ::gig::File* pGig = Gigs.Borrow(Key.FileName, (GigConsumer*) Key.Index); // conversion kinda hackish :/          ::gig::File* pGig = Gigs.Borrow(Key.FileName, reinterpret_cast<GigConsumer*>(Key.Index)); // conversion kinda hackish :/
545    
546          // we pass this to the progress callback mechanism of libgig          // we pass this to the progress callback mechanism of libgig
547          progress_callback_arg_t callbackArg;          progress_callback_arg_t callbackArg;
# Line 558  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 569  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(), (gig::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, (gig::EngineChannel*) pConsumer);                  CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, maxSamplesPerCycle);
582              }              }
583    
584              pRgn = pInstrument->GetNextRegion();              pRgn = pInstrument->GetNextRegion();
# Line 585  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          gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(pConsumer);          // (try to resolve the audio device context)
597          // 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 an engine with higher value of 'MaxSamplesPerSecond'
598          pEntry->MaxSamplesPerCycle =          pEntry->MaxSamplesPerCycle = maxSamplesPerCycle;
599              (!pEngineChannel) ? 0 /* don't care for instrument editors */ :          
                 (pEngineChannel->GetEngine()) ?  
                     dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle()  
                     : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE;  
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, (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    
612      void InstrumentResourceManager::OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg) {      void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) {
613          instr_entry_t* pEntry = (instr_entry_t*) pArg;          // TODO: we could delete Region and Instrument here if they have become unused
         gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(pConsumer);  
         uint maxSamplesPerCycle =  
             (pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle()  
                                           : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE;  
         if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) {  
             Update(pResource, pConsumer);  
         }  
     }  
   
     /**  
      * Give back an instrument. This should be used instead of  
      * HandBack if there are some dimension regions that are still in  
      * use. (When an instrument is changed, the voices currently  
      * playing are allowed to keep playing with the old instrument  
      * until note off arrives. New notes will use the new instrument.)  
      */  
     void InstrumentResourceManager::HandBackInstrument(::gig::Instrument* pResource, InstrumentConsumer* pConsumer,  
                                                        RTList< ::gig::DimensionRegion*>* pDimRegionsInUse) {  
         DimRegInfoMutex.Lock();  
         for (RTList< ::gig::DimensionRegion*>::Iterator i = pDimRegionsInUse->first() ; i != pDimRegionsInUse->end() ; i++) {  
             DimRegInfo[*i].refCount++;  
             SampleRefCount[(*i)->pSample]++;  
         }  
         HandBack(pResource, pConsumer, true);  
         DimRegInfoMutex.Unlock();  
614      }      }
615    
616      /**      void InstrumentResourceManager::DeleteSampleIfNotUsed(::gig::Sample* pSample, region_info_t* pRegInfo) {
617       * Give back a dimension region that belongs to an instrument that          ::gig::File* gig = pRegInfo->file;
618       * was previously handed back.          ::RIFF::File* riff = static_cast< ::RIFF::File*>(pRegInfo->pArg);
619       */          if (gig) {
620      void InstrumentResourceManager::HandBackDimReg(::gig::DimensionRegion* pDimReg) {              gig->DeleteSample(pSample);
621          DimRegInfoMutex.Lock();              if (!gig->GetFirstSample()) {
622          dimreg_info_t& dimRegInfo = DimRegInfo[pDimReg];                  dmsg(2,("No more samples in use - freeing gig\n"));
623          int dimRegRefCount = --dimRegInfo.refCount;                  delete gig;
624          int sampleRefCount = --SampleRefCount[pDimReg->pSample];                  delete riff;
         if (dimRegRefCount == 0) {  
             ::gig::File* gig = dimRegInfo.file;  
             ::RIFF::File* riff = dimRegInfo.riff;  
             DimRegInfo.erase(pDimReg);  
             // TODO: we could delete Region and Instrument here if  
             // they have become unused  
   
             if (sampleRefCount == 0) {  
                 SampleRefCount.erase(pDimReg->pSample);  
   
                 if (gig) {  
                     gig->DeleteSample(pDimReg->pSample);  
                     if (!gig->GetFirstSample()) {  
                         dmsg(2,("No more samples in use - freeing gig\n"));  
                         delete gig;  
                         delete riff;  
                     }  
                 }  
625              }              }
626          }          }
         DimRegInfoMutex.Unlock();  
627      }      }
628    
629      /**      /**
# Line 675  namespace LinuxSampler { namespace gig { Line 634  namespace LinuxSampler { namespace gig {
634       *                   (may be NULL, in this case default amount of samples       *                   (may be NULL, in this case default amount of samples
635       *                   will be cached)       *                   will be cached)
636       */       */
637      void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel) {      void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, EngineChannel* pEngineChannel) {
638          gig::Engine* pEngine =          Engine* pEngine =
639              (pEngineChannel && pEngineChannel->GetEngine()) ?              (pEngineChannel && pEngineChannel->GetEngine()) ?
640                  dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine()) : NULL;                  dynamic_cast<Engine*>(pEngineChannel->GetEngine()) : NULL;
641          CacheInitialSamples(pSample, pEngine);          CacheInitialSamples(pSample, pEngine);
642      }      }
643    
# Line 693  namespace LinuxSampler { namespace gig { Line 652  namespace LinuxSampler { namespace gig {
652       *                   (may be NULL, in this case default amount of samples       *                   (may be NULL, in this case default amount of samples
653       *                   will be cached)       *                   will be cached)
654       */       */
655      void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::Engine* 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 706  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()  
                           : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE;  
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) {
# Line 758  namespace LinuxSampler { namespace gig { Line 721  namespace LinuxSampler { namespace gig {
721       * @param bLock - whether we should lock (mutex) the instrument manager       * @param bLock - whether we should lock (mutex) the instrument manager
722       *                during this call and unlock at the end of this call       *                during this call and unlock at the end of this call
723       */       */
724      std::set<gig::EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) {      std::set<EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) {
725          if (bLock) Lock();          if (bLock) Lock();
726          std::set<gig::EngineChannel*> result;          std::set<EngineChannel*> result;
727          std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);          std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);
728          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();
729          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();
730          for (; iter != end; ++iter) {          for (; iter != end; ++iter) {
731              gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter);              EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter);
732              if (!pEngineChannel) continue;              if (!pEngineChannel) continue;
733              result.insert(pEngineChannel);              result.insert(pEngineChannel);
734          }          }
# Line 781  namespace LinuxSampler { namespace gig { Line 744  namespace LinuxSampler { namespace gig {
744       * @param bLock - whether we should lock (mutex) the instrument manager       * @param bLock - whether we should lock (mutex) the instrument manager
745       *                during this call and unlock at the end of this call       *                during this call and unlock at the end of this call
746       */       */
747      std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) {      std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) {
748          if (bLock) Lock();          if (bLock) Lock();
749          std::set<gig::Engine*> result;          std::set<Engine*> result;
750          std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);          std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);
751          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();
752          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();
753          for (; iter != end; ++iter) {          for (; iter != end; ++iter) {
754              gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter);              EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter);
755              if (!pEngineChannel) continue;              if (!pEngineChannel) continue;
756              gig::Engine* pEngine = dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine());              Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine());
757              if (!pEngine) continue;              if (!pEngine) continue;
758              result.insert(pEngine);              result.insert(pEngine);
759          }          }
# Line 806  namespace LinuxSampler { namespace gig { Line 769  namespace LinuxSampler { namespace gig {
769       * @param bLock - whether we should lock (mutex) the instrument manager       * @param bLock - whether we should lock (mutex) the instrument manager
770       *                during this call and unlock at the end of this call       *                during this call and unlock at the end of this call
771       */       */
772      std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::File* pFile, bool bLock) {      std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::File* pFile, bool bLock) {
773          if (bLock) Lock();          if (bLock) Lock();
774          // get all instruments (currently in usage) that use that same gig::File          // get all instruments (currently in usage) that use that same gig::File
775          std::vector< ::gig::Instrument*> instrumentsOfInterest =          std::vector< ::gig::Instrument*> instrumentsOfInterest =
776              GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/);              GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/);
777    
778          // get all engines that use that same gig::File          // get all engines that use that same gig::File
779          std::set<gig::Engine*> result;          std::set<Engine*> result;
780          {          {
781              for (int i = 0; i < instrumentsOfInterest.size(); i++) {              for (int i = 0; i < instrumentsOfInterest.size(); i++) {
782                  std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(instrumentsOfInterest[i]);                  std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(instrumentsOfInterest[i]);
783                  std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();                  std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();
784                  std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();                  std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();
785                  for (; iter != end; ++iter) {                  for (; iter != end; ++iter) {
786                      gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter);                      EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter);
787                      if (!pEngineChannel) continue;                      if (!pEngineChannel) continue;
788                      gig::Engine* pEngine = dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine());                      Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine());
789                      if (!pEngine) continue;                      if (!pEngine) continue;
790                      // the unique, sorted container std::set makes                      // the unique, sorted container std::set makes
791                      // sure we won't have duplicates                      // sure we won't have duplicates
# Line 876  namespace LinuxSampler { namespace gig { Line 839  namespace LinuxSampler { namespace gig {
839          // get all engines that use that same gig::Instrument          // get all engines that use that same gig::Instrument
840          suspendedEngines = GetEnginesUsing(pInstrument, true/*lock*/);          suspendedEngines = GetEnginesUsing(pInstrument, true/*lock*/);
841          // finally, completely suspend all engines that use that same gig::Instrument          // finally, completely suspend all engines that use that same gig::Instrument
842          std::set<gig::Engine*>::iterator iter = suspendedEngines.begin();          std::set<Engine*>::iterator iter = suspendedEngines.begin();
843          std::set<gig::Engine*>::iterator end  = suspendedEngines.end();          std::set<Engine*>::iterator end  = suspendedEngines.end();
844          for (; iter != end; ++iter) (*iter)->SuspendAll();          for (; iter != end; ++iter) (*iter)->SuspendAll();
845      }      }
846    
# Line 900  namespace LinuxSampler { namespace gig { Line 863  namespace LinuxSampler { namespace gig {
863          // get all engines that use that same gig::File          // get all engines that use that same gig::File
864          suspendedEngines = GetEnginesUsing(pFile, true/*lock*/);          suspendedEngines = GetEnginesUsing(pFile, true/*lock*/);
865          // finally, completely suspend all engines that use that same gig::File          // finally, completely suspend all engines that use that same gig::File
866          std::set<gig::Engine*>::iterator iter = suspendedEngines.begin();          std::set<Engine*>::iterator iter = suspendedEngines.begin();
867          std::set<gig::Engine*>::iterator end  = suspendedEngines.end();          std::set<Engine*>::iterator end  = suspendedEngines.end();
868          for (; iter != end; ++iter) (*iter)->SuspendAll();          for (; iter != end; ++iter) (*iter)->SuspendAll();
869      }      }
870    
# Line 936  namespace LinuxSampler { namespace gig { Line 899  namespace LinuxSampler { namespace gig {
899      }      }
900    
901      void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) {      void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) {
902          dmsg(1,("Freeing gig file from memory..."));          dmsg(1,("Freeing gig file '%s' from memory ...", pResource->GetFileName().c_str()));
903    
904          // Delete as much as possible of the gig file. Some of the          // Delete as much as possible of the gig file. Some of the
905          // dimension regions and samples may still be in use - these          // dimension regions and samples may still be in use - these
# Line 957  namespace LinuxSampler { namespace gig { Line 920  namespace LinuxSampler { namespace gig {
920                  for (int i = 0 ; i < region->DimensionRegions ; i++)                  for (int i = 0 ; i < region->DimensionRegions ; i++)
921                  {                  {
922                      ::gig::DimensionRegion *d = region->pDimensionRegions[i];                      ::gig::DimensionRegion *d = region->pDimensionRegions[i];
923                      std::map< ::gig::DimensionRegion*, dimreg_info_t>::iterator iter = parent->DimRegInfo.find(d);                      std::map< ::gig::DimensionRegion*, region_info_t>::iterator iter = parent->RegionInfo.find(d);
924                      if (iter != parent->DimRegInfo.end()) {                      if (iter != parent->RegionInfo.end()) {
925                          dimreg_info_t& dimRegInfo = (*iter).second;                          region_info_t& dimRegInfo = (*iter).second;
926                          dimRegInfo.file = pResource;                          dimRegInfo.file = pResource;
927                          dimRegInfo.riff = (::RIFF::File*)pArg;                          dimRegInfo.pArg = (::RIFF::File*)pArg;
928                          deleteFile = deleteInstrument = deleteRegion = false;                          deleteFile = deleteInstrument = deleteRegion = false;
929                      }                      }
930                  }                  }

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

  ViewVC Help
Powered by ViewVC