/[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 2327 by persson, Sat Mar 10 16:16:14 2012 UTC revision 3719 by schoenebeck, Wed Jan 15 16:12:51 2020 UTC
# Line 1  Line 1 
1    
2  /***************************************************************************  /***************************************************************************
3   *                                                                         *   *                                                                         *
4   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
5   *                                                                         *   *                                                                         *
6   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
7   *   Copyright (C) 2005 - 2012 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2020 Christian Schoenebeck                       *
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 41  namespace LinuxSampler { namespace gig { Line 42  namespace LinuxSampler { namespace gig {
42      // we use this to react on events concerning an instrument on behalf of an instrument editor      // we use this to react on events concerning an instrument on behalf of an instrument editor
43      class InstrumentEditorProxy : public InstrumentConsumer {      class InstrumentEditorProxy : public InstrumentConsumer {
44      public:      public:
45            virtual ~InstrumentEditorProxy() {}
46    
47          virtual void ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {          virtual void ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {
48              //TODO: inform the instrument editor about the pending update              //TODO: inform the instrument editor about the pending update
49          }          }
# Line 69  namespace LinuxSampler { namespace gig { Line 72  namespace LinuxSampler { namespace gig {
72       *                    instrument ID       *                    instrument ID
73       */       */
74      void InstrumentResourceManager::OnInstrumentLoadingProgress(::gig::progress_t* pProgress) {      void InstrumentResourceManager::OnInstrumentLoadingProgress(::gig::progress_t* pProgress) {
75          dmsg(7,("gig::InstrumentResourceManager: progress %f%", pProgress->factor));          dmsg(7,("gig::InstrumentResourceManager: progress %f%%", pProgress->factor));
76          progress_callback_arg_t* pArg = static_cast<progress_callback_arg_t*>(pProgress->custom);          progress_callback_arg_t* pArg = static_cast<progress_callback_arg_t*>(pProgress->custom);
77          // we randomly schedule 90% for the .gig file loading and the remaining 10% later for sample caching          // we randomly schedule 90% for the .gig file loading and the remaining 10% later for sample caching
78          const float localProgress = 0.9f * pProgress->factor;          const float localProgress = 0.9f * pProgress->factor;
# Line 208  namespace LinuxSampler { namespace gig { Line 211  namespace LinuxSampler { namespace gig {
211          }          }
212      }      }
213    
214      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) {
215          const String sDataType    = GetInstrumentDataStructureName(ID);          const String sDataType    = GetInstrumentDataStructureName(ID);
216          const String sDataVersion = GetInstrumentDataStructureVersion(ID);          const String sDataVersion = GetInstrumentDataStructureVersion(ID);
217          // find instrument editors capable to handle given instrument          // find instrument editors capable to handle given instrument
# Line 235  namespace LinuxSampler { namespace gig { Line 238  namespace LinuxSampler { namespace gig {
238          InstrumentEditorProxies.add(pProxy);          InstrumentEditorProxies.add(pProxy);
239          InstrumentEditorProxiesMutex.Unlock();          InstrumentEditorProxiesMutex.Unlock();
240          // launch the instrument editor for the given instrument          // launch the instrument editor for the given instrument
241          pEditor->Launch(pInstrument, sDataType, sDataVersion, pUserData);          pEditor->Launch(pEngineChannel, pInstrument, sDataType, sDataVersion, pUserData);
242    
243          // register the instrument editor as virtual MIDI device as well ...          // register the instrument editor as virtual MIDI device as well ...
244          VirtualMidiDevice* pVirtualMidiDevice =          VirtualMidiDevice* pVirtualMidiDevice =
# Line 271  namespace LinuxSampler { namespace gig { Line 274  namespace LinuxSampler { namespace gig {
274          int iProxyIndex                = -1;          int iProxyIndex                = -1;
275    
276          // first find the editor proxy entry for this editor          // first find the editor proxy entry for this editor
277          InstrumentEditorProxiesMutex.Lock();          {
278          for (int i = 0; i < InstrumentEditorProxies.size(); i++) {              LockGuard lock(InstrumentEditorProxiesMutex);
279              InstrumentEditorProxy* pCurProxy =              for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
280                  dynamic_cast<InstrumentEditorProxy*>(                  InstrumentEditorProxy* pCurProxy =
281                      InstrumentEditorProxies[i]                      dynamic_cast<InstrumentEditorProxy*>(
282                  );                          InstrumentEditorProxies[i]
283              if (pCurProxy->pEditor == pSender) {                          );
284                  pProxy      = pCurProxy;                  if (pCurProxy->pEditor == pSender) {
285                  iProxyIndex = i;                      pProxy      = pCurProxy;
286                  pInstrument = pCurProxy->pInstrument;                      iProxyIndex = i;
287                        pInstrument = pCurProxy->pInstrument;
288                    }
289              }              }
290          }          }
         InstrumentEditorProxiesMutex.Unlock();  
291    
292          if (!pProxy) {          if (!pProxy) {
293              std::cerr << "Eeeek, could not find instrument editor proxy, "              std::cerr << "Eeeek, could not find instrument editor proxy, "
# Line 311  namespace LinuxSampler { namespace gig { Line 315  namespace LinuxSampler { namespace gig {
315    
316          // finally delete proxy entry and hand back instrument          // finally delete proxy entry and hand back instrument
317          if (pInstrument) {          if (pInstrument) {
318              InstrumentEditorProxiesMutex.Lock();              {
319              InstrumentEditorProxies.remove(iProxyIndex);                  LockGuard lock(InstrumentEditorProxiesMutex);
320              InstrumentEditorProxiesMutex.Unlock();                  InstrumentEditorProxies.remove(iProxyIndex);
321                }
322    
323              HandBack(pInstrument, pProxy);              HandBack(pInstrument, pProxy);
324              delete pProxy;              delete pProxy;
# Line 390  namespace LinuxSampler { namespace gig { Line 395  namespace LinuxSampler { namespace gig {
395      }      }
396    
397      void InstrumentResourceManager::OnDataStructureToBeChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) {      void InstrumentResourceManager::OnDataStructureToBeChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) {
398            dmsg(5,("gig::InstrumentResourceManager::OnDataStructureToBeChanged(%s)\n", sStructType.c_str()));
399          //TODO: remove code duplication          //TODO: remove code duplication
400          if (sStructType == "gig::File") {          if (sStructType == "gig::File") {
401              // completely suspend all engines that use that file              // completely suspend all engines that use that file
# Line 430  namespace LinuxSampler { namespace gig { Line 436  namespace LinuxSampler { namespace gig {
436              std::set<Engine*>::iterator end  = engines.end();              std::set<Engine*>::iterator end  = engines.end();
437              for (; iter != end; ++iter) (*iter)->Suspend(pRegion);              for (; iter != end; ++iter) (*iter)->Suspend(pRegion);
438              Unlock();              Unlock();
439            } else if (sStructType == "gig::Script") {
440                // no need to suspend anything here, since the sampler is
441                // processing a translated VM representation of the original script
442                // source code, not accessing the source code itself during playback
443                ::gig::Script* pScript = (::gig::Script*) pStruct;
444                // remember the original source code of the script, since the script
445                // resource manager uses the source code as key
446                pendingScriptUpdatesMutex.Lock();
447                pendingScriptUpdates[pScript] = pScript->GetScriptAsText();
448                pendingScriptUpdatesMutex.Unlock();
449          } else {          } else {
450              std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data "              std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data "
451                           "structure '" << sStructType << "' requested to be "                           "structure '" << sStructType << "' requested to be "
# Line 440  namespace LinuxSampler { namespace gig { Line 456  namespace LinuxSampler { namespace gig {
456      }      }
457    
458      void InstrumentResourceManager::OnDataStructureChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) {      void InstrumentResourceManager::OnDataStructureChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) {
459            dmsg(5,("gig::InstrumentResourceManager::OnDataStructureChanged(%s)\n", sStructType.c_str()));
460          //TODO: remove code duplication          //TODO: remove code duplication
461          if (sStructType == "gig::File") {          if (sStructType == "gig::File") {
462              // resume all previously suspended engines              // resume all previously suspended engines
# Line 497  namespace LinuxSampler { namespace gig { Line 514  namespace LinuxSampler { namespace gig {
514              std::set<Engine*>::iterator end  = engines.end();              std::set<Engine*>::iterator end  = engines.end();
515              for (; iter != end; ++iter) (*iter)->Resume(pRegion);              for (; iter != end; ++iter) (*iter)->Resume(pRegion);
516              Unlock();              Unlock();
517            } else if (sStructType == "gig::Script") {
518                // inform all engine channels which are using this script, that
519                // they need to reload (parse) the script's source code text
520                ::gig::Script* pScript = (::gig::Script*) pStruct;
521                pendingScriptUpdatesMutex.Lock();
522                if (pendingScriptUpdates.count(pScript)) {
523                    const String& code = pendingScriptUpdates[pScript];
524                    std::set<EngineChannel*> channels = GetEngineChannelsUsingScriptSourceCode(code, true/*lock*/);
525                    pendingScriptUpdates.erase(pScript);
526                    std::set<EngineChannel*>::iterator iter = channels.begin();
527                    std::set<EngineChannel*>::iterator end  = channels.end();
528                    for (; iter != end; ++iter) (*iter)->reloadScript(pScript);
529                }
530                pendingScriptUpdatesMutex.Unlock();
531          } else {          } else {
532              std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data "              std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data "
533                           "structure '" << sStructType << "' requested to be "                           "structure '" << sStructType << "' requested to be "
# Line 671  namespace LinuxSampler { namespace gig { Line 702  namespace LinuxSampler { namespace gig {
702              // number of '0' samples (silence samples) behind the official buffer              // number of '0' samples (silence samples) behind the official buffer
703              // 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
704              // the sample.              // the sample.
705              const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3;              const uint neededSilenceSamples = uint((maxSamplesPerCycle << CONFIG_MAX_PITCH) + 6);
706              const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize;              const uint currentlyCachedSilenceSamples = uint(pSample->GetCache().NullExtensionSize / pSample->FrameSize);
707              if (currentlyCachedSilenceSamples < neededSilenceSamples) {              if (currentlyCachedSilenceSamples < neededSilenceSamples) {
708                  dmsg(3,("Caching whole sample (sample name: \"%s\", sample size: %d)\n", pSample->pInfo->Name.c_str(), pSample->SamplesTotal));                  dmsg(3,("Caching whole sample (sample name: \"%s\", sample size: %llu)\n", pSample->pInfo->Name.c_str(), (long long)pSample->SamplesTotal));
709                  ::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(neededSilenceSamples);                  ::gig::buffer_t buf = pSample->LoadSampleDataWithNullSamplesExtension(neededSilenceSamples);
710                  dmsg(4,("Cached %d Bytes, %d silence bytes.\n", buf.Size, buf.NullExtensionSize));                  dmsg(4,("Cached %llu Bytes, %llu silence bytes.\n", (long long)buf.Size, (long long)buf.NullExtensionSize));
711              }              }
712          }          }
713          else { // we only cache CONFIG_PRELOAD_SAMPLES and stream the other sample points from disk          else { // we only cache CONFIG_PRELOAD_SAMPLES and stream the other sample points from disk
# Line 687  namespace LinuxSampler { namespace gig { Line 718  namespace LinuxSampler { namespace gig {
718      }      }
719    
720      void InstrumentResourceManager::UncacheInitialSamples(::gig::Sample* pSample) {      void InstrumentResourceManager::UncacheInitialSamples(::gig::Sample* pSample) {
721          dmsg(1,("Uncaching sample %x\n",pSample));          dmsg(1,("Uncaching sample %p\n",(void*)pSample));
722          if (pSample->GetCache().Size) pSample->ReleaseSampleData();          if (pSample->GetCache().Size) pSample->ReleaseSampleData();
723      }      }
724    
# Line 711  namespace LinuxSampler { namespace gig { Line 742  namespace LinuxSampler { namespace gig {
742          if (bLock) Unlock();          if (bLock) Unlock();
743          return result;          return result;
744      }      }
745    
746        /**
747         * Returns a list with all gig engine channels that are currently using
748         * the given real-time instrument script (provided as source code).
749         *
750         * @param pScript - search criteria
751         * @param bLock - whether we should lock (mutex) the instrument manager
752         *                during this call and unlock at the end of this call
753         */
754        std::set<EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsingScriptSourceCode(const String& code, bool bLock) {
755            if (bLock) Lock();
756            std::set<EngineChannel*> result;
757            std::set<InstrumentScriptConsumer*> consumers = scripts.ConsumersOf(code);
758            std::set<InstrumentScriptConsumer*>::iterator iter = consumers.begin();
759            std::set<InstrumentScriptConsumer*>::iterator end  = consumers.end();
760            for (; iter != end; ++iter) {
761                EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter);
762                if (!pEngineChannel) continue;
763                result.insert(pEngineChannel);
764            }
765            if (bLock) Unlock();
766            return result;
767        }
768    
769      /**      /**
770       * Returns a list with all gig engine channels that are currently using       * Returns a list with all gig engine channels that are currently using

Legend:
Removed from v.2327  
changed lines
  Added in v.3719

  ViewVC Help
Powered by ViewVC