/[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 1525 by schoenebeck, Sun Nov 25 17:47:46 2007 UTC revision 1653 by schoenebeck, Wed Jan 30 01:51:46 2008 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 - 2007 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2008 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 67  namespace LinuxSampler { namespace gig { Line 67  namespace LinuxSampler { namespace gig {
67    
68          // the instrument we borrowed on behalf of the editor          // the instrument we borrowed on behalf of the editor
69          ::gig::Instrument* pInstrument;          ::gig::Instrument* pInstrument;
70            // the instrument editor we work on behalf
71            InstrumentEditor* pEditor;
72      };      };
73    
74      /**      /**
# Line 196  namespace LinuxSampler { namespace gig { Line 198  namespace LinuxSampler { namespace gig {
198          ::gig::Instrument* pInstrument = Borrow(ID, pProxy);          ::gig::Instrument* pInstrument = Borrow(ID, pProxy);
199          // remember the proxy and instrument for this instrument editor          // remember the proxy and instrument for this instrument editor
200          pProxy->pInstrument = pInstrument;          pProxy->pInstrument = pInstrument;
201            pProxy->pEditor     = pEditor;
202          InstrumentEditorProxiesMutex.Lock();          InstrumentEditorProxiesMutex.Lock();
203          InstrumentEditorProxies[pEditor] = pProxy;          InstrumentEditorProxies.add(pProxy);
204          InstrumentEditorProxiesMutex.Unlock();          InstrumentEditorProxiesMutex.Unlock();
205          // launch the instrument editor for the given instrument          // launch the instrument editor for the given instrument
206          pEditor->Launch(pInstrument, sDataType, sDataVersion);          pEditor->Launch(pInstrument, sDataType, sDataVersion);
# Line 214  namespace LinuxSampler { namespace gig { Line 217  namespace LinuxSampler { namespace gig {
217          dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n"));          dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n"));
218          // hand back instrument and free proxy          // hand back instrument and free proxy
219          InstrumentEditorProxiesMutex.Lock();          InstrumentEditorProxiesMutex.Lock();
220          if (InstrumentEditorProxies.count(pSender)) {          for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
221              InstrumentEditorProxy* pProxy =              InstrumentEditorProxy* pProxy =
222                  dynamic_cast<InstrumentEditorProxy*>(                  dynamic_cast<InstrumentEditorProxy*>(
223                      InstrumentEditorProxies[pSender]                      InstrumentEditorProxies[i]
224                  );                  );
225              InstrumentEditorProxies.erase(pSender);              if (pProxy->pEditor == pSender) {
226              InstrumentEditorProxiesMutex.Unlock();                  InstrumentEditorProxies.remove(i);
227              HandBack(pProxy->pInstrument, pProxy);                  InstrumentEditorProxiesMutex.Unlock();
228              if (pProxy) delete pProxy;                  HandBack(pProxy->pInstrument, pProxy);
229          } else {                  if (pProxy) delete pProxy;
230              InstrumentEditorProxiesMutex.Unlock();                  return;
231              std::cerr << "Eeeek, could not find instrument editor proxy, this is a bug!\n" << std::flush;              }
232          }          }
233            InstrumentEditorProxiesMutex.Unlock();
234            std::cerr << "Eeeek, could not find instrument editor proxy, this is a bug!\n" << std::flush;
235    
236          // Note that we don't need to free the editor here. As it          // Note that we don't need to free the editor here. As it
237          // derives from Thread, it will delete itself when the thread          // derives from Thread, it will delete itself when the thread
238          // dies.          // dies.
239      }      }
240    
241        /**
242         * Try to inform the respective instrument editor(s), that a note on
243         * event just occured. This method is called by the MIDI thread. If any
244         * obstacles are in the way (e.g. if a wait for an unlock would be
245         * required) we give up immediately, since the RT safeness of the MIDI
246         * thread has absolute priority.
247         */
248        void InstrumentResourceManager::TrySendNoteOnToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) {
249            const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation
250            if (!bGotLock) return; // hell, forget it, not worth the hassle
251            for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
252                InstrumentEditorProxy* pProxy =
253                    dynamic_cast<InstrumentEditorProxy*>(
254                        InstrumentEditorProxies[i]
255                    );
256                if (pProxy->pInstrument == pInstrument)
257                    pProxy->pEditor->SendNoteOnToEditor(Key, Velocity);
258            }
259            InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation
260        }
261    
262        /**
263         * Try to inform the respective instrument editor(s), that a note off
264         * event just occured. This method is called by the MIDI thread. If any
265         * obstacles are in the way (e.g. if a wait for an unlock would be
266         * required) we give up immediately, since the RT safeness of the MIDI
267         * thread has absolute priority.
268         */
269        void InstrumentResourceManager::TrySendNoteOffToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) {
270            const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation
271            if (!bGotLock) return; // hell, forget it, not worth the hassle
272            for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
273                InstrumentEditorProxy* pProxy =
274                    dynamic_cast<InstrumentEditorProxy*>(
275                        InstrumentEditorProxies[i]
276                    );
277                if (pProxy->pInstrument == pInstrument)
278                    pProxy->pEditor->SendNoteOffToEditor(Key, Velocity);
279            }
280            InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation
281        }
282    
283      void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) {      void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) {
284          if (Samples.empty()) {          if (Samples.empty()) {
285              std::cerr << "gig::InstrumentResourceManager: WARNING, "              std::cerr << "gig::InstrumentResourceManager: WARNING, "
# Line 463  namespace LinuxSampler { namespace gig { Line 510  namespace LinuxSampler { namespace gig {
510       * Give back an instrument. This should be used instead of       * Give back an instrument. This should be used instead of
511       * HandBack if there are some dimension regions that are still in       * HandBack if there are some dimension regions that are still in
512       * use. (When an instrument is changed, the voices currently       * use. (When an instrument is changed, the voices currently
513       * playing is allowed to keep playing with the old instrument       * playing are allowed to keep playing with the old instrument
514       * until note off arrives. New notes will use the new instrument.)       * until note off arrives. New notes will use the new instrument.)
515       */       */
516      void InstrumentResourceManager::HandBackInstrument(::gig::Instrument* pResource, InstrumentConsumer* pConsumer,      void InstrumentResourceManager::HandBackInstrument(::gig::Instrument* pResource, InstrumentConsumer* pConsumer,
517                                                         ::gig::DimensionRegion** dimRegionsInUse) {                                                         RTList< ::gig::DimensionRegion*>* pDimRegionsInUse) {
518          DimRegInfoMutex.Lock();          DimRegInfoMutex.Lock();
519          for (int i = 0 ; dimRegionsInUse[i] ; i++) {          for (RTList< ::gig::DimensionRegion*>::Iterator i = pDimRegionsInUse->first() ; i != pDimRegionsInUse->end() ; i++) {
520              DimRegInfo[dimRegionsInUse[i]].refCount++;              DimRegInfo[*i].refCount++;
521              SampleRefCount[dimRegionsInUse[i]->pSample]++;              SampleRefCount[(*i)->pSample]++;
522          }          }
523          HandBack(pResource, pConsumer, true);          HandBack(pResource, pConsumer, true);
524          DimRegInfoMutex.Unlock();          DimRegInfoMutex.Unlock();

Legend:
Removed from v.1525  
changed lines
  Added in v.1653

  ViewVC Help
Powered by ViewVC