/[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 53 by schoenebeck, Mon Apr 26 17:15:51 2004 UTC revision 1765 by persson, Sat Sep 6 16:44:42 2008 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck         *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6     *   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   ***************************************************************************/   ***************************************************************************/
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            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          // create new engine channel
84          Engine* pNewEngine = NULL;          EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
85          switch (EngineType) {          if (!pNewEngineChannel) throw Exception("Unknown engine type");
86              case engine_type_gig:  
87                  pNewEngine = new gig::Engine;          pNewEngineChannel->SetSamplerChannel(this);
88                  break;  
89              default:          // dereference midi input port.
90                  throw LinuxSamplerException("Unknown engine type");          MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
91            // disconnect old engine channel
92            if (pEngineChannel) {
93                Engine* engine = pEngineChannel->GetEngine();
94                if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
95    
96                if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
97                if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
98                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          // disconnect old engine          // connect new engine channel
106          if (pEngine) {          if (pAudioOutputDevice) {
107              if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine);              pNewEngineChannel->Connect(pAudioOutputDevice);
108              if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);              pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
             delete pEngine;  
109          }          }
110            if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());
111            pEngineChannel = pNewEngineChannel;
112    
113          // connect new engine          // from now on get MIDI device and port from EngineChannel object
114          pEngine = pNewEngine;          this->pMidiInputDevice = NULL;
115          if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index());          this->iMidiPort        = 0;
116          if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);  
117          dmsg(1,("SamplerChannel: Engine loaded.\n"));          pEngineChannel->StatusChanged(true);
118            fireEngineChanged();
119            dmsg(2,("OK\n"));
120      }      }
121    
122      void SamplerChannel::SetAudioOutputDevice(audio_output_type_t AudioType) {      void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
123          // get / create desired audio device          if(pAudioOutputDevice == pDevice) return;
         AudioOutputDevice* pDevice = pSampler->GetAudioOutputDevice(AudioType);  
         if (!pDevice) pDevice = pSampler->CreateAudioOutputDevice(AudioType);  
124    
125          // disconnect old device          // disconnect old device
126          if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);          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 (pEngine) pAudioOutputDevice->Connect(pEngine);          if (pEngineChannel) {
140                pEngineChannel->Connect(pAudioOutputDevice);
141                pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
142            }
143      }      }
144    
145      void SamplerChannel::SetMidiInputDevice(midi_input_type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) {      void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
146          // get / create desired midi device         SetMidiInput(pDevice, 0, GetMidiInputChannel());
147          MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType);      }
         if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType);  
148    
149          // disconnect old device      void SamplerChannel::SetMidiInputPort(int MidiPort) {
150          if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine);         SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
151        }
152    
153          // connect new device      void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
154          pMidiInputDevice = pDevice;         SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
         if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel);  
155      }      }
156    
157      Engine* SamplerChannel::GetEngine() {      void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) {
158          return pEngine;          if (!pDevice) throw Exception("No MIDI input device assigned.");
159    
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
165            if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel);
166            // remember new device, port and channel if not engine channel yet created
167            if (!pEngineChannel) {
168                this->pMidiInputDevice = pDevice;
169                this->iMidiPort        = iMidiPort;
170                this->midiChannel      = MidiChannel;
171            }
172    
173            // connect new device port
174            if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);
175            // Ooops.
176            if (pNewMidiInputPort == NULL)
177                throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
178      }      }
179    
180      MidiInputDevice* SamplerChannel::GetMidiInputDevice() {      EngineChannel* SamplerChannel::GetEngineChannel() {
181          return pMidiInputDevice;          return pEngineChannel;
182        }
183    
184        midi_chan_t SamplerChannel::GetMidiInputChannel() {
185            if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
186            return this->midiChannel;
187        }
188    
189        int SamplerChannel::GetMidiInputPort() {
190            MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;
191            if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
192            return iMidiPort;
193      }      }
194    
195      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {      AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
196          return pAudioOutputDevice;          return pAudioOutputDevice;
197      }      }
198    
199        MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
200            if (pEngineChannel)
201                pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;
202            return pMidiInputDevice;
203        }
204    
205      uint SamplerChannel::Index() {      uint SamplerChannel::Index() {
206          if (iIndex >= 0) return iIndex;          if (iIndex >= 0) return iIndex;
207    
208          std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin();          Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
209          for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) {          for (; iter != pSampler->mSamplerChannels.end(); iter++) {
210              if (*iter == this) {              if (iter->second == this) {
211                  iIndex = i;                  iIndex = iter->first;
212                  return i;                  return iIndex;
213              }              }
214          }          }
215    
216          throw LinuxSamplerException("SamplerChannel index not found");          throw Exception("Internal error: SamplerChannel index not found");
217        }
218    
219        Sampler* SamplerChannel::GetSampler() {
220            return pSampler;
221        }
222    
223        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;
249            MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
250            if (pMidiInputDevice)
251                pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
252            return pMidiInputPort;
253        }
254    
255    
256    
257      // ******************************************************************      // ******************************************************************
258      // * Sampler      // * Sampler
259    
260      Sampler::Sampler() {      Sampler::Sampler() {
261            eventHandler.SetSampler(this);
262      }      }
263    
264      Sampler::~Sampler() {      Sampler::~Sampler() {
265          // delete sampler channels          Reset();
266          {      }
267              std::vector<SamplerChannel*>::iterator iter = vSamplerChannels.begin();  
268              for (; iter != vSamplerChannels.end(); iter++) delete *iter;      uint Sampler::SamplerChannels() {
269            return mSamplerChannels.size();
270        }
271    
272        void Sampler::AddChannelCountListener(ChannelCountListener* l) {
273            llChannelCountListeners.AddListener(l);
274        }
275    
276        void Sampler::RemoveChannelCountListener(ChannelCountListener* l) {
277           llChannelCountListeners.RemoveListener(l);
278        }
279    
280        void Sampler::fireChannelCountChanged(int NewCount) {
281            for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
282                llChannelCountListeners.GetListener(i)->ChannelCountChanged(NewCount);
283          }          }
284        }
285    
286          // delete midi input devices      void Sampler::fireChannelAdded(SamplerChannel* pChannel) {
287          {          for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
288              MidiInputDeviceMap::iterator iter = MidiInputDevices.begin();              llChannelCountListeners.GetListener(i)->ChannelAdded(pChannel);
             for (; iter != MidiInputDevices.end(); iter++) {  
                 MidiInputDevice* pDevice = iter->second;  
                 pDevice->StopListen();  
                 delete pDevice;  
             }  
289          }          }
290        }
291    
292          // delete audio output devices      void Sampler::fireChannelToBeRemoved(SamplerChannel* pChannel) {
293          {          for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
294              AudioOutputDeviceMap::iterator iter = AudioOutputDevices.begin();              llChannelCountListeners.GetListener(i)->ChannelToBeRemoved(pChannel);
             for (; iter != AudioOutputDevices.end(); iter++) {  
                 AudioOutputDevice* pDevice = iter->second;  
                 pDevice->Stop();  
                 delete pDevice;  
             }  
295          }          }
296      }      }
297    
298      uint Sampler::SamplerChannels() {      void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) {
299          vSamplerChannels.size();          llAudioDeviceCountListeners.AddListener(l);
300        }
301    
302        void Sampler::RemoveAudioDeviceCountListener(AudioDeviceCountListener* l) {
303            llAudioDeviceCountListeners.RemoveListener(l);
304        }
305    
306        void Sampler::fireAudioDeviceCountChanged(int NewCount) {
307            for (int i = 0; i < llAudioDeviceCountListeners.GetListenerCount(); i++) {
308                llAudioDeviceCountListeners.GetListener(i)->AudioDeviceCountChanged(NewCount);
309            }
310        }
311    
312        void Sampler::AddMidiDeviceCountListener(MidiDeviceCountListener* l) {
313            llMidiDeviceCountListeners.AddListener(l);
314        }
315    
316        void Sampler::RemoveMidiDeviceCountListener(MidiDeviceCountListener* l) {
317            llMidiDeviceCountListeners.RemoveListener(l);
318        }
319    
320        void Sampler::fireMidiDeviceCountChanged(int NewCount) {
321            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
322                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCountChanged(NewCount);
323            }
324        }
325    
326        void Sampler::fireMidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
327            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
328                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceToBeDestroyed(pDevice);
329            }
330        }
331    
332        void Sampler::fireMidiDeviceCreated(MidiInputDevice* pDevice) {
333            for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
334                llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCreated(pDevice);
335            }
336        }
337    
338        void Sampler::AddVoiceCountListener(VoiceCountListener* l) {
339            llVoiceCountListeners.AddListener(l);
340        }
341    
342        void Sampler::RemoveVoiceCountListener(VoiceCountListener* l) {
343            llVoiceCountListeners.RemoveListener(l);
344        }
345    
346        void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) {
347            for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) {
348                llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount);
349            }
350        }
351    
352        void Sampler::AddStreamCountListener(StreamCountListener* l) {
353            llStreamCountListeners.AddListener(l);
354        }
355    
356        void Sampler::RemoveStreamCountListener(StreamCountListener* l) {
357            llStreamCountListeners.RemoveListener(l);
358        }
359    
360        void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) {
361            for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) {
362                llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount);
363            }
364        }
365    
366        void Sampler::AddBufferFillListener(BufferFillListener* l) {
367            llBufferFillListeners.AddListener(l);
368        }
369    
370        void Sampler::RemoveBufferFillListener(BufferFillListener* l) {
371            llBufferFillListeners.RemoveListener(l);
372        }
373    
374        void Sampler::fireBufferFillChanged(int ChannelId, String FillData) {
375            for (int i = 0; i < llBufferFillListeners.GetListenerCount(); i++) {
376                llBufferFillListeners.GetListener(i)->BufferFillChanged(ChannelId, FillData);
377            }
378      }      }
379    
380        void Sampler::AddTotalStreamCountListener(TotalStreamCountListener* l) {
381            llTotalStreamCountListeners.AddListener(l);
382        }
383    
384        void Sampler::RemoveTotalStreamCountListener(TotalStreamCountListener* l) {
385            llTotalStreamCountListeners.RemoveListener(l);
386        }
387    
388        void Sampler::fireTotalStreamCountChanged(int NewCount) {
389            for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) {
390                llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount);
391            }
392        }
393    
394        void Sampler::AddTotalVoiceCountListener(TotalVoiceCountListener* l) {
395            llTotalVoiceCountListeners.AddListener(l);
396        }
397    
398        void Sampler::RemoveTotalVoiceCountListener(TotalVoiceCountListener* l) {
399            llTotalVoiceCountListeners.RemoveListener(l);
400        }
401    
402        void Sampler::fireTotalVoiceCountChanged(int NewCount) {
403            for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) {
404                llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount);
405            }
406        }
407    
408        void Sampler::AddFxSendCountListener(FxSendCountListener* l) {
409            llFxSendCountListeners.AddListener(l);
410        }
411    
412        void Sampler::RemoveFxSendCountListener(FxSendCountListener* l) {
413            llFxSendCountListeners.RemoveListener(l);
414        }
415    
416        void Sampler::fireFxSendCountChanged(int ChannelId, int NewCount) {
417            for (int i = 0; i < llFxSendCountListeners.GetListenerCount(); i++) {
418                llFxSendCountListeners.GetListener(i)->FxSendCountChanged(ChannelId, NewCount);
419            }
420        }
421    
422        void Sampler::EventHandler::EngineToBeChanged(int ChannelId) {
423            // nothing to do here
424        }
425    
426        void Sampler::EventHandler::EngineChanged(int ChannelId) {
427            EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel();
428            if(engineChannel == NULL) return;
429            engineChannel->AddFxSendCountListener(this);
430        }
431    
432        void Sampler::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
433            pSampler->fireFxSendCountChanged(ChannelId, NewCount);
434        }
435    
436    
437      SamplerChannel* Sampler::AddSamplerChannel() {      SamplerChannel* Sampler::AddSamplerChannel() {
438            // if there's no sampler channel yet
439            if (!mSamplerChannels.size()) {
440                SamplerChannel* pChannel = new SamplerChannel(this);
441                mSamplerChannels[0] = pChannel;
442                fireChannelAdded(pChannel);
443                fireChannelCountChanged(1);
444                pChannel->AddEngineChangeListener(&eventHandler);
445                return pChannel;
446            }
447    
448            // get the highest used sampler channel index
449            uint lastIndex = (--(mSamplerChannels.end()))->first;
450    
451            // check if we reached the index limit
452            if (lastIndex + 1 < lastIndex) {
453                // search for an unoccupied sampler channel index starting from 0
454                for (uint i = 0; i < lastIndex; i++) {
455                    if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue;
456                    // we found an unused index, so insert the new channel there
457                    SamplerChannel* pChannel = new SamplerChannel(this);
458                    mSamplerChannels[i] = pChannel;
459                    fireChannelAdded(pChannel);
460                    fireChannelCountChanged(SamplerChannels());
461                    pChannel->AddEngineChangeListener(&eventHandler);
462                    return pChannel;
463                }
464                throw Exception("Internal error: could not find unoccupied sampler channel index.");
465            }
466    
467            // we have not reached the index limit so we just add the channel past the highest index
468          SamplerChannel* pChannel = new SamplerChannel(this);          SamplerChannel* pChannel = new SamplerChannel(this);
469          vSamplerChannels.push_back(pChannel);          mSamplerChannels[lastIndex + 1] = pChannel;
470            fireChannelAdded(pChannel);
471            fireChannelCountChanged(SamplerChannels());
472            pChannel->AddEngineChangeListener(&eventHandler);
473          return pChannel;          return pChannel;
474      }      }
475    
476      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {      SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
477          if (uiSamplerChannel >= SamplerChannels()) return NULL;          return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
478          return vSamplerChannels[uiSamplerChannel];      }
479    
480        std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
481            return mSamplerChannels;
482      }      }
483    
484      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {      void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
485          std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin();          SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
486          for (; iterChan != vSamplerChannels.end(); iterChan++) {          for (; iterChan != mSamplerChannels.end(); iterChan++) {
487              if (*iterChan == pSamplerChannel) {              if (iterChan->second == pSamplerChannel) {
488                  vSamplerChannels.erase(iterChan);                  fireChannelToBeRemoved(pSamplerChannel);
489                    pSamplerChannel->RemoveAllEngineChangeListeners();
490                    mSamplerChannels.erase(iterChan);
491                  delete pSamplerChannel;                  delete pSamplerChannel;
492                    fireChannelCountChanged(SamplerChannels());
493                  return;                  return;
494              }              }
495          }          }
# Line 194  namespace LinuxSampler { Line 501  namespace LinuxSampler {
501          RemoveSamplerChannel(pChannel);          RemoveSamplerChannel(pChannel);
502      }      }
503    
504      AudioOutputDevice* Sampler::CreateAudioOutputDevice(audio_output_type_t AudioType) {      std::vector<String> Sampler::AvailableAudioOutputDrivers() {
505          // check if device already created          return AudioOutputDeviceFactory::AvailableDrivers();
506          AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType);      }
507          if (pDevice) return pDevice;  
508        std::vector<String> Sampler::AvailableMidiInputDrivers() {
509            return MidiInputDeviceFactory::AvailableDrivers();
510        }
511    
512        std::vector<String> Sampler::AvailableEngineTypes() {
513            return EngineFactory::AvailableEngineTypes();
514        }
515    
516        AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) {
517          // create new device          // create new device
518          switch (AudioType) {          AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
519              case audio_output_type_alsa:  
520                  pDevice = new AudioOutputDeviceAlsa;          // add new audio device to the audio device list
521                  break;          for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
522              case audio_output_type_jack:              if (!mAudioOutputDevices[i]) {
523                  pDevice = new AudioOutputDeviceJack;                  mAudioOutputDevices[i] = pDevice;
524                  break;                  break;
525              default:              }
                 throw LinuxSamplerException("Unknown audio output device type");  
526          }          }
527    
528          // activate device          fireAudioDeviceCountChanged(AudioOutputDevices());
         pDevice->Play();  
   
529          return pDevice;          return pDevice;
530      }      }
531    
532      AudioOutputDevice* Sampler::GetAudioOutputDevice(audio_output_type_t AudioType) {      uint Sampler::AudioOutputDevices() {
533          AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType);          return mAudioOutputDevices.size();
         return (iter != AudioOutputDevices.end()) ? iter->second : NULL;  
534      }      }
535    
536      MidiInputDevice* Sampler::CreateMidiInputDevice(midi_input_type_t MidiType) {      uint Sampler::MidiInputDevices() {
537          // check if device already created          return mMidiInputDevices.size();
538          MidiInputDevice* pDevice = GetMidiInputDevice(MidiType);      }
         if (pDevice) return pDevice;  
539    
540          // create new device      std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
541          switch (MidiType) {          return mAudioOutputDevices;
542              case midi_input_type_alsa:      }
543                  pDevice = new MidiInputDeviceAlsa;  
544        std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
545            return mMidiInputDevices;
546        }
547    
548        void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
549            AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
550            for (; iter != mAudioOutputDevices.end(); iter++) {
551                if (iter->second == pDevice) {
552                    // check if there are still sampler engines connected to this device
553                    for (uint i = 0; i < SamplerChannels(); i++)
554                        if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device.");
555    
556                    // disable device
557                    pDevice->Stop();
558    
559                    // remove device from the device list
560                    mAudioOutputDevices.erase(iter);
561    
562                    // destroy and free device from memory
563                    delete pDevice;
564    
565                    fireAudioDeviceCountChanged(AudioOutputDevices());
566                  break;                  break;
567              default:              }
                 throw LinuxSamplerException("Unknown audio output device type");  
568          }          }
569        }
570    
571        void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
572            MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
573            for (; iter != mMidiInputDevices.end(); iter++) {
574                if (iter->second == pDevice) {
575                    // check if there are still sampler engines connected to this device
576                    for (uint i = 0; i < SamplerChannels(); i++)
577                        if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
578    
579                    fireMidiDeviceToBeDestroyed(pDevice);
580    
581                    // disable device
582                    pDevice->StopListen();
583    
584                    // remove device from the device list
585                    mMidiInputDevices.erase(iter);
586    
587          // activate device                  // destroy and free device from memory
588          pDevice->Listen();                  delete pDevice;
589    
590                    fireMidiDeviceCountChanged(MidiInputDevices());
591                    break;
592                }
593            }
594        }
595    
596        MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) {
597            // create new device
598            MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
599    
600            // add new device to the midi device list
601            for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
602                    if (!mMidiInputDevices[i]) {
603                            mMidiInputDevices[i] = pDevice;
604                            break;
605                    }
606            }
607    
608            fireMidiDeviceCreated(pDevice);
609            fireMidiDeviceCountChanged(MidiInputDevices());
610          return pDevice;          return pDevice;
611      }      }
612    
613      MidiInputDevice* Sampler::GetMidiInputDevice(midi_input_type_t MidiType) {      int Sampler::GetDiskStreamCount() {
614          MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType);          int count = 0;
615          return (iter != MidiInputDevices.end()) ? iter->second : NULL;          std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
616    
617            for(; it != EngineFactory::EngineInstances().end(); it++) {
618                count += (*it)->DiskStreamCount();
619            }
620    
621            return count;
622        }
623    
624        int Sampler::GetVoiceCount() {
625            int count = 0;
626            std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
627    
628            for(; it != EngineFactory::EngineInstances().end(); it++) {
629                count += (*it)->VoiceCount();
630            }
631    
632            return count;
633        }
634    
635        void Sampler::Reset() {
636            // delete sampler channels
637            try {
638                while (true) {
639                        SamplerChannelMap::iterator iter = mSamplerChannels.begin();
640                        if (iter == mSamplerChannels.end()) break;
641                        RemoveSamplerChannel(iter->second);
642                }
643            }
644            catch(...) {
645                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
646                exit(EXIT_FAILURE);
647            }
648    
649            // delete midi input devices
650            try {
651                while (true) {
652                        MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
653                        if (iter == mMidiInputDevices.end()) break;
654                        DestroyMidiInputDevice(iter->second);
655                }
656            }
657            catch(...) {
658                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
659                exit(EXIT_FAILURE);
660            }
661    
662            // delete audio output devices
663            try {
664                while (true) {
665                        AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
666                        if (iter == mAudioOutputDevices.end()) break;
667                        DestroyAudioOutputDevice(iter->second);
668                }
669            }
670            catch(...) {
671                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
672                exit(EXIT_FAILURE);
673            }
674    
675            // delete MIDI instrument maps
676            try {
677                MidiInstrumentMapper::RemoveAllMaps();
678            }
679            catch(...) {
680                std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush;
681                exit(EXIT_FAILURE);
682            }
683    
684            // unload all instrument editor DLLs
685            InstrumentEditorFactory::ClosePlugins();
686        }
687    
688        bool Sampler::EnableDenormalsAreZeroMode() {
689            Features::detect();
690            return Features::enableDenormalsAreZeroMode();
691        }
692    
693        void Sampler::fireStatistics() {
694            static const LSCPEvent::event_t eventsArr[] = {
695                LSCPEvent::event_voice_count, LSCPEvent::event_stream_count,
696                LSCPEvent::event_buffer_fill, LSCPEvent::event_total_voice_count
697            };
698            static const std::list<LSCPEvent::event_t> events(eventsArr, eventsArr + 4);
699    
700            if (LSCPServer::EventSubscribers(events))
701            {
702                LSCPServer::LockRTNotify();
703                std::map<uint,SamplerChannel*> channels = GetSamplerChannels();
704                std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
705                for (; iter != channels.end(); iter++) {
706                    SamplerChannel* pSamplerChannel = iter->second;
707                    EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
708                    if (!pEngineChannel) continue;
709                    Engine* pEngine = pEngineChannel->GetEngine();
710                    if (!pEngine) continue;
711                    fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount());
712                    fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());
713                    fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());
714                    fireTotalStreamCountChanged(GetDiskStreamCount());
715                    fireTotalVoiceCountChanged(GetVoiceCount());
716                }
717                LSCPServer::UnlockRTNotify();
718            }
719      }      }
720    
721  } // namespace LinuxSampler  } // namespace LinuxSampler

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

  ViewVC Help
Powered by ViewVC