/[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 1897 by persson, Sun May 10 09:31:51 2009 UTC revision 2500 by schoenebeck, Fri Jan 10 12:20:05 2014 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 - 2009 Christian Schoenebeck                       *   *   Copyright (C) 2005 - 2014 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 44  namespace LinuxSampler { Line 44  namespace LinuxSampler {
44          pSampler           = pS;          pSampler           = pS;
45          pEngineChannel     = NULL;          pEngineChannel     = NULL;
46          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
         pMidiInputDevice   = NULL;  
47          iMidiPort          = 0;          iMidiPort          = 0;
48          midiChannel        = midi_chan_all;          midiChannel        = midi_chan_all;
49          iIndex             = -1;          iIndex             = -1;
# Line 55  namespace LinuxSampler { Line 54  namespace LinuxSampler {
54              Engine* engine = pEngineChannel->GetEngine();              Engine* engine = pEngineChannel->GetEngine();
55              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
56    
             MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel());  
             if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);  
57              if (pEngineChannel) {              if (pEngineChannel) {
58                    pEngineChannel->DisconnectAllMidiInputPorts();
59                    
60                  if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();                  if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
61                  EngineChannelFactory::Destroy(pEngineChannel);                  EngineChannelFactory::Destroy(pEngineChannel);
62    
# Line 70  namespace LinuxSampler { Line 69  namespace LinuxSampler {
69    
70      void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {      void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {
71          dmsg(2,("SamplerChannel: Assigning engine type..."));          dmsg(2,("SamplerChannel: Assigning engine type..."));
72            
73          if (pEngineChannel) {          if (pEngineChannel) {
74              if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) {              if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) {
75                  dmsg(2,("OK\n"));                  dmsg(2,("OK\n"));
# Line 84  namespace LinuxSampler { Line 83  namespace LinuxSampler {
83          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
84          if (!pNewEngineChannel) throw Exception("Unknown engine type");          if (!pNewEngineChannel) throw Exception("Unknown engine type");
85    
86          pNewEngineChannel->SetSamplerChannel(this);          // remember current MIDI input connections
87            std::vector<MidiInputPort*> vMidiInputs = GetMidiInputPorts();
88            midi_chan_t midiChannel = GetMidiInputChannel();
89            
90            try {
91                pNewEngineChannel->SetSamplerChannel(this);
92    
93          // dereference midi input port.              // disconnect old engine channel
94          MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());              if (pEngineChannel) {
95          // disconnect old engine channel                  Engine* engine = pEngineChannel->GetEngine();
96          if (pEngineChannel) {                  if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
             Engine* engine = pEngineChannel->GetEngine();  
             if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);  
97    
98              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);                  pEngineChannel->DisconnectAllMidiInputPorts();
99              if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();                  if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
100              EngineChannelFactory::Destroy(pEngineChannel);                  EngineChannelFactory::Destroy(pEngineChannel);
101                    pEngineChannel = NULL;
102    
103              // reconnect engine if it still exists                  // reconnect engine if it still exists
104              const std::set<Engine*>& engines = EngineFactory::EngineInstances();                  const std::set<Engine*>& engines = EngineFactory::EngineInstances();
105              if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);                  if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
106          }              }
107    
108          // connect new engine channel              // connect new engine channel
109          if (pAudioOutputDevice) {              if (pAudioOutputDevice) {
110              pNewEngineChannel->Connect(pAudioOutputDevice);                  pNewEngineChannel->Connect(pAudioOutputDevice);
111              pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());                  pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
112                }
113                pNewEngineChannel->SetMidiChannel(midiChannel);
114                for (int i = 0; i < vMidiInputs.size(); ++i) {
115                    pNewEngineChannel->Connect(vMidiInputs[i]);
116                }
117            } catch (...) {
118                EngineChannelFactory::Destroy(pNewEngineChannel);
119                throw; // re-throw the same exception
120          }          }
         if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());  
121          pEngineChannel = pNewEngineChannel;          pEngineChannel = pNewEngineChannel;
122    
123          // from now on get MIDI device and port from EngineChannel object          // from now on get MIDI input ports from EngineChannel object
124          this->pMidiInputDevice = NULL;          this->vMidiInputs.clear();
125          this->iMidiPort        = 0;          this->iMidiPort        = 0;
126    
127          pEngineChannel->StatusChanged(true);          pEngineChannel->StatusChanged(true);
# Line 119  namespace LinuxSampler { Line 129  namespace LinuxSampler {
129          dmsg(2,("OK\n"));          dmsg(2,("OK\n"));
130      }      }
131    
132      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
133          if(pAudioOutputDevice == pDevice) return;          if(pAudioOutputDevice == pDevice) return;
134    
135          // disconnect old device          // disconnect old device
136          if (pAudioOutputDevice && pEngineChannel) {          if (pAudioOutputDevice && pEngineChannel) {
137                if (!pAudioOutputDevice->isAutonomousDevice())
138                    throw Exception("The audio output device '" + pAudioOutputDevice->Driver() + "' cannot be dropped from this sampler channel!");
139    
140              Engine* engine = pEngineChannel->GetEngine();              Engine* engine = pEngineChannel->GetEngine();
141              pAudioOutputDevice->Disconnect(engine);              pAudioOutputDevice->Disconnect(engine);
142    
# Line 142  namespace LinuxSampler { Line 155  namespace LinuxSampler {
155          }          }
156      }      }
157    
158      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {      void SamplerChannel::Connect(MidiInputPort* pPort) throw (Exception) {
159            if (!pPort) throw Exception("No MIDI input port provided");
160    
161            // prevent attempts to connect non-autonomous MIDI ports
162            // (host plugins like VST, AU, LV2, DSSI)
163            if (!pPort->GetDevice()->isAutonomousDevice())
164                throw Exception("The MIDI input port '" + pPort->GetDevice()->Driver() + "' cannot be managed manually!");
165    
166            std::vector<MidiInputPort*> vMidiPorts = GetMidiInputPorts();
167    
168            // ignore if port is already connected
169            for (int i = 0; i < vMidiPorts.size(); ++i) {
170                if (vMidiPorts[i] == pPort) return;
171            }
172    
173            // connect this new port
174            if (pEngineChannel) {
175                pEngineChannel->Connect(pPort);
176            } else { // no engine channel yet, remember it for future connection ...
177                const midi_conn_t c = {
178                    pPort->GetDevice()->MidiInputDeviceID(),
179                    pPort->GetPortNumber()
180                };
181                this->vMidiInputs.push_back(c);
182            }
183        }
184    
185        void SamplerChannel::Disconnect(MidiInputPort* pPort) throw (Exception) {
186            if (!pPort) return;
187    
188            // prevent attempts to alter channels with non-autonomous devices
189            // (host plugins like VST, AU, LV2, DSSI)
190            if (!pPort->GetDevice()->isAutonomousDevice())
191                throw Exception("The MIDI input port '" + pPort->GetDevice()->Driver() + "' cannot be managed manually!");
192    
193            // disconnect this port
194            if (pEngineChannel) {
195                pEngineChannel->Disconnect(pPort);
196            } else { // no engine channel yet, forget it regarding future connection ...
197                const midi_conn_t c = {
198                    pPort->GetDevice()->MidiInputDeviceID(),
199                    pPort->GetPortNumber()
200                };
201                for (int i = this->vMidiInputs.size() - 1; i >= 0; --i) {
202                    if (this->vMidiInputs[i] == c)
203                        this->vMidiInputs.erase(this->vMidiInputs.begin() + i);
204                    // no break or return here, for safety reasons
205                    // (just in case there were really duplicates for some reason)
206                }
207            }
208        }
209    
210        void SamplerChannel::DisconnectAllMidiInputPorts() throw (Exception) {
211            std::vector<MidiInputPort*> vMidiPorts = GetMidiInputPorts();
212            for (int i = 0; i < vMidiPorts.size(); ++i) Disconnect(vMidiPorts[i]);
213        }
214    
215        std::vector<MidiInputPort*> SamplerChannel::GetMidiInputPorts() {
216            std::vector<MidiInputPort*> v;
217            if (pEngineChannel) {
218                MidiInputPort* pPort = pEngineChannel->GetMidiInputPort(0);
219                for (int i = 0; pPort; pPort = pEngineChannel->GetMidiInputPort(++i))
220                    v.push_back(pPort);
221            } else {
222                for (int i = 0; i < this->vMidiInputs.size(); ++i) {
223                    MidiInputPort* pPort = _getPortForID(this->vMidiInputs[i]);
224                    if (pPort) v.push_back(pPort);
225                }
226            }
227            return v;
228        }
229    
230        void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
231         SetMidiInput(pDevice, 0, GetMidiInputChannel());         SetMidiInput(pDevice, 0, GetMidiInputChannel());
232      }      }
233    
234      void SamplerChannel::SetMidiInputPort(int MidiPort) {      void SamplerChannel::SetMidiInputPort(int MidiPort) throw (Exception) {
235         SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());         SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
236      }      }
237    
238      void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
239         SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);          if (!isValidMidiChan(MidiChannel)) throw Exception("Invalid MIDI channel (" + ToString(int(MidiChannel)) + ")");
240            if (pEngineChannel) pEngineChannel->SetMidiChannel(MidiChannel);
241            this->midiChannel = MidiChannel;
242      }      }
243    
244      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) throw (Exception) {
245          if (!pDevice) throw Exception("No MIDI input device assigned.");          if (!pDevice) throw Exception("No MIDI input device assigned.");
246    
247          // get old and new midi input port          // apply new MIDI channel
248          MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());          SetMidiInputChannel(MidiChannel);
249          MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);  
250            MidiInputPort* pNewPort = pDevice->GetPort(iMidiPort);
251          // disconnect old device port          if (!pNewPort) throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
252          if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel);  
253          // remember new device, port and channel if not engine channel yet created          std::vector<MidiInputPort*> vMidiPorts = GetMidiInputPorts();
254          if (!pEngineChannel) {  
255              this->pMidiInputDevice = pDevice;          // prevent attempts to remove non-autonomous MIDI ports
256              this->iMidiPort        = iMidiPort;          // (host plugins like VST, AU, LV2, DSSI)
257              this->midiChannel      = MidiChannel;          for (int i = 0; i < vMidiPorts.size(); ++i) {
258          }              if (vMidiPorts[i] == pNewPort) continue;
259                if (!vMidiPorts[i]->GetDevice()->isAutonomousDevice())
260          // connect new device port                  throw Exception("The MIDI input port '" + vMidiPorts[i]->GetDevice()->Driver() + "' cannot be altered on this sampler channel!");
261          if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);          }
262          // Ooops.  
263          if (pNewMidiInputPort == NULL)          if (pEngineChannel) {
264              throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + ".");              // remove all current connections
265                pEngineChannel->DisconnectAllMidiInputPorts();
266                // create the new connection (alone)
267                pEngineChannel->Connect(pNewPort);
268            } else { // if there is no engine channel yet, then store connection for future ...
269                // delete all previously scheduled connections
270                this->vMidiInputs.clear();
271                // store the new connection (alone)
272                const midi_conn_t c = {
273                    pNewPort->GetDevice()->MidiInputDeviceID(),
274                    pNewPort->GetPortNumber()
275                };
276                this->vMidiInputs.push_back(c);
277                this->iMidiPort = iMidiPort;
278            }
279      }      }
280    
281      EngineChannel* SamplerChannel::GetEngineChannel() {      EngineChannel* SamplerChannel::GetEngineChannel() {
# Line 187  namespace LinuxSampler { Line 288  namespace LinuxSampler {
288      }      }
289    
290      int SamplerChannel::GetMidiInputPort() {      int SamplerChannel::GetMidiInputPort() {
291          MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;          MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort(0) : NULL;
292          if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();          if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
293          return iMidiPort;          return iMidiPort;
294      }      }
# Line 198  namespace LinuxSampler { Line 299  namespace LinuxSampler {
299    
300      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
301          if (pEngineChannel)          if (pEngineChannel)
302              pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;              return (pEngineChannel->GetMidiInputPort(0)) ? pEngineChannel->GetMidiInputPort(0)->GetDevice() : NULL;
303          return pMidiInputDevice;  
304            if (vMidiInputs.empty())
305                return NULL;
306    
307            std::map<uint, MidiInputDevice*> mAllDevices = MidiInputDeviceFactory::Devices();
308            if (!mAllDevices.count(vMidiInputs[0].deviceID))
309                return NULL;
310    
311            return mAllDevices[vMidiInputs[0].deviceID];
312      }      }
313    
314      uint SamplerChannel::Index() {      uint SamplerChannel::Index() {
# Line 243  namespace LinuxSampler { Line 352  namespace LinuxSampler {
352              llEngineChangeListeners.GetListener(i)->EngineChanged(Index());              llEngineChangeListeners.GetListener(i)->EngineChanged(Index());
353          }          }
354      }      }
355        
356        /**
357         * Takes a numeric MIDI device ID, port ID pair as argument and returns
358         * the actual MIDI input port associated with that unique ID pair.
359         */
360        MidiInputPort* SamplerChannel::_getPortForID(const midi_conn_t& c) {
361            std::map<uint, MidiInputDevice*> mAllDevices = MidiInputDeviceFactory::Devices();
362            if (!mAllDevices.count(c.deviceID))
363                return NULL;
364    
365      MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {          return mAllDevices[c.deviceID]->GetPort(c.portNr);
         MidiInputPort* pMidiInputPort = NULL;  
         MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();  
         if (pMidiInputDevice)  
             pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);  
         return pMidiInputPort;  
366      }      }
367    
368    
   
369      // ******************************************************************      // ******************************************************************
370      // * Sampler      // * Sampler
371    
# Line 555  namespace LinuxSampler { Line 667  namespace LinuxSampler {
667          // create new device          // create new device
668          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
669    
         // 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;  
             }  
         }  
   
670          fireAudioDeviceCountChanged(AudioOutputDevices());          fireAudioDeviceCountChanged(AudioOutputDevices());
671          return pDevice;          return pDevice;
672      }      }
673    
674      uint Sampler::AudioOutputDevices() {      uint Sampler::AudioOutputDevices() {
675          return mAudioOutputDevices.size();          return AudioOutputDeviceFactory::Devices().size();
676      }      }
677    
678      uint Sampler::MidiInputDevices() {      uint Sampler::MidiInputDevices() {
679          return mMidiInputDevices.size();          return MidiInputDeviceFactory::Devices().size();
680      }      }
681    
682      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
683          return mAudioOutputDevices;          return AudioOutputDeviceFactory::Devices();
684      }      }
685    
686      std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {      std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
687          return mMidiInputDevices;          return MidiInputDeviceFactory::Devices();
688      }      }
689    
690      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
691          AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();          if (pDevice) {
692          for (; iter != mAudioOutputDevices.end(); iter++) {              // check if there are still sampler engines connected to this device
693              if (iter->second == pDevice) {              for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
694                  // check if there are still sampler engines connected to this device                   iterChan != mSamplerChannels.end(); iterChan++
695                  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.");
696                       iterChan != mSamplerChannels.end(); iterChan++)  
697                      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 ?
698                AudioOutputDeviceFactory::Destroy(pDevice);
699                  // 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;  
             }  
700          }          }
701      }      }
702    
# Line 613  namespace LinuxSampler { Line 706  namespace LinuxSampler {
706           * 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
707           * by calling GetAudioOutputDevices() to prevent that.           * by calling GetAudioOutputDevices() to prevent that.
708           */           */
709          AudioOutputDeviceMap devs = GetAudioOutputDevices();          std::map<uint, AudioOutputDevice*> devs = GetAudioOutputDevices();
710          AudioOutputDeviceMap::iterator iter = devs.begin();          std::map<uint, AudioOutputDevice*>::iterator iter = devs.begin();
711          for(; iter != devs.end(); iter++) {          for (; iter != devs.end(); iter++) {
712              DestroyAudioOutputDevice(iter->second);              AudioOutputDevice* pDevice = iter->second;
713    
714                // skip non-autonomous devices
715                if (!pDevice->isAutonomousDevice()) continue;
716    
717                DestroyAudioOutputDevice(pDevice);
718          }          }
719      }      }
720    
721      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
722          MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();          if (pDevice) {
723          for (; iter != mMidiInputDevices.end(); iter++) {              // check if there are still sampler engines connected to this device
724              if (iter->second == pDevice) {              for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
725                  // check if there are still sampler engines connected to this device                   iterChan != mSamplerChannels.end(); ++iterChan)
726                  for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();              {
727                       iterChan != mSamplerChannels.end(); iterChan++)                  std::vector<MidiInputPort*> vPorts = iterChan->second->GetMidiInputPorts();
728                      if (iterChan->second->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device.");                  for (int k = 0; k < vPorts.size(); ++k)
729                        if (vPorts[k]->GetDevice() == pDevice)
730                  fireMidiDeviceToBeDestroyed(pDevice);                          throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device.");
   
                 // disable device  
                 pDevice->StopListen();  
   
                 // remove device from the device list  
                 mMidiInputDevices.erase(iter);  
   
                 // destroy and free device from memory  
                 delete pDevice;  
   
                 fireMidiDeviceCountChanged(MidiInputDevices());  
                 break;  
731              }              }
732    
733                fireMidiDeviceToBeDestroyed(pDevice);
734                MidiInputDeviceFactory::Destroy(pDevice);
735                fireMidiDeviceCountChanged(MidiInputDevices());
736          }          }
737      }      }
738    
# Line 652  namespace LinuxSampler { Line 742  namespace LinuxSampler {
742           * 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
743           * by calling GetMidiInputDevices() to prevent that.           * by calling GetMidiInputDevices() to prevent that.
744           */           */
745          MidiInputDeviceMap devs = GetMidiInputDevices();          std::map<uint, MidiInputDevice*> devs = GetMidiInputDevices();
746          MidiInputDeviceMap::iterator iter = devs.begin();          std::map<uint, MidiInputDevice*>::iterator iter = devs.begin();
747          for(; iter != devs.end(); iter++) {          for (; iter != devs.end(); iter++) {
748              DestroyMidiInputDevice(iter->second);              MidiInputDevice* pDevice = iter->second;
749    
750                // skip non-autonomous devices
751                if (!pDevice->isAutonomousDevice()) continue;
752    
753                DestroyMidiInputDevice(pDevice);
754          }          }
755      }      }
756    
# Line 663  namespace LinuxSampler { Line 758  namespace LinuxSampler {
758          // create new device          // create new device
759          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
760    
         // 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;  
                 }  
         }  
   
761          fireMidiDeviceCreated(pDevice);          fireMidiDeviceCreated(pDevice);
762          fireMidiDeviceCountChanged(MidiInputDevices());          fireMidiDeviceCountChanged(MidiInputDevices());
763          return pDevice;          return pDevice;
# Line 698  namespace LinuxSampler { Line 785  namespace LinuxSampler {
785          return count;          return count;
786      }      }
787    
788        int Sampler::GetGlobalMaxVoices() {
789            return GLOBAL_MAX_VOICES; // see common/global_private.cpp
790        }
791    
792        int Sampler::GetGlobalMaxStreams() {
793            return GLOBAL_MAX_STREAMS; // see common/global_private.cpp
794        }
795    
796        void Sampler::SetGlobalMaxVoices(int n) throw (Exception) {
797            if (n < 1) throw Exception("Maximum voices may not be less than 1");
798            GLOBAL_MAX_VOICES = n; // see common/global_private.cpp
799            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
800            if (engines.size() > 0) {
801                std::set<Engine*>::iterator iter = engines.begin();
802                std::set<Engine*>::iterator end  = engines.end();
803                for (; iter != end; ++iter) {
804                    (*iter)->SetMaxVoices(n);
805                }
806            }
807        }
808    
809        void Sampler::SetGlobalMaxStreams(int n) throw (Exception) {
810            if (n < 0) throw Exception("Maximum disk streams may not be negative");
811            GLOBAL_MAX_STREAMS = n; // see common/global_private.cpp
812            const std::set<Engine*>& engines = EngineFactory::EngineInstances();
813            if (engines.size() > 0) {
814                std::set<Engine*>::iterator iter = engines.begin();
815                std::set<Engine*>::iterator end  = engines.end();
816                for (; iter != end; ++iter) {
817                    (*iter)->SetMaxDiskStreams(n);
818                }
819            }
820        }
821    
822      void Sampler::Reset() {      void Sampler::Reset() {
823          // delete sampler channels          // delete sampler channels
824          try {          try {
825              RemoveAllSamplerChannels();              RemoveAllSamplerChannels();
826          }          }
827          catch(...) {          catch(...) {
828              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 831  namespace LinuxSampler {
831    
832          // delete midi input devices          // delete midi input devices
833          try {          try {
834              DestroyAllMidiInputDevices();              DestroyAllMidiInputDevices();
835          }          }
836          catch(...) {          catch(...) {
837              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 840  namespace LinuxSampler {
840    
841          // delete audio output devices          // delete audio output devices
842          try {          try {
843              DestroyAllAudioOutputDevices();              DestroyAllAudioOutputDevices();
844          }          }
845          catch(...) {          catch(...) {
846              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 874  namespace LinuxSampler {
874    
875          if (LSCPServer::EventSubscribers(events))          if (LSCPServer::EventSubscribers(events))
876          {          {
877              LSCPServer::LockRTNotify();              LockGuard lock(LSCPServer::RTNotifyMutex);
878              std::map<uint,SamplerChannel*> channels = GetSamplerChannels();              std::map<uint,SamplerChannel*> channels = GetSamplerChannels();
879              std::map<uint,SamplerChannel*>::iterator iter = channels.begin();              std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
880              for (; iter != channels.end(); iter++) {              for (; iter != channels.end(); iter++) {
# Line 766  namespace LinuxSampler { Line 887  namespace LinuxSampler {
887                  fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());                  fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());
888                  fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());                  fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());
889              }              }
890                
891              fireTotalStreamCountChanged(GetDiskStreamCount());              fireTotalStreamCountChanged(GetDiskStreamCount());
892              fireTotalVoiceCountChanged(GetVoiceCount());              fireTotalVoiceCountChanged(GetVoiceCount());
   
             LSCPServer::UnlockRTNotify();  
893          }          }
894      }      }
895    

Legend:
Removed from v.1897  
changed lines
  Added in v.2500

  ViewVC Help
Powered by ViewVC