/[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 556 by schoenebeck, Sat May 21 01:10:12 2005 UTC revision 1835 by iliev, Mon Feb 16 17:56:50 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 Christian Schoenebeck                              *   *   Copyright (C) 2005 - 2008 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This program 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  *
10   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 2 of the License, or     *
11   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
12   *                                                                         *   *                                                                         *
13   *   This program is distributed in the hope that it will be useful,       *   *   This library is distributed in the hope that it will be useful,       *
14   *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *   *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16   *   GNU General Public License for more details.                          *   *   GNU General Public License for more details.                          *
17   *                                                                         *   *                                                                         *
18   *   You should have received a copy of the GNU General Public License     *   *   You should have received a copy of the GNU General Public License     *
19   *   along with this program; if not, write to the Free Software           *   *   along with this library; if not, write to the Free Software           *
20   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
21   *   MA  02111-1307  USA                                                   *   *   MA  02111-1307  USA                                                   *
22   ***************************************************************************/   ***************************************************************************/
# Line 25  Line 25 
25    
26  #include "Sampler.h"  #include "Sampler.h"
27    
28    #include "common/global_private.h"
29    #include "engines/EngineFactory.h"
30  #include "engines/EngineChannelFactory.h"  #include "engines/EngineChannelFactory.h"
31    #include "plugins/InstrumentEditorFactory.h"
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"
35    #include "common/Features.h"
36  #include "network/lscpserver.h"  #include "network/lscpserver.h"
37    
38  namespace LinuxSampler {  namespace LinuxSampler {
# Line 38  namespace LinuxSampler { Line 43  namespace LinuxSampler {
43      SamplerChannel::SamplerChannel(Sampler* pS) {      SamplerChannel::SamplerChannel(Sampler* pS) {
44          pSampler           = pS;          pSampler           = pS;
45          pEngineChannel     = NULL;          pEngineChannel     = NULL;
         pMidiInputDevice   = NULL;  
46          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
47          midiPort           = 0;          pMidiInputDevice   = NULL;
48          midiChannel        = MidiInputPort::midi_chan_all;          iMidiPort          = 0;
49            midiChannel        = midi_chan_all;
50          iIndex             = -1;          iIndex             = -1;
51      }      }
52    
53      SamplerChannel::~SamplerChannel() {      SamplerChannel::~SamplerChannel() {
54          if (pEngineChannel) {          if (pEngineChannel) {
55              MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);              Engine* engine = pEngineChannel->GetEngine();
56                if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
57    
58                MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel());
59              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
60              if (pEngineChannel) {              if (pEngineChannel) {
61                  if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();                  if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
62                  delete pEngineChannel;                  EngineChannelFactory::Destroy(pEngineChannel);
63    
64                    // reconnect engine if it still exists
65                    const std::set<Engine*>& engines = EngineFactory::EngineInstances();
66                    if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
67              }              }
68          }          }
69      }      }
70    
71      void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) {      void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {
72          dmsg(2,("SamplerChannel: Assigning engine type..."));          dmsg(2,("SamplerChannel: Assigning engine type..."));
73            
74            if (pEngineChannel) {
75                if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) {
76                    dmsg(2,("OK\n"));
77                    return;
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 LinuxSamplerException("Unknown engine type");          if (!pNewEngineChannel) throw Exception("Unknown engine type");
86    
87            pNewEngineChannel->SetSamplerChannel(this);
88    
89          // dereference midi input port.          // dereference midi input port.
90          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
91          // disconnect old engine          // disconnect old engine channel
92          if (pEngineChannel) {          if (pEngineChannel) {
93                Engine* engine = pEngineChannel->GetEngine();
94                if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
95    
96              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);              if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
97              if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();              if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
98              delete pEngineChannel;              EngineChannelFactory::Destroy(pEngineChannel);
99    
100                // reconnect engine if it still exists
101                const std::set<Engine*>& engines = EngineFactory::EngineInstances();
102                if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
103          }          }
104    
105          // connect new engine channel          // connect new engine channel
         pEngineChannel = pNewEngineChannel;  
         if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, this->midiChannel);  
106          if (pAudioOutputDevice) {          if (pAudioOutputDevice) {
107              pNewEngineChannel->Connect(pAudioOutputDevice);              pNewEngineChannel->Connect(pAudioOutputDevice);
108              pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());              pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
109          }          }
110            if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());
111            pEngineChannel = pNewEngineChannel;
112    
113            // from now on get MIDI device and port from EngineChannel object
114            this->pMidiInputDevice = NULL;
115            this->iMidiPort        = 0;
116    
117            pEngineChannel->StatusChanged(true);
118            fireEngineChanged();
119          dmsg(2,("OK\n"));          dmsg(2,("OK\n"));
120      }      }
121    
122      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
123            if(pAudioOutputDevice == pDevice) return;
124    
125          // disconnect old device          // disconnect old device
126          if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice();          if (pAudioOutputDevice && pEngineChannel) {
127                Engine* engine = pEngineChannel->GetEngine();
128                pAudioOutputDevice->Disconnect(engine);
129    
130                pEngineChannel->DisconnectAudioOutputDevice();
131    
132                // reconnect engine if it still exists
133                const std::set<Engine*>& engines = EngineFactory::EngineInstances();
134                if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
135            }
136    
137          // connect new device          // connect new device
138          pAudioOutputDevice = pDevice;          pAudioOutputDevice = pDevice;
139          if (pEngineChannel) {          if (pEngineChannel) {
# Line 94  namespace LinuxSampler { Line 143  namespace LinuxSampler {
143      }      }
144    
145      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
146         SetMidiInput(pDevice, this->midiPort, this->midiChannel);         SetMidiInput(pDevice, 0, GetMidiInputChannel());
147      }      }
148    
149      void SamplerChannel::SetMidiInputPort(int MidiPort) {      void SamplerChannel::SetMidiInputPort(int MidiPort) {
150         SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel);         SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
151      }      }
152    
153      void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
154         SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel);         SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
155      }      }
156    
157      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) {
158          // dereference old midi input port.          if (!pDevice) throw Exception("No MIDI input device assigned.");
159          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);  
160            // get old and new midi input port
161            MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
162            MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);
163    
164          // disconnect old device port          // disconnect old device port
165          if (pMidiInputPort && pEngineChannel) pMidiInputPort->Disconnect(pEngineChannel);          if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel);
166          // new device, port and channel          // remember new device, port and channel if not engine channel yet created
167          pMidiInputDevice = pDevice;          if (!pEngineChannel) {
168          this->midiPort = iMidiPort;              this->pMidiInputDevice = pDevice;
169          this->midiChannel = MidiChannel;              this->iMidiPort        = iMidiPort;
170                this->midiChannel      = MidiChannel;
171            }
172    
173          // connect new device port          // connect new device port
174          pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);
         if (pMidiInputPort && pEngineChannel) pMidiInputPort->Connect(pEngineChannel, MidiChannel);  
175          // Ooops.          // Ooops.
176          if (pMidiInputPort == NULL)          if (pNewMidiInputPort == NULL)
177              throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");              throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
178      }      }
179    
180      EngineChannel* SamplerChannel::GetEngineChannel() {      EngineChannel* SamplerChannel::GetEngineChannel() {
181          return pEngineChannel;          return pEngineChannel;
182      }      }
183    
184      MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {      midi_chan_t SamplerChannel::GetMidiInputChannel() {
185            if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
186          return this->midiChannel;          return this->midiChannel;
187      }      }
188    
189      int SamplerChannel::GetMidiInputPort() {      int SamplerChannel::GetMidiInputPort() {
190          MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);          MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;
191          return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1);          if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
192            return iMidiPort;
193      }      }
194    
195      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
# Line 140  namespace LinuxSampler { Line 197  namespace LinuxSampler {
197      }      }
198    
199      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
200            if (pEngineChannel)
201                pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;
202          return pMidiInputDevice;          return pMidiInputDevice;
203      }      }
204    
# Line 154  namespace LinuxSampler { Line 213  namespace LinuxSampler {
213              }              }
214          }          }
215    
216          throw LinuxSamplerException("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      MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) {      void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) {
224            llEngineChangeListeners.AddListener(l);
225        }
226    
227        void SamplerChannel::RemoveEngineChangeListener(EngineChangeListener* l) {
228           llEngineChangeListeners.RemoveListener(l);
229        }
230    
231        void SamplerChannel::RemoveAllEngineChangeListeners() {
232           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() {
242            for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {
243                llEngineChangeListeners.GetListener(i)->EngineChanged(Index());
244            }
245        }
246    
247        MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {
248          MidiInputPort* pMidiInputPort = NULL;          MidiInputPort* pMidiInputPort = NULL;
249            MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
250          if (pMidiInputDevice)          if (pMidiInputDevice)
251              pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);              pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
252          return pMidiInputPort;          return pMidiInputPort;
# Line 170  namespace LinuxSampler { Line 258  namespace LinuxSampler {
258      // * Sampler      // * Sampler
259    
260      Sampler::Sampler() {      Sampler::Sampler() {
261            eventHandler.SetSampler(this);
262            uiOldTotalVoiceCount = uiOldTotalStreamCount = 0;
263      }      }
264    
265      Sampler::~Sampler() {      Sampler::~Sampler() {
# Line 180  namespace LinuxSampler { Line 270  namespace LinuxSampler {
270          return mSamplerChannels.size();          return mSamplerChannels.size();
271      }      }
272    
273        void Sampler::AddChannelCountListener(ChannelCountListener* l) {
274            llChannelCountListeners.AddListener(l);
275        }
276    
277        void Sampler::RemoveChannelCountListener(ChannelCountListener* l) {
278           llChannelCountListeners.RemoveListener(l);
279        }
280    
281        void Sampler::fireChannelCountChanged(int NewCount) {
282            for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
283                llChannelCountListeners.GetListener(i)->ChannelCountChanged(NewCount);
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) {
300            llAudioDeviceCountListeners.AddListener(l);
301        }
302    
303        void Sampler::RemoveAudioDeviceCountListener(AudioDeviceCountListener* l) {
304            llAudioDeviceCountListeners.RemoveListener(l);
305        }
306    
307        void Sampler::fireAudioDeviceCountChanged(int NewCount) {
308            for (int i = 0; i < llAudioDeviceCountListeners.GetListenerCount(); i++) {
309                llAudioDeviceCountListeners.GetListener(i)->AudioDeviceCountChanged(NewCount);
310            }
311        }
312    
313        void Sampler::AddMidiDeviceCountListener(MidiDeviceCountListener* l) {
314            llMidiDeviceCountListeners.AddListener(l);
315        }
316    
317        void Sampler::RemoveMidiDeviceCountListener(MidiDeviceCountListener* l) {
318            llMidiDeviceCountListeners.RemoveListener(l);
319        }
320    
321        void Sampler::fireMidiDeviceCountChanged(int NewCount) {
322            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
323                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCountChanged(NewCount);
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) {
340            llVoiceCountListeners.AddListener(l);
341        }
342    
343        void Sampler::RemoveVoiceCountListener(VoiceCountListener* l) {
344            llVoiceCountListeners.RemoveListener(l);
345        }
346    
347        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++) {
357                llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount);
358            }
359        }
360    
361        void Sampler::AddStreamCountListener(StreamCountListener* l) {
362            llStreamCountListeners.AddListener(l);
363        }
364    
365        void Sampler::RemoveStreamCountListener(StreamCountListener* l) {
366            llStreamCountListeners.RemoveListener(l);
367        }
368    
369        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++) {
379                llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount);
380            }
381        }
382    
383        void Sampler::AddBufferFillListener(BufferFillListener* l) {
384            llBufferFillListeners.AddListener(l);
385        }
386    
387        void Sampler::RemoveBufferFillListener(BufferFillListener* l) {
388            llBufferFillListeners.RemoveListener(l);
389        }
390    
391        void Sampler::fireBufferFillChanged(int ChannelId, String FillData) {
392            for (int i = 0; i < llBufferFillListeners.GetListenerCount(); i++) {
393                llBufferFillListeners.GetListener(i)->BufferFillChanged(ChannelId, FillData);
394            }
395        }
396    
397        void Sampler::AddTotalStreamCountListener(TotalStreamCountListener* l) {
398            llTotalStreamCountListeners.AddListener(l);
399        }
400    
401        void Sampler::RemoveTotalStreamCountListener(TotalStreamCountListener* l) {
402            llTotalStreamCountListeners.RemoveListener(l);
403        }
404    
405        void Sampler::fireTotalStreamCountChanged(int NewCount) {
406            if (NewCount == uiOldTotalStreamCount) return;
407            uiOldTotalStreamCount = NewCount;
408    
409            for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) {
410                llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount);
411            }
412        }
413    
414        void Sampler::AddTotalVoiceCountListener(TotalVoiceCountListener* l) {
415            llTotalVoiceCountListeners.AddListener(l);
416        }
417    
418        void Sampler::RemoveTotalVoiceCountListener(TotalVoiceCountListener* l) {
419            llTotalVoiceCountListeners.RemoveListener(l);
420        }
421    
422        void Sampler::fireTotalVoiceCountChanged(int NewCount) {
423            if (NewCount == uiOldTotalVoiceCount) return;
424            uiOldTotalVoiceCount = NewCount;
425    
426            for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) {
427                llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount);
428            }
429        }
430    
431        void Sampler::AddFxSendCountListener(FxSendCountListener* l) {
432            llFxSendCountListeners.AddListener(l);
433        }
434    
435        void Sampler::RemoveFxSendCountListener(FxSendCountListener* l) {
436            llFxSendCountListeners.RemoveListener(l);
437        }
438    
439        void Sampler::fireFxSendCountChanged(int ChannelId, int NewCount) {
440            for (int i = 0; i < llFxSendCountListeners.GetListenerCount(); i++) {
441                llFxSendCountListeners.GetListener(i)->FxSendCountChanged(ChannelId, NewCount);
442            }
443        }
444    
445        void Sampler::EventHandler::EngineToBeChanged(int ChannelId) {
446            // nothing to do here
447        }
448    
449        void Sampler::EventHandler::EngineChanged(int ChannelId) {
450            EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel();
451            if(engineChannel == NULL) return;
452            engineChannel->AddFxSendCountListener(this);
453        }
454    
455        void Sampler::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
456            pSampler->fireFxSendCountChanged(ChannelId, NewCount);
457        }
458    
459    
460      SamplerChannel* Sampler::AddSamplerChannel() {      SamplerChannel* Sampler::AddSamplerChannel() {
461          // if there's no sampler channel yet          // if there's no sampler channel yet
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              LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1));              fireChannelAdded(pChannel);
466                fireChannelCountChanged(1);
467                pChannel->AddEngineChangeListener(&eventHandler);
468              return pChannel;              return pChannel;
469          }          }
470    
# Line 200  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                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i));                  fireChannelAdded(pChannel);
483                    fireChannelCountChanged(SamplerChannels());
484                    pChannel->AddEngineChangeListener(&eventHandler);
485                  return pChannel;                  return pChannel;
486              }              }
487              throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");              throw Exception("Internal error: could not find unoccupied sampler channel index.");
488          }          }
489    
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          LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1));          fireChannelAdded(pChannel);
494            fireChannelCountChanged(SamplerChannels());
495            pChannel->AddEngineChangeListener(&eventHandler);
496          return pChannel;          return pChannel;
497      }      }
498    
# Line 225  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();
515                  mSamplerChannels.erase(iterChan);                  mSamplerChannels.erase(iterChan);
516                  delete pSamplerChannel;                  delete pSamplerChannel;
517                  LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size()));                  fireChannelCountChanged(SamplerChannels());
518                  return;                  return;
519              }              }
520          }          }
# Line 239  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      }      }
545    
546      AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {      std::vector<String> Sampler::AvailableMidiInputDrivers() {
547            return MidiInputDeviceFactory::AvailableDrivers();
548        }
549    
550        std::vector<String> Sampler::AvailableEngineTypes() {
551            return EngineFactory::AvailableEngineTypes();
552        }
553    
554        AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) {
555          // create new device          // create new device
556          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
557    
# Line 255  namespace LinuxSampler { Line 563  namespace LinuxSampler {
563              }              }
564          }          }
565    
566            fireAudioDeviceCountChanged(AudioOutputDevices());
567          return pDevice;          return pDevice;
568      }      }
569    
# Line 274  namespace LinuxSampler { Line 583  namespace LinuxSampler {
583          return mMidiInputDevices;          return mMidiInputDevices;
584      }      }
585    
586      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) {      void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
587          AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();          AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
588          for (; iter != mAudioOutputDevices.end(); iter++) {          for (; iter != mAudioOutputDevices.end(); iter++) {
589              if (iter->second == pDevice) {              if (iter->second == pDevice) {
590                  // check if there are still sampler engines connected to this device                  // check if there are still sampler engines connected to this device
591                  for (uint i = 0; i < SamplerChannels(); i++)                  for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
592                      if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the audio output device.");                       iterChan != mSamplerChannels.end(); iterChan++)
593                        if (iterChan->second->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the audio output device.");
594    
595                  // disable device                  // disable device
596                  pDevice->Stop();                  pDevice->Stop();
# Line 290  namespace LinuxSampler { Line 600  namespace LinuxSampler {
600    
601                  // destroy and free device from memory                  // destroy and free device from memory
602                  delete pDevice;                  delete pDevice;
603    
604                    fireAudioDeviceCountChanged(AudioOutputDevices());
605                    break;
606              }              }
607          }          }
608      }      }
609    
610      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) {      void Sampler::DestroyAllAudioOutputDevices() throw (Exception) {
611            /*
612             * In maps iterator invalidation occurs when the iterator point
613             * to the element that is being erased. So we need to copy the map
614             * by calling GetAudioOutputDevices() to prevent that.
615             */
616            AudioOutputDeviceMap devs = GetAudioOutputDevices();
617            AudioOutputDeviceMap::iterator iter = devs.begin();
618            for(; iter != devs.end(); iter++) {
619                DestroyAudioOutputDevice(iter->second);
620            }
621        }
622    
623        void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
624          MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();          MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
625          for (; iter != mMidiInputDevices.end(); iter++) {          for (; iter != mMidiInputDevices.end(); iter++) {
626              if (iter->second == pDevice) {              if (iter->second == pDevice) {
627                  // check if there are still sampler engines connected to this device                  // check if there are still sampler engines connected to this device
628                  for (uint i = 0; i < SamplerChannels(); i++)                  for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
629                      if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device.");                       iterChan != mSamplerChannels.end(); iterChan++)
630                        if (iterChan->second->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device.");
631    
632                    fireMidiDeviceToBeDestroyed(pDevice);
633    
634                  // disable device                  // disable device
635                  pDevice->StopListen();                  pDevice->StopListen();
# Line 310  namespace LinuxSampler { Line 639  namespace LinuxSampler {
639    
640                  // destroy and free device from memory                  // destroy and free device from memory
641                  delete pDevice;                  delete pDevice;
642    
643                    fireMidiDeviceCountChanged(MidiInputDevices());
644                    break;
645              }              }
646          }          }
647      }      }
648    
649      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {      void Sampler::DestroyAllMidiInputDevices() throw (Exception) {
650            /*
651             * In maps iterator invalidation occurs when the iterator point
652             * to the element that is being erased. So we need to copy the map
653             * by calling GetMidiInputDevices() to prevent that.
654             */
655            MidiInputDeviceMap devs = GetMidiInputDevices();
656            MidiInputDeviceMap::iterator iter = devs.begin();
657            for(; iter != devs.end(); iter++) {
658                DestroyMidiInputDevice(iter->second);
659            }
660        }
661    
662        MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) {
663          // create new device          // create new device
664          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);          MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
665    
# Line 326  namespace LinuxSampler { Line 671  namespace LinuxSampler {
671                  }                  }
672          }          }
673    
674            fireMidiDeviceCreated(pDevice);
675            fireMidiDeviceCountChanged(MidiInputDevices());
676          return pDevice;          return pDevice;
677      }      }
678    
679        int Sampler::GetDiskStreamCount() {
680            int count = 0;
681            std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
682    
683            for(; it != EngineFactory::EngineInstances().end(); it++) {
684                count += (*it)->DiskStreamCount();
685            }
686    
687            return count;
688        }
689    
690        int Sampler::GetVoiceCount() {
691            int count = 0;
692            std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
693    
694            for(; it != EngineFactory::EngineInstances().end(); it++) {
695                count += (*it)->VoiceCount();
696            }
697    
698            return count;
699        }
700    
701      void Sampler::Reset() {      void Sampler::Reset() {
702          // delete sampler channels          // delete sampler channels
703          try {          try {
704              while (true) {              RemoveAllSamplerChannels();
                     SamplerChannelMap::iterator iter = mSamplerChannels.begin();  
                     if (iter == mSamplerChannels.end()) break;  
                     RemoveSamplerChannel(iter->second);  
             }  
705          }          }
706          catch(...) {          catch(...) {
707              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 345  namespace LinuxSampler { Line 710  namespace LinuxSampler {
710    
711          // delete midi input devices          // delete midi input devices
712          try {          try {
713              while (true) {              DestroyAllMidiInputDevices();
                     MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();  
                     if (iter == mMidiInputDevices.end()) break;  
                     DestroyMidiInputDevice(iter->second);  
             }  
714          }          }
715          catch(...) {          catch(...) {
716              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 358  namespace LinuxSampler { Line 719  namespace LinuxSampler {
719    
720          // delete audio output devices          // delete audio output devices
721          try {          try {
722              while (true) {              DestroyAllAudioOutputDevices();
                     AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();  
                     if (iter == mAudioOutputDevices.end()) break;  
                     DestroyAudioOutputDevice(iter->second);  
             }  
723          }          }
724          catch(...) {          catch(...) {
725              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;
726              exit(EXIT_FAILURE);              exit(EXIT_FAILURE);
727          }          }
728    
729            // delete MIDI instrument maps
730            try {
731                MidiInstrumentMapper::RemoveAllMaps();
732            }
733            catch(...) {
734                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush;
735                exit(EXIT_FAILURE);
736            }
737    
738            // unload all instrument editor DLLs
739            InstrumentEditorFactory::ClosePlugins();
740        }
741    
742        bool Sampler::EnableDenormalsAreZeroMode() {
743            Features::detect();
744            return Features::enableDenormalsAreZeroMode();
745        }
746    
747        void Sampler::fireStatistics() {
748            static const LSCPEvent::event_t eventsArr[] = {
749                LSCPEvent::event_voice_count, LSCPEvent::event_stream_count,
750                LSCPEvent::event_buffer_fill, LSCPEvent::event_total_voice_count
751            };
752            static const std::list<LSCPEvent::event_t> events(eventsArr, eventsArr + 4);
753    
754            if (LSCPServer::EventSubscribers(events))
755            {
756                LSCPServer::LockRTNotify();
757                std::map<uint,SamplerChannel*> channels = GetSamplerChannels();
758                std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
759                for (; iter != channels.end(); iter++) {
760                    SamplerChannel* pSamplerChannel = iter->second;
761                    EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
762                    if (!pEngineChannel) continue;
763                    Engine* pEngine = pEngineChannel->GetEngine();
764                    if (!pEngine) continue;
765                    fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount());
766                    fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());
767                    fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());
768                }
769                
770                fireTotalStreamCountChanged(GetDiskStreamCount());
771                fireTotalVoiceCountChanged(GetVoiceCount());
772    
773                LSCPServer::UnlockRTNotify();
774            }
775      }      }
776    
777  } // namespace LinuxSampler  } // namespace LinuxSampler

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

  ViewVC Help
Powered by ViewVC