--- linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2008/09/11 18:11:06 1774 +++ 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 * @@ -160,7 +160,7 @@ 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; @@ -185,16 +185,33 @@ } 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; + + // 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; + } } } @@ -216,7 +233,7 @@ } } - 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 @@ -243,14 +260,14 @@ 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; + 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(); @@ -260,6 +277,8 @@ std::set::iterator end = engineChannels.end(); for (; iter != end; ++iter) (*iter)->Connect(pVirtualMidiDevice); Unlock(); + + return pEditor; } /** @@ -453,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 @@ -496,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 @@ -743,7 +789,7 @@ */ std::set InstrumentResourceManager::GetEngineChannelsUsing(::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(); @@ -766,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();