/[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 61 by schoenebeck, Mon May 3 19:29:44 2004 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 - 2013 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   ***************************************************************************/   ***************************************************************************/
23    
24    #include <sstream>
25    
26  #include "Sampler.h"  #include "Sampler.h"
27    
28  #include "audiodriver/AudioOutputDeviceAlsa.h"  #include "common/global_private.h"
29  #include "audiodriver/AudioOutputDeviceJack.h"  #include "engines/EngineFactory.h"
30  #include "mididriver/MidiInputDeviceAlsa.h"  #include "engines/EngineChannelFactory.h"
31  #include "engines/gig/Engine.h"  #include "plugins/InstrumentEditorFactory.h"
32    #include "drivers/audio/AudioOutputDeviceFactory.h"
33    #include "drivers/midi/MidiInputDeviceFactory.h"
34    #include "drivers/midi/MidiInstrumentMapper.h"
35    #include "common/Features.h"
36    #include "network/lscpserver.h"
37    
38  namespace LinuxSampler {  namespace LinuxSampler {
39    
# Line 34  namespace LinuxSampler { Line 42  namespace LinuxSampler {
42    
43      SamplerChannel::SamplerChannel(Sampler* pS) {      SamplerChannel::SamplerChannel(Sampler* pS) {
44          pSampler           = pS;          pSampler           = pS;
45          pEngine            = NULL;          pEngineChannel     = NULL;
         pMidiInputDevice   = NULL;  
46          pAudioOutputDevice = NULL;          pAudioOutputDevice = NULL;
47            pMidiInputDevice   = NULL;
48            iMidiPort          = 0;
49            midiChannel        = midi_chan_all;
50          iIndex             = -1;          iIndex             = -1;
51      }      }
52    
53      SamplerChannel::~SamplerChannel() {      SamplerChannel::~SamplerChannel() {
54          if (pEngine) {          if (pEngineChannel) {
55              if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine);              Engine* engine = pEngineChannel->GetEngine();
56              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
57              delete pEngine;  
58                MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel());
59                if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
60                if (pEngineChannel) {
61                    if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
62                    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::LoadEngine(engine_type_t EngineType) {      void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {
72          dmsg(1,("SamplerChannel: Loading engine\n"));          dmsg(2,("SamplerChannel: Assigning engine type..."));
73    
74          // create new engine          if (pEngineChannel) {
75          Engine* pNewEngine = NULL;              if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) {
76          switch (EngineType) {                  dmsg(2,("OK\n"));
77              case engine_type_gig:                  return;
78                  pNewEngine = new gig::Engine;              }
                 break;  
             default:  
                 throw LinuxSamplerException("Unknown engine type");  
79          }          }
80    
81          // disconnect old engine          fireEngineToBeChanged();
82          if (pEngine) {  
83              if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine);          // create new engine channel
84              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
85              delete pEngine;          if (!pNewEngineChannel) throw Exception("Unknown engine type");
86    
87            pNewEngineChannel->SetSamplerChannel(this);
88    
89            // dereference midi input port.
90            MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
91            midi_chan_t midiChannel = GetMidiInputChannel();
92            // disconnect old engine channel
93            if (pEngineChannel) {
94                Engine* engine = pEngineChannel->GetEngine();
95                if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
96    
97                if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
98                if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
99                EngineChannelFactory::Destroy(pEngineChannel);
100    
101                // reconnect engine if it still exists
102                const std::set<Engine*>& engines = EngineFactory::EngineInstances();
103                if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
104          }          }
105    
106          // connect new engine          // connect new engine channel
107          pEngine = pNewEngine;          if (pAudioOutputDevice) {
108          if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index());              pNewEngineChannel->Connect(pAudioOutputDevice);
109          if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);              pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
110          dmsg(1,("SamplerChannel: Engine loaded.\n"));          }
111            if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, midiChannel);
112            pEngineChannel = pNewEngineChannel;
113    
114            // from now on get MIDI device and port from EngineChannel object
115            this->pMidiInputDevice = NULL;
116            this->iMidiPort        = 0;
117    
118            pEngineChannel->StatusChanged(true);
119            fireEngineChanged();
120            dmsg(2,("OK\n"));
121      }      }
122    
123      void SamplerChannel::SetAudioOutputDevice(audio_output_type_t AudioType) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
124          // get / create desired audio device          if(pAudioOutputDevice == pDevice) return;
         AudioOutputDevice* pDevice = pSampler->GetAudioOutputDevice(AudioType);  
         if (!pDevice) pDevice = pSampler->CreateAudioOutputDevice(AudioType);  
125    
126          // disconnect old device          // disconnect old device
127          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);          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();
132                pAudioOutputDevice->Disconnect(engine);
133    
134                pEngineChannel->DisconnectAudioOutputDevice();
135    
136                // reconnect engine if it still exists
137                const std::set<Engine*>& engines = EngineFactory::EngineInstances();
138                if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
139            }
140    
141          // connect new device          // connect new device
142          pAudioOutputDevice = pDevice;          pAudioOutputDevice = pDevice;
143          if (pEngine) pAudioOutputDevice->Connect(pEngine);          if (pEngineChannel) {
144                pEngineChannel->Connect(pAudioOutputDevice);
145                pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
146            }
147      }      }
148    
149      void SamplerChannel::SetMidiInputDevice(midi_input_type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
150          // get / create desired midi device         SetMidiInput(pDevice, 0, GetMidiInputChannel());
151          MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType);      }
         if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType);  
152    
153          // disconnect old device      void SamplerChannel::SetMidiInputPort(int MidiPort) throw (Exception) {
154          if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine);         SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
155        }
156    
157          // connect new device      void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
158          pMidiInputDevice = pDevice;         SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
         if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel);  
159      }      }
160    
161      Engine* SamplerChannel::GetEngine() {      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) throw (Exception) {
162          return pEngine;          if (!pDevice) throw Exception("No MIDI input device assigned.");
163    
164            // get old and new midi input port
165            MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
166            MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);
167    
168            // disconnect old device port
169            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
179            if (!pEngineChannel) {
180                this->pMidiInputDevice = pDevice;
181                this->iMidiPort        = iMidiPort;
182                this->midiChannel      = MidiChannel;
183            }
184    
185            // connect new device port
186            if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);
187            // Ooops.
188            if (pNewMidiInputPort == NULL)
189                throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
190      }      }
191    
192      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {      EngineChannel* SamplerChannel::GetEngineChannel() {
193          return pMidiInputDevice;          return pEngineChannel;
194        }
195    
196        midi_chan_t SamplerChannel::GetMidiInputChannel() {
197            if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
198            return this->midiChannel;
199        }
200    
201        int SamplerChannel::GetMidiInputPort() {
202            MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;
203            if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
204            return iMidiPort;
205      }      }
206    
207      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
208          return pAudioOutputDevice;          return pAudioOutputDevice;
209      }      }
210    
211        MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
212            if (pEngineChannel)
213                pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;
214            return pMidiInputDevice;
215        }
216    
217      uint SamplerChannel::Index() {      uint SamplerChannel::Index() {
218          if (iIndex >= 0) return iIndex;          if (iIndex >= 0) return iIndex;
219    
220          std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin();          Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
221          for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) {          for (; iter != pSampler->mSamplerChannels.end(); iter++) {
222              if (*iter == this) {              if (iter->second == this) {
223                  iIndex = i;                  iIndex = iter->first;
224                  return i;                  return iIndex;
225              }              }
226          }          }
227    
228          throw LinuxSamplerException("SamplerChannel index not found");          throw Exception("Internal error: SamplerChannel index not found");
229        }
230    
231        Sampler* SamplerChannel::GetSampler() {
232            return pSampler;
233        }
234    
235        void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) {
236            llEngineChangeListeners.AddListener(l);
237      }      }
238    
239        void SamplerChannel::RemoveEngineChangeListener(EngineChangeListener* l) {
240           llEngineChangeListeners.RemoveListener(l);
241        }
242    
243        void SamplerChannel::RemoveAllEngineChangeListeners() {
244           llEngineChangeListeners.RemoveAllListeners();
245        }
246    
247        void SamplerChannel::fireEngineToBeChanged() {
248            for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {
249                llEngineChangeListeners.GetListener(i)->EngineToBeChanged(Index());
250            }
251        }
252    
253        void SamplerChannel::fireEngineChanged() {
254            for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {
255                llEngineChangeListeners.GetListener(i)->EngineChanged(Index());
256            }
257        }
258    
259        MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {
260            MidiInputPort* pMidiInputPort = NULL;
261            MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
262            if (pMidiInputDevice)
263                pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
264            return pMidiInputPort;
265        }
266    
267    
268    
269      // ******************************************************************      // ******************************************************************
270      // * Sampler      // * Sampler
271    
272      Sampler::Sampler() {      Sampler::Sampler() {
273            eventHandler.SetSampler(this);
274            uiOldTotalVoiceCount = uiOldTotalStreamCount = 0;
275      }      }
276    
277      Sampler::~Sampler() {      Sampler::~Sampler() {
278          // delete sampler channels          Reset();
279          {      }
280              std::vector<SamplerChannel*>::iterator iter = vSamplerChannels.begin();  
281              for (; iter != vSamplerChannels.end(); iter++) delete *iter;      uint Sampler::SamplerChannels() {
282            return mSamplerChannels.size();
283        }
284    
285        void Sampler::AddChannelCountListener(ChannelCountListener* l) {
286            llChannelCountListeners.AddListener(l);
287        }
288    
289        void Sampler::RemoveChannelCountListener(ChannelCountListener* l) {
290           llChannelCountListeners.RemoveListener(l);
291        }
292    
293        void Sampler::fireChannelCountChanged(int NewCount) {
294            for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
295                llChannelCountListeners.GetListener(i)->ChannelCountChanged(NewCount);
296          }          }
297        }
298    
299          // delete midi input devices      void Sampler::fireChannelAdded(SamplerChannel* pChannel) {
300          {          for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
301              MidiInputDeviceMap::iterator iter = MidiInputDevices.begin();              llChannelCountListeners.GetListener(i)->ChannelAdded(pChannel);
             for (; iter != MidiInputDevices.end(); iter++) {  
                 MidiInputDevice* pDevice = iter->second;  
                 pDevice->StopListen();  
                 delete pDevice;  
             }  
302          }          }
303        }
304    
305          // delete audio output devices      void Sampler::fireChannelToBeRemoved(SamplerChannel* pChannel) {
306          {          for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
307              AudioOutputDeviceMap::iterator iter = AudioOutputDevices.begin();              llChannelCountListeners.GetListener(i)->ChannelToBeRemoved(pChannel);
             for (; iter != AudioOutputDevices.end(); iter++) {  
                 AudioOutputDevice* pDevice = iter->second;  
                 pDevice->Stop();  
                 delete pDevice;  
             }  
308          }          }
309      }      }
310    
311      uint Sampler::SamplerChannels() {      void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) {
312          vSamplerChannels.size();          llAudioDeviceCountListeners.AddListener(l);
313        }
314    
315        void Sampler::RemoveAudioDeviceCountListener(AudioDeviceCountListener* l) {
316            llAudioDeviceCountListeners.RemoveListener(l);
317        }
318    
319        void Sampler::fireAudioDeviceCountChanged(int NewCount) {
320            for (int i = 0; i < llAudioDeviceCountListeners.GetListenerCount(); i++) {
321                llAudioDeviceCountListeners.GetListener(i)->AudioDeviceCountChanged(NewCount);
322            }
323        }
324    
325        void Sampler::AddMidiDeviceCountListener(MidiDeviceCountListener* l) {
326            llMidiDeviceCountListeners.AddListener(l);
327        }
328    
329        void Sampler::RemoveMidiDeviceCountListener(MidiDeviceCountListener* l) {
330            llMidiDeviceCountListeners.RemoveListener(l);
331        }
332    
333        void Sampler::fireMidiDeviceCountChanged(int NewCount) {
334            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
335                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCountChanged(NewCount);
336            }
337        }
338    
339        void Sampler::fireMidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
340            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
341                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceToBeDestroyed(pDevice);
342            }
343        }
344    
345        void Sampler::fireMidiDeviceCreated(MidiInputDevice* pDevice) {
346            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
347                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCreated(pDevice);
348            }
349        }
350    
351        void Sampler::AddVoiceCountListener(VoiceCountListener* l) {
352            llVoiceCountListeners.AddListener(l);
353        }
354    
355        void Sampler::RemoveVoiceCountListener(VoiceCountListener* l) {
356            llVoiceCountListeners.RemoveListener(l);
357        }
358    
359        void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) {
360            std::map<uint, uint>::iterator it = mOldVoiceCounts.find(ChannelId);
361            if (it != mOldVoiceCounts.end()) {
362                uint oldCount = it->second;
363                if (NewCount == oldCount) return;
364            }
365    
366            mOldVoiceCounts[ChannelId] = NewCount;
367    
368            for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) {
369                llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount);
370            }
371        }
372    
373        void Sampler::AddStreamCountListener(StreamCountListener* l) {
374            llStreamCountListeners.AddListener(l);
375        }
376    
377        void Sampler::RemoveStreamCountListener(StreamCountListener* l) {
378            llStreamCountListeners.RemoveListener(l);
379        }
380    
381        void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) {
382            std::map<uint, uint>::iterator it = mOldStreamCounts.find(ChannelId);
383            if (it != mOldStreamCounts.end()) {
384                uint oldCount = it->second;
385                if (NewCount == oldCount) return;
386            }
387    
388            mOldStreamCounts[ChannelId] = NewCount;
389    
390            for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) {
391                llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount);
392            }
393        }
394    
395        void Sampler::AddBufferFillListener(BufferFillListener* l) {
396            llBufferFillListeners.AddListener(l);
397        }
398    
399        void Sampler::RemoveBufferFillListener(BufferFillListener* l) {
400            llBufferFillListeners.RemoveListener(l);
401        }
402    
403        void Sampler::fireBufferFillChanged(int ChannelId, String FillData) {
404            for (int i = 0; i < llBufferFillListeners.GetListenerCount(); i++) {
405                llBufferFillListeners.GetListener(i)->BufferFillChanged(ChannelId, FillData);
406            }
407        }
408    
409        void Sampler::AddTotalStreamCountListener(TotalStreamCountListener* l) {
410            llTotalStreamCountListeners.AddListener(l);
411        }
412    
413        void Sampler::RemoveTotalStreamCountListener(TotalStreamCountListener* l) {
414            llTotalStreamCountListeners.RemoveListener(l);
415        }
416    
417        void Sampler::fireTotalStreamCountChanged(int NewCount) {
418            if (NewCount == uiOldTotalStreamCount) return;
419            uiOldTotalStreamCount = NewCount;
420    
421            for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) {
422                llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount);
423            }
424        }
425    
426        void Sampler::AddTotalVoiceCountListener(TotalVoiceCountListener* l) {
427            llTotalVoiceCountListeners.AddListener(l);
428        }
429    
430        void Sampler::RemoveTotalVoiceCountListener(TotalVoiceCountListener* l) {
431            llTotalVoiceCountListeners.RemoveListener(l);
432        }
433    
434        void Sampler::fireTotalVoiceCountChanged(int NewCount) {
435            if (NewCount == uiOldTotalVoiceCount) return;
436            uiOldTotalVoiceCount = NewCount;
437    
438            for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) {
439                llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount);
440            }
441        }
442    
443        void Sampler::AddFxSendCountListener(FxSendCountListener* l) {
444            llFxSendCountListeners.AddListener(l);
445      }      }
446    
447        void Sampler::RemoveFxSendCountListener(FxSendCountListener* l) {
448            llFxSendCountListeners.RemoveListener(l);
449        }
450    
451        void Sampler::fireFxSendCountChanged(int ChannelId, int NewCount) {
452            for (int i = 0; i < llFxSendCountListeners.GetListenerCount(); i++) {
453                llFxSendCountListeners.GetListener(i)->FxSendCountChanged(ChannelId, NewCount);
454            }
455        }
456    
457        void Sampler::EventHandler::EngineToBeChanged(int ChannelId) {
458            // nothing to do here
459        }
460    
461        void Sampler::EventHandler::EngineChanged(int ChannelId) {
462            EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel();
463            if(engineChannel == NULL) return;
464            engineChannel->AddFxSendCountListener(this);
465        }
466    
467        void Sampler::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
468            pSampler->fireFxSendCountChanged(ChannelId, NewCount);
469        }
470    
471    
472      SamplerChannel* Sampler::AddSamplerChannel() {      SamplerChannel* Sampler::AddSamplerChannel() {
473            // if there's no sampler channel yet
474            if (!mSamplerChannels.size()) {
475                SamplerChannel* pChannel = new SamplerChannel(this);
476                mSamplerChannels[0] = pChannel;
477                fireChannelAdded(pChannel);
478                fireChannelCountChanged(1);
479                pChannel->AddEngineChangeListener(&eventHandler);
480                return pChannel;
481            }
482    
483            // get the highest used sampler channel index
484            uint lastIndex = (--(mSamplerChannels.end()))->first;
485    
486            // check if we reached the index limit
487            if (lastIndex + 1 < lastIndex) {
488                // search for an unoccupied sampler channel index starting from 0
489                for (uint i = 0; i < lastIndex; i++) {
490                    if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue;
491                    // we found an unused index, so insert the new channel there
492                    SamplerChannel* pChannel = new SamplerChannel(this);
493                    mSamplerChannels[i] = pChannel;
494                    fireChannelAdded(pChannel);
495                    fireChannelCountChanged(SamplerChannels());
496                    pChannel->AddEngineChangeListener(&eventHandler);
497                    return pChannel;
498                }
499                throw Exception("Internal error: could not find unoccupied sampler channel index.");
500            }
501    
502            // we have not reached the index limit so we just add the channel past the highest index
503          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
504          vSamplerChannels.push_back(pChannel);          mSamplerChannels[lastIndex + 1] = pChannel;
505            fireChannelAdded(pChannel);
506            fireChannelCountChanged(SamplerChannels());
507            pChannel->AddEngineChangeListener(&eventHandler);
508          return pChannel;          return pChannel;
509      }      }
510    
511      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
512          if (uiSamplerChannel >= SamplerChannels()) return NULL;          return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
513          return vSamplerChannels[uiSamplerChannel];      }
514    
515        std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
516            return mSamplerChannels;
517      }      }
518    
519      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
520          std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin();          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
521          for (; iterChan != vSamplerChannels.end(); iterChan++) {          for (; iterChan != mSamplerChannels.end(); iterChan++) {
522              if (*iterChan == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
523                  vSamplerChannels.erase(iterChan);                  fireChannelToBeRemoved(pSamplerChannel);
524                    mOldVoiceCounts.erase(pSamplerChannel->Index());
525                    mOldStreamCounts.erase(pSamplerChannel->Index());
526                    pSamplerChannel->RemoveAllEngineChangeListeners();
527                    mSamplerChannels.erase(iterChan);
528                  delete pSamplerChannel;                  delete pSamplerChannel;
529                    fireChannelCountChanged(SamplerChannels());
530                  return;                  return;
531              }              }
532          }          }
# Line 194  namespace LinuxSampler { Line 538  namespace LinuxSampler {
538          RemoveSamplerChannel(pChannel);          RemoveSamplerChannel(pChannel);
539      }      }
540    
541      AudioOutputDevice* Sampler::CreateAudioOutputDevice(audio_output_type_t AudioType) {      void Sampler::RemoveAllSamplerChannels() {
542          // check if device already created          /*
543          AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType);           * In maps iterator invalidation occurs when the iterator point
544          if (pDevice) return pDevice;           * to the element that is being erased. So we need to copy the map
545             * by calling GetSamplerChannels() to prevent that.
546             */
547            SamplerChannelMap chns = GetSamplerChannels();
548            SamplerChannelMap::iterator iter = chns.begin();
549            for(; iter != chns.end(); iter++) {
550                RemoveSamplerChannel(iter->second);
551            }
552        }
553    
554        std::vector<String> Sampler::AvailableAudioOutputDrivers() {
555            return AudioOutputDeviceFactory::AvailableDrivers();
556        }
557    
558        std::vector<String> Sampler::AvailableMidiInputDrivers() {
559            return MidiInputDeviceFactory::AvailableDrivers();
560        }
561    
562        std::vector<String> Sampler::AvailableEngineTypes() {
563            return EngineFactory::AvailableEngineTypes();
564        }
565    
566        AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) {
567          // create new device          // create new device
568          switch (AudioType) {          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
569              case audio_output_type_alsa:  
570                  pDevice = new AudioOutputDeviceAlsa;          fireAudioDeviceCountChanged(AudioOutputDevices());
571                  break;          return pDevice;
572              case audio_output_type_jack:      }
573                  pDevice = new AudioOutputDeviceJack;  
574                  break;      uint Sampler::AudioOutputDevices() {
575              default:          return AudioOutputDeviceFactory::Devices().size();
576                  throw LinuxSamplerException("Unknown audio output device type");      }
577    
578        uint Sampler::MidiInputDevices() {
579            return MidiInputDeviceFactory::Devices().size();
580        }
581    
582        std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
583            return AudioOutputDeviceFactory::Devices();
584        }
585    
586        std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
587            return MidiInputDeviceFactory::Devices();
588        }
589    
590        void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
591            if (pDevice) {
592                // check if there are still sampler engines connected to this device
593                for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
594                     iterChan != mSamplerChannels.end(); iterChan++
595                ) if (iterChan->second->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the audio output device.");
596    
597                //TODO: should we add fireAudioDeviceToBeDestroyed() here ?
598                AudioOutputDeviceFactory::Destroy(pDevice);
599                fireAudioDeviceCountChanged(AudioOutputDevices());
600          }          }
601        }
602    
603          // activate device      void Sampler::DestroyAllAudioOutputDevices() throw (Exception) {
604          pDevice->Play();          /*
605             * In maps iterator invalidation occurs when the iterator point
606             * to the element that is being erased. So we need to copy the map
607             * by calling GetAudioOutputDevices() to prevent that.
608             */
609            std::map<uint, AudioOutputDevice*> devs = GetAudioOutputDevices();
610            std::map<uint, AudioOutputDevice*>::iterator iter = devs.begin();
611            for (; iter != devs.end(); iter++) {
612                AudioOutputDevice* pDevice = iter->second;
613    
614          return pDevice;              // skip non-autonomous devices
615                if (!pDevice->isAutonomousDevice()) continue;
616    
617                DestroyAudioOutputDevice(pDevice);
618            }
619      }      }
620    
621      AudioOutputDevice* Sampler::GetAudioOutputDevice(audio_output_type_t AudioType) {      void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
622          AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType);          if (pDevice) {
623          return (iter != AudioOutputDevices.end()) ? iter->second : NULL;              // check if there are still sampler engines connected to this device
624                for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
625                     iterChan != mSamplerChannels.end(); iterChan++
626                ) if (iterChan->second->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device.");
627    
628                fireMidiDeviceToBeDestroyed(pDevice);
629                MidiInputDeviceFactory::Destroy(pDevice);
630                fireMidiDeviceCountChanged(MidiInputDevices());
631            }
632      }      }
633    
634      MidiInputDevice* Sampler::CreateMidiInputDevice(midi_input_type_t MidiType) {      void Sampler::DestroyAllMidiInputDevices() throw (Exception) {
635          // check if device already created          /*
636          MidiInputDevice* pDevice = GetMidiInputDevice(MidiType);           * In maps iterator invalidation occurs when the iterator point
637          if (pDevice) return pDevice;           * to the element that is being erased. So we need to copy the map
638             * by calling GetMidiInputDevices() to prevent that.
639             */
640            std::map<uint, MidiInputDevice*> devs = GetMidiInputDevices();
641            std::map<uint, MidiInputDevice*>::iterator iter = devs.begin();
642            for (; iter != devs.end(); iter++) {
643                MidiInputDevice* pDevice = iter->second;
644    
645          // create new device              // skip non-autonomous devices
646          switch (MidiType) {              if (!pDevice->isAutonomousDevice()) continue;
647              case midi_input_type_alsa:  
648                  pDevice = new MidiInputDeviceAlsa;              DestroyMidiInputDevice(pDevice);
                 break;  
             default:  
                 throw LinuxSamplerException("Unknown audio output device type");  
649          }          }
650        }
651    
652          // activate device      MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) {
653          pDevice->Listen();          // create new device
654            MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
655    
656            fireMidiDeviceCreated(pDevice);
657            fireMidiDeviceCountChanged(MidiInputDevices());
658          return pDevice;          return pDevice;
659      }      }
660    
661      MidiInputDevice* Sampler::GetMidiInputDevice(midi_input_type_t MidiType) {      int Sampler::GetDiskStreamCount() {
662          MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType);          int count = 0;
663          return (iter != MidiInputDevices.end()) ? iter->second : NULL;          std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
664    
665            for(; it != EngineFactory::EngineInstances().end(); it++) {
666                count += (*it)->DiskStreamCount();
667            }
668    
669            return count;
670        }
671    
672        int Sampler::GetVoiceCount() {
673            int count = 0;
674            std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
675    
676            for(; it != EngineFactory::EngineInstances().end(); it++) {
677                count += (*it)->VoiceCount();
678            }
679    
680            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() {
718            // delete sampler channels
719            try {
720                RemoveAllSamplerChannels();
721            }
722            catch(...) {
723                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
724                exit(EXIT_FAILURE);
725            }
726    
727            // delete midi input devices
728            try {
729                DestroyAllMidiInputDevices();
730            }
731            catch(...) {
732                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
733                exit(EXIT_FAILURE);
734            }
735    
736            // delete audio output devices
737            try {
738                DestroyAllAudioOutputDevices();
739            }
740            catch(...) {
741                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
742                exit(EXIT_FAILURE);
743            }
744    
745            // delete MIDI instrument maps
746            try {
747                MidiInstrumentMapper::RemoveAllMaps();
748            }
749            catch(...) {
750                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush;
751                exit(EXIT_FAILURE);
752            }
753    
754            // unload all instrument editor DLLs
755            InstrumentEditorFactory::ClosePlugins();
756        }
757    
758        bool Sampler::EnableDenormalsAreZeroMode() {
759            Features::detect();
760            return Features::enableDenormalsAreZeroMode();
761        }
762    
763        void Sampler::fireStatistics() {
764            static const LSCPEvent::event_t eventsArr[] = {
765                LSCPEvent::event_voice_count, LSCPEvent::event_stream_count,
766                LSCPEvent::event_buffer_fill, LSCPEvent::event_total_voice_count
767            };
768            static const std::list<LSCPEvent::event_t> events(eventsArr, eventsArr + 4);
769    
770            if (LSCPServer::EventSubscribers(events))
771            {
772                LockGuard lock(LSCPServer::RTNotifyMutex);
773                std::map<uint,SamplerChannel*> channels = GetSamplerChannels();
774                std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
775                for (; iter != channels.end(); iter++) {
776                    SamplerChannel* pSamplerChannel = iter->second;
777                    EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
778                    if (!pEngineChannel) continue;
779                    Engine* pEngine = pEngineChannel->GetEngine();
780                    if (!pEngine) continue;
781                    fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount());
782                    fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());
783                    fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());
784                }
785    
786                fireTotalStreamCountChanged(GetDiskStreamCount());
787                fireTotalVoiceCountChanged(GetVoiceCount());
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.61  
changed lines
  Added in v.2427

  ViewVC Help
Powered by ViewVC