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

Legend:
Removed from v.1038  
changed lines
  Added in v.1852

  ViewVC Help
Powered by ViewVC