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

Legend:
Removed from v.64  
changed lines
  Added in v.1212

  ViewVC Help
Powered by ViewVC