--- linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2008/01/30 01:51:46 1653 +++ linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2009/03/27 12:16:12 1876 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 - 2008 Christian Schoenebeck * + * Copyright (C) 2005 - 2009 Christian Schoenebeck * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -146,37 +146,94 @@ } 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. + + // only return keyswitch range if keyswitching is used + bool hasKeyswitches = false; + for (::gig::Region* pRegion = pInstrument->GetFirstRegion() ; + pRegion && !hasKeyswitches ; + pRegion = pInstrument->GetNextRegion()) { + for (int i = 0 ; i < pRegion->Dimensions ; i++) { + if (pRegion->pDimensionDefinitions[i].dimension == ::gig::dimension_keyboard) { + hasKeyswitches = true; + break; + } + } + } + + if (hasKeyswitches) { + 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 + "'"); } } - void InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID) throw (InstrumentManagerException) { + InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID, void* pUserData) throw (InstrumentManagerException) { const String sDataType = GetInstrumentDataStructureName(ID); const String sDataVersion = GetInstrumentDataStructureVersion(ID); // find instrument editors capable to handle given instrument @@ -203,7 +260,25 @@ InstrumentEditorProxies.add(pProxy); InstrumentEditorProxiesMutex.Unlock(); // launch the instrument editor for the given instrument - pEditor->Launch(pInstrument, sDataType, sDataVersion); + pEditor->Launch(pInstrument, sDataType, sDataVersion, pUserData); + + // 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 pEditor; + } + // 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(); + + return pEditor; } /** @@ -215,29 +290,66 @@ */ 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(); for (int i = 0; i < InstrumentEditorProxies.size(); i++) { - InstrumentEditorProxy* pProxy = + InstrumentEditorProxy* pCurProxy = dynamic_cast( InstrumentEditorProxies[i] ); - if (pProxy->pEditor == pSender) { - InstrumentEditorProxies.remove(i); - InstrumentEditorProxiesMutex.Unlock(); - HandBack(pProxy->pInstrument, pProxy); - if (pProxy) delete pProxy; - return; + if (pCurProxy->pEditor == pSender) { + pProxy = pCurProxy; + iProxyIndex = i; + pInstrument = pCurProxy->pInstrument; } } InstrumentEditorProxiesMutex.Unlock(); - std::cerr << "Eeeek, could not find instrument editor proxy, this is a bug!\n" << std::flush; + + 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(); + + HandBack(pInstrument, pProxy); + delete pProxy; + } // Note that we don't need to free the editor here. As it // derives from Thread, it will delete itself when the thread // 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 @@ -254,7 +366,7 @@ InstrumentEditorProxies[i] ); if (pProxy->pInstrument == pInstrument) - pProxy->pEditor->SendNoteOnToEditor(Key, Velocity); + pProxy->pEditor->SendNoteOnToDevice(Key, Velocity); } InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation } @@ -275,10 +387,11 @@ InstrumentEditorProxies[i] ); if (pProxy->pInstrument == pInstrument) - pProxy->pEditor->SendNoteOffToEditor(Key, Velocity); + 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()) { @@ -359,6 +472,28 @@ } else if (sStructType == "gig::Instrument") { // resume all previously suspended engines ResumeAllEngines(); + } else if (sStructType == "gig::Sample") { + // we're assuming here, that OnDataStructureToBeChanged() with + // "gig::File" was called previously, so we won't resume anything + // here, but just re-cache the given sample + Lock(); + ::gig::Sample* pSample = (::gig::Sample*) pStruct; + ::gig::File* pFile = (::gig::File*) pSample->GetParent(); + UncacheInitialSamples(pSample); + // now re-cache ... + std::vector< ::gig::Instrument*> instruments = + GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); + for (int i = 0; i < instruments.size(); i++) { + if (SampleReferencedByInstrument(pSample, instruments[i])) { + std::set engineChannels = + GetEngineChannelsUsing(instruments[i], false/*don't lock again*/); + std::set::iterator iter = engineChannels.begin(); + std::set::iterator end = engineChannels.end(); + for (; iter != end; ++iter) + CacheInitialSamples(pSample, *iter); + } + } + Unlock(); } else if (sStructType == "gig::Region") { // advice the engines to resume the given region, that is to // using it for playback again @@ -402,11 +537,16 @@ Lock(); ::gig::Sample* pSample = (::gig::Sample*) pOldSample; ::gig::File* pFile = (::gig::File*) pSample->GetParent(); + bool bSampleStillInUse = false; std::vector< ::gig::Instrument*> instruments = GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/); - for (int i = 0; i < instruments.size(); i++) - if (!SampleReferencedByInstrument(pSample, instruments[i])) - UncacheInitialSamples(pSample); + for (int i = 0; i < instruments.size(); i++) { + if (SampleReferencedByInstrument(pSample, instruments[i])) { + bSampleStillInUse = true; + break; + } + } + if (!bSampleStillInUse) UncacheInitialSamples(pSample); Unlock(); } // make sure new sample reference is cached @@ -640,6 +780,29 @@ } /** + * 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 * instrument. * @@ -649,7 +812,7 @@ */ std::set InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) { if (bLock) Lock(); - std::set result; + std::set result; std::set*> consumers = ConsumersOf(pInstrument); std::set*>::iterator iter = consumers.begin(); std::set*>::iterator end = consumers.end();