/[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 970 by schoenebeck, Wed Dec 6 22:28:17 2006 UTC revision 2309 by iliev, Mon Jan 30 10:40:19 2012 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6   *   Copyright (C) 2005, 2006 Christian Schoenebeck                        *   *   Copyright (C) 2005 - 2011 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 24  Line 24 
24  #include <sstream>  #include <sstream>
25    
26  #include "InstrumentResourceManager.h"  #include "InstrumentResourceManager.h"
27    #include "EngineChannel.h"
28    #include "Engine.h"
29    
30  // We need to know the maximum number of sample points which are going to  #include "../../common/global_private.h"
31  // be processed for each render cycle of the audio output driver, to know  #include "../../plugins/InstrumentEditorFactory.h"
 // how much initial sample points we need to cache into RAM. If the given  
 // sampler channel does not have an audio output device assigned yet  
 // though, we simply use this default value.  
 #define GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE     128  
32    
33  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
34    
# Line 47  namespace LinuxSampler { namespace gig { Line 45  namespace LinuxSampler { namespace gig {
45          InstrumentManager::instrument_id_t* pInstrumentKey;          InstrumentManager::instrument_id_t* pInstrumentKey;
46      };      };
47    
48        // we use this to react on events concerning an instrument on behalf of an instrument editor
49        class InstrumentEditorProxy : public InstrumentConsumer {
50        public:
51            virtual void ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {
52                //TODO: inform the instrument editor about the pending update
53            }
54    
55            virtual void ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {
56                //TODO:: inform the instrument editor about finished update
57            }
58    
59            virtual void OnResourceProgress(float fProgress) {
60                //TODO: inform the instrument editor about the progress of an update
61            }
62    
63            // the instrument we borrowed on behalf of the editor
64            ::gig::Instrument* pInstrument;
65            // the instrument editor we work on behalf
66            InstrumentEditor* pEditor;
67        };
68    
69      /**      /**
70       * Callback function which will be called by libgig during loading of       * Callback function which will be called by libgig during loading of
71       * instruments to inform about the current progress. Or to be more       * instruments to inform about the current progress. Or to be more
# Line 68  namespace LinuxSampler { namespace gig { Line 87  namespace LinuxSampler { namespace gig {
87          return Entries();          return Entries();
88      }      }
89    
90      InstrumentManager::mode_t InstrumentResourceManager::GetMode(const instrument_id_t& ID) {      String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) {
91          return static_cast<InstrumentManager::mode_t>(AvailabilityMode(ID));          Lock();
92            ::gig::Instrument* pInstrument = Resource(ID, false);
93            String res = (pInstrument) ? pInstrument->pInfo->Name : "";
94            Unlock();
95            return res;
96        }
97    
98        String InstrumentResourceManager::GetInstrumentDataStructureName(instrument_id_t ID) {
99            return ::gig::libraryName();
100      }      }
101    
102      void InstrumentResourceManager::SetMode(const instrument_id_t& ID, InstrumentManager::mode_t Mode) {      String InstrumentResourceManager::GetInstrumentDataStructureVersion(instrument_id_t ID) {
103          dmsg(2,("gig::InstrumentResourceManager: setting mode for %s (Index=%d) to %d\n",ID.FileName.c_str(),ID.Index,Mode));          return ::gig::libraryVersion();
         SetAvailabilityMode(ID, static_cast<ResourceManager<InstrumentManager::instrument_id_t, ::gig::Instrument>::mode_t>(Mode));  
104      }      }
105    
106      String InstrumentResourceManager::GetInstrumentName(instrument_id_t ID) {      std::vector<InstrumentResourceManager::instrument_id_t> InstrumentResourceManager::GetInstrumentFileContent(String File) throw (InstrumentManagerException) {
107            ::RIFF::File* riff = NULL;
108            ::gig::File*  gig  = NULL;
109            try {
110                std::vector<instrument_id_t> result;
111                riff = new ::RIFF::File(File);
112                gig  = new ::gig::File(riff);
113                gig->SetAutoLoad(false); // avoid time consuming samples scanning
114                for (int i = 0; gig->GetInstrument(i); i++) {
115                    instrument_id_t id;
116                    id.FileName = File;
117                    id.Index    = i;
118                    result.push_back(id);
119                }
120                if (gig)  delete gig;
121                if (riff) delete riff;
122                return result;
123            } catch (::RIFF::Exception e) {
124                if (gig)  delete gig;
125                if (riff) delete riff;
126                throw InstrumentManagerException(e.Message);
127            } catch (...) {
128                if (gig)  delete gig;
129                if (riff) delete riff;
130                throw InstrumentManagerException("Unknown exception while trying to parse '" + File + "'");
131            }
132        }
133    
134        InstrumentResourceManager::instrument_info_t InstrumentResourceManager::GetInstrumentInfo(instrument_id_t ID) throw (InstrumentManagerException) {
135          Lock();          Lock();
136          ::gig::Instrument* pInstrument = Resource(ID, false);          ::gig::Instrument* pInstrument = Resource(ID, false);
137          String res = (pInstrument) ? pInstrument->pInfo->Name : "";          bool loaded = (pInstrument != NULL);
138            if (!loaded) Unlock();
139    
140            ::RIFF::File* riff = NULL;
141            ::gig::File*  gig  = NULL;
142            try {
143                if(!loaded) {
144                    riff = new ::RIFF::File(ID.FileName);
145                    gig  = new ::gig::File(riff);
146                    gig->SetAutoLoad(false); // avoid time consuming samples scanning
147                    pInstrument = gig->GetInstrument(ID.Index);
148                }
149    
150                if (!pInstrument) throw InstrumentManagerException("There is no instrument " + ToString(ID.Index) + " in " + ID.FileName);
151    
152                instrument_info_t info;
153                for (int i = 0; i < 128; i++) { info.KeyBindings[i] = info.KeySwitchBindings[i] = 0; }
154    
155                ::gig::File* pFile = (::gig::File*) pInstrument->GetParent();
156    
157                if (pFile->pVersion) {
158                    info.FormatVersion = ToString(pFile->pVersion->major);
159                    info.Product = pFile->pInfo->Product;
160                    info.Artists = pFile->pInfo->Artists;
161                }
162    
163                info.InstrumentName = pInstrument->pInfo->Name;
164    
165                ::gig::Region* pRegion = pInstrument->GetFirstRegion();
166                while (pRegion) {
167                    int low = pRegion->KeyRange.low;
168                    int high = pRegion->KeyRange.high;
169                    if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) {
170                        std::cerr << "Invalid key range: " << low << " - " << high << std::endl;
171                    } else {
172                        for (int i = low; i <= high; i++) info.KeyBindings[i] = 1;
173                    }
174    
175                    pRegion = pInstrument->GetNextRegion();
176                }
177    
178                if (loaded) { // retrieve keyswitching only if the instrument is fully loaded.
179    
180                    // only return keyswitch range if keyswitching is used
181                    bool hasKeyswitches = false;
182                    for (::gig::Region* pRegion = pInstrument->GetFirstRegion() ;
183                         pRegion && !hasKeyswitches ;
184                         pRegion = pInstrument->GetNextRegion()) {
185                        for (int i = 0 ; i < pRegion->Dimensions ; i++) {
186                            if (pRegion->pDimensionDefinitions[i].dimension == ::gig::dimension_keyboard) {
187                                hasKeyswitches = true;
188                                break;
189                            }
190                        }
191                    }
192    
193                    if (hasKeyswitches) {
194                        int low = pInstrument->DimensionKeyRange.low;
195                        int high = pInstrument->DimensionKeyRange.high;
196                        if (low < 0 || low > 127 || high < 0 || high > 127 || low > high) {
197                            std::cerr << "Invalid keyswitch range: " << low << " - " << high << std::endl;
198                        } else {
199                            for (int i = low; i <= high; i++) info.KeySwitchBindings[i] = 1;
200                        }
201                    }
202                }
203    
204                if (loaded) Unlock();
205    
206                if (gig)  delete gig;
207                if (riff) delete riff;
208                return info;
209            } catch (::RIFF::Exception e) {
210                if (loaded) Unlock();
211                if (gig)  delete gig;
212                if (riff) delete riff;
213                throw InstrumentManagerException(e.Message);
214            } catch (...) {
215                if (loaded) Unlock();
216                if (gig)  delete gig;
217                if (riff) delete riff;
218                throw InstrumentManagerException("Unknown exception while trying to parse '" + ID.FileName + "'");
219            }
220        }
221    
222        InstrumentEditor* InstrumentResourceManager::LaunchInstrumentEditor(instrument_id_t ID, void* pUserData) throw (InstrumentManagerException) {
223            const String sDataType    = GetInstrumentDataStructureName(ID);
224            const String sDataVersion = GetInstrumentDataStructureVersion(ID);
225            // find instrument editors capable to handle given instrument
226            std::vector<String> vEditors =
227                InstrumentEditorFactory::MatchingEditors(sDataType, sDataVersion);
228            if (!vEditors.size())
229                throw InstrumentManagerException(
230                    "There is no instrument editor capable to handle this instrument"
231                );
232            // simply use the first editor in the result set
233            dmsg(1,("Found matching editor '%s' for instrument ('%s', %d) having data structure ('%s','%s')\n",
234                vEditors[0].c_str(), ID.FileName.c_str(), ID.Index, sDataType.c_str(), sDataVersion.c_str()));
235            InstrumentEditor* pEditor = InstrumentEditorFactory::Create(vEditors[0]);
236            // register for receiving notifications from the instrument editor
237            pEditor->AddListener(this);
238            // create a proxy that reacts on notification on behalf of the editor
239            InstrumentEditorProxy* pProxy = new InstrumentEditorProxy;
240            // borrow the instrument on behalf of the instrument editor
241            ::gig::Instrument* pInstrument = Borrow(ID, pProxy);
242            // remember the proxy and instrument for this instrument editor
243            pProxy->pInstrument = pInstrument;
244            pProxy->pEditor     = pEditor;
245            InstrumentEditorProxiesMutex.Lock();
246            InstrumentEditorProxies.add(pProxy);
247            InstrumentEditorProxiesMutex.Unlock();
248            // launch the instrument editor for the given instrument
249            pEditor->Launch(pInstrument, sDataType, sDataVersion, pUserData);
250    
251            // register the instrument editor as virtual MIDI device as well ...
252            VirtualMidiDevice* pVirtualMidiDevice =
253                dynamic_cast<VirtualMidiDevice*>(pEditor);
254            if (!pVirtualMidiDevice) {
255                std::cerr << "Instrument editor not a virtual MIDI device\n" << std::flush;
256                return pEditor;
257            }
258            // 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 ? )
259            Lock();
260            std::set<EngineChannel*> engineChannels =
261                GetEngineChannelsUsing(pInstrument, false/*don't lock again*/);
262            std::set<EngineChannel*>::iterator iter = engineChannels.begin();
263            std::set<EngineChannel*>::iterator end  = engineChannels.end();
264            for (; iter != end; ++iter) (static_cast<AbstractEngineChannel*>(*iter))->Connect(pVirtualMidiDevice);
265          Unlock();          Unlock();
266          return res;  
267            return pEditor;
268        }
269    
270        /**
271         * Will be called by the respective instrument editor once it left its
272         * Main() loop. That way we can handle cleanup before its thread finally
273         * dies.
274         *
275         * @param pSender - instrument editor that stops execution
276         */
277        void InstrumentResourceManager::OnInstrumentEditorQuit(InstrumentEditor* pSender) {
278            dmsg(1,("InstrumentResourceManager: instrument editor quit, doing cleanup\n"));
279    
280            ::gig::Instrument* pInstrument = NULL;
281            InstrumentEditorProxy* pProxy  = NULL;
282            int iProxyIndex                = -1;
283    
284            // first find the editor proxy entry for this editor
285            InstrumentEditorProxiesMutex.Lock();
286            for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
287                InstrumentEditorProxy* pCurProxy =
288                    dynamic_cast<InstrumentEditorProxy*>(
289                        InstrumentEditorProxies[i]
290                    );
291                if (pCurProxy->pEditor == pSender) {
292                    pProxy      = pCurProxy;
293                    iProxyIndex = i;
294                    pInstrument = pCurProxy->pInstrument;
295                }
296            }
297            InstrumentEditorProxiesMutex.Unlock();
298    
299            if (!pProxy) {
300                std::cerr << "Eeeek, could not find instrument editor proxy, "
301                             "this is a bug!\n" << std::flush;
302                return;
303            }
304    
305            // now unregister editor as not being available as a virtual MIDI device anymore
306            VirtualMidiDevice* pVirtualMidiDevice =
307                dynamic_cast<VirtualMidiDevice*>(pSender);
308            if (pVirtualMidiDevice) {
309                Lock();
310                // NOTE: see note in LaunchInstrumentEditor()
311                std::set<EngineChannel*> engineChannels =
312                    GetEngineChannelsUsing(pInstrument, false/*don't lock again*/);
313                std::set<EngineChannel*>::iterator iter = engineChannels.begin();
314                std::set<EngineChannel*>::iterator end  = engineChannels.end();
315                for (; iter != end; ++iter) (*iter)->Disconnect(pVirtualMidiDevice);
316                Unlock();
317            } else {
318                std::cerr << "Could not unregister editor as not longer acting as "
319                             "virtual MIDI device. Wasn't it registered?\n"
320                          << std::flush;
321            }
322    
323            // finally delete proxy entry and hand back instrument
324            if (pInstrument) {
325                InstrumentEditorProxiesMutex.Lock();
326                InstrumentEditorProxies.remove(iProxyIndex);
327                InstrumentEditorProxiesMutex.Unlock();
328    
329                HandBack(pInstrument, pProxy);
330                delete pProxy;
331            }
332    
333            // Note that we don't need to free the editor here. As it
334            // derives from Thread, it will delete itself when the thread
335            // dies.
336        }
337    
338    #if 0 // currently unused :
339        /**
340         * Try to inform the respective instrument editor(s), that a note on
341         * event just occured. This method is called by the MIDI thread. If any
342         * obstacles are in the way (e.g. if a wait for an unlock would be
343         * required) we give up immediately, since the RT safeness of the MIDI
344         * thread has absolute priority.
345         */
346        void InstrumentResourceManager::TrySendNoteOnToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) {
347            const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation
348            if (!bGotLock) return; // hell, forget it, not worth the hassle
349            for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
350                InstrumentEditorProxy* pProxy =
351                    dynamic_cast<InstrumentEditorProxy*>(
352                        InstrumentEditorProxies[i]
353                    );
354                if (pProxy->pInstrument == pInstrument)
355                    pProxy->pEditor->SendNoteOnToDevice(Key, Velocity);
356            }
357            InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation
358        }
359    
360        /**
361         * Try to inform the respective instrument editor(s), that a note off
362         * event just occured. This method is called by the MIDI thread. If any
363         * obstacles are in the way (e.g. if a wait for an unlock would be
364         * required) we give up immediately, since the RT safeness of the MIDI
365         * thread has absolute priority.
366         */
367        void InstrumentResourceManager::TrySendNoteOffToEditors(uint8_t Key, uint8_t Velocity, ::gig::Instrument* pInstrument) {
368            const bool bGotLock = InstrumentEditorProxiesMutex.Trylock(); // naively assumes RT safe implementation
369            if (!bGotLock) return; // hell, forget it, not worth the hassle
370            for (int i = 0; i < InstrumentEditorProxies.size(); i++) {
371                InstrumentEditorProxy* pProxy =
372                    dynamic_cast<InstrumentEditorProxy*>(
373                        InstrumentEditorProxies[i]
374                    );
375                if (pProxy->pInstrument == pInstrument)
376                    pProxy->pEditor->SendNoteOffToDevice(Key, Velocity);
377            }
378            InstrumentEditorProxiesMutex.Unlock(); // naively assumes RT safe implementation
379        }
380    #endif // unused
381    
382        void InstrumentResourceManager::OnSamplesToBeRemoved(std::set<void*> Samples, InstrumentEditor* pSender) {
383            if (Samples.empty()) {
384                std::cerr << "gig::InstrumentResourceManager: WARNING, "
385                             "OnSamplesToBeRemoved() called with empty list, this "
386                             "is a bug!\n" << std::flush;
387                return;
388            }
389            // TODO: ATM we assume here that all samples are from the same file
390            ::gig::Sample* pFirstSample = (::gig::Sample*) *Samples.begin();
391            ::gig::File* pCriticalFile = dynamic_cast< ::gig::File*>(pFirstSample->GetParent());
392            // completely suspend all engines that use that same file
393            SuspendEnginesUsing(pCriticalFile);
394        }
395    
396        void InstrumentResourceManager::OnSamplesRemoved(InstrumentEditor* pSender) {
397            // resume all previously, completely suspended engines
398            // (we don't have to un-cache the removed samples here, since that is
399            // automatically done by the gig::Sample destructor)
400            ResumeAllEngines();
401        }
402    
403        void InstrumentResourceManager::OnDataStructureToBeChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) {
404            //TODO: remove code duplication
405            if (sStructType == "gig::File") {
406                // completely suspend all engines that use that file
407                ::gig::File* pFile = (::gig::File*) pStruct;
408                SuspendEnginesUsing(pFile);
409            } else if (sStructType == "gig::Instrument") {
410                // completely suspend all engines that use that instrument
411                ::gig::Instrument* pInstrument = (::gig::Instrument*) pStruct;
412                SuspendEnginesUsing(pInstrument);
413            } else if (sStructType == "gig::Region") {
414                // only advice the engines to suspend the given region, so they'll
415                // only ignore that region (and probably already other suspended
416                // ones), but beside that continue normal playback
417                ::gig::Region* pRegion = (::gig::Region*) pStruct;
418                ::gig::Instrument* pInstrument =
419                    (::gig::Instrument*) pRegion->GetParent();
420                Lock();
421                std::set<Engine*> engines =
422                    GetEnginesUsing(pInstrument, false/*don't lock again*/);
423                std::set<Engine*>::iterator iter = engines.begin();
424                std::set<Engine*>::iterator end  = engines.end();
425                for (; iter != end; ++iter) (*iter)->Suspend(pRegion);
426                Unlock();
427            } else if (sStructType == "gig::DimensionRegion") {
428                // only advice the engines to suspend the given DimensionRegions's
429                // parent region, so they'll only ignore that region (and probably
430                // already other suspended ones), but beside that continue normal
431                // playback
432                ::gig::DimensionRegion* pDimReg =
433                    (::gig::DimensionRegion*) pStruct;
434                ::gig::Region* pRegion = pDimReg->GetParent();
435                ::gig::Instrument* pInstrument =
436                    (::gig::Instrument*) pRegion->GetParent();
437                Lock();
438                std::set<Engine*> engines =
439                    GetEnginesUsing(pInstrument, false/*don't lock again*/);
440                std::set<Engine*>::iterator iter = engines.begin();
441                std::set<Engine*>::iterator end  = engines.end();
442                for (; iter != end; ++iter) (*iter)->Suspend(pRegion);
443                Unlock();
444            } else {
445                std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data "
446                             "structure '" << sStructType << "' requested to be "
447                             "suspended by instrument editor. This is a bug!\n"
448                          << std::flush;
449                //TODO: we should inform the instrument editor that something seriously went wrong
450            }
451        }
452    
453        void InstrumentResourceManager::OnDataStructureChanged(void* pStruct, String sStructType, InstrumentEditor* pSender) {
454            //TODO: remove code duplication
455            if (sStructType == "gig::File") {
456                // resume all previously suspended engines
457                ResumeAllEngines();
458            } else if (sStructType == "gig::Instrument") {
459                // resume all previously suspended engines
460                ResumeAllEngines();
461            } else if (sStructType == "gig::Sample") {
462                // we're assuming here, that OnDataStructureToBeChanged() with
463                // "gig::File" was called previously, so we won't resume anything
464                // here, but just re-cache the given sample
465                Lock();
466                ::gig::Sample* pSample = (::gig::Sample*) pStruct;
467                ::gig::File* pFile = (::gig::File*) pSample->GetParent();
468                UncacheInitialSamples(pSample);
469                // now re-cache ...
470                std::vector< ::gig::Instrument*> instruments =
471                    GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/);
472                for (int i = 0; i < instruments.size(); i++) {
473                    if (SampleReferencedByInstrument(pSample, instruments[i])) {
474                        std::set<EngineChannel*> engineChannels =
475                            GetEngineChannelsUsing(instruments[i], false/*don't lock again*/);
476                        std::set<EngineChannel*>::iterator iter = engineChannels.begin();
477                        std::set<EngineChannel*>::iterator end  = engineChannels.end();
478                        for (; iter != end; ++iter)
479                            CacheInitialSamples(pSample, *iter);
480                    }
481                }
482                Unlock();
483            } else if (sStructType == "gig::Region") {
484                // advice the engines to resume the given region, that is to
485                // using it for playback again
486                ::gig::Region* pRegion = (::gig::Region*) pStruct;
487                ::gig::Instrument* pInstrument =
488                    (::gig::Instrument*) pRegion->GetParent();
489                Lock();
490                std::set<Engine*> engines =
491                    GetEnginesUsing(pInstrument, false/*don't lock again*/);
492                std::set<Engine*>::iterator iter = engines.begin();
493                std::set<Engine*>::iterator end  = engines.end();
494                for (; iter != end; ++iter) (*iter)->Resume(pRegion);
495                Unlock();
496            } else if (sStructType == "gig::DimensionRegion") {
497                // advice the engines to resume the given DimensionRegion's parent
498                // region, that is to using it for playback again
499                ::gig::DimensionRegion* pDimReg =
500                    (::gig::DimensionRegion*) pStruct;
501                ::gig::Region* pRegion = pDimReg->GetParent();
502                ::gig::Instrument* pInstrument =
503                    (::gig::Instrument*) pRegion->GetParent();
504                Lock();
505                std::set<Engine*> engines =
506                    GetEnginesUsing(pInstrument, false/*don't lock again*/);
507                std::set<Engine*>::iterator iter = engines.begin();
508                std::set<Engine*>::iterator end  = engines.end();
509                for (; iter != end; ++iter) (*iter)->Resume(pRegion);
510                Unlock();
511            } else {
512                std::cerr << "gig::InstrumentResourceManager: ERROR, unknown data "
513                             "structure '" << sStructType << "' requested to be "
514                             "resumed by instrument editor. This is a bug!\n"
515                          << std::flush;
516                //TODO: we should inform the instrument editor that something seriously went wrong
517            }
518        }
519    
520        void InstrumentResourceManager::OnSampleReferenceChanged(void* pOldSample, void* pNewSample, InstrumentEditor* pSender) {
521            // uncache old sample in case it's not used by anybody anymore
522            if (pOldSample) {
523                Lock();
524                ::gig::Sample* pSample = (::gig::Sample*) pOldSample;
525                ::gig::File* pFile = (::gig::File*) pSample->GetParent();
526                bool bSampleStillInUse = false;
527                std::vector< ::gig::Instrument*> instruments =
528                    GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/);
529                for (int i = 0; i < instruments.size(); i++) {
530                    if (SampleReferencedByInstrument(pSample, instruments[i])) {
531                        bSampleStillInUse = true;
532                        break;
533                    }
534                }
535                if (!bSampleStillInUse) UncacheInitialSamples(pSample);
536                Unlock();
537            }
538            // make sure new sample reference is cached
539            if (pNewSample) {
540                Lock();
541                ::gig::Sample* pSample = (::gig::Sample*) pNewSample;
542                ::gig::File* pFile = (::gig::File*) pSample->GetParent();
543                // get all engines that use that same gig::File
544                std::set<Engine*> engines = GetEnginesUsing(pFile, false/*don't lock again*/);
545                std::set<Engine*>::iterator iter = engines.begin();
546                std::set<Engine*>::iterator end  = engines.end();
547                for (; iter != end; ++iter)
548                    CacheInitialSamples(pSample, *iter);
549                Unlock();
550            }
551      }      }
552    
553      ::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) {      ::gig::Instrument* InstrumentResourceManager::Create(instrument_id_t Key, InstrumentConsumer* pConsumer, void*& pArg) {
554          // get gig file from inernal gig file manager          // get gig file from internal gig file manager
555          ::gig::File* pGig = Gigs.Borrow(Key.FileName, (GigConsumer*) Key.Index); // conversion kinda hackish :/          ::gig::File* pGig = Gigs.Borrow(Key.FileName, reinterpret_cast<GigConsumer*>(Key.Index)); // conversion kinda hackish :/
556    
557          // we pass this to the progress callback mechanism of libgig          // we pass this to the progress callback mechanism of libgig
558          progress_callback_arg_t callbackArg;          progress_callback_arg_t callbackArg;
# Line 103  namespace LinuxSampler { namespace gig { Line 568  namespace LinuxSampler { namespace gig {
568          if (!pInstrument) {          if (!pInstrument) {
569              std::stringstream msg;              std::stringstream msg;
570              msg << "There's no instrument with index " << Key.Index << ".";              msg << "There's no instrument with index " << Key.Index << ".";
571              throw InstrumentResourceManagerException(msg.str());              throw InstrumentManagerException(msg.str());
572          }          }
573          pGig->GetFirstSample(); // just to force complete instrument loading          pGig->GetFirstSample(); // just to force complete instrument loading
574          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));
# Line 119  namespace LinuxSampler { namespace gig { Line 584  namespace LinuxSampler { namespace gig {
584    
585              if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) {              if (pRgn->GetSample() && !pRgn->GetSample()->GetCache().Size) {
586                  dmsg(2,("C"));                  dmsg(2,("C"));
587                  CacheInitialSamples(pRgn->GetSample(), (gig::EngineChannel*) pConsumer);                  CacheInitialSamples(pRgn->GetSample(), (EngineChannel*) pConsumer);
588              }              }
589              for (uint i = 0; i < pRgn->DimensionRegions; i++) {              for (uint i = 0; i < pRgn->DimensionRegions; i++) {
590                  CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (gig::EngineChannel*) pConsumer);                  CacheInitialSamples(pRgn->pDimensionRegions[i]->pSample, (EngineChannel*) pConsumer);
591              }              }
592    
593              pRgn = pInstrument->GetNextRegion();              pRgn = pInstrument->GetNextRegion();
# Line 137  namespace LinuxSampler { namespace gig { Line 602  namespace LinuxSampler { namespace gig {
602          pEntry->ID.Index      = Key.Index;          pEntry->ID.Index      = Key.Index;
603          pEntry->pGig          = pGig;          pEntry->pGig          = pGig;
604    
605          gig::EngineChannel* pEngineChannel = (gig::EngineChannel*) pConsumer;          // (try to resolve the audio device context)
606            EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(pConsumer);
607            Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine());
608            AudioOutputDevice* pDevice = (pEngine) ? pEngine->pAudioOutputDevice : NULL;
609          // and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond'          // and we save this to check if we need to reallocate for a engine with higher value of 'MaxSamplesPerSecond'
610          pEntry->MaxSamplesPerCycle =          pEntry->MaxSamplesPerCycle =
611              (pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle()              (pDevice) ? pDevice->MaxSamplesPerCycle() : DefaultMaxSamplesPerCycle();
612                                            : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE;          
613          pArg = pEntry;          pArg = pEntry;
614    
615          return pInstrument;          return pInstrument;
# Line 150  namespace LinuxSampler { namespace gig { Line 618  namespace LinuxSampler { namespace gig {
618      void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) {      void InstrumentResourceManager::Destroy( ::gig::Instrument* pResource, void* pArg) {
619          instr_entry_t* pEntry = (instr_entry_t*) pArg;          instr_entry_t* pEntry = (instr_entry_t*) pArg;
620          // we don't need the .gig file here anymore          // we don't need the .gig file here anymore
621          Gigs.HandBack(pEntry->pGig, (GigConsumer*) pEntry->ID.Index); // conversion kinda hackish :/          Gigs.HandBack(pEntry->pGig, reinterpret_cast<GigConsumer*>(pEntry->ID.Index)); // conversion kinda hackish :/
622          delete pEntry;          delete pEntry;
623      }      }
624    
625      void InstrumentResourceManager::OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg) {      void InstrumentResourceManager::OnBorrow(::gig::Instrument* pResource, InstrumentConsumer* pConsumer, void*& pArg) {
626          instr_entry_t* pEntry = (instr_entry_t*) pArg;          instr_entry_t* pEntry = (instr_entry_t*) pArg;
627          gig::EngineChannel* pEngineChannel = dynamic_cast<gig::EngineChannel*>(pConsumer);          
628            // (try to resolve the audio device context)
629            EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(pConsumer);
630            Engine* pEngine = pEngineChannel ? dynamic_cast<Engine*>(pEngineChannel->GetEngine()) : NULL;
631            AudioOutputDevice* pDevice = (pEngine) ? pEngine->pAudioOutputDevice : NULL;
632            
633          uint maxSamplesPerCycle =          uint maxSamplesPerCycle =
634              (pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle()              (pDevice) ? pDevice->MaxSamplesPerCycle() : DefaultMaxSamplesPerCycle();
635                                            : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE;  
636          if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) {          if (pEntry->MaxSamplesPerCycle < maxSamplesPerCycle) {
637                dmsg(1,("Completely reloading instrument due to insufficient precached samples ...\n"));
638              Update(pResource, pConsumer);              Update(pResource, pConsumer);
639          }          }
640      }      }
641    
642        void InstrumentResourceManager::DeleteRegionIfNotUsed(::gig::DimensionRegion* pRegion, region_info_t* pRegInfo) {
643            // TODO: we could delete Region and Instrument here if they have become unused
644        }
645    
646        void InstrumentResourceManager::DeleteSampleIfNotUsed(::gig::Sample* pSample, region_info_t* pRegInfo) {
647            ::gig::File* gig = pRegInfo->file;
648            ::RIFF::File* riff = static_cast< ::RIFF::File*>(pRegInfo->pArg);
649            if (gig) {
650                gig->DeleteSample(pSample);
651                if (!gig->GetFirstSample()) {
652                    dmsg(2,("No more samples in use - freeing gig\n"));
653                    delete gig;
654                    delete riff;
655                }
656            }
657        }
658    
659        /**
660         * Just a wrapper around the other @c CacheInitialSamples() method.
661         *
662         *  @param pSample - points to the sample to be cached
663         *  @param pEngine - pointer to Gig Engine Channel which caused this call
664         *                   (may be NULL, in this case default amount of samples
665         *                   will be cached)
666         */
667        void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, EngineChannel* pEngineChannel) {
668            Engine* pEngine =
669                (pEngineChannel && pEngineChannel->GetEngine()) ?
670                    dynamic_cast<Engine*>(pEngineChannel->GetEngine()) : NULL;
671            CacheInitialSamples(pSample, pEngine);
672        }
673    
674      /**      /**
675       *  Caches a certain size at the beginning of the given sample in RAM. If the       *  Caches a certain size at the beginning of the given sample in RAM. If the
676       *  sample is very short, the whole sample will be loaded into RAM and thus       *  sample is very short, the whole sample will be loaded into RAM and thus
# Line 172  namespace LinuxSampler { namespace gig { Line 678  namespace LinuxSampler { namespace gig {
678       *  samples is needed to compensate disk reading latency.       *  samples is needed to compensate disk reading latency.
679       *       *
680       *  @param pSample - points to the sample to be cached       *  @param pSample - points to the sample to be cached
681       *  @param pEngineChannel - pointer to Gig Engine Channel which caused this call       *  @param pEngine - pointer to Gig Engine which caused this call
682         *                   (may be NULL, in this case default amount of samples
683         *                   will be cached)
684       */       */
685      void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, gig::EngineChannel* pEngineChannel) {      void InstrumentResourceManager::CacheInitialSamples(::gig::Sample* pSample, AbstractEngine* pEngine) {
686          if (!pSample) {          if (!pSample) {
687              dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n"));              dmsg(4,("gig::InstrumentResourceManager: Skipping sample (pSample == NULL)\n"));
688              return;              return;
# Line 188  namespace LinuxSampler { namespace gig { Line 696  namespace LinuxSampler { namespace gig {
696              // border, to allow the interpolator do it's work even at the end of              // border, to allow the interpolator do it's work even at the end of
697              // the sample.              // the sample.
698              const uint maxSamplesPerCycle =              const uint maxSamplesPerCycle =
699                  (pEngineChannel && pEngineChannel->GetEngine()) ? dynamic_cast<gig::Engine*>(pEngineChannel->GetEngine())->pAudioOutputDevice->MaxSamplesPerCycle()                  (pEngine) ? pEngine->pAudioOutputDevice->MaxSamplesPerCycle()
700                                                : GIG_RESOURCE_MANAGER_DEFAULT_MAX_SAMPLES_PER_CYCLE;                            : DefaultMaxSamplesPerCycle();
701              const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3;              const uint neededSilenceSamples = (maxSamplesPerCycle << CONFIG_MAX_PITCH) + 3;
702              const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize;              const uint currentlyCachedSilenceSamples = pSample->GetCache().NullExtensionSize / pSample->FrameSize;
703              if (currentlyCachedSilenceSamples < neededSilenceSamples) {              if (currentlyCachedSilenceSamples < neededSilenceSamples) {
# Line 205  namespace LinuxSampler { namespace gig { Line 713  namespace LinuxSampler { namespace gig {
713          if (!pSample->GetCache().Size) std::cerr << "Unable to cache sample - maybe memory full!" << std::endl << std::flush;          if (!pSample->GetCache().Size) std::cerr << "Unable to cache sample - maybe memory full!" << std::endl << std::flush;
714      }      }
715    
716        void InstrumentResourceManager::UncacheInitialSamples(::gig::Sample* pSample) {
717            dmsg(1,("Uncaching sample %x\n",pSample));
718            if (pSample->GetCache().Size) pSample->ReleaseSampleData();
719        }
720    
721        /**
722         * Returns a list with all instruments currently in use, that are part of
723         * the given file.
724         *
725         * @param pFile - search criteria
726         * @param bLock - whether we should lock (mutex) the instrument manager
727         *                during this call and unlock at the end of this call
728         */
729        std::vector< ::gig::Instrument*> InstrumentResourceManager::GetInstrumentsCurrentlyUsedOf(::gig::File* pFile, bool bLock) {
730            if (bLock) Lock();
731            std::vector< ::gig::Instrument*> result;
732            std::vector< ::gig::Instrument*> allInstruments = Resources(false/*don't lock again*/);
733            for (int i = 0; i < allInstruments.size(); i++)
734                if (
735                    (::gig::File*) allInstruments[i]->GetParent()
736                    == pFile
737                ) result.push_back(allInstruments[i]);
738            if (bLock) Unlock();
739            return result;
740        }
741    
742        /**
743         * Returns a list with all gig engine channels that are currently using
744         * the given instrument.
745         *
746         * @param pInstrument - search criteria
747         * @param bLock - whether we should lock (mutex) the instrument manager
748         *                during this call and unlock at the end of this call
749         */
750        std::set<EngineChannel*> InstrumentResourceManager::GetEngineChannelsUsing(::gig::Instrument* pInstrument, bool bLock) {
751            if (bLock) Lock();
752            std::set<EngineChannel*> result;
753            std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);
754            std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();
755            std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();
756            for (; iter != end; ++iter) {
757                EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter);
758                if (!pEngineChannel) continue;
759                result.insert(pEngineChannel);
760            }
761            if (bLock) Unlock();
762            return result;
763        }
764    
765        /**
766         * Returns a list with all gig Engines that are currently using the given
767         * instrument.
768         *
769         * @param pInstrument - search criteria
770         * @param bLock - whether we should lock (mutex) the instrument manager
771         *                during this call and unlock at the end of this call
772         */
773        std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::Instrument* pInstrument, bool bLock) {
774            if (bLock) Lock();
775            std::set<Engine*> result;
776            std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(pInstrument);
777            std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();
778            std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();
779            for (; iter != end; ++iter) {
780                EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter);
781                if (!pEngineChannel) continue;
782                Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine());
783                if (!pEngine) continue;
784                result.insert(pEngine);
785            }
786            if (bLock) Unlock();
787            return result;
788        }
789    
790        /**
791         * Returns a list with all gig Engines that are currently using an
792         * instrument that is part of the given instrument file.
793         *
794         * @param pFile - search criteria
795         * @param bLock - whether we should lock (mutex) the instrument manager
796         *                during this call and unlock at the end of this call
797         */
798        std::set<Engine*> InstrumentResourceManager::GetEnginesUsing(::gig::File* pFile, bool bLock) {
799            if (bLock) Lock();
800            // get all instruments (currently in usage) that use that same gig::File
801            std::vector< ::gig::Instrument*> instrumentsOfInterest =
802                GetInstrumentsCurrentlyUsedOf(pFile, false/*don't lock again*/);
803    
804            // get all engines that use that same gig::File
805            std::set<Engine*> result;
806            {
807                for (int i = 0; i < instrumentsOfInterest.size(); i++) {
808                    std::set<ResourceConsumer< ::gig::Instrument>*> consumers = ConsumersOf(instrumentsOfInterest[i]);
809                    std::set<ResourceConsumer< ::gig::Instrument>*>::iterator iter = consumers.begin();
810                    std::set<ResourceConsumer< ::gig::Instrument>*>::iterator end  = consumers.end();
811                    for (; iter != end; ++iter) {
812                        EngineChannel* pEngineChannel = dynamic_cast<EngineChannel*>(*iter);
813                        if (!pEngineChannel) continue;
814                        Engine* pEngine = dynamic_cast<Engine*>(pEngineChannel->GetEngine());
815                        if (!pEngine) continue;
816                        // the unique, sorted container std::set makes
817                        // sure we won't have duplicates
818                        result.insert(pEngine);
819                    }
820                }
821            }
822            if (bLock) Unlock();
823            return result;
824        }
825    
826        /**
827         * Returns @c true in case the given sample is referenced somewhere by the
828         * given instrument, @c false otherwise.
829         *
830         * @param pSample - sample reference
831         * @param pInstrument - instrument that might use that sample
832         */
833        bool InstrumentResourceManager::SampleReferencedByInstrument(::gig::Sample* pSample, ::gig::Instrument* pInstrument) {
834            for (
835                ::gig::Region* pRegion = pInstrument->GetFirstRegion();
836                pRegion; pRegion = pInstrument->GetNextRegion()
837            ) {
838                for (
839                    int i = 0; i < pRegion->DimensionRegions &&
840                    pRegion->pDimensionRegions[i]; i++
841                ) {
842                    if (pRegion->pDimensionRegions[i]->pSample == pSample)
843                        return true;
844                }
845            }
846            return false;
847        }
848    
849        /**
850         * Suspend all gig engines that use the given instrument. This means
851         * completely stopping playback on those engines and killing all their
852         * voices and disk streams. This method will block until all voices AND
853         * their disk streams are finally deleted and the engine turned into a
854         * complete idle loop.
855         *
856         * All @c SuspendEnginesUsing() methods only serve one thread by one and
857         * block all other threads until the current active thread called
858         * @c ResumeAllEngines() .
859         *
860         * @param pInstrument - search criteria
861         */
862        void InstrumentResourceManager::SuspendEnginesUsing(::gig::Instrument* pInstrument) {
863            // make sure no other thread suspends whole engines at the same time
864            suspendedEnginesMutex.Lock();
865            // get all engines that use that same gig::Instrument
866            suspendedEngines = GetEnginesUsing(pInstrument, true/*lock*/);
867            // finally, completely suspend all engines that use that same gig::Instrument
868            std::set<Engine*>::iterator iter = suspendedEngines.begin();
869            std::set<Engine*>::iterator end  = suspendedEngines.end();
870            for (; iter != end; ++iter) (*iter)->SuspendAll();
871        }
872    
873        /**
874         * Suspend all gig engines that use the given instrument file. This means
875         * completely stopping playback on those engines and killing all their
876         * voices and disk streams. This method will block until all voices AND
877         * their disk streams are finally deleted and the engine turned into a
878         * complete idle loop.
879         *
880         * All @c SuspendEnginesUsing() methods only serve one thread by one and
881         * block all other threads until the current active thread called
882         * @c ResumeAllEngines() .
883         *
884         * @param pFile - search criteria
885         */
886        void InstrumentResourceManager::SuspendEnginesUsing(::gig::File* pFile) {
887            // make sure no other thread suspends whole engines at the same time
888            suspendedEnginesMutex.Lock();
889            // get all engines that use that same gig::File
890            suspendedEngines = GetEnginesUsing(pFile, true/*lock*/);
891            // finally, completely suspend all engines that use that same gig::File
892            std::set<Engine*>::iterator iter = suspendedEngines.begin();
893            std::set<Engine*>::iterator end  = suspendedEngines.end();
894            for (; iter != end; ++iter) (*iter)->SuspendAll();
895        }
896    
897        /**
898         * MUST be called after one called one of the @c SuspendEnginesUsing()
899         * methods, to resume normal playback on all previously suspended engines.
900         * As it's only possible for one thread to suspend whole engines at the
901         * same time, this method doesn't take any arguments.
902         */
903        void InstrumentResourceManager::ResumeAllEngines() {
904            // resume all previously completely suspended engines
905            std::set<Engine*>::iterator iter = suspendedEngines.begin();
906            std::set<Engine*>::iterator end  = suspendedEngines.end();
907            for (; iter != end; ++iter) (*iter)->ResumeAll();
908            // no more suspended engines ...
909            suspendedEngines.clear();
910            // allow another thread to suspend whole engines
911            suspendedEnginesMutex.Unlock();
912        }
913    
914    
915    
916      // internal gig file manager      // internal gig file manager
# Line 219  namespace LinuxSampler { namespace gig { Line 925  namespace LinuxSampler { namespace gig {
925      }      }
926    
927      void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) {      void InstrumentResourceManager::GigResourceManager::Destroy(::gig::File* pResource, void* pArg) {
928          dmsg(1,("Freeing gig file from memory..."));          dmsg(1,("Freeing gig file '%s' from memory ...", pResource->GetFileName().c_str()));
929          delete pResource;  
930          delete (::RIFF::File*) pArg;          // Delete as much as possible of the gig file. Some of the
931            // dimension regions and samples may still be in use - these
932            // will be deleted later by the HandBackDimReg function.
933            bool deleteFile = true;
934            ::gig::Instrument* nextInstrument;
935            for (::gig::Instrument* instrument = pResource->GetFirstInstrument() ;
936                 instrument ;
937                 instrument = nextInstrument) {
938                nextInstrument = pResource->GetNextInstrument();
939                bool deleteInstrument = true;
940                ::gig::Region* nextRegion;
941                for (::gig::Region *region = instrument->GetFirstRegion() ;
942                     region ;
943                     region = nextRegion) {
944                    nextRegion = instrument->GetNextRegion();
945                    bool deleteRegion = true;
946                    for (int i = 0 ; i < region->DimensionRegions ; i++)
947                    {
948                        ::gig::DimensionRegion *d = region->pDimensionRegions[i];
949                        std::map< ::gig::DimensionRegion*, region_info_t>::iterator iter = parent->RegionInfo.find(d);
950                        if (iter != parent->RegionInfo.end()) {
951                            region_info_t& dimRegInfo = (*iter).second;
952                            dimRegInfo.file = pResource;
953                            dimRegInfo.pArg = (::RIFF::File*)pArg;
954                            deleteFile = deleteInstrument = deleteRegion = false;
955                        }
956                    }
957                    if (deleteRegion) instrument->DeleteRegion(region);
958                }
959                if (deleteInstrument) pResource->DeleteInstrument(instrument);
960            }
961            if (deleteFile) {
962                delete pResource;
963                delete (::RIFF::File*) pArg;
964            } else {
965                dmsg(2,("keeping some samples that are in use..."));
966                ::gig::Sample* nextSample;
967                for (::gig::Sample* sample = pResource->GetFirstSample() ;
968                     sample ;
969                     sample = nextSample) {
970                    nextSample = pResource->GetNextSample();
971                    if (parent->SampleRefCount.find(sample) == parent->SampleRefCount.end()) {
972                        pResource->DeleteSample(sample);
973                    }
974                }
975            }
976          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));
977      }      }
978    

Legend:
Removed from v.970  
changed lines
  Added in v.2309

  ViewVC Help
Powered by ViewVC