--- linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2008/02/03 16:21:38 1662 +++ linuxsampler/trunk/src/engines/gig/InstrumentResourceManager.cpp 2009/03/18 22:16:14 1868 @@ -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) throw (InstrumentManagerException) { const String sDataType = GetInstrumentDataStructureName(ID); const String sDataVersion = GetInstrumentDataStructureVersion(ID); // find instrument editors capable to handle given instrument @@ -210,7 +267,7 @@ 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(); @@ -220,6 +277,8 @@ std::set::iterator end = engineChannels.end(); for (; iter != end; ++iter) (*iter)->Connect(pVirtualMidiDevice); Unlock(); + + return pEditor; } /** @@ -413,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 @@ -456,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 @@ -703,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(); @@ -726,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();