/[svn]/linuxsampler/trunk/src/Sampler.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/Sampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1212 - (hide annotations) (download)
Tue May 29 23:59:36 2007 UTC (16 years, 10 months ago) by schoenebeck
File size: 23406 byte(s)
* added highly experimental support for on-the-fly instrument editing
  within the sampler's process (by using instrument editor plugins),
  you'll notice the new "Registered instrument editors:" message on
  startup, the plugin path can be overridden at compile time with
  ./configure --enable-plugin-dir=/some/dir
* added a new LSCP command "EDIT INSTRUMENT <sampler-channel>" to spawn
  a matching instrument editor for the instrument on the given sampler
  channel (LSCP command syntax might be subject to change soon)
* config.h is not going to be installed along with liblinuxsampler's
  API header files anymore (not necessary anymore)
* take care of $(DESTDIR) when creating the instruments DB on 'make
  install' rule (needed for packaging and cross compilation)
* bumped version to 0.4.0.5cvs

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 61 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 1008 * Copyright (C) 2005 - 2007 Christian Schoenebeck *
7 schoenebeck 53 * *
8 schoenebeck 1008 * This library is free software; you can redistribute it and/or modify *
9 schoenebeck 53 * 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 *
11     * (at your option) any later version. *
12     * *
13 schoenebeck 1008 * This library is distributed in the hope that it will be useful, *
14 schoenebeck 53 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19 schoenebeck 1008 * along with this library; if not, write to the Free Software *
20 schoenebeck 53 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24 schoenebeck 123 #include <sstream>
25    
26 schoenebeck 53 #include "Sampler.h"
27    
28 iliev 778 #include "engines/EngineFactory.h"
29 schoenebeck 411 #include "engines/EngineChannelFactory.h"
30 schoenebeck 1212 #include "engines/InstrumentEditorFactory.h"
31 schoenebeck 203 #include "drivers/audio/AudioOutputDeviceFactory.h"
32     #include "drivers/midi/MidiInputDeviceFactory.h"
33 schoenebeck 1008 #include "drivers/midi/MidiInstrumentMapper.h"
34 schoenebeck 53
35     namespace LinuxSampler {
36    
37     // ******************************************************************
38     // * SamplerChannel
39    
40     SamplerChannel::SamplerChannel(Sampler* pS) {
41     pSampler = pS;
42 schoenebeck 411 pEngineChannel = NULL;
43 schoenebeck 675 pAudioOutputDevice = NULL;
44 capela 159 pMidiInputDevice = NULL;
45 schoenebeck 675 iMidiPort = 0;
46     midiChannel = midi_chan_all;
47 schoenebeck 53 iIndex = -1;
48     }
49    
50     SamplerChannel::~SamplerChannel() {
51 schoenebeck 411 if (pEngineChannel) {
52 persson 840 Engine* engine = pEngineChannel->GetEngine();
53     if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
54    
55 schoenebeck 675 MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel());
56 schoenebeck 411 if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
57 schoenebeck 420 if (pEngineChannel) {
58     if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
59 schoenebeck 660 EngineChannelFactory::Destroy(pEngineChannel);
60 persson 840
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 schoenebeck 420 }
65 schoenebeck 53 }
66     }
67    
68 schoenebeck 880 void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {
69 schoenebeck 411 dmsg(2,("SamplerChannel: Assigning engine type..."));
70 schoenebeck 53
71 schoenebeck 411 // create new engine channel
72     EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
73 schoenebeck 880 if (!pNewEngineChannel) throw Exception("Unknown engine type");
74 schoenebeck 53
75 schoenebeck 660 //FIXME: hack to allow fast retrieval of engine channel's sampler channel index
76     pNewEngineChannel->iSamplerChannelIndex = Index();
77    
78 capela 159 // dereference midi input port.
79 schoenebeck 675 MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
80     // disconnect old engine channel
81 schoenebeck 411 if (pEngineChannel) {
82 persson 840 Engine* engine = pEngineChannel->GetEngine();
83     if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
84    
85 schoenebeck 411 if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
86 schoenebeck 412 if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
87 schoenebeck 660 EngineChannelFactory::Destroy(pEngineChannel);
88 persson 840
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 schoenebeck 53 }
93    
94 schoenebeck 411 // connect new engine channel
95 schoenebeck 412 if (pAudioOutputDevice) {
96     pNewEngineChannel->Connect(pAudioOutputDevice);
97     pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
98     }
99 persson 846 if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());
100 schoenebeck 675 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 iliev 730 pEngineChannel->StatusChanged(true);
107 iliev 1130 fireEngineChanged();
108 schoenebeck 64 dmsg(2,("OK\n"));
109 schoenebeck 53 }
110    
111 schoenebeck 123 void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
112 iliev 960 if(pAudioOutputDevice == pDevice) return;
113    
114 schoenebeck 53 // disconnect old device
115 persson 840 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 schoenebeck 53 // connect new device
127     pAudioOutputDevice = pDevice;
128 schoenebeck 412 if (pEngineChannel) {
129     pEngineChannel->Connect(pAudioOutputDevice);
130     pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
131     }
132 schoenebeck 53 }
133    
134 capela 159 void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
135 schoenebeck 675 SetMidiInput(pDevice, 0, GetMidiInputChannel());
136 capela 159 }
137 schoenebeck 203
138 capela 159 void SamplerChannel::SetMidiInputPort(int MidiPort) {
139 schoenebeck 675 SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
140 capela 159 }
141 schoenebeck 203
142 schoenebeck 675 void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
143     SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
144 capela 159 }
145 schoenebeck 203
146 schoenebeck 675 void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) {
147 schoenebeck 880 if (!pDevice) throw Exception("No MIDI input device assigned.");
148 schoenebeck 675
149     // get old and new midi input port
150     MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
151     MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);
152    
153 capela 159 // disconnect old device port
154 schoenebeck 675 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 capela 159 // connect new device port
163 schoenebeck 675 if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);
164 capela 159 // Ooops.
165 schoenebeck 675 if (pNewMidiInputPort == NULL)
166 schoenebeck 880 throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
167 schoenebeck 53 }
168    
169 schoenebeck 411 EngineChannel* SamplerChannel::GetEngineChannel() {
170     return pEngineChannel;
171 schoenebeck 53 }
172    
173 schoenebeck 675 midi_chan_t SamplerChannel::GetMidiInputChannel() {
174     if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
175 capela 159 return this->midiChannel;
176 schoenebeck 53 }
177    
178 capela 159 int SamplerChannel::GetMidiInputPort() {
179 schoenebeck 675 MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;
180     if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
181     return iMidiPort;
182 capela 159 }
183    
184 schoenebeck 53 AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
185     return pAudioOutputDevice;
186     }
187    
188 senkov 155 MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
189 schoenebeck 675 if (pEngineChannel)
190     pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;
191 capela 159 return pMidiInputDevice;
192 senkov 155 }
193    
194 schoenebeck 53 uint SamplerChannel::Index() {
195     if (iIndex >= 0) return iIndex;
196    
197 schoenebeck 209 Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
198     for (; iter != pSampler->mSamplerChannels.end(); iter++) {
199     if (iter->second == this) {
200     iIndex = iter->first;
201     return iIndex;
202 schoenebeck 53 }
203     }
204    
205 schoenebeck 880 throw Exception("Internal error: SamplerChannel index not found");
206 schoenebeck 53 }
207    
208 iliev 1130 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 schoenebeck 675 MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {
227 schoenebeck 221 MidiInputPort* pMidiInputPort = NULL;
228 schoenebeck 675 MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
229 capela 159 if (pMidiInputDevice)
230 schoenebeck 221 pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
231 capela 159 return pMidiInputPort;
232     }
233 schoenebeck 53
234 schoenebeck 212
235    
236 schoenebeck 53 // ******************************************************************
237     // * Sampler
238    
239     Sampler::Sampler() {
240 iliev 1130 eventHandler.SetSampler(this);
241 schoenebeck 53 }
242    
243     Sampler::~Sampler() {
244 schoenebeck 212 Reset();
245 schoenebeck 53 }
246    
247     uint Sampler::SamplerChannels() {
248 schoenebeck 209 return mSamplerChannels.size();
249 schoenebeck 53 }
250    
251 iliev 1130 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     void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) {
266     llAudioDeviceCountListeners.AddListener(l);
267     }
268    
269     void Sampler::RemoveAudioDeviceCountListener(AudioDeviceCountListener* l) {
270     llAudioDeviceCountListeners.RemoveListener(l);
271     }
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     void Sampler::AddMidiDeviceCountListener(MidiDeviceCountListener* l) {
280     llMidiDeviceCountListeners.AddListener(l);
281     }
282    
283     void Sampler::RemoveMidiDeviceCountListener(MidiDeviceCountListener* l) {
284     llMidiDeviceCountListeners.RemoveListener(l);
285     }
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     void Sampler::AddVoiceCountListener(VoiceCountListener* l) {
294     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 schoenebeck 53 SamplerChannel* Sampler::AddSamplerChannel() {
375 schoenebeck 209 // if there's no sampler channel yet
376     if (!mSamplerChannels.size()) {
377     SamplerChannel* pChannel = new SamplerChannel(this);
378     mSamplerChannels[0] = pChannel;
379 iliev 1130 fireChannelCountChanged(1);
380     pChannel->AddEngineChangeListener(&eventHandler);
381 schoenebeck 209 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 iliev 1130 fireChannelCountChanged(SamplerChannels());
396     pChannel->AddEngineChangeListener(&eventHandler);
397 schoenebeck 209 return pChannel;
398     }
399 schoenebeck 880 throw Exception("Internal error: could not find unoccupied sampler channel index.");
400 schoenebeck 209 }
401    
402     // we have not reached the index limit so we just add the channel past the highest index
403 schoenebeck 53 SamplerChannel* pChannel = new SamplerChannel(this);
404 schoenebeck 209 mSamplerChannels[lastIndex + 1] = pChannel;
405 iliev 1130 fireChannelCountChanged(SamplerChannels());
406     pChannel->AddEngineChangeListener(&eventHandler);
407 schoenebeck 53 return pChannel;
408     }
409    
410     SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
411 schoenebeck 209 return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
412 schoenebeck 53 }
413    
414 schoenebeck 209 std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
415     return mSamplerChannels;
416     }
417    
418 schoenebeck 53 void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
419 schoenebeck 209 SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
420     for (; iterChan != mSamplerChannels.end(); iterChan++) {
421     if (iterChan->second == pSamplerChannel) {
422 iliev 1130 pSamplerChannel->RemoveAllEngineChangeListeners();
423 schoenebeck 209 mSamplerChannels.erase(iterChan);
424 schoenebeck 53 delete pSamplerChannel;
425 iliev 1130 fireChannelCountChanged(SamplerChannels());
426 schoenebeck 53 return;
427     }
428     }
429     }
430    
431     void Sampler::RemoveSamplerChannel(uint uiSamplerChannel) {
432     SamplerChannel* pChannel = GetSamplerChannel(uiSamplerChannel);
433     if (!pChannel) return;
434     RemoveSamplerChannel(pChannel);
435     }
436    
437 schoenebeck 123 std::vector<String> Sampler::AvailableAudioOutputDrivers() {
438     return AudioOutputDeviceFactory::AvailableDrivers();
439     }
440 schoenebeck 53
441 schoenebeck 900 std::vector<String> Sampler::AvailableMidiInputDrivers() {
442     return MidiInputDeviceFactory::AvailableDrivers();
443     }
444    
445     std::vector<String> Sampler::AvailableEngineTypes() {
446     return EngineFactory::AvailableEngineTypes();
447     }
448    
449 schoenebeck 880 AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) {
450 schoenebeck 53 // create new device
451 schoenebeck 123 AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
452 schoenebeck 53
453 schoenebeck 64 // add new audio device to the audio device list
454 schoenebeck 123 for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
455     if (!mAudioOutputDevices[i]) {
456     mAudioOutputDevices[i] = pDevice;
457     break;
458     }
459     }
460 schoenebeck 64
461 iliev 1130 fireAudioDeviceCountChanged(AudioOutputDevices());
462 schoenebeck 53 return pDevice;
463     }
464    
465 schoenebeck 123 uint Sampler::AudioOutputDevices() {
466     return mAudioOutputDevices.size();
467 schoenebeck 53 }
468    
469 senkov 155 uint Sampler::MidiInputDevices() {
470     return mMidiInputDevices.size();
471     }
472    
473 schoenebeck 123 std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
474     return mAudioOutputDevices;
475     }
476    
477 senkov 155 std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
478     return mMidiInputDevices;
479     }
480    
481 schoenebeck 880 void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
482 schoenebeck 123 AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
483     for (; iter != mAudioOutputDevices.end(); iter++) {
484     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 schoenebeck 880 if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device.");
488 schoenebeck 123
489     // disable device
490     pDevice->Stop();
491    
492 persson 837 // remove device from the device list
493     mAudioOutputDevices.erase(iter);
494    
495 schoenebeck 123 // destroy and free device from memory
496     delete pDevice;
497 persson 837
498 iliev 1130 fireAudioDeviceCountChanged(AudioOutputDevices());
499 persson 837 break;
500 schoenebeck 123 }
501     }
502     }
503    
504 schoenebeck 880 void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
505 senkov 155 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 schoenebeck 880 if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
511 schoenebeck 53
512 senkov 155 // disable device
513     pDevice->StopListen();
514    
515 persson 837 // remove device from the device list
516     mMidiInputDevices.erase(iter);
517    
518 senkov 155 // destroy and free device from memory
519     delete pDevice;
520 persson 837
521 iliev 1130 fireMidiDeviceCountChanged(MidiInputDevices());
522 persson 837 break;
523 senkov 155 }
524 schoenebeck 53 }
525 senkov 155 }
526 schoenebeck 53
527 schoenebeck 880 MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) {
528 senkov 155 // create new device
529 schoenebeck 551 MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
530 senkov 155
531     // add new device to the midi device list
532     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 schoenebeck 64
539 iliev 1130 fireMidiDeviceCountChanged(MidiInputDevices());
540 schoenebeck 53 return pDevice;
541     }
542    
543 iliev 778 int Sampler::GetVoiceCount() {
544     int count = 0;
545     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 schoenebeck 212 void Sampler::Reset() {
555     // delete sampler channels
556     try {
557 senkov 329 while (true) {
558     SamplerChannelMap::iterator iter = mSamplerChannels.begin();
559     if (iter == mSamplerChannels.end()) break;
560     RemoveSamplerChannel(iter->second);
561 schoenebeck 212 }
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 senkov 329 while (true) {
571     MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
572     if (iter == mMidiInputDevices.end()) break;
573     DestroyMidiInputDevice(iter->second);
574 schoenebeck 212 }
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 senkov 329 while (true) {
584     AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
585     if (iter == mAudioOutputDevices.end()) break;
586     DestroyAudioOutputDevice(iter->second);
587 schoenebeck 212 }
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 schoenebeck 1008
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 schoenebeck 1212
603     // unload all instrument editor DLLs
604     InstrumentEditorFactory::ClosePlugins();
605 schoenebeck 212 }
606    
607 schoenebeck 53 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC