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

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

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

revision 2012 by iliev, Fri Oct 23 17:53:17 2009 UTC revision 2027 by iliev, Tue Nov 3 19:27:42 2009 UTC
# Line 23  Line 23 
23   ***************************************************************************/   ***************************************************************************/
24    
25  #include <sstream>  #include <sstream>
 #include <sf2/SF.h>  
26    
27  #include "InstrumentResourceManager.h"  #include "InstrumentResourceManager.h"
28  #include "EngineChannel.h"  #include "EngineChannel.h"
# Line 55  namespace LinuxSampler { namespace sf2 { Line 54  namespace LinuxSampler { namespace sf2 {
54    
55      String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) {      String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) {
56          Lock();          Lock();
57          ::sf2::InstrumentBase* pInstrument = Resource(ID, false);          ::sf2::Preset* pInstrument = Resource(ID, false);
58          String res = (pInstrument) ? pInstrument->GetName() : "";          String res = (pInstrument) ? pInstrument->GetName() : "";
59          Unlock();          Unlock();
60          return res;          return res;
# Line 90  namespace LinuxSampler { namespace sf2 { Line 89  namespace LinuxSampler { namespace sf2 {
89    
90      InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) {      InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) {
91          Lock();          Lock();
92          ::sf2::InstrumentBase* pInstrument = Resource(ID, false);          ::sf2::Preset* pInstrument = Resource(ID, false);
93          bool loaded = (pInstrument != NULL);          bool loaded = (pInstrument != NULL);
94          if (!loaded) Unlock();          if (!loaded) Unlock();
95    
# Line 117  namespace LinuxSampler { namespace sf2 { Line 116  namespace LinuxSampler { namespace sf2 {
116              for (int i = 0; i < pInstrument->GetRegionCount(); i++) {              for (int i = 0; i < pInstrument->GetRegionCount(); i++) {
117                  int low = pInstrument->GetRegion(i)->loKey;                  int low = pInstrument->GetRegion(i)->loKey;
118                  int high = pInstrument->GetRegion(i)->hiKey;                  int high = pInstrument->GetRegion(i)->hiKey;
119                  if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) {                  if (low == ::sf2::NONE || high == ::sf2::NONE) {
120                      std::cerr << "Invalid key range: " << low << " - " << high << std::endl;                      ::sf2::Instrument* layer = pInstrument->GetRegion(i)->pInstrument;
121                        for (int j = 0; j < layer->GetRegionCount(); j++) {
122                            int lo = layer->GetRegion(j)->loKey;
123                            int hi = layer->GetRegion(j)->hiKey;
124                            SetKeyBindings(info.KeyBindings, lo, hi, ::sf2::NONE);
125                        }
126                  } else {                  } else {
127                      for (int i = low; i <= high; i++) info.KeyBindings[i] = 1;                      SetKeyBindings(info.KeyBindings, low, high, ::sf2::NONE);
128                  }                  }
129              }              }
130    
# Line 142  namespace LinuxSampler { namespace sf2 { Line 146  namespace LinuxSampler { namespace sf2 {
146          }          }
147      }      }
148    
149      ::sf2::InstrumentBase* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) {      ::sf2::Preset* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) {
150          // get sfz file from internal sfz file manager          // get sfz file from internal sfz file manager
151          ::sf2::File* pSf2 = Sf2s.Borrow(Key.FileName, (Sf2Consumer*) Key.Index); // conversion kinda hackish :/          ::sf2::File* pSf2 = Sf2s.Borrow(Key.FileName, (Sf2Consumer*) Key.Index); // conversion kinda hackish :/
152    
153          dmsg(1,("Loading sf2 instrument ('%s',%d)...",Key.FileName.c_str(),Key.Index));          dmsg(1,("Loading sf2 instrument ('%s',%d)...",Key.FileName.c_str(),Key.Index));
154          ::sf2::InstrumentBase* pInstrument = GetSfInstrument(pSf2, Key.Index);          ::sf2::Preset* pInstrument = GetSfInstrument(pSf2, Key.Index);
155          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));
156    
157          // cache initial samples points (for actually needed samples)          // cache initial samples points (for actually needed samples)
# Line 156  namespace LinuxSampler { namespace sf2 { Line 160  namespace LinuxSampler { namespace sf2 {
160          for(int i = 0 ; i < pInstrument->GetRegionCount(); i++) {          for(int i = 0 ; i < pInstrument->GetRegionCount(); i++) {
161              ::sf2::Instrument* sf2Instr = pInstrument->GetRegion(i)->pInstrument;              ::sf2::Instrument* sf2Instr = pInstrument->GetRegion(i)->pInstrument;
162              if(sf2Instr) regTotal += sf2Instr->GetRegionCount();              if(sf2Instr) regTotal += sf2Instr->GetRegionCount();
             else regTotal++; // if sf2Instr is null, than pInstrument is not a preset  
163          }          }
164          for(int i = 0 ; i < pInstrument->GetRegionCount(); i++) {          for(int i = 0 ; i < pInstrument->GetRegionCount(); i++) {
165              ::sf2::Instrument* sf2Instr = pInstrument->GetRegion(i)->pInstrument;              ::sf2::Instrument* sf2Instr = pInstrument->GetRegion(i)->pInstrument;
             ::sf2::Sample* sf2Sample = pInstrument->GetRegion(i)->GetSample();  
166              if(sf2Instr != NULL) {              if(sf2Instr != NULL) {
167                  // pInstrument is ::sf2::Preset                  // pInstrument is ::sf2::Preset
168                  for(int j = 0; j < sf2Instr->GetRegionCount(); j++) {                  for(int j = 0; j < sf2Instr->GetRegionCount(); j++) {
# Line 168  namespace LinuxSampler { namespace sf2 { Line 170  namespace LinuxSampler { namespace sf2 {
170                      DispatchResourceProgressEvent(Key, localProgress);                      DispatchResourceProgressEvent(Key, localProgress);
171                      CacheInitialSamples(sf2Instr->GetRegion(j)->GetSample(), dynamic_cast<AbstractEngineChannel*>(pConsumer));                      CacheInitialSamples(sf2Instr->GetRegion(j)->GetSample(), dynamic_cast<AbstractEngineChannel*>(pConsumer));
172                  }                  }
             } else if(sf2Sample != NULL) {  
                 // pInstrument is ::sf2::Instrument  
                 CacheInitialSamples(sf2Sample, dynamic_cast<AbstractEngineChannel*>(pConsumer));  
173              }              }
174          }          }
175          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));
# Line 194  namespace LinuxSampler { namespace sf2 { Line 193  namespace LinuxSampler { namespace sf2 {
193          return pInstrument;          return pInstrument;
194      }      }
195    
196      void InstrumentResourceManager::Destroy( ::sf2::InstrumentBase* pResource, void* pArg) {      void InstrumentResourceManager::Destroy( ::sf2::Preset* pResource, void* pArg) {
197          instr_entry_t* pEntry = (instr_entry_t*) pArg;          instr_entry_t* pEntry = (instr_entry_t*) pArg;
198          // we don't need the .sf2 file here anymore          // we don't need the .sf2 file here anymore
199          Sf2s.HandBack(pEntry->pSf2, (Sf2Consumer*) pEntry->ID.Index); // conversion kinda hackish :/          Sf2s.HandBack(pEntry->pSf2, (Sf2Consumer*) pEntry->ID.Index); // conversion kinda hackish :/
200          delete pEntry;          delete pEntry;
201      }      }
202    
203      void InstrumentResourceManager::OnBorrow(::sf2::InstrumentBase* pResource, InstrumentConsumer* pConsumer, void*& pArg) {      void InstrumentResourceManager::OnBorrow(::sf2::Preset* pResource, InstrumentConsumer* pConsumer, void*& pArg) {
204          instr_entry_t* pEntry = (instr_entry_t*) pArg;          instr_entry_t* pEntry = (instr_entry_t*) pArg;
205          EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(pConsumer);          EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(pConsumer);
206          uint maxSamplesPerCycle =          uint maxSamplesPerCycle =
# Line 245  namespace LinuxSampler { namespace sf2 { Line 244  namespace LinuxSampler { namespace sf2 {
244      void InstrumentResourceManager::Sf2ResourceManager::Destroy(::sf2::File* pResource, void* pArg) {      void InstrumentResourceManager::Sf2ResourceManager::Destroy(::sf2::File* pResource, void* pArg) {
245          dmsg(1,("Freeing sf2 file from memory..."));          dmsg(1,("Freeing sf2 file from memory..."));
246    
247        /*  // Delete as much as possible of the sfz file. Some of the          // Delete as much as possible of the sf2 file. Some of the
248          // regions and samples may still be in use - these          // regions and samples may still be in use - these
249          // will be deleted later by the HandBackRegion function.          // will be deleted later by the HandBackRegion function.
250          bool deleteInstrument = true;          bool deleteFile = true;
         ::sf2::Instrument* pInstr = pResource->GetInstrument();  
251    
252          for (int i = pInstr->regions.size() - 1; i >= 0 ; i--) {          for (int i = pResource->GetInstrumentCount() - 1; i >= 0; i--) {
253              ::sfz::Region* pRegion = pInstr->regions[i];              ::sf2::Instrument* pInstr = pResource->GetInstrument(i);
254              std::map< ::sfz::Region*, region_info_t>::iterator iter = parent->RegionInfo.find(pRegion);              bool deleteInstrument = true;
255              if (iter != parent->RegionInfo.end()) {  
256                  region_info_t& regInfo = (*iter).second;              for (int j = pInstr->GetRegionCount() - 1; j >= 0 ; j--) {
257                  regInfo.file = pResource;                  ::sf2::Region* pRegion = pInstr->GetRegion(j);
258                  deleteInstrument = false;                  std::map< ::sf2::Region*, region_info_t>::iterator iter = parent->RegionInfo.find(pRegion);
259              } else {                  if (iter != parent->RegionInfo.end()) {
260                  SampleFile* sf = pRegion->GetSample(false);                      region_info_t& regInfo = (*iter).second;
261                  if (sf != NULL) pInstr->GetSampleManager()->RemoveSampleConsumer(sf, pRegion);                      regInfo.file = pResource;
262                  if (sf == NULL || !pInstr->GetSampleManager()->HasSampleConsumers(sf)) pInstr->DestroyRegion(pRegion);                      deleteFile = deleteInstrument = false;
263                    } else {
264                        pInstr->DeleteRegion(pRegion);
265                    }
266              }              }
267    
268                if (deleteInstrument) pResource->DeleteInstrument(pInstr);
269          }          }
270    
271          if(deleteInstrument) delete pResource;          if (deleteFile) {
272          else dmsg(2,("keeping some samples that are in use..."));*/              delete pResource;
273                delete (::RIFF::File*) pArg;
274            } else {
275                dmsg(2,("keeping some samples that are in use..."));
276                for (int i = pResource->GetSampleCount() - 1; i >= 0; i--) {
277                    ::sf2::Sample* sample = pResource->GetSample(i);
278                    if (parent->SampleRefCount.find(sample) == parent->SampleRefCount.end()) {
279                        pResource->DeleteSample(sample);
280                    }
281                }
282            }
283    
284          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));
285      }      }
286    
287      int InstrumentResourceManager::GetSfInstrumentCount(::sf2::File* pFile) {      int InstrumentResourceManager::GetSfInstrumentCount(::sf2::File* pFile) {
288          return pFile->GetInstrumentCount() + pFile->GetPresetCount();          return pFile->GetPresetCount();
289      }      }
290    
291      ::sf2::InstrumentBase* InstrumentResourceManager::GetSfInstrument(::sf2::File* pFile, int idx) {      ::sf2::Preset* InstrumentResourceManager::GetSfInstrument(::sf2::File* pFile, int idx) {
292          if (idx >= pFile->GetInstrumentCount() + pFile->GetPresetCount()) {          if (idx >= pFile->GetPresetCount()) {
293              throw InstrumentManagerException("There is no instrument with index " + ToString(idx));              throw InstrumentManagerException("There is no instrument with index " + ToString(idx));
294          }          }
295            return pFile->GetPreset(idx);
         if (idx < pFile->GetInstrumentCount()) {  
             return pFile->GetInstrument(idx);  
         }  
           
         int presetIdx = idx - pFile->GetInstrumentCount();  
         return pFile->GetPreset(presetIdx);  
296      }      }
297    
298  }} // namespace LinuxSampler::sfz  }} // namespace LinuxSampler::sfz

Legend:
Removed from v.2012  
changed lines
  Added in v.2027

  ViewVC Help
Powered by ViewVC