/[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 1321 by schoenebeck, Tue Sep 4 01:12:49 2007 UTC revision 1659 by schoenebeck, Sun Feb 3 00:13:27 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 25  Line 25 
25    
26  #include "InstrumentResourceManager.h"  #include "InstrumentResourceManager.h"
27    
28  #include "../InstrumentEditorFactory.h"  #include "../../common/global_private.h"
29    #include "../../plugins/InstrumentEditorFactory.h"
30    
31  // We need to know the maximum number of sample points which are going to  // We need to know the maximum number of sample points which are going to
32  // be processed for each render cycle of the audio output driver, to know  // be processed for each render cycle of the audio output driver, to know
# Line 66  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 114  namespace LinuxSampler { namespace gig { Line 117  namespace LinuxSampler { namespace gig {
117          return ::gig::libraryVersion();          return ::gig::libraryVersion();
118      }      }
119    
120        std::vector<InstrumentResourceManager::instrument_id_t> InstrumentResourceManager::GetInstrumentFileContent(String File) throw (InstrumentManagerException) {
121            ::RIFF::File* riff = NULL;
122            ::gig::File*  gig  = NULL;
123            try {
124                std::vector<instrument_id_t> result;
125                riff = new ::RIFF::File(File);
126                gig  = new ::gig::File(riff);
127                gig->SetAutoLoad(false); // avoid time consuming samples scanning
128                for (int i = 0; gig->GetInstrument(i); i++) {
129                    instrument_id_t id;
130                    id.FileName = File;
131                    id.Index    = i;
132                    result.push_back(id);
133                }
134                if (gig)  delete gig;
135                if (riff) delete riff;
136                return result;
137            } catch (::RIFF::Exception e) {
138                if (gig)  delete gig;
139                if (riff) delete riff;
140                throw InstrumentManagerException(e.Message);
141            } catch (...) {
142                if (gig)  delete gig;
143                if (riff) delete riff;
144                throw InstrumentManagerException("Unknown exception while trying to parse '" + File + "'");
145            }
146        }
147    
148        InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) {
149            std::vector<instrument_id_t> result;
150            ::RIFF::File* riff = NULL;
151            ::gig::File*  gig  = NULL;
152            try {
153                riff = new ::RIFF::File(ID.FileName);
154                gig  = new ::gig::File(riff);
155                gig->SetAutoLoad(false); // avoid time consuming samples scanning
156                ::gig::Instrument* pInstrument = gig->GetInstrument(ID.Index);
157                if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName);
158                instrument_info_t info;
159                if (gig->pVersion) {
160                    info.FormatVersion = ToString(gig->pVersion->major);
161                    info.Product = gig->pInfo->Product;
162                    info.Artists = gig->pInfo->Artists;
163                }
164                info.InstrumentName = pInstrument->pInfo->Name;
165                if (gig)  delete gig;
166                if (riff) delete riff;
167                return info;
168            } catch (::RIFF::Exception e) {
169                if (gig)  delete gig;
170                if (riff) delete riff;
171                throw InstrumentManagerException(e.Message);
172            } catch (...) {
173                if (gig)  delete gig;
174                if (riff) delete riff;
175                throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'");
176            }
177        }
178    
179      void InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException) {      void InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException) {
180          const String sDataType    = GetInstrumentDataStructureName(ID);          const String sDataType    = GetInstrumentDataStructureName(ID);
181          const String sDataVersion = GetInstrumentDataStructureVersion(ID);          const String sDataVersion = GetInstrumentDataStructureVersion(ID);
# Line 136  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);
207    
208            // register the instrument editor as virtual MIDI device as well ...
209            VirtualMidiDevice* pVirtualMidiDevice =
210                dynamic_cast<VirtualMidiDevice*>(pEditor);
211            if (!pVirtualMidiDevice) {
212                std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush;
213                return;
214            }
215            // NOTE: for now connect the virtual MIDI keyboard of the instrument editor (if any) with all engine channels that have the same instrument as the editor was opened for ( other ideas ? )
216            Lock();
217            std::set<gig::EngineChannel*> engineChannels =
218                GetEngineChannelsUsing(pInstrument, false/*don't lock again*/);
219            std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin();
220            std::set<gig::EngineChannel*>::iterator end  = engineChannels.end();
221            for (; iter != end; ++iter) (*iter)->Connect(pVirtualMidiDevice);
222            Unlock();
223      }      }
224    
225      /**      /**
# Line 152  namespace LinuxSampler { namespace gig { Line 231  namespace LinuxSampler { namespace gig {
231       */       */
232      void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) {      void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) {
233          dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n"));          dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n"));
234          // hand back instrument and free proxy  
235            ::gig::Instrument* pInstrument = NULL;
236            InstrumentEditorProxy* pProxy  = NULL;
237            int iProxyIndex                = -1;
238    
239            // first find the editor proxy entry for this editor
240          InstrumentEditorProxiesMutex.Lock();          InstrumentEditorProxiesMutex.Lock();
241          if (InstrumentEditorProxies.count(pSender)) {          for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
242              InstrumentEditorProxy* pProxy =              InstrumentEditorProxy* pCurProxy =
243                  dynamic_cast<InstrumentEditorProxy*>(                  dynamic_cast<InstrumentEditorProxy*>(
244                      InstrumentEditorProxies[pSender]                      InstrumentEditorProxies[i]
245                  );                  );
246              InstrumentEditorProxies.erase(pSender);              if (pCurProxy->pEditor == pSender) {
247              InstrumentEditorProxiesMutex.Unlock();                  pProxy      = pCurProxy;
248              HandBack(pProxy->pInstrument, pProxy);                  iProxyIndex = i;
249              if (pProxy) delete pProxy;                  pInstrument = pCurProxy->pInstrument;
250                }
251            }
252            InstrumentEditorProxiesMutex.Unlock();
253    
254            if (!pProxy) {
255                std::cerr << "Eeeek, could not find instrument editor proxy, "
256                             "this is a bug!\n" << std::flush;
257                return;
258            }
259    
260            // now unregister editor as not being available as a virtual MIDI device anymore
261            VirtualMidiDevice* pVirtualMidiDevice =
262                dynamic_cast<VirtualMidiDevice*>(pSender);
263            if (pVirtualMidiDevice) {
264                Lock();
265                // NOTE: see note in LaunchInstrumentEditor()
266                std::set<gig::EngineChannel*> engineChannels =
267                    GetEngineChannelsUsing(pInstrument, false/*don't lock again*/);
268                std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin();
269                std::set<gig::EngineChannel*>::iterator end  = engineChannels.end();
270                for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice);
271                Unlock();
272          } else {          } else {
273                std::cerr << "Could not unregister editor as not longer acting as "
274                             "virtual MIDI device. Wasn't it registered?\n"
275                          << std::flush;
276            }
277    
278            // finally delete proxy entry and hand back instrument
279            if (pInstrument) {
280                InstrumentEditorProxiesMutex.Lock();
281                InstrumentEditorProxies.remove(iProxyIndex);
282              InstrumentEditorProxiesMutex.Unlock();              InstrumentEditorProxiesMutex.Unlock();
283              std::cerr << "Eeeek, could not find instrument editor proxy, this is a bug!\n" << std::flush;  
284                HandBack(pInstrument, pProxy);
285                delete pProxy;
286          }          }
287          // free the editor  
288          InstrumentEditorFactory::Destroy(pSender);          // Note that we don't need to free the editor here. As it
289            // derives from Thread, it will delete itself when the thread
290            // dies.
291        }
292    
293        /**
294         * Try to inform the respective instrument editor(s), that a note on
295         * event just occured. This method is called by the MIDI thread. If any
296         * obstacles are in the way (e.g. if a wait for an unlock would be
297         * required) we give up immediately, since the RT safeness of the MIDI
298         * thread has absolute priority.
299         */
300        void InstrumentResourceManager::TrySendNoteOnToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) {
301            const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation
302            if (!bGotLock) return; // hell, forget it, not worth the hassle
303            for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
304                InstrumentEditorProxy* pProxy =
305                    dynamic_cast<InstrumentEditorProxy*>(
306                        InstrumentEditorProxies[i]
307                    );
308                if (pProxy->pInstrument == pInstrument)
309                    pProxy->pEditor->SendNoteOnToDevice(Key, Velocity);
310            }
311            InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation
312        }
313    
314        /**
315         * Try to inform the respective instrument editor(s), that a note off
316         * event just occured. This method is called by the MIDI thread. If any
317         * obstacles are in the way (e.g. if a wait for an unlock would be
318         * required) we give up immediately, since the RT safeness of the MIDI
319         * thread has absolute priority.
320         */
321        void InstrumentResourceManager::TrySendNoteOffToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) {
322            const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation
323            if (!bGotLock) return; // hell, forget it, not worth the hassle
324            for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
325                InstrumentEditorProxy* pProxy =
326                    dynamic_cast<InstrumentEditorProxy*>(
327                        InstrumentEditorProxies[i]
328                    );
329                if (pProxy->pInstrument == pInstrument)
330                    pProxy->pEditor->SendNoteOffToDevice(Key, Velocity);
331            }
332            InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation
333      }      }
334    
335      void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) {      void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) {
# Line 401  namespace LinuxSampler { namespace gig { Line 562  namespace LinuxSampler { namespace gig {
562       * Give back an instrument. This should be used instead of       * Give back an instrument. This should be used instead of
563       * HandBack if there are some dimension regions that are still in       * HandBack if there are some dimension regions that are still in
564       * use. (When an instrument is changed, the voices currently       * use. (When an instrument is changed, the voices currently
565       * playing is allowed to keep playing with the old instrument       * playing are allowed to keep playing with the old instrument
566       * until note off arrives. New notes will use the new instrument.)       * until note off arrives. New notes will use the new instrument.)
567       */       */
568      void InstrumentResourceManager::HandBackInstrument(::gig::Instrument* pResource, InstrumentConsumer* pConsumer,      void InstrumentResourceManager::HandBackInstrument(::gig::Instrument* pResource, InstrumentConsumer* pConsumer,
569                                                         ::gig::DimensionRegion** dimRegionsInUse) {                                                         RTList< ::gig::DimensionRegion*>* pDimRegionsInUse) {
570          DimRegInfoMutex.Lock();          DimRegInfoMutex.Lock();
571          for (int i = 0 ; dimRegionsInUse[i] ; i++) {          for (RTList< ::gig::DimensionRegion*>::Iterator i = pDimRegionsInUse->first() ; i != pDimRegionsInUse->end() ; i++) {
572              DimRegInfo[dimRegionsInUse[i]].refCount++;              DimRegInfo[*i].refCount++;
573              SampleRefCount[dimRegionsInUse[i]->pSample]++;              SampleRefCount[(*i)->pSample]++;
574          }          }
575          HandBack(pResource, pConsumer, true);          HandBack(pResource, pConsumer, true);
576          DimRegInfoMutex.Unlock();          DimRegInfoMutex.Unlock();
# Line 529  namespace LinuxSampler { namespace gig { Line 690  namespace LinuxSampler { namespace gig {
690          if (bLock) Unlock();          if (bLock) Unlock();
691          return result;          return result;
692      }      }
693    
694        /**
695         * Returns a list with all gig engine channels that are currently using
696         * the given instrument.
697         *
698         * @param pInstrument - search criteria
699         * @param bLock - whether we should lock (mutex) the instrument manager
700         *                during this call and unlock at the end of this call
701         */
702        std::set<gig::EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) {
703            if (bLock) Lock();
704            std::set<gig::EngineChannel*> result;
705            std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);
706            std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();
707            std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();
708            for (; iter != end; ++iter) {
709                gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter);
710                if (!pEngineChannel) continue;
711                result.insert(pEngineChannel);
712            }
713            if (bLock) Unlock();
714            return result;
715        }
716    
717      /**      /**
718       * Returns a list with all gig Engines that are currently using the given       * Returns a list with all gig Engines that are currently using the given

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

  ViewVC Help
Powered by ViewVC