/[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 1659 by schoenebeck, Sun Feb 3 00:13:27 2008 UTC revision 1852 by schoenebeck, Sun Mar 1 22:22:03 2009 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 - 2009 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 146  namespace LinuxSampler { namespace gig { Line 146  namespace LinuxSampler { namespace gig {
146      }      }
147    
148      InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) {      InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) {
149          std::vector<instrument_id_t> result;          Lock();
150            ::gig::Instrument* pInstrument = Resource(ID, false);
151            bool loaded = (pInstrument != NULL);
152            if (!loaded) Unlock();
153    
154          ::RIFF::File* riff = NULL;          ::RIFF::File* riff = NULL;
155          ::gig::File*  gig  = NULL;          ::gig::File*  gig  = NULL;
156          try {          try {
157              riff = new ::RIFF::File(ID.FileName);              if(!loaded) {
158              gig  = new ::gig::File(riff);                  riff = new ::RIFF::File(ID.FileName);
159              gig->SetAutoLoad(false); // avoid time consuming samples scanning                  gig  = new ::gig::File(riff);
160              ::gig::Instrument* pInstrument = gig->GetInstrument(ID.Index);                  gig->SetAutoLoad(false); // avoid time consuming samples scanning
161                    pInstrument = gig->GetInstrument(ID.Index);
162                }
163    
164              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);
165    
166              instrument_info_t info;              instrument_info_t info;
167              if (gig->pVersion) {              for (int i = 0; i < 128; i++) { info.KeyBindings[i] = info.KeySwitchBindings[i] = 0; }
168                  info.FormatVersion = ToString(gig->pVersion->major);  
169                  info.Product = gig->pInfo->Product;              ::gig::File* pFile = (::gig::File*) pInstrument->GetParent();
170                  info.Artists = gig->pInfo->Artists;  
171                if (pFile->pVersion) {
172                    info.FormatVersion = ToString(pFile->pVersion->major);
173                    info.Product = pFile->pInfo->Product;
174                    info.Artists = pFile->pInfo->Artists;
175              }              }
176    
177              info.InstrumentName = pInstrument->pInfo->Name;              info.InstrumentName = pInstrument->pInfo->Name;
178    
179                ::gig::Region* pRegion = pInstrument->GetFirstRegion();
180                while (pRegion) {
181                    int low = pRegion->KeyRange.low;
182                    int high = pRegion->KeyRange.high;
183                    if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) {
184                        std::cerr << "Invalid key range: " << low << " - " << high << std::endl;
185                    } else {
186                        for (int i = low; i <= high; i++) info.KeyBindings[i] = 1;
187                    }
188    
189                    pRegion = pInstrument->GetNextRegion();
190                }
191    
192                if (loaded) { // retrieve keyswitching only if the instrument is fully loaded.
193    
194                    // only return keyswitch range if keyswitching is used
195                    bool hasKeyswitches = false;
196                    for (::gig::Region* pRegion = pInstrument->GetFirstRegion() ;
197                         pRegion && !hasKeyswitches ;
198                         pRegion = pInstrument->GetNextRegion()) {
199                        for (int i = 0 ; i < pRegion->Dimensions ; i++) {
200                            if (pRegion->pDimensionDefinitions[i].dimension == ::gig::dimension_keyboard) {
201                                hasKeyswitches = true;
202                                break;
203                            }
204                        }
205                    }
206    
207                    if (hasKeyswitches) {
208                        int low = pInstrument->DimensionKeyRange.low;
209                        int high = pInstrument->DimensionKeyRange.high;
210                        if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) {
211                            std::cerr << "Invalid keyswitch range: " << low << " - " << high << std::endl;
212                        } else {
213                            for (int i = low; i <= high; i++) info.KeySwitchBindings[i] = 1;
214                        }
215                    }
216                }
217    
218                if (loaded) Unlock();
219    
220              if (gig)  delete gig;              if (gig)  delete gig;
221              if (riff) delete riff;              if (riff) delete riff;
222              return info;              return info;
223          } catch (::RIFF::Exception e) {          } catch (::RIFF::Exception e) {
224                if (loaded) Unlock();
225              if (gig)  delete gig;              if (gig)  delete gig;
226              if (riff) delete riff;              if (riff) delete riff;
227              throw InstrumentManagerException(e.Message);              throw InstrumentManagerException(e.Message);
228          } catch (...) {          } catch (...) {
229                if (loaded) Unlock();
230              if (gig)  delete gig;              if (gig)  delete gig;
231              if (riff) delete riff;              if (riff) delete riff;
232              throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'");              throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'");
# Line 290  namespace LinuxSampler { namespace gig { Line 347  namespace LinuxSampler { namespace gig {
347          // dies.          // dies.
348      }      }
349    
350    #if 0 // currently unused :
351      /**      /**
352       * Try to inform the respective instrument editor(s), that a note on       * Try to inform the respective instrument editor(s), that a note on
353       * event just occured. This method is called by the MIDI thread. If any       * event just occured. This method is called by the MIDI thread. If any
# Line 331  namespace LinuxSampler { namespace gig { Line 389  namespace LinuxSampler { namespace gig {
389          }          }
390          InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation          InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation
391      }      }
392    #endif // unused
393    
394      void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) {      void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) {
395          if (Samples.empty()) {          if (Samples.empty()) {
# Line 411  namespace LinuxSampler { namespace gig { Line 470  namespace LinuxSampler { namespace gig {
470          } else if (sStructType == "gig::Instrument") {          } else if (sStructType == "gig::Instrument") {
471              // resume all previously suspended engines              // resume all previously suspended engines
472              ResumeAllEngines();              ResumeAllEngines();
473            } else if (sStructType == "gig::Sample") {
474                // we're assuming here, that OnDataStructureToBeChanged() with
475                // "gig::File" was called previously, so we won't resume anything
476                // here, but just re-cache the given sample
477                Lock();
478                ::gig::Sample* pSample = (::gig::Sample*) pStruct;
479                ::gig::File* pFile = (::gig::File*) pSample->GetParent();
480                UncacheInitialSamples(pSample);
481                // now re-cache ...
482                std::vector< ::gig::Instrument*> instruments =
483                    GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/);
484                for (int i = 0; i < instruments.size(); i++) {
485                    if (SampleReferencedByInstrument(pSample, instruments[i])) {
486                        std::set<gig::EngineChannel*> engineChannels =
487                            GetEngineChannelsUsing(instruments[i], false/*don't lock again*/);
488                        std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin();
489                        std::set<gig::EngineChannel*>::iterator end  = engineChannels.end();
490                        for (; iter != end; ++iter)
491                            CacheInitialSamples(pSample, *iter);
492                    }
493                }
494                Unlock();
495          } else if (sStructType == "gig::Region") {          } else if (sStructType == "gig::Region") {
496              // advice the engines to resume the given region, that is to              // advice the engines to resume the given region, that is to
497              // using it for playback again              // using it for playback again
# Line 454  namespace LinuxSampler { namespace gig { Line 535  namespace LinuxSampler { namespace gig {
535              Lock();              Lock();
536              ::gig::Sample* pSample = (::gig::Sample*) pOldSample;              ::gig::Sample* pSample = (::gig::Sample*) pOldSample;
537              ::gig::File* pFile = (::gig::File*) pSample->GetParent();              ::gig::File* pFile = (::gig::File*) pSample->GetParent();
538                bool bSampleStillInUse = false;
539              std::vector< ::gig::Instrument*> instruments =              std::vector< ::gig::Instrument*> instruments =
540                  GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/);                  GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/);
541              for (int i = 0; i < instruments.size(); i++)              for (int i = 0; i < instruments.size(); i++) {
542                  if (!SampleReferencedByInstrument(pSample, instruments[i]))                  if (SampleReferencedByInstrument(pSample, instruments[i])) {
543                      UncacheInitialSamples(pSample);                      bSampleStillInUse = true;
544                        break;
545                    }
546                }
547                if (!bSampleStillInUse) UncacheInitialSamples(pSample);
548              Unlock();              Unlock();
549          }          }
550          // make sure new sample reference is cached          // make sure new sample reference is cached
# Line 701  namespace LinuxSampler { namespace gig { Line 787  namespace LinuxSampler { namespace gig {
787       */       */
788      std::set<gig::EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) {      std::set<gig::EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) {
789          if (bLock) Lock();          if (bLock) Lock();
790          std::set<gig::EngineChannel*> result;          std::set<gig::EngineChannel*> result;
791          std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);          std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);
792          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();
793          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();
# Line 724  namespace LinuxSampler { namespace gig { Line 810  namespace LinuxSampler { namespace gig {
810       */       */
811      std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) {      std::set<gig::Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) {
812          if (bLock) Lock();          if (bLock) Lock();
813          std::set<gig::Engine*> result;          std::set<gig::Engine*> result;
814          std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);          std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);
815          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();
816          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();          std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();

Legend:
Removed from v.1659  
changed lines
  Added in v.1852

  ViewVC Help
Powered by ViewVC