/[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 1646 by persson, Sun Jan 20 15:04:51 2008 UTC revision 1774 by iliev, Thu Sep 11 18:11:06 2008 UTC
# 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 144  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                    pRegion = pInstrument->GetNextRegion();
189                }
190                
191                if (loaded) { // retrieve keyswitching only if the instrument is fully loaded.
192                    int low = pInstrument->DimensionKeyRange.low;
193                    int high = pInstrument->DimensionKeyRange.high;
194                    if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) {
195                        std::cerr << "Invalid keyswitch range: " << low << " - " << high << std::endl;
196                    } else {
197                        for (int i = low; i <= high; i++) info.KeySwitchBindings[i] = 1;
198                    }
199                }
200    
201                if (loaded) Unlock();
202    
203              if (gig)  delete gig;              if (gig)  delete gig;
204              if (riff) delete riff;              if (riff) delete riff;
205              return info;              return info;
206          } catch (::RIFF::Exception e) {          } catch (::RIFF::Exception e) {
207                if (loaded) Unlock();
208              if (gig)  delete gig;              if (gig)  delete gig;
209              if (riff) delete riff;              if (riff) delete riff;
210              throw InstrumentManagerException(e.Message);              throw InstrumentManagerException(e.Message);
211          } catch (...) {          } catch (...) {
212                if (loaded) Unlock();
213              if (gig)  delete gig;              if (gig)  delete gig;
214              if (riff) delete riff;              if (riff) delete riff;
215              throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'");              throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'");
# Line 196  namespace LinuxSampler { namespace gig { Line 238  namespace LinuxSampler { namespace gig {
238          ::gig::Instrument* pInstrument = Borrow(ID, pProxy);          ::gig::Instrument* pInstrument = Borrow(ID, pProxy);
239          // remember the proxy and instrument for this instrument editor          // remember the proxy and instrument for this instrument editor
240          pProxy->pInstrument = pInstrument;          pProxy->pInstrument = pInstrument;
241            pProxy->pEditor     = pEditor;
242          InstrumentEditorProxiesMutex.Lock();          InstrumentEditorProxiesMutex.Lock();
243          InstrumentEditorProxies[pEditor] = pProxy;          InstrumentEditorProxies.add(pProxy);
244          InstrumentEditorProxiesMutex.Unlock();          InstrumentEditorProxiesMutex.Unlock();
245          // launch the instrument editor for the given instrument          // launch the instrument editor for the given instrument
246          pEditor->Launch(pInstrument, sDataType, sDataVersion);          pEditor->Launch(pInstrument, sDataType, sDataVersion);
247    
248            // register the instrument editor as virtual MIDI device as well ...
249            VirtualMidiDevice* pVirtualMidiDevice =
250                dynamic_cast<VirtualMidiDevice*>(pEditor);
251            if (!pVirtualMidiDevice) {
252                std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush;
253                return;
254            }
255            // 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 ? )
256            Lock();
257            std::set<gig::EngineChannel*> engineChannels =
258                GetEngineChannelsUsing(pInstrument, false/*don't lock again*/);
259            std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin();
260            std::set<gig::EngineChannel*>::iterator end  = engineChannels.end();
261            for (; iter != end; ++iter) (*iter)->Connect(pVirtualMidiDevice);
262            Unlock();
263      }      }
264    
265      /**      /**
# Line 212  namespace LinuxSampler { namespace gig { Line 271  namespace LinuxSampler { namespace gig {
271       */       */
272      void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) {      void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) {
273          dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n"));          dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n"));
274          // hand back instrument and free proxy  
275            ::gig::Instrument* pInstrument = NULL;
276            InstrumentEditorProxy* pProxy  = NULL;
277            int iProxyIndex                = -1;
278    
279            // first find the editor proxy entry for this editor
280          InstrumentEditorProxiesMutex.Lock();          InstrumentEditorProxiesMutex.Lock();
281          if (InstrumentEditorProxies.count(pSender)) {          for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
282              InstrumentEditorProxy* pProxy =              InstrumentEditorProxy* pCurProxy =
283                  dynamic_cast<InstrumentEditorProxy*>(                  dynamic_cast<InstrumentEditorProxy*>(
284                      InstrumentEditorProxies[pSender]                      InstrumentEditorProxies[i]
285                  );                  );
286              InstrumentEditorProxies.erase(pSender);              if (pCurProxy->pEditor == pSender) {
287              InstrumentEditorProxiesMutex.Unlock();                  pProxy      = pCurProxy;
288              HandBack(pProxy->pInstrument, pProxy);                  iProxyIndex = i;
289              if (pProxy) delete pProxy;                  pInstrument = pCurProxy->pInstrument;
290                }
291            }
292            InstrumentEditorProxiesMutex.Unlock();
293    
294            if (!pProxy) {
295                std::cerr << "Eeeek, could not find instrument editor proxy, "
296                             "this is a bug!\n" << std::flush;
297                return;
298            }
299    
300            // now unregister editor as not being available as a virtual MIDI device anymore
301            VirtualMidiDevice* pVirtualMidiDevice =
302                dynamic_cast<VirtualMidiDevice*>(pSender);
303            if (pVirtualMidiDevice) {
304                Lock();
305                // NOTE: see note in LaunchInstrumentEditor()
306                std::set<gig::EngineChannel*> engineChannels =
307                    GetEngineChannelsUsing(pInstrument, false/*don't lock again*/);
308                std::set<gig::EngineChannel*>::iterator iter = engineChannels.begin();
309                std::set<gig::EngineChannel*>::iterator end  = engineChannels.end();
310                for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice);
311                Unlock();
312          } else {          } else {
313                std::cerr << "Could not unregister editor as not longer acting as "
314                             "virtual MIDI device. Wasn't it registered?\n"
315                          << std::flush;
316            }
317    
318            // finally delete proxy entry and hand back instrument
319            if (pInstrument) {
320                InstrumentEditorProxiesMutex.Lock();
321                InstrumentEditorProxies.remove(iProxyIndex);
322              InstrumentEditorProxiesMutex.Unlock();              InstrumentEditorProxiesMutex.Unlock();
323              std::cerr << "Eeeek, could not find instrument editor proxy, this is a bug!\n" << std::flush;  
324                HandBack(pInstrument, pProxy);
325                delete pProxy;
326          }          }
327    
328          // 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
# Line 233  namespace LinuxSampler { namespace gig { Line 330  namespace LinuxSampler { namespace gig {
330          // dies.          // dies.
331      }      }
332    
333    #if 0 // currently unused :
334        /**
335         * Try to inform the respective instrument editor(s), that a note on
336         * event just occured. This method is called by the MIDI thread. If any
337         * obstacles are in the way (e.g. if a wait for an unlock would be
338         * required) we give up immediately, since the RT safeness of the MIDI
339         * thread has absolute priority.
340         */
341        void InstrumentResourceManager::TrySendNoteOnToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) {
342            const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation
343            if (!bGotLock) return; // hell, forget it, not worth the hassle
344            for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
345                InstrumentEditorProxy* pProxy =
346                    dynamic_cast<InstrumentEditorProxy*>(
347                        InstrumentEditorProxies[i]
348                    );
349                if (pProxy->pInstrument == pInstrument)
350                    pProxy->pEditor->SendNoteOnToDevice(Key, Velocity);
351            }
352            InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation
353        }
354    
355        /**
356         * Try to inform the respective instrument editor(s), that a note off
357         * event just occured. This method is called by the MIDI thread. If any
358         * obstacles are in the way (e.g. if a wait for an unlock would be
359         * required) we give up immediately, since the RT safeness of the MIDI
360         * thread has absolute priority.
361         */
362        void InstrumentResourceManager::TrySendNoteOffToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) {
363            const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation
364            if (!bGotLock) return; // hell, forget it, not worth the hassle
365            for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
366                InstrumentEditorProxy* pProxy =
367                    dynamic_cast<InstrumentEditorProxy*>(
368                        InstrumentEditorProxies[i]
369                    );
370                if (pProxy->pInstrument == pInstrument)
371                    pProxy->pEditor->SendNoteOffToDevice(Key, Velocity);
372            }
373            InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation
374        }
375    #endif // unused
376    
377      void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) {      void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) {
378          if (Samples.empty()) {          if (Samples.empty()) {
379              std::cerr << "gig::InstrumentResourceManager: WARNING, "              std::cerr << "gig::InstrumentResourceManager: WARNING, "
# Line 591  namespace LinuxSampler { namespace gig { Line 732  namespace LinuxSampler { namespace gig {
732          if (bLock) Unlock();          if (bLock) Unlock();
733          return result;          return result;
734      }      }
735    
736        /**
737         * Returns a list with all gig engine channels that are currently using
738         * the given instrument.
739         *
740         * @param pInstrument - search criteria
741         * @param bLock - whether we should lock (mutex) the instrument manager
742         *                during this call and unlock at the end of this call
743         */
744        std::set<gig::EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) {
745            if (bLock) Lock();
746            std::set<gig::EngineChannel*> result;
747            std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);
748            std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();
749            std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();
750            for (; iter != end; ++iter) {
751                gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(*iter);
752                if (!pEngineChannel) continue;
753                result.insert(pEngineChannel);
754            }
755            if (bLock) Unlock();
756            return result;
757        }
758    
759      /**      /**
760       * 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.1646  
changed lines
  Added in v.1774

  ViewVC Help
Powered by ViewVC