/[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 1765 by persson, Sat Sep 6 16:44:42 2008 UTC revision 1934 by schoenebeck, Sun Jul 12 10:35:55 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 - 2008 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2009 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 70  namespace LinuxSampler { Line 70  namespace LinuxSampler {
70    
71      void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {      void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {
72          dmsg(2,("SamplerChannel: Assigning engine type..."));          dmsg(2,("SamplerChannel: Assigning engine type..."));
73            
74          if (pEngineChannel) {          if (pEngineChannel) {
75              if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) {              if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) {
76                  dmsg(2,("OK\n"));                  dmsg(2,("OK\n"));
# Line 259  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 344  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 358  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 386  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 400  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 486  namespace LinuxSampler { Line 509  namespace LinuxSampler {
509          for (; iterChan != mSamplerChannels.end(); iterChan++) {          for (; iterChan != mSamplerChannels.end(); iterChan++) {
510              if (iterChan->second == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
511                  fireChannelToBeRemoved(pSamplerChannel);                  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 501  namespace LinuxSampler { Line 526  namespace LinuxSampler {
526          RemoveSamplerChannel(pChannel);          RemoveSamplerChannel(pChannel);
527      }      }
528    
529        void Sampler::RemoveAllSamplerChannels() {
530            /*
531             * In maps iterator invalidation occurs when the iterator point
532             * to the element that is being erased. So we need to copy the map
533             * by calling GetSamplerChannels() to prevent that.
534             */
535            SamplerChannelMap chns = GetSamplerChannels();
536            SamplerChannelMap::iterator iter = chns.begin();
537            for(; iter != chns.end(); iter++) {
538                RemoveSamplerChannel(iter->second);
539            }
540        }
541    
542      std::vector<String> Sampler::AvailableAudioOutputDrivers() {      std::vector<String> Sampler::AvailableAudioOutputDrivers() {
543          return AudioOutputDeviceFactory::AvailableDrivers();          return AudioOutputDeviceFactory::AvailableDrivers();
544      }      }
# Line 517  namespace LinuxSampler { Line 555  namespace LinuxSampler {
555          // create new device          // create new device
556          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
557    
         // add new audio device to the audio device list  
         for (uint i = 0; ; i++) { // seek for a free place starting from the beginning  
             if (!mAudioOutputDevices[i]) {  
                 mAudioOutputDevices[i] = pDevice;  
                 break;  
             }  
         }  
   
558          fireAudioDeviceCountChanged(AudioOutputDevices());          fireAudioDeviceCountChanged(AudioOutputDevices());
559          return pDevice;          return pDevice;
560      }      }
561    
562      uint Sampler::AudioOutputDevices() {      uint Sampler::AudioOutputDevices() {
563          return mAudioOutputDevices.size();          return AudioOutputDeviceFactory::Devices().size();
564      }      }
565    
566      uint Sampler::MidiInputDevices() {      uint Sampler::MidiInputDevices() {
567          return mMidiInputDevices.size();          return MidiInputDeviceFactory::Devices().size();
568      }      }
569    
570      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
571          return mAudioOutputDevices;          return AudioOutputDeviceFactory::Devices();
572      }      }
573    
574      std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {      std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
575          return mMidiInputDevices;          return MidiInputDeviceFactory::Devices();
576      }      }
577    
578      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
579          AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();          if (pDevice) {
580          for (; iter != mAudioOutputDevices.end(); iter++) {              // check if there are still sampler engines connected to this device
581              if (iter->second == pDevice) {              for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
582                  // check if there are still sampler engines connected to this device                   iterChan != mSamplerChannels.end(); iterChan++
583                  for (uint i = 0; i < SamplerChannels(); i++)              ) if (iterChan->second->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the audio output device.");
584                      if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device.");  
585                //TODO: should we add fireAudioDeviceToBeDestroyed() here ?
586                  // disable device              AudioOutputDeviceFactory::Destroy(pDevice);
587                  pDevice->Stop();              fireAudioDeviceCountChanged(AudioOutputDevices());
588            }
589        }
590    
591                  // remove device from the device list      void Sampler::DestroyAllAudioOutputDevices() throw (Exception) {
592                  mAudioOutputDevices.erase(iter);          /*
593             * In maps iterator invalidation occurs when the iterator point
594             * to the element that is being erased. So we need to copy the map
595             * by calling GetAudioOutputDevices() to prevent that.
596             */
597            std::map<uint, AudioOutputDevice*> devs = GetAudioOutputDevices();
598            std::map<uint, AudioOutputDevice*>::iterator iter = devs.begin();
599            for (; iter != devs.end(); iter++) {
600                AudioOutputDevice* pDevice = iter->second;
601    
602                  // destroy and free device from memory              // skip non-autonomous devices
603                  delete pDevice;              if (!pDevice->isAutonomousDevice()) continue;
604    
605                  fireAudioDeviceCountChanged(AudioOutputDevices());              DestroyAudioOutputDevice(pDevice);
                 break;  
             }  
606          }          }
607      }      }
608    
609      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
610          MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();          if (pDevice) {
611          for (; iter != mMidiInputDevices.end(); iter++) {              // check if there are still sampler engines connected to this device
612              if (iter->second == pDevice) {              for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
613                  // check if there are still sampler engines connected to this device                   iterChan != mSamplerChannels.end(); iterChan++
614                  for (uint i = 0; i < SamplerChannels(); i++)              ) if (iterChan->second->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device.");
615                      if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device.");  
616                fireMidiDeviceToBeDestroyed(pDevice);
617                  fireMidiDeviceToBeDestroyed(pDevice);              MidiInputDeviceFactory::Destroy(pDevice);
618                fireMidiDeviceCountChanged(MidiInputDevices());
619                  // disable device          }
620                  pDevice->StopListen();      }
621    
622                  // remove device from the device list      void Sampler::DestroyAllMidiInputDevices() throw (Exception) {
623                  mMidiInputDevices.erase(iter);          /*
624             * In maps iterator invalidation occurs when the iterator point
625             * to the element that is being erased. So we need to copy the map
626             * by calling GetMidiInputDevices() to prevent that.
627             */
628            std::map<uint, MidiInputDevice*> devs = GetMidiInputDevices();
629            std::map<uint, MidiInputDevice*>::iterator iter = devs.begin();
630            for (; iter != devs.end(); iter++) {
631                MidiInputDevice* pDevice = iter->second;
632    
633                  // destroy and free device from memory              // skip non-autonomous devices
634                  delete pDevice;              if (!pDevice->isAutonomousDevice()) continue;
635    
636                  fireMidiDeviceCountChanged(MidiInputDevices());              DestroyMidiInputDevice(pDevice);
                 break;  
             }  
637          }          }
638      }      }
639    
# Line 597  namespace LinuxSampler { Line 641  namespace LinuxSampler {
641          // create new device          // create new device
642          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
643    
         // add new device to the midi device list  
         for (uint i = 0; ; i++) { // seek for a free place starting from the beginning  
                 if (!mMidiInputDevices[i]) {  
                         mMidiInputDevices[i] = pDevice;  
                         break;  
                 }  
         }  
   
644          fireMidiDeviceCreated(pDevice);          fireMidiDeviceCreated(pDevice);
645          fireMidiDeviceCountChanged(MidiInputDevices());          fireMidiDeviceCountChanged(MidiInputDevices());
646          return pDevice;          return pDevice;
# Line 635  namespace LinuxSampler { Line 671  namespace LinuxSampler {
671      void Sampler::Reset() {      void Sampler::Reset() {
672          // delete sampler channels          // delete sampler channels
673          try {          try {
674              while (true) {              RemoveAllSamplerChannels();
                     SamplerChannelMap::iterator iter = mSamplerChannels.begin();  
                     if (iter == mSamplerChannels.end()) break;  
                     RemoveSamplerChannel(iter->second);  
             }  
675          }          }
676          catch(...) {          catch(...) {
677              std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;              std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
# Line 648  namespace LinuxSampler { Line 680  namespace LinuxSampler {
680    
681          // delete midi input devices          // delete midi input devices
682          try {          try {
683              while (true) {              DestroyAllMidiInputDevices();
                     MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();  
                     if (iter == mMidiInputDevices.end()) break;  
                     DestroyMidiInputDevice(iter->second);  
             }  
684          }          }
685          catch(...) {          catch(...) {
686              std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;              std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
# Line 661  namespace LinuxSampler { Line 689  namespace LinuxSampler {
689    
690          // delete audio output devices          // delete audio output devices
691          try {          try {
692              while (true) {              DestroyAllAudioOutputDevices();
                     AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();  
                     if (iter == mAudioOutputDevices.end()) break;  
                     DestroyAudioOutputDevice(iter->second);  
             }  
693          }          }
694          catch(...) {          catch(...) {
695              std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;              std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
# Line 711  namespace LinuxSampler { Line 735  namespace LinuxSampler {
735                  fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount());                  fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount());
736                  fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());                  fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());
737                  fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());                  fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());
                 fireTotalStreamCountChanged(GetDiskStreamCount());  
                 fireTotalVoiceCountChanged(GetVoiceCount());  
738              }              }
739    
740                fireTotalStreamCountChanged(GetDiskStreamCount());
741                fireTotalVoiceCountChanged(GetVoiceCount());
742    
743              LSCPServer::UnlockRTNotify();              LSCPServer::UnlockRTNotify();
744          }          }
745      }      }
746    
747    #if defined(WIN32)
748        static HINSTANCE dllInstance = NULL;
749    
750        String Sampler::GetInstallDir() {
751            char buf[MAX_PATH + 1];
752            if (GetModuleFileName(dllInstance, buf, MAX_PATH)) {
753                String s(buf);
754                size_t n = s.rfind('\\');
755                if (n != String::npos) {
756                    return s.substr(0, n);
757                }
758            }
759            return "";
760        }
761    #endif
762  } // namespace LinuxSampler  } // namespace LinuxSampler
763    
764    #if defined(WIN32)
765    extern "C" {
766        BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
767        {
768            switch (reason) {
769            case DLL_PROCESS_ATTACH:
770                LinuxSampler::dllInstance = instance;
771                break;
772            }
773            return TRUE;
774        }
775    }
776    #endif

Legend:
Removed from v.1765  
changed lines
  Added in v.1934

  ViewVC Help
Powered by ViewVC