/[svn]/linuxsampler/trunk/src/Sampler.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/Sampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1541 by iliev, Tue Dec 4 18:09:26 2007 UTC revision 1789 by iliev, Sat Nov 1 19:01:27 2008 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 - 2007 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2008 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library 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 32  Line 32 
32  #include "drivers/audio/AudioOutputDeviceFactory.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
33  #include "drivers/midi/MidiInputDeviceFactory.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
34  #include "drivers/midi/MidiInstrumentMapper.h"  #include "drivers/midi/MidiInstrumentMapper.h"
35    #include "common/Features.h"
36    #include "network/lscpserver.h"
37    
38  namespace LinuxSampler {  namespace LinuxSampler {
39    
# Line 76  namespace LinuxSampler { Line 78  namespace LinuxSampler {
78              }              }
79          }          }
80    
81            fireEngineToBeChanged();
82    
83          // create new engine channel          // create new engine channel
84          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
85          if (!pNewEngineChannel) throw Exception("Unknown engine type");          if (!pNewEngineChannel) throw Exception("Unknown engine type");
86    
87          //FIXME: hack to allow fast retrieval of engine channel's sampler channel index          pNewEngineChannel->SetSamplerChannel(this);
         pNewEngineChannel->iSamplerChannelIndex = Index();  
88    
89          // dereference midi input port.          // dereference midi input port.
90          MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());          MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
# Line 213  namespace LinuxSampler { Line 216  namespace LinuxSampler {
216          throw Exception("Internal error: SamplerChannel index not found");          throw Exception("Internal error: SamplerChannel index not found");
217      }      }
218    
219        Sampler* SamplerChannel::GetSampler() {
220            return pSampler;
221        }
222    
223      void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) {      void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) {
224          llEngineChangeListeners.AddListener(l);          llEngineChangeListeners.AddListener(l);
225      }      }
# Line 225  namespace LinuxSampler { Line 232  namespace LinuxSampler {
232         llEngineChangeListeners.RemoveAllListeners();         llEngineChangeListeners.RemoveAllListeners();
233      }      }
234    
235        void SamplerChannel::fireEngineToBeChanged() {
236            for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {
237                llEngineChangeListeners.GetListener(i)->EngineToBeChanged(Index());
238            }
239        }
240    
241      void SamplerChannel::fireEngineChanged() {      void SamplerChannel::fireEngineChanged() {
242          for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {          for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {
243              llEngineChangeListeners.GetListener(i)->EngineChanged(Index());              llEngineChangeListeners.GetListener(i)->EngineChanged(Index());
# Line 246  namespace LinuxSampler { Line 259  namespace LinuxSampler {
259    
260      Sampler::Sampler() {      Sampler::Sampler() {
261          eventHandler.SetSampler(this);          eventHandler.SetSampler(this);
262            uiOldTotalVoiceCount = uiOldTotalStreamCount = 0;
263      }      }
264    
265      Sampler::~Sampler() {      Sampler::~Sampler() {
# Line 270  namespace LinuxSampler { Line 284  namespace LinuxSampler {
284          }          }
285      }      }
286    
287        void Sampler::fireChannelAdded(SamplerChannel* pChannel) {
288            for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
289                llChannelCountListeners.GetListener(i)->ChannelAdded(pChannel);
290            }
291        }
292    
293        void Sampler::fireChannelToBeRemoved(SamplerChannel* pChannel) {
294            for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
295                llChannelCountListeners.GetListener(i)->ChannelToBeRemoved(pChannel);
296            }
297        }
298    
299      void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) {      void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) {
300          llAudioDeviceCountListeners.AddListener(l);          llAudioDeviceCountListeners.AddListener(l);
301      }      }
# Line 298  namespace LinuxSampler { Line 324  namespace LinuxSampler {
324          }          }
325      }      }
326    
327        void Sampler::fireMidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
328            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
329                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceToBeDestroyed(pDevice);
330            }
331        }
332    
333        void Sampler::fireMidiDeviceCreated(MidiInputDevice* pDevice) {
334            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
335                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCreated(pDevice);
336            }
337        }
338    
339      void Sampler::AddVoiceCountListener(VoiceCountListener* l) {      void Sampler::AddVoiceCountListener(VoiceCountListener* l) {
340          llVoiceCountListeners.AddListener(l);          llVoiceCountListeners.AddListener(l);
341      }      }
# Line 307  namespace LinuxSampler { Line 345  namespace LinuxSampler {
345      }      }
346    
347      void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) {      void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) {
348            std::map<uint, uint>::iterator it = mOldVoiceCounts.find(ChannelId);
349            if (it != mOldVoiceCounts.end()) {
350                uint oldCount = it->second;
351                if (NewCount == oldCount) return;
352            }
353    
354            mOldVoiceCounts[ChannelId] = NewCount;
355    
356          for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) {          for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) {
357              llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount);              llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount);
358          }          }
# Line 321  namespace LinuxSampler { Line 367  namespace LinuxSampler {
367      }      }
368    
369      void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) {      void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) {
370            std::map<uint, uint>::iterator it = mOldStreamCounts.find(ChannelId);
371            if (it != mOldStreamCounts.end()) {
372                uint oldCount = it->second;
373                if (NewCount == oldCount) return;
374            }
375    
376            mOldStreamCounts[ChannelId] = NewCount;
377    
378          for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) {          for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) {
379              llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount);              llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount);
380          }          }
# Line 349  namespace LinuxSampler { Line 403  namespace LinuxSampler {
403      }      }
404    
405      void Sampler::fireTotalStreamCountChanged(int NewCount) {      void Sampler::fireTotalStreamCountChanged(int NewCount) {
406            if (NewCount == uiOldTotalStreamCount) return;
407            uiOldTotalStreamCount = NewCount;
408    
409          for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) {          for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) {
410              llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount);              llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount);
411          }          }
# Line 363  namespace LinuxSampler { Line 420  namespace LinuxSampler {
420      }      }
421    
422      void Sampler::fireTotalVoiceCountChanged(int NewCount) {      void Sampler::fireTotalVoiceCountChanged(int NewCount) {
423            if (NewCount == uiOldTotalVoiceCount) return;
424            uiOldTotalVoiceCount = NewCount;
425    
426          for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) {          for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) {
427              llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount);              llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount);
428          }          }
# Line 382  namespace LinuxSampler { Line 442  namespace LinuxSampler {
442          }          }
443      }      }
444    
445        void Sampler::EventHandler::EngineToBeChanged(int ChannelId) {
446            // nothing to do here
447        }
448    
449      void Sampler::EventHandler::EngineChanged(int ChannelId) {      void Sampler::EventHandler::EngineChanged(int ChannelId) {
450          EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel();          EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel();
451          if(engineChannel == NULL) return;          if(engineChannel == NULL) return;
# Line 398  namespace LinuxSampler { Line 462  namespace LinuxSampler {
462          if (!mSamplerChannels.size()) {          if (!mSamplerChannels.size()) {
463              SamplerChannel* pChannel = new SamplerChannel(this);              SamplerChannel* pChannel = new SamplerChannel(this);
464              mSamplerChannels[0] = pChannel;              mSamplerChannels[0] = pChannel;
465                fireChannelAdded(pChannel);
466              fireChannelCountChanged(1);              fireChannelCountChanged(1);
467              pChannel->AddEngineChangeListener(&eventHandler);              pChannel->AddEngineChangeListener(&eventHandler);
468              return pChannel;              return pChannel;
# Line 414  namespace LinuxSampler { Line 479  namespace LinuxSampler {
479                  // we found an unused index, so insert the new channel there                  // we found an unused index, so insert the new channel there
480                  SamplerChannel* pChannel = new SamplerChannel(this);                  SamplerChannel* pChannel = new SamplerChannel(this);
481                  mSamplerChannels[i] = pChannel;                  mSamplerChannels[i] = pChannel;
482                    fireChannelAdded(pChannel);
483                  fireChannelCountChanged(SamplerChannels());                  fireChannelCountChanged(SamplerChannels());
484                  pChannel->AddEngineChangeListener(&eventHandler);                  pChannel->AddEngineChangeListener(&eventHandler);
485                  return pChannel;                  return pChannel;
# Line 424  namespace LinuxSampler { Line 490  namespace LinuxSampler {
490          // we have not reached the index limit so we just add the channel past the highest index          // we have not reached the index limit so we just add the channel past the highest index
491          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
492          mSamplerChannels[lastIndex + 1] = pChannel;          mSamplerChannels[lastIndex + 1] = pChannel;
493            fireChannelAdded(pChannel);
494          fireChannelCountChanged(SamplerChannels());          fireChannelCountChanged(SamplerChannels());
495          pChannel->AddEngineChangeListener(&eventHandler);          pChannel->AddEngineChangeListener(&eventHandler);
496          return pChannel;          return pChannel;
# Line 441  namespace LinuxSampler { Line 508  namespace LinuxSampler {
508          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
509          for (; iterChan != mSamplerChannels.end(); iterChan++) {          for (; iterChan != mSamplerChannels.end(); iterChan++) {
510              if (iterChan->second == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
511                    fireChannelToBeRemoved(pSamplerChannel);
512                    mOldVoiceCounts.erase(pSamplerChannel->Index());
513                    mOldStreamCounts.erase(pSamplerChannel->Index());
514                  pSamplerChannel->RemoveAllEngineChangeListeners();                  pSamplerChannel->RemoveAllEngineChangeListeners();
515                  mSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
516                  delete pSamplerChannel;                  delete pSamplerChannel;
# Line 505  namespace LinuxSampler { Line 575  namespace LinuxSampler {
575          for (; iter != mAudioOutputDevices.end(); iter++) {          for (; iter != mAudioOutputDevices.end(); iter++) {
576              if (iter->second == pDevice) {              if (iter->second == pDevice) {
577                  // check if there are still sampler engines connected to this device                  // check if there are still sampler engines connected to this device
578                  for (uint i = 0; i < SamplerChannels(); i++)                  for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
579                      if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device.");                       iterChan != mSamplerChannels.end(); iterChan++)
580                        if (iterChan->second->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the audio output device.");
581    
582                  // disable device                  // disable device
583                  pDevice->Stop();                  pDevice->Stop();
# Line 528  namespace LinuxSampler { Line 599  namespace LinuxSampler {
599          for (; iter != mMidiInputDevices.end(); iter++) {          for (; iter != mMidiInputDevices.end(); iter++) {
600              if (iter->second == pDevice) {              if (iter->second == pDevice) {
601                  // check if there are still sampler engines connected to this device                  // check if there are still sampler engines connected to this device
602                  for (uint i = 0; i < SamplerChannels(); i++)                  for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
603                      if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device.");                       iterChan != mSamplerChannels.end(); iterChan++)
604                        if (iterChan->second->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device.");
605    
606                    fireMidiDeviceToBeDestroyed(pDevice);
607    
608                  // disable device                  // disable device
609                  pDevice->StopListen();                  pDevice->StopListen();
# Line 558  namespace LinuxSampler { Line 632  namespace LinuxSampler {
632                  }                  }
633          }          }
634    
635            fireMidiDeviceCreated(pDevice);
636          fireMidiDeviceCountChanged(MidiInputDevices());          fireMidiDeviceCountChanged(MidiInputDevices());
637          return pDevice;          return pDevice;
638      }      }
# Line 637  namespace LinuxSampler { Line 712  namespace LinuxSampler {
712          InstrumentEditorFactory::ClosePlugins();          InstrumentEditorFactory::ClosePlugins();
713      }      }
714    
715        bool Sampler::EnableDenormalsAreZeroMode() {
716            Features::detect();
717            return Features::enableDenormalsAreZeroMode();
718        }
719    
720        void Sampler::fireStatistics() {
721            static const LSCPEvent::event_t eventsArr[] = {
722                LSCPEvent::event_voice_count, LSCPEvent::event_stream_count,
723                LSCPEvent::event_buffer_fill, LSCPEvent::event_total_voice_count
724            };
725            static const std::list<LSCPEvent::event_t> events(eventsArr, eventsArr + 4);
726    
727            if (LSCPServer::EventSubscribers(events))
728            {
729                LSCPServer::LockRTNotify();
730                std::map<uint,SamplerChannel*> channels = GetSamplerChannels();
731                std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
732                for (; iter != channels.end(); iter++) {
733                    SamplerChannel* pSamplerChannel = iter->second;
734                    EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
735                    if (!pEngineChannel) continue;
736                    Engine* pEngine = pEngineChannel->GetEngine();
737                    if (!pEngine) continue;
738                    fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount());
739                    fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());
740                    fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());
741                }
742                
743                fireTotalStreamCountChanged(GetDiskStreamCount());
744                fireTotalVoiceCountChanged(GetVoiceCount());
745    
746                LSCPServer::UnlockRTNotify();
747            }
748        }
749    
750  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.1541  
changed lines
  Added in v.1789

  ViewVC Help
Powered by ViewVC