/[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 1835 by iliev, Mon Feb 16 17:56:50 2009 UTC revision 2427 by persson, Sat Mar 2 07:03:04 2013 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 - 2013 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 88  namespace LinuxSampler { Line 88  namespace LinuxSampler {
88    
89          // dereference midi input port.          // dereference midi input port.
90          MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());          MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
91            midi_chan_t midiChannel = GetMidiInputChannel();
92          // disconnect old engine channel          // disconnect old engine channel
93          if (pEngineChannel) {          if (pEngineChannel) {
94              Engine* engine = pEngineChannel->GetEngine();              Engine* engine = pEngineChannel->GetEngine();
# Line 107  namespace LinuxSampler { Line 108  namespace LinuxSampler {
108              pNewEngineChannel->Connect(pAudioOutputDevice);              pNewEngineChannel->Connect(pAudioOutputDevice);
109              pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());              pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
110          }          }
111          if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());          if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, midiChannel);
112          pEngineChannel = pNewEngineChannel;          pEngineChannel = pNewEngineChannel;
113    
114          // from now on get MIDI device and port from EngineChannel object          // from now on get MIDI device and port from EngineChannel object
# Line 119  namespace LinuxSampler { Line 120  namespace LinuxSampler {
120          dmsg(2,("OK\n"));          dmsg(2,("OK\n"));
121      }      }
122    
123      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
124          if(pAudioOutputDevice == pDevice) return;          if(pAudioOutputDevice == pDevice) return;
125    
126          // disconnect old device          // disconnect old device
127          if (pAudioOutputDevice && pEngineChannel) {          if (pAudioOutputDevice && pEngineChannel) {
128                if (!pAudioOutputDevice->isAutonomousDevice())
129                    throw Exception("The audio output device '" + pAudioOutputDevice->Driver() + "' cannot be dropped from this sampler channel!");
130    
131              Engine* engine = pEngineChannel->GetEngine();              Engine* engine = pEngineChannel->GetEngine();
132              pAudioOutputDevice->Disconnect(engine);              pAudioOutputDevice->Disconnect(engine);
133    
# Line 142  namespace LinuxSampler { Line 146  namespace LinuxSampler {
146          }          }
147      }      }
148    
149      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
150         SetMidiInput(pDevice, 0, GetMidiInputChannel());         SetMidiInput(pDevice, 0, GetMidiInputChannel());
151      }      }
152    
153      void SamplerChannel::SetMidiInputPort(int MidiPort) {      void SamplerChannel::SetMidiInputPort(int MidiPort) throw (Exception) {
154         SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());         SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
155      }      }
156    
# Line 154  namespace LinuxSampler { Line 158  namespace LinuxSampler {
158         SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);         SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
159      }      }
160    
161      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) throw (Exception) {
162          if (!pDevice) throw Exception("No MIDI input device assigned.");          if (!pDevice) throw Exception("No MIDI input device assigned.");
163    
164          // get old and new midi input port          // get old and new midi input port
# Line 162  namespace LinuxSampler { Line 166  namespace LinuxSampler {
166          MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);          MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);
167    
168          // disconnect old device port          // disconnect old device port
169          if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel);          if (pOldMidiInputPort && pEngineChannel) {
170                MidiInputDevice* pOldDevice = pOldMidiInputPort->GetDevice();
171                if (pOldMidiInputPort != pNewMidiInputPort &&
172                    pOldDevice && !pOldDevice->isAutonomousDevice()
173                ) throw Exception("The MIDI input port '" + pOldDevice->Driver() + "' cannot be altered on this sampler channel!");
174    
175                pOldMidiInputPort->Disconnect(pEngineChannel);
176            }
177    
178          // remember new device, port and channel if not engine channel yet created          // remember new device, port and channel if not engine channel yet created
179          if (!pEngineChannel) {          if (!pEngineChannel) {
180              this->pMidiInputDevice = pDevice;              this->pMidiInputDevice = pDevice;
# Line 555  namespace LinuxSampler { Line 567  namespace LinuxSampler {
567          // create new device          // create new device
568          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
569    
         // 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;  
             }  
         }  
   
570          fireAudioDeviceCountChanged(AudioOutputDevices());          fireAudioDeviceCountChanged(AudioOutputDevices());
571          return pDevice;          return pDevice;
572      }      }
573    
574      uint Sampler::AudioOutputDevices() {      uint Sampler::AudioOutputDevices() {
575          return mAudioOutputDevices.size();          return AudioOutputDeviceFactory::Devices().size();
576      }      }
577    
578      uint Sampler::MidiInputDevices() {      uint Sampler::MidiInputDevices() {
579          return mMidiInputDevices.size();          return MidiInputDeviceFactory::Devices().size();
580      }      }
581    
582      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
583          return mAudioOutputDevices;          return AudioOutputDeviceFactory::Devices();
584      }      }
585    
586      std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {      std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
587          return mMidiInputDevices;          return MidiInputDeviceFactory::Devices();
588      }      }
589    
590      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
591          AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();          if (pDevice) {
592          for (; iter != mAudioOutputDevices.end(); iter++) {              // check if there are still sampler engines connected to this device
593              if (iter->second == pDevice) {              for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
594                  // check if there are still sampler engines connected to this device                   iterChan != mSamplerChannels.end(); iterChan++
595                  for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();              ) if (iterChan->second->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the audio output device.");
596                       iterChan != mSamplerChannels.end(); iterChan++)  
597                      if (iterChan->second->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the audio output device.");              //TODO: should we add fireAudioDeviceToBeDestroyed() here ?
598                AudioOutputDeviceFactory::Destroy(pDevice);
599                  // disable device              fireAudioDeviceCountChanged(AudioOutputDevices());
                 pDevice->Stop();  
   
                 // remove device from the device list  
                 mAudioOutputDevices.erase(iter);  
   
                 // destroy and free device from memory  
                 delete pDevice;  
   
                 fireAudioDeviceCountChanged(AudioOutputDevices());  
                 break;  
             }  
600          }          }
601      }      }
602    
# Line 613  namespace LinuxSampler { Line 606  namespace LinuxSampler {
606           * to the element that is being erased. So we need to copy the map           * to the element that is being erased. So we need to copy the map
607           * by calling GetAudioOutputDevices() to prevent that.           * by calling GetAudioOutputDevices() to prevent that.
608           */           */
609          AudioOutputDeviceMap devs = GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devs = GetAudioOutputDevices();
610          AudioOutputDeviceMap::iterator iter = devs.begin();          std::map<uint, AudioOutputDevice*>::iterator iter = devs.begin();
611          for(; iter != devs.end(); iter++) {          for (; iter != devs.end(); iter++) {
612              DestroyAudioOutputDevice(iter->second);              AudioOutputDevice* pDevice = iter->second;
613    
614                // skip non-autonomous devices
615                if (!pDevice->isAutonomousDevice()) continue;
616    
617                DestroyAudioOutputDevice(pDevice);
618          }          }
619      }      }
620    
621      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
622          MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();          if (pDevice) {
623          for (; iter != mMidiInputDevices.end(); iter++) {              // check if there are still sampler engines connected to this device
624              if (iter->second == pDevice) {              for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
625                  // check if there are still sampler engines connected to this device                   iterChan != mSamplerChannels.end(); iterChan++
626                  for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();              ) if (iterChan->second->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device.");
627                       iterChan != mSamplerChannels.end(); iterChan++)  
628                      if (iterChan->second->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device.");              fireMidiDeviceToBeDestroyed(pDevice);
629                MidiInputDeviceFactory::Destroy(pDevice);
630                  fireMidiDeviceToBeDestroyed(pDevice);              fireMidiDeviceCountChanged(MidiInputDevices());
   
                 // disable device  
                 pDevice->StopListen();  
   
                 // remove device from the device list  
                 mMidiInputDevices.erase(iter);  
   
                 // destroy and free device from memory  
                 delete pDevice;  
   
                 fireMidiDeviceCountChanged(MidiInputDevices());  
                 break;  
             }  
631          }          }
632      }      }
633    
# Line 652  namespace LinuxSampler { Line 637  namespace LinuxSampler {
637           * to the element that is being erased. So we need to copy the map           * to the element that is being erased. So we need to copy the map
638           * by calling GetMidiInputDevices() to prevent that.           * by calling GetMidiInputDevices() to prevent that.
639           */           */
640          MidiInputDeviceMap devs = GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devs = GetMidiInputDevices();
641          MidiInputDeviceMap::iterator iter = devs.begin();          std::map<uint, MidiInputDevice*>::iterator iter = devs.begin();
642          for(; iter != devs.end(); iter++) {          for (; iter != devs.end(); iter++) {
643              DestroyMidiInputDevice(iter->second);              MidiInputDevice* pDevice = iter->second;
644    
645                // skip non-autonomous devices
646                if (!pDevice->isAutonomousDevice()) continue;
647    
648                DestroyMidiInputDevice(pDevice);
649          }          }
650      }      }
651    
# Line 663  namespace LinuxSampler { Line 653  namespace LinuxSampler {
653          // create new device          // create new device
654          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
655    
         // 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;  
                 }  
         }  
   
656          fireMidiDeviceCreated(pDevice);          fireMidiDeviceCreated(pDevice);
657          fireMidiDeviceCountChanged(MidiInputDevices());          fireMidiDeviceCountChanged(MidiInputDevices());
658          return pDevice;          return pDevice;
# Line 698  namespace LinuxSampler { Line 680  namespace LinuxSampler {
680          return count;          return count;
681      }      }
682    
683        int Sampler::GetGlobalMaxVoices() {
684            return GLOBAL_MAX_VOICES; // see common/global_private.cpp
685        }
686    
687        int Sampler::GetGlobalMaxStreams() {
688            return GLOBAL_MAX_STREAMS; // see common/global_private.cpp
689        }
690    
691        void Sampler::SetGlobalMaxVoices(int n) throw (Exception) {
692            if (n < 1) throw Exception("Maximum voices may not be less than 1");
693            GLOBAL_MAX_VOICES = n; // see common/global_private.cpp
694            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
695            if (engines.size() > 0) {
696                std::set<Engine*>::iterator iter = engines.begin();
697                std::set<Engine*>::iterator end  = engines.end();
698                for (; iter != end; ++iter) {
699                    (*iter)->SetMaxVoices(n);
700                }
701            }
702        }
703    
704        void Sampler::SetGlobalMaxStreams(int n) throw (Exception) {
705            if (n < 0) throw Exception("Maximum disk streams may not be negative");
706            GLOBAL_MAX_STREAMS = n; // see common/global_private.cpp
707            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
708            if (engines.size() > 0) {
709                std::set<Engine*>::iterator iter = engines.begin();
710                std::set<Engine*>::iterator end  = engines.end();
711                for (; iter != end; ++iter) {
712                    (*iter)->SetMaxDiskStreams(n);
713                }
714            }
715        }
716    
717      void Sampler::Reset() {      void Sampler::Reset() {
718          // delete sampler channels          // delete sampler channels
719          try {          try {
720              RemoveAllSamplerChannels();              RemoveAllSamplerChannels();
721          }          }
722          catch(...) {          catch(...) {
723              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 710  namespace LinuxSampler { Line 726  namespace LinuxSampler {
726    
727          // delete midi input devices          // delete midi input devices
728          try {          try {
729              DestroyAllMidiInputDevices();              DestroyAllMidiInputDevices();
730          }          }
731          catch(...) {          catch(...) {
732              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 719  namespace LinuxSampler { Line 735  namespace LinuxSampler {
735    
736          // delete audio output devices          // delete audio output devices
737          try {          try {
738              DestroyAllAudioOutputDevices();              DestroyAllAudioOutputDevices();
739          }          }
740          catch(...) {          catch(...) {
741              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 753  namespace LinuxSampler { Line 769  namespace LinuxSampler {
769    
770          if (LSCPServer::EventSubscribers(events))          if (LSCPServer::EventSubscribers(events))
771          {          {
772              LSCPServer::LockRTNotify();              LockGuard lock(LSCPServer::RTNotifyMutex);
773              std::map<uint,SamplerChannel*> channels = GetSamplerChannels();              std::map<uint,SamplerChannel*> channels = GetSamplerChannels();
774              std::map<uint,SamplerChannel*>::iterator iter = channels.begin();              std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
775              for (; iter != channels.end(); iter++) {              for (; iter != channels.end(); iter++) {
# Line 766  namespace LinuxSampler { Line 782  namespace LinuxSampler {
782                  fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());                  fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());
783                  fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());                  fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());
784              }              }
785                
786              fireTotalStreamCountChanged(GetDiskStreamCount());              fireTotalStreamCountChanged(GetDiskStreamCount());
787              fireTotalVoiceCountChanged(GetVoiceCount());              fireTotalVoiceCountChanged(GetVoiceCount());
   
             LSCPServer::UnlockRTNotify();  
788          }          }
789      }      }
790    
791    #if defined(WIN32)
792        static HINSTANCE dllInstance = NULL;
793    
794        String Sampler::GetInstallDir() {
795            char buf[MAX_PATH + 1];
796            if (GetModuleFileName(dllInstance, buf, MAX_PATH)) {
797                String s(buf);
798                size_t n = s.rfind('\\');
799                if (n != String::npos) {
800                    return s.substr(0, n);
801                }
802            }
803            return "";
804        }
805    #endif
806  } // namespace LinuxSampler  } // namespace LinuxSampler
807    
808    #if defined(WIN32)
809    extern "C" {
810        BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
811        {
812            switch (reason) {
813            case DLL_PROCESS_ATTACH:
814                LinuxSampler::dllInstance = instance;
815                break;
816            }
817            return TRUE;
818        }
819    }
820    #endif

Legend:
Removed from v.1835  
changed lines
  Added in v.2427

  ViewVC Help
Powered by ViewVC