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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1424 - (hide annotations) (download)
Sun Oct 14 22:00:17 2007 UTC (16 years, 6 months ago) by schoenebeck
File size: 23651 byte(s)
* code cleanup:
- global.h now only covers global definitions that are needed for the C++
  API header files, all implementation internal global definitions are now
  in global_private.h
- atomic.h is not exposed to the C++ API anymore (replaced the references
  in SynchronizedConfig.h for this with local definitions)
- no need to include config.h anymore for using LS's API header files
- DB instruments classes are not exposed to the C++ API
- POSIX callback functions of Thread.h are hidden
- the (optional) gig Engine benchmark compiles again
- updated Doxyfile.in
- fixed warnings in API doc generation
* preparations for release 0.5.0

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

  ViewVC Help
Powered by ViewVC