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

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

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

revision 2325 by schoenebeck, Mon Sep 19 21:48:45 2011 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 - 2008 Christian Schoenebeck                       *
7   *   Copyright (C) 2009 - 2011 Grigor Iliev                                *   *   Copyright (C) 2009 - 2012 Christian Schoenebeck and Grigor Iliev      *
8   *                                                                         *   *                                                                         *
9   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
10   *   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 35  Line 35 
35    
36  namespace LinuxSampler { namespace sfz {  namespace LinuxSampler { namespace sfz {
37    
     // data stored as long as an instrument resource exists  
     struct instr_entry_t {  
         InstrumentManager::instrument_id_t ID;  
         ::sfz::File*                       pSfz;  
         uint                               MaxSamplesPerCycle; ///< if some engine requests an already allocated instrument with a higher value, we have to reallocate the instrument  
     };  
   
     // some data needed for the libgig callback function  
     struct progress_callback_arg_t {  
         InstrumentResourceManager*          pManager;  
         InstrumentManager::instrument_id_t* pInstrumentKey;  
     };  
   
     std::vector<InstrumentResourceManager::instrument_id_t> InstrumentResourceManager::Instruments() {  
         return Entries();  
     }  
   
38      String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) {      String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) {
39          Lock();          Lock();
40          ::sfz::Instrument* pInstrument = Resource(ID, false);          ::sfz::Instrument* pInstrument = Resource(ID, false);
# Line 78  namespace LinuxSampler { namespace sfz { Line 61  namespace LinuxSampler { namespace sfz {
61    
62          ::sfz::File*  sfz  = NULL;          ::sfz::File*  sfz  = NULL;
63          try {          try {
64              if(!loaded) {              if (!loaded) {
65                  sfz  = new ::sfz::File(ID.FileName);                  sfz  = new ::sfz::File(ID.FileName);
66                  pInstrument = sfz->GetInstrument();                  pInstrument = sfz->GetInstrument();
67              }              }
# Line 129  namespace LinuxSampler { namespace sfz { Line 112  namespace LinuxSampler { namespace sfz {
112          // cache initial samples points (for actually needed samples)          // cache initial samples points (for actually needed samples)
113          dmsg(1,("Caching initial samples..."));          dmsg(1,("Caching initial samples..."));
114          int regionCount = pInstrument->regions.size();          int regionCount = pInstrument->regions.size();
115          for(int i = 0 ; i < regionCount; i++) {          uint maxSamplesPerCycle = GetMaxSamplesPerCycle(pConsumer);
116              const float localProgress = (float) i / (float) regionCount;          for (int i = 0 ; i < regionCount ; i++) {
117                float localProgress = (float) i / (float) regionCount;
118              DispatchResourceProgressEvent(Key, localProgress);              DispatchResourceProgressEvent(Key, localProgress);
119              CacheInitialSamples(pInstrument->regions[i]->GetSample(), dynamic_cast<AbstractEngineChannel*>(pConsumer));              CacheInitialSamples(pInstrument->regions[i]->GetSample(), maxSamplesPerCycle);
120              //pInstrument->regions[i]->GetSample()->Close();              //pInstrument->regions[i]->GetSample()->Close();
121          }          }
122          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));
# Line 142  namespace LinuxSampler { namespace sfz { Line 126  namespace LinuxSampler { namespace sfz {
126          instr_entry_t* pEntry = new instr_entry_t;          instr_entry_t* pEntry = new instr_entry_t;
127          pEntry->ID.FileName   = Key.FileName;          pEntry->ID.FileName   = Key.FileName;
128          pEntry->ID.Index      = Key.Index;          pEntry->ID.Index      = Key.Index;
129          pEntry->pSfz          = pSfz;          pEntry->pFile         = pSfz;
130    
131          // (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'
132          EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(pConsumer);          pEntry->MaxSamplesPerCycle = maxSamplesPerCycle;
         AudioOutputDevice* pDevice =  
             (pEngineChannel) ? dynamic_cast<Engine*>(pEngineChannel->GetEngine())->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();  
133                    
134          pArg = pEntry;          pArg = pEntry;
135    
136          return pInstrument;          return pInstrument;
137      }      }
138    
139      void InstrumentResourceManager::Destroy( ::sfz::Instrument* pResource, void* pArg) {      void InstrumentResourceManager::Destroy(::sfz::Instrument* pResource, void* pArg) {
140          instr_entry_t* pEntry = (instr_entry_t*) pArg;          instr_entry_t* pEntry = (instr_entry_t*) pArg;
141          // we don't need the .sfz file here anymore          // we don't need the .sfz file here anymore
142          Sfzs.HandBack(pEntry->pSfz, reinterpret_cast<SfzConsumer*>(pEntry->ID.Index)); // conversion kinda hackish :/          Sfzs.HandBack(pEntry->pFile, reinterpret_cast<SfzConsumer*>(pEntry->ID.Index)); // conversion kinda hackish :/
143          delete pEntry;          delete pEntry;
144      }      }
145    
     void InstrumentResourceManager::OnBorrow(::sfz::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);  
         AudioOutputDevice* pDevice =  
             (pEngineChannel) ? dynamic_cast<Engine*>(pEngineChannel->GetEngine())->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);  
         }  
     }  
   
146      void InstrumentResourceManager::DeleteRegionIfNotUsed(::sfz::Region* pRegion, region_info_t* pRegInfo) {      void InstrumentResourceManager::DeleteRegionIfNotUsed(::sfz::Region* pRegion, region_info_t* pRegInfo) {
147          ::sfz::File* file = pRegInfo->file;          ::sfz::File* file = pRegInfo->file;
148          if (file == NULL) return;          if (file == NULL) return;
149    
150          file->GetInstrument()->DestroyRegion(pRegion);          file->GetInstrument()->DestroyRegion(pRegion);
151          if(file->GetInstrument()->regions.empty()) {          if (file->GetInstrument()->regions.empty()) {
152              dmsg(2,("No more regions in use - freeing sfz\n"));              dmsg(2,("No more regions in use - freeing sfz\n"));
153              delete file;              delete file;
154          }          }
# Line 229  namespace LinuxSampler { namespace sfz { Line 190  namespace LinuxSampler { namespace sfz {
190              }              }
191          }          }
192    
193          if(deleteInstrument) delete pResource;          if (deleteInstrument) delete pResource;
194          else dmsg(2,("keeping some samples that are in use..."));          else dmsg(2,("keeping some samples that are in use..."));
195    
196          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));

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

  ViewVC Help
Powered by ViewVC