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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1761 - (hide annotations) (download)
Fri Aug 29 15:42:06 2008 UTC (15 years, 7 months ago) by iliev
File size: 26264 byte(s)
* fixed a crash which occurs when removing a sampler channel waiting
  to start instrument loading after another channel

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 1686 * Copyright (C) 2005 - 2008 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 1723 #include "common/Features.h"
36 schoenebeck 53
37     namespace LinuxSampler {
38    
39     // ******************************************************************
40     // * SamplerChannel
41    
42     SamplerChannel::SamplerChannel(Sampler* pS) {
43     pSampler = pS;
44 schoenebeck 411 pEngineChannel = NULL;
45 schoenebeck 675 pAudioOutputDevice = NULL;
46 capela 159 pMidiInputDevice = NULL;
47 schoenebeck 675 iMidiPort = 0;
48     midiChannel = midi_chan_all;
49 schoenebeck 53 iIndex = -1;
50     }
51    
52     SamplerChannel::~SamplerChannel() {
53 schoenebeck 411 if (pEngineChannel) {
54 persson 840 Engine* engine = pEngineChannel->GetEngine();
55     if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
56    
57 schoenebeck 675 MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel());
58 schoenebeck 411 if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
59 schoenebeck 420 if (pEngineChannel) {
60     if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
61 schoenebeck 660 EngineChannelFactory::Destroy(pEngineChannel);
62 persson 840
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 schoenebeck 420 }
67 schoenebeck 53 }
68     }
69    
70 schoenebeck 880 void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {
71 schoenebeck 411 dmsg(2,("SamplerChannel: Assigning engine type..."));
72 iliev 1283
73     if (pEngineChannel) {
74     if (!strcasecmp(pEngineChannel->EngineName().c_str(), EngineType.c_str())) {
75     dmsg(2,("OK\n"));
76     return;
77     }
78     }
79 schoenebeck 53
80 schoenebeck 1686 fireEngineToBeChanged();
81    
82 schoenebeck 411 // create new engine channel
83     EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
84 schoenebeck 880 if (!pNewEngineChannel) throw Exception("Unknown engine type");
85 schoenebeck 53
86 iliev 1761 pNewEngineChannel->SetSamplerChannel(this);
87 schoenebeck 660
88 capela 159 // dereference midi input port.
89 schoenebeck 675 MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
90     // disconnect old engine channel
91 schoenebeck 411 if (pEngineChannel) {
92 persson 840 Engine* engine = pEngineChannel->GetEngine();
93     if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
94    
95 schoenebeck 411 if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
96 schoenebeck 412 if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
97 schoenebeck 660 EngineChannelFactory::Destroy(pEngineChannel);
98 persson 840
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 schoenebeck 53 }
103    
104 schoenebeck 411 // connect new engine channel
105 schoenebeck 412 if (pAudioOutputDevice) {
106     pNewEngineChannel->Connect(pAudioOutputDevice);
107     pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
108     }
109 persson 846 if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());
110 schoenebeck 675 pEngineChannel = pNewEngineChannel;
111    
112     // from now on get MIDI device and port from EngineChannel object
113     this->pMidiInputDevice = NULL;
114     this->iMidiPort = 0;
115    
116 iliev 730 pEngineChannel->StatusChanged(true);
117 iliev 1130 fireEngineChanged();
118 schoenebeck 64 dmsg(2,("OK\n"));
119 schoenebeck 53 }
120    
121 schoenebeck 123 void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
122 iliev 960 if(pAudioOutputDevice == pDevice) return;
123    
124 schoenebeck 53 // disconnect old device
125 persson 840 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 schoenebeck 53 // connect new device
137     pAudioOutputDevice = pDevice;
138 schoenebeck 412 if (pEngineChannel) {
139     pEngineChannel->Connect(pAudioOutputDevice);
140     pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
141     }
142 schoenebeck 53 }
143    
144 capela 159 void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
145 schoenebeck 675 SetMidiInput(pDevice, 0, GetMidiInputChannel());
146 capela 159 }
147 schoenebeck 203
148 capela 159 void SamplerChannel::SetMidiInputPort(int MidiPort) {
149 schoenebeck 675 SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
150 capela 159 }
151 schoenebeck 203
152 schoenebeck 675 void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
153     SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
154 capela 159 }
155 schoenebeck 203
156 schoenebeck 675 void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) {
157 schoenebeck 880 if (!pDevice) throw Exception("No MIDI input device assigned.");
158 schoenebeck 675
159     // get old and new midi input port
160     MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
161     MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);
162    
163 capela 159 // disconnect old device port
164 schoenebeck 675 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 capela 159 // connect new device port
173 schoenebeck 675 if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);
174 capela 159 // Ooops.
175 schoenebeck 675 if (pNewMidiInputPort == NULL)
176 schoenebeck 880 throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
177 schoenebeck 53 }
178    
179 schoenebeck 411 EngineChannel* SamplerChannel::GetEngineChannel() {
180     return pEngineChannel;
181 schoenebeck 53 }
182    
183 schoenebeck 675 midi_chan_t SamplerChannel::GetMidiInputChannel() {
184     if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
185 capela 159 return this->midiChannel;
186 schoenebeck 53 }
187    
188 capela 159 int SamplerChannel::GetMidiInputPort() {
189 schoenebeck 675 MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;
190     if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
191     return iMidiPort;
192 capela 159 }
193    
194 schoenebeck 53 AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
195     return pAudioOutputDevice;
196     }
197    
198 senkov 155 MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
199 schoenebeck 675 if (pEngineChannel)
200     pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;
201 capela 159 return pMidiInputDevice;
202 senkov 155 }
203    
204 schoenebeck 53 uint SamplerChannel::Index() {
205     if (iIndex >= 0) return iIndex;
206    
207 schoenebeck 209 Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
208     for (; iter != pSampler->mSamplerChannels.end(); iter++) {
209     if (iter->second == this) {
210     iIndex = iter->first;
211     return iIndex;
212 schoenebeck 53 }
213     }
214    
215 schoenebeck 880 throw Exception("Internal error: SamplerChannel index not found");
216 schoenebeck 53 }
217    
218 iliev 1761 Sampler* SamplerChannel::GetSampler() {
219     return pSampler;
220     }
221    
222 iliev 1130 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 schoenebeck 1686 void SamplerChannel::fireEngineToBeChanged() {
235     for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {
236     llEngineChangeListeners.GetListener(i)->EngineToBeChanged(Index());
237     }
238     }
239    
240 iliev 1130 void SamplerChannel::fireEngineChanged() {
241     for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {
242     llEngineChangeListeners.GetListener(i)->EngineChanged(Index());
243     }
244     }
245    
246 schoenebeck 675 MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {
247 schoenebeck 221 MidiInputPort* pMidiInputPort = NULL;
248 schoenebeck 675 MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
249 capela 159 if (pMidiInputDevice)
250 schoenebeck 221 pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
251 capela 159 return pMidiInputPort;
252     }
253 schoenebeck 53
254 schoenebeck 212
255    
256 schoenebeck 53 // ******************************************************************
257     // * Sampler
258    
259     Sampler::Sampler() {
260 iliev 1130 eventHandler.SetSampler(this);
261 schoenebeck 53 }
262    
263     Sampler::~Sampler() {
264 schoenebeck 212 Reset();
265 schoenebeck 53 }
266    
267     uint Sampler::SamplerChannels() {
268 schoenebeck 209 return mSamplerChannels.size();
269 schoenebeck 53 }
270    
271 iliev 1130 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 schoenebeck 1686 void Sampler::fireChannelAdded(SamplerChannel* pChannel) {
286     for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
287     llChannelCountListeners.GetListener(i)->ChannelAdded(pChannel);
288     }
289     }
290    
291     void Sampler::fireChannelToBeRemoved(SamplerChannel* pChannel) {
292     for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
293     llChannelCountListeners.GetListener(i)->ChannelToBeRemoved(pChannel);
294     }
295     }
296    
297 iliev 1130 void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) {
298     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 schoenebeck 1695 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 iliev 1130 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 iliev 1541 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 iliev 1130 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 schoenebeck 1686 void Sampler::EventHandler::EngineToBeChanged(int ChannelId) {
422     // nothing to do here
423     }
424    
425 iliev 1130 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 schoenebeck 53 SamplerChannel* Sampler::AddSamplerChannel() {
437 schoenebeck 209 // if there's no sampler channel yet
438     if (!mSamplerChannels.size()) {
439     SamplerChannel* pChannel = new SamplerChannel(this);
440     mSamplerChannels[0] = pChannel;
441 schoenebeck 1686 fireChannelAdded(pChannel);
442 iliev 1130 fireChannelCountChanged(1);
443     pChannel->AddEngineChangeListener(&eventHandler);
444 schoenebeck 209 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 schoenebeck 1686 fireChannelAdded(pChannel);
459 iliev 1130 fireChannelCountChanged(SamplerChannels());
460     pChannel->AddEngineChangeListener(&eventHandler);
461 schoenebeck 209 return pChannel;
462     }
463 schoenebeck 880 throw Exception("Internal error: could not find unoccupied sampler channel index.");
464 schoenebeck 209 }
465    
466     // we have not reached the index limit so we just add the channel past the highest index
467 schoenebeck 53 SamplerChannel* pChannel = new SamplerChannel(this);
468 schoenebeck 209 mSamplerChannels[lastIndex + 1] = pChannel;
469 schoenebeck 1686 fireChannelAdded(pChannel);
470 iliev 1130 fireChannelCountChanged(SamplerChannels());
471     pChannel->AddEngineChangeListener(&eventHandler);
472 schoenebeck 53 return pChannel;
473     }
474    
475     SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
476 schoenebeck 209 return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
477 schoenebeck 53 }
478    
479 schoenebeck 209 std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
480     return mSamplerChannels;
481     }
482    
483 schoenebeck 53 void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
484 schoenebeck 209 SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
485     for (; iterChan != mSamplerChannels.end(); iterChan++) {
486     if (iterChan->second == pSamplerChannel) {
487 schoenebeck 1686 fireChannelToBeRemoved(pSamplerChannel);
488 iliev 1130 pSamplerChannel->RemoveAllEngineChangeListeners();
489 schoenebeck 209 mSamplerChannels.erase(iterChan);
490 schoenebeck 53 delete pSamplerChannel;
491 iliev 1130 fireChannelCountChanged(SamplerChannels());
492 schoenebeck 53 return;
493     }
494     }
495     }
496    
497     void Sampler::RemoveSamplerChannel(uint uiSamplerChannel) {
498     SamplerChannel* pChannel = GetSamplerChannel(uiSamplerChannel);
499     if (!pChannel) return;
500     RemoveSamplerChannel(pChannel);
501     }
502    
503 schoenebeck 123 std::vector<String> Sampler::AvailableAudioOutputDrivers() {
504     return AudioOutputDeviceFactory::AvailableDrivers();
505     }
506 schoenebeck 53
507 schoenebeck 900 std::vector<String> Sampler::AvailableMidiInputDrivers() {
508     return MidiInputDeviceFactory::AvailableDrivers();
509     }
510    
511     std::vector<String> Sampler::AvailableEngineTypes() {
512     return EngineFactory::AvailableEngineTypes();
513     }
514    
515 schoenebeck 880 AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) {
516 schoenebeck 53 // create new device
517 schoenebeck 123 AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
518 schoenebeck 53
519 schoenebeck 64 // add new audio device to the audio device list
520 schoenebeck 123 for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
521     if (!mAudioOutputDevices[i]) {
522     mAudioOutputDevices[i] = pDevice;
523     break;
524     }
525     }
526 schoenebeck 64
527 iliev 1130 fireAudioDeviceCountChanged(AudioOutputDevices());
528 schoenebeck 53 return pDevice;
529     }
530    
531 schoenebeck 123 uint Sampler::AudioOutputDevices() {
532     return mAudioOutputDevices.size();
533 schoenebeck 53 }
534    
535 senkov 155 uint Sampler::MidiInputDevices() {
536     return mMidiInputDevices.size();
537     }
538    
539 schoenebeck 123 std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
540     return mAudioOutputDevices;
541     }
542    
543 senkov 155 std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
544     return mMidiInputDevices;
545     }
546    
547 schoenebeck 880 void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
548 schoenebeck 123 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 schoenebeck 880 if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device.");
554 schoenebeck 123
555     // disable device
556     pDevice->Stop();
557    
558 persson 837 // remove device from the device list
559     mAudioOutputDevices.erase(iter);
560    
561 schoenebeck 123 // destroy and free device from memory
562     delete pDevice;
563 persson 837
564 iliev 1130 fireAudioDeviceCountChanged(AudioOutputDevices());
565 persson 837 break;
566 schoenebeck 123 }
567     }
568     }
569    
570 schoenebeck 880 void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
571 senkov 155 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 schoenebeck 880 if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
577 schoenebeck 53
578 schoenebeck 1695 fireMidiDeviceToBeDestroyed(pDevice);
579    
580 senkov 155 // disable device
581     pDevice->StopListen();
582    
583 persson 837 // remove device from the device list
584     mMidiInputDevices.erase(iter);
585    
586 senkov 155 // destroy and free device from memory
587     delete pDevice;
588 persson 837
589 iliev 1130 fireMidiDeviceCountChanged(MidiInputDevices());
590 persson 837 break;
591 senkov 155 }
592 schoenebeck 53 }
593 senkov 155 }
594 schoenebeck 53
595 schoenebeck 880 MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) {
596 senkov 155 // create new device
597 schoenebeck 551 MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
598 senkov 155
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 schoenebeck 64
607 schoenebeck 1695 fireMidiDeviceCreated(pDevice);
608 iliev 1130 fireMidiDeviceCountChanged(MidiInputDevices());
609 schoenebeck 53 return pDevice;
610     }
611    
612 iliev 1541 int Sampler::GetDiskStreamCount() {
613     int count = 0;
614     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 iliev 778 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 schoenebeck 212 void Sampler::Reset() {
635     // delete sampler channels
636     try {
637 senkov 329 while (true) {
638     SamplerChannelMap::iterator iter = mSamplerChannels.begin();
639     if (iter == mSamplerChannels.end()) break;
640     RemoveSamplerChannel(iter->second);
641 schoenebeck 212 }
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 senkov 329 while (true) {
651     MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
652     if (iter == mMidiInputDevices.end()) break;
653     DestroyMidiInputDevice(iter->second);
654 schoenebeck 212 }
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 senkov 329 while (true) {
664     AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
665     if (iter == mAudioOutputDevices.end()) break;
666     DestroyAudioOutputDevice(iter->second);
667 schoenebeck 212 }
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 schoenebeck 1008
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 schoenebeck 1212
683     // unload all instrument editor DLLs
684     InstrumentEditorFactory::ClosePlugins();
685 schoenebeck 212 }
686    
687 schoenebeck 1723 bool Sampler::EnableDenormalsAreZeroMode() {
688     Features::detect();
689     return Features::enableDenormalsAreZeroMode();
690     }
691    
692 schoenebeck 53 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC