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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1777 - (hide annotations) (download)
Mon Sep 15 16:58:10 2008 UTC (15 years, 7 months ago) by persson
File size: 27922 byte(s)
* added experimental support for running LinuxSampler as a DSSI, LV2
  and VST plugin

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

  ViewVC Help
Powered by ViewVC