--- linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2008/01/20 15:04:51 1646 +++ linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2008/09/11 18:11:06 1774 @@ -67,6 +67,8 @@ // the instrument we borrowed on behalf of the editor ::gig::Instrument* pInstrument; + // the instrument editor we work on behalf + InstrumentEditor* pEditor; }; /** @@ -144,30 +146,70 @@ } InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) { - std::vector result; + Lock(); + ::gig::Instrument* pInstrument = Resource(ID, false); + bool loaded = (pInstrument != NULL); + if (!loaded) Unlock(); + ::RIFF::File* riff = NULL; ::gig::File* gig = NULL; try { - riff = new ::RIFF::File(ID.FileName); - gig = new ::gig::File(riff); - gig->SetAutoLoad(false); // avoid time consuming samples scanning - ::gig::Instrument* pInstrument = gig->GetInstrument(ID.Index); + if(!loaded) { + riff = new ::RIFF::File(ID.FileName); + gig = new ::gig::File(riff); + gig->SetAutoLoad(false); // avoid time consuming samples scanning + pInstrument = gig->GetInstrument(ID.Index); + } + if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName); + instrument_info_t info; - if (gig->pVersion) { - info.FormatVersion = ToString(gig->pVersion->major); - info.Product = gig->pInfo->Product; - info.Artists = gig->pInfo->Artists; + for (int i = 0; i < 128; i++) { info.KeyBindings[i] = info.KeySwitchBindings[i] = 0; } + + ::gig::File* pFile = (::gig::File*) pInstrument->GetParent(); + + if (pFile->pVersion) { + info.FormatVersion = ToString(pFile->pVersion->major); + info.Product = pFile->pInfo->Product; + info.Artists = pFile->pInfo->Artists; } + info.InstrumentName = pInstrument->pInfo->Name; + + ::gig::Region* pRegion = pInstrument->GetFirstRegion(); + while (pRegion) { + int low = pRegion->KeyRange.low; + int high = pRegion->KeyRange.high; + if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { + std::cerr << "Invalid key range: " << low << " - " << high << std::endl; + } else { + for (int i = low; i <= high; i++) info.KeyBindings[i] = 1; + } + pRegion = pInstrument->GetNextRegion(); + } + + if (loaded) { // retrieve keyswitching only if the instrument is fully loaded. + int low = pInstrument->DimensionKeyRange.low; + int high = pInstrument->DimensionKeyRange.high; + if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) { + std::cerr << "Invalid keyswitch range: " << low << " - " << high << std::endl; + } else { + for (int i = low; i <= high; i++) info.KeySwitchBindings[i] = 1; + } + } + + if (loaded) Unlock(); + if (gig) delete gig; if (riff) delete riff; return info; } catch (::RIFF::Exception e) { + if (loaded) Unlock(); if (gig) delete gig; if (riff) delete riff; throw InstrumentManagerException(e.Message); } catch (...) { + if (loaded) Unlock(); if (gig) delete gig; if (riff) delete riff; throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'"); @@ -196,11 +238,28 @@ ::gig::Instrument* pInstrument = Borrow(ID, pProxy); // remember the proxy and instrument for this instrument editor pProxy->pInstrument = pInstrument; + pProxy->pEditor = pEditor; InstrumentEditorProxiesMutex.Lock(); - InstrumentEditorProxies[pEditor] = pProxy; + InstrumentEditorProxies.add(pProxy); InstrumentEditorProxiesMutex.Unlock(); // launch the instrument editor for the given instrument pEditor->Launch(pInstrument, sDataType, sDataVersion); + + // register the instrument editor as virtual MIDI device as well ... + VirtualMidiDevice* pVirtualMidiDevice = + dynamic_cast(pEditor); + if (!pVirtualMidiDevice) { + std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush; + return; + } + // 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 ? ) + Lock(); + std::set engineChannels = + GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); + std::set::iterator iter = engineChannels.begin(); + std::set::iterator end = engineChannels.end(); + for (; iter != end; ++iter) (*iter)->Connect(pVirtualMidiDevice); + Unlock(); } /** @@ -212,20 +271,58 @@ */ void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) { dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n")); - // hand back instrument and free proxy + + ::gig::Instrument* pInstrument = NULL; + InstrumentEditorProxy* pProxy = NULL; + int iProxyIndex = -1; + + // first find the editor proxy entry for this editor InstrumentEditorProxiesMutex.Lock(); - if (InstrumentEditorProxies.count(pSender)) { - InstrumentEditorProxy* pProxy = + for (int i = 0; i < InstrumentEditorProxies.size(); i++) { + InstrumentEditorProxy* pCurProxy = dynamic_cast( - InstrumentEditorProxies[pSender] + InstrumentEditorProxies[i] ); - InstrumentEditorProxies.erase(pSender); - InstrumentEditorProxiesMutex.Unlock(); - HandBack(pProxy->pInstrument, pProxy); - if (pProxy) delete pProxy; + if (pCurProxy->pEditor == pSender) { + pProxy = pCurProxy; + iProxyIndex = i; + pInstrument = pCurProxy->pInstrument; + } + } + InstrumentEditorProxiesMutex.Unlock(); + + if (!pProxy) { + std::cerr << "Eeeek, could not find instrument editor proxy, " + "this is a bug!\n" << std::flush; + return; + } + + // now unregister editor as not being available as a virtual MIDI device anymore + VirtualMidiDevice* pVirtualMidiDevice = + dynamic_cast(pSender); + if (pVirtualMidiDevice) { + Lock(); + // NOTE: see note in LaunchInstrumentEditor() + std::set engineChannels = + GetEngineChannelsUsing(pInstrument, false/*don't lock again*/); + std::set::iterator iter = engineChannels.begin(); + std::set::iterator end = engineChannels.end(); + for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice); + Unlock(); } else { + std::cerr << "Could not unregister editor as not longer acting as " + "virtual MIDI device. Wasn't it registered?\n" + << std::flush; + } + + // finally delete proxy entry and hand back instrument + if (pInstrument) { + InstrumentEditorProxiesMutex.Lock(); + InstrumentEditorProxies.remove(iProxyIndex); InstrumentEditorProxiesMutex.Unlock(); - std::cerr << "Eeeek, could not find instrument editor proxy, this is a bug!\n" << std::flush; + + HandBack(pInstrument, pProxy); + delete pProxy; } // Note that we don't need to free the editor here. As it @@ -233,6 +330,50 @@ // dies. } +#if 0 // currently unused : + /** + * Try to inform the respective instrument editor(s), that a note on + * event just occured. This method is called by the MIDI thread. If any + * obstacles are in the way (e.g. if a wait for an unlock would be + * required) we give up immediately, since the RT safeness of the MIDI + * thread has absolute priority. + */ + void InstrumentResourceManager::TrySendNoteOnToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) { + const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation + if (!bGotLock) return; // hell, forget it, not worth the hassle + for (int i = 0; i < InstrumentEditorProxies.size(); i++) { + InstrumentEditorProxy* pProxy = + dynamic_cast( + InstrumentEditorProxies[i] + ); + if (pProxy->pInstrument == pInstrument) + pProxy->pEditor->SendNoteOnToDevice(Key, Velocity); + } + InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation + } + + /** + * Try to inform the respective instrument editor(s), that a note off + * event just occured. This method is called by the MIDI thread. If any + * obstacles are in the way (e.g. if a wait for an unlock would be + * required) we give up immediately, since the RT safeness of the MIDI + * thread has absolute priority. + */ + void InstrumentResourceManager::TrySendNoteOffToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) { + const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation + if (!bGotLock) return; // hell, forget it, not worth the hassle + for (int i = 0; i < InstrumentEditorProxies.size(); i++) { + InstrumentEditorProxy* pProxy = + dynamic_cast( + InstrumentEditorProxies[i] + ); + if (pProxy->pInstrument == pInstrument) + pProxy->pEditor->SendNoteOffToDevice(Key, Velocity); + } + InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation + } +#endif // unused + void InstrumentResourceManager::OnSamplesToBeRemoved(std::set Samples, InstrumentEditor* pSender) { if (Samples.empty()) { std::cerr << "gig::InstrumentResourceManager: WARNING, " @@ -591,6 +732,29 @@ if (bLock) Unlock(); return result; } + + /** + * Returns a list with all gig engine channels that are currently using + * the given instrument. + * + * @param pInstrument - search criteria + * @param bLock - whether we should lock (mutex) the instrument manager + * during this call and unlock at the end of this call + */ + std::set InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) { + if (bLock) Lock(); + std::set result; + std::set*> consumers = ConsumersOf(pInstrument); + std::set*>::iterator iter = consumers.begin(); + std::set*>::iterator end = consumers.end(); + for (; iter != end; ++iter) { + gig::EngineChannel* pEngineChannel = dynamic_cast(*iter); + if (!pEngineChannel) continue; + result.insert(pEngineChannel); + } + if (bLock) Unlock(); + return result; + } /** * Returns a list with all gig Engines that are currently using the given