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

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

  ViewVC Help
Powered by ViewVC