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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4021 - (hide annotations) (download)
Wed Jan 5 16:11:04 2022 UTC (2 years, 3 months ago) by schoenebeck
File size: 36256 byte(s)
* Sampler::Reset() no longer unloads instrument editor plugins, this is a
  workaround for the issue that instrument editor plugins were no longer
  available after sending a LSCP "RESET" command (probably due to a race).

* Bumped version (2.2.0.svn16).

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 4021 * Copyright (C) 2005 - 2022 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     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 420 if (pEngineChannel) {
58 schoenebeck 2500 pEngineChannel->DisconnectAllMidiInputPorts();
59    
60 schoenebeck 420 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 schoenebeck 1934
73 iliev 1283 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 schoenebeck 2500 // remember current MIDI input connections
87     std::vector<MidiInputPort*> vMidiInputs = GetMidiInputPorts();
88 schoenebeck 2123 midi_chan_t midiChannel = GetMidiInputChannel();
89 schoenebeck 2500
90     try {
91     pNewEngineChannel->SetSamplerChannel(this);
92 persson 840
93 schoenebeck 2500 // disconnect old engine channel
94     if (pEngineChannel) {
95     Engine* engine = pEngineChannel->GetEngine();
96     if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
97 persson 840
98 schoenebeck 2500 pEngineChannel->DisconnectAllMidiInputPorts();
99     if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
100     EngineChannelFactory::Destroy(pEngineChannel);
101     pEngineChannel = NULL;
102 schoenebeck 53
103 schoenebeck 2500 // reconnect engine if it still exists
104     const std::set<Engine*>& engines = EngineFactory::EngineInstances();
105     if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
106     }
107    
108     // connect new engine channel
109     if (pAudioOutputDevice) {
110     pNewEngineChannel->Connect(pAudioOutputDevice);
111     pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
112     }
113     pNewEngineChannel->SetMidiChannel(midiChannel);
114     for (int i = 0; i < vMidiInputs.size(); ++i) {
115     pNewEngineChannel->Connect(vMidiInputs[i]);
116     }
117     } catch (...) {
118     EngineChannelFactory::Destroy(pNewEngineChannel);
119     throw; // re-throw the same exception
120 schoenebeck 412 }
121 schoenebeck 675 pEngineChannel = pNewEngineChannel;
122    
123 schoenebeck 2500 // from now on get MIDI input ports from EngineChannel object
124     this->vMidiInputs.clear();
125 schoenebeck 675 this->iMidiPort = 0;
126    
127 iliev 730 pEngineChannel->StatusChanged(true);
128 iliev 1130 fireEngineChanged();
129 schoenebeck 64 dmsg(2,("OK\n"));
130 schoenebeck 53 }
131    
132 schoenebeck 1937 void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
133 iliev 960 if(pAudioOutputDevice == pDevice) return;
134    
135 schoenebeck 53 // disconnect old device
136 persson 840 if (pAudioOutputDevice && pEngineChannel) {
137 schoenebeck 1937 if (!pAudioOutputDevice->isAutonomousDevice())
138     throw Exception("The audio output device '" + pAudioOutputDevice->Driver() + "' cannot be dropped from this sampler channel!");
139    
140 persson 840 Engine* engine = pEngineChannel->GetEngine();
141     pAudioOutputDevice->Disconnect(engine);
142    
143     pEngineChannel->DisconnectAudioOutputDevice();
144    
145     // reconnect engine if it still exists
146     const std::set<Engine*>& engines = EngineFactory::EngineInstances();
147     if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
148     }
149    
150 schoenebeck 53 // connect new device
151     pAudioOutputDevice = pDevice;
152 schoenebeck 412 if (pEngineChannel) {
153     pEngineChannel->Connect(pAudioOutputDevice);
154     pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
155     }
156 schoenebeck 53 }
157    
158 schoenebeck 2500 void SamplerChannel::Connect(MidiInputPort* pPort) throw (Exception) {
159     if (!pPort) throw Exception("No MIDI input port provided");
160    
161     // prevent attempts to connect non-autonomous MIDI ports
162     // (host plugins like VST, AU, LV2, DSSI)
163     if (!pPort->GetDevice()->isAutonomousDevice())
164     throw Exception("The MIDI input port '" + pPort->GetDevice()->Driver() + "' cannot be managed manually!");
165    
166     std::vector<MidiInputPort*> vMidiPorts = GetMidiInputPorts();
167    
168     // ignore if port is already connected
169     for (int i = 0; i < vMidiPorts.size(); ++i) {
170     if (vMidiPorts[i] == pPort) return;
171     }
172    
173     // connect this new port
174     if (pEngineChannel) {
175     pEngineChannel->Connect(pPort);
176     } else { // no engine channel yet, remember it for future connection ...
177     const midi_conn_t c = {
178 schoenebeck 2503 static_cast<uint>(pPort->GetDevice()->MidiInputDeviceID()),
179 schoenebeck 2500 pPort->GetPortNumber()
180     };
181     this->vMidiInputs.push_back(c);
182     }
183     }
184    
185     void SamplerChannel::Disconnect(MidiInputPort* pPort) throw (Exception) {
186     if (!pPort) return;
187    
188     // prevent attempts to alter channels with non-autonomous devices
189     // (host plugins like VST, AU, LV2, DSSI)
190     if (!pPort->GetDevice()->isAutonomousDevice())
191     throw Exception("The MIDI input port '" + pPort->GetDevice()->Driver() + "' cannot be managed manually!");
192    
193     // disconnect this port
194     if (pEngineChannel) {
195     pEngineChannel->Disconnect(pPort);
196     } else { // no engine channel yet, forget it regarding future connection ...
197     const midi_conn_t c = {
198 schoenebeck 2503 static_cast<uint>(pPort->GetDevice()->MidiInputDeviceID()),
199 schoenebeck 2500 pPort->GetPortNumber()
200     };
201 schoenebeck 3054 for (ssize_t i = this->vMidiInputs.size() - 1; i >= 0; --i) {
202 schoenebeck 2500 if (this->vMidiInputs[i] == c)
203     this->vMidiInputs.erase(this->vMidiInputs.begin() + i);
204     // no break or return here, for safety reasons
205     // (just in case there were really duplicates for some reason)
206     }
207     }
208     }
209    
210     void SamplerChannel::DisconnectAllMidiInputPorts() throw (Exception) {
211     std::vector<MidiInputPort*> vMidiPorts = GetMidiInputPorts();
212     for (int i = 0; i < vMidiPorts.size(); ++i) Disconnect(vMidiPorts[i]);
213     }
214    
215     std::vector<MidiInputPort*> SamplerChannel::GetMidiInputPorts() {
216     std::vector<MidiInputPort*> v;
217     if (pEngineChannel) {
218     MidiInputPort* pPort = pEngineChannel->GetMidiInputPort(0);
219     for (int i = 0; pPort; pPort = pEngineChannel->GetMidiInputPort(++i))
220     v.push_back(pPort);
221     } else {
222     for (int i = 0; i < this->vMidiInputs.size(); ++i) {
223     MidiInputPort* pPort = _getPortForID(this->vMidiInputs[i]);
224     if (pPort) v.push_back(pPort);
225     }
226     }
227     return v;
228     }
229    
230 schoenebeck 1937 void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
231 schoenebeck 675 SetMidiInput(pDevice, 0, GetMidiInputChannel());
232 capela 159 }
233 schoenebeck 203
234 schoenebeck 1937 void SamplerChannel::SetMidiInputPort(int MidiPort) throw (Exception) {
235 schoenebeck 675 SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
236 capela 159 }
237 schoenebeck 203
238 schoenebeck 675 void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
239 schoenebeck 2500 if (!isValidMidiChan(MidiChannel)) throw Exception("Invalid MIDI channel (" + ToString(int(MidiChannel)) + ")");
240     if (pEngineChannel) pEngineChannel->SetMidiChannel(MidiChannel);
241     this->midiChannel = MidiChannel;
242 capela 159 }
243 schoenebeck 203
244 schoenebeck 1937 void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) throw (Exception) {
245 schoenebeck 880 if (!pDevice) throw Exception("No MIDI input device assigned.");
246 schoenebeck 675
247 schoenebeck 2500 // apply new MIDI channel
248     SetMidiInputChannel(MidiChannel);
249 schoenebeck 675
250 schoenebeck 2500 MidiInputPort* pNewPort = pDevice->GetPort(iMidiPort);
251     if (!pNewPort) throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
252 schoenebeck 1937
253 schoenebeck 2500 std::vector<MidiInputPort*> vMidiPorts = GetMidiInputPorts();
254    
255     // prevent attempts to remove non-autonomous MIDI ports
256     // (host plugins like VST, AU, LV2, DSSI)
257     for (int i = 0; i < vMidiPorts.size(); ++i) {
258     if (vMidiPorts[i] == pNewPort) continue;
259     if (!vMidiPorts[i]->GetDevice()->isAutonomousDevice())
260     throw Exception("The MIDI input port '" + vMidiPorts[i]->GetDevice()->Driver() + "' cannot be altered on this sampler channel!");
261 schoenebeck 1937 }
262    
263 schoenebeck 2500 if (pEngineChannel) {
264     // remove all current connections
265     pEngineChannel->DisconnectAllMidiInputPorts();
266     // create the new connection (alone)
267     pEngineChannel->Connect(pNewPort);
268     } else { // if there is no engine channel yet, then store connection for future ...
269     // delete all previously scheduled connections
270     this->vMidiInputs.clear();
271     // store the new connection (alone)
272     const midi_conn_t c = {
273 schoenebeck 2503 static_cast<uint>(pNewPort->GetDevice()->MidiInputDeviceID()),
274 schoenebeck 2500 pNewPort->GetPortNumber()
275     };
276     this->vMidiInputs.push_back(c);
277     this->iMidiPort = iMidiPort;
278 schoenebeck 675 }
279 schoenebeck 53 }
280    
281 schoenebeck 411 EngineChannel* SamplerChannel::GetEngineChannel() {
282     return pEngineChannel;
283 schoenebeck 53 }
284    
285 schoenebeck 675 midi_chan_t SamplerChannel::GetMidiInputChannel() {
286     if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
287 capela 159 return this->midiChannel;
288 schoenebeck 53 }
289    
290 capela 159 int SamplerChannel::GetMidiInputPort() {
291 schoenebeck 2500 MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort(0) : NULL;
292 schoenebeck 675 if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
293     return iMidiPort;
294 capela 159 }
295    
296 schoenebeck 53 AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
297     return pAudioOutputDevice;
298     }
299    
300 senkov 155 MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
301 schoenebeck 675 if (pEngineChannel)
302 schoenebeck 2500 return (pEngineChannel->GetMidiInputPort(0)) ? pEngineChannel->GetMidiInputPort(0)->GetDevice() : NULL;
303    
304     if (vMidiInputs.empty())
305     return NULL;
306    
307     std::map<uint, MidiInputDevice*> mAllDevices = MidiInputDeviceFactory::Devices();
308     if (!mAllDevices.count(vMidiInputs[0].deviceID))
309     return NULL;
310    
311     return mAllDevices[vMidiInputs[0].deviceID];
312 senkov 155 }
313    
314 schoenebeck 53 uint SamplerChannel::Index() {
315     if (iIndex >= 0) return iIndex;
316    
317 schoenebeck 209 Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
318     for (; iter != pSampler->mSamplerChannels.end(); iter++) {
319     if (iter->second == this) {
320     iIndex = iter->first;
321     return iIndex;
322 schoenebeck 53 }
323     }
324    
325 schoenebeck 880 throw Exception("Internal error: SamplerChannel index not found");
326 schoenebeck 53 }
327    
328 iliev 1761 Sampler* SamplerChannel::GetSampler() {
329     return pSampler;
330     }
331    
332 iliev 1130 void SamplerChannel::AddEngineChangeListener(EngineChangeListener* l) {
333     llEngineChangeListeners.AddListener(l);
334     }
335    
336     void SamplerChannel::RemoveEngineChangeListener(EngineChangeListener* l) {
337     llEngineChangeListeners.RemoveListener(l);
338     }
339    
340     void SamplerChannel::RemoveAllEngineChangeListeners() {
341     llEngineChangeListeners.RemoveAllListeners();
342     }
343    
344 schoenebeck 1686 void SamplerChannel::fireEngineToBeChanged() {
345     for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {
346     llEngineChangeListeners.GetListener(i)->EngineToBeChanged(Index());
347     }
348     }
349    
350 iliev 1130 void SamplerChannel::fireEngineChanged() {
351     for (int i = 0; i < llEngineChangeListeners.GetListenerCount(); i++) {
352     llEngineChangeListeners.GetListener(i)->EngineChanged(Index());
353     }
354     }
355 schoenebeck 2500
356     /**
357     * Takes a numeric MIDI device ID, port ID pair as argument and returns
358     * the actual MIDI input port associated with that unique ID pair.
359     */
360     MidiInputPort* SamplerChannel::_getPortForID(const midi_conn_t& c) {
361     std::map<uint, MidiInputDevice*> mAllDevices = MidiInputDeviceFactory::Devices();
362     if (!mAllDevices.count(c.deviceID))
363     return NULL;
364 iliev 1130
365 schoenebeck 2500 return mAllDevices[c.deviceID]->GetPort(c.portNr);
366 capela 159 }
367 schoenebeck 53
368 schoenebeck 212
369 schoenebeck 53 // ******************************************************************
370     // * Sampler
371    
372     Sampler::Sampler() {
373 iliev 1130 eventHandler.SetSampler(this);
374 iliev 1789 uiOldTotalVoiceCount = uiOldTotalStreamCount = 0;
375 schoenebeck 53 }
376    
377     Sampler::~Sampler() {
378 schoenebeck 212 Reset();
379 schoenebeck 53 }
380    
381     uint Sampler::SamplerChannels() {
382 schoenebeck 3054 return (uint) mSamplerChannels.size();
383 schoenebeck 53 }
384    
385 iliev 1130 void Sampler::AddChannelCountListener(ChannelCountListener* l) {
386     llChannelCountListeners.AddListener(l);
387     }
388    
389     void Sampler::RemoveChannelCountListener(ChannelCountListener* l) {
390     llChannelCountListeners.RemoveListener(l);
391     }
392    
393     void Sampler::fireChannelCountChanged(int NewCount) {
394     for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
395     llChannelCountListeners.GetListener(i)->ChannelCountChanged(NewCount);
396     }
397     }
398    
399 schoenebeck 1686 void Sampler::fireChannelAdded(SamplerChannel* pChannel) {
400     for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
401     llChannelCountListeners.GetListener(i)->ChannelAdded(pChannel);
402     }
403     }
404    
405     void Sampler::fireChannelToBeRemoved(SamplerChannel* pChannel) {
406     for (int i = 0; i < llChannelCountListeners.GetListenerCount(); i++) {
407     llChannelCountListeners.GetListener(i)->ChannelToBeRemoved(pChannel);
408     }
409     }
410    
411 iliev 1130 void Sampler::AddAudioDeviceCountListener(AudioDeviceCountListener* l) {
412     llAudioDeviceCountListeners.AddListener(l);
413     }
414    
415     void Sampler::RemoveAudioDeviceCountListener(AudioDeviceCountListener* l) {
416     llAudioDeviceCountListeners.RemoveListener(l);
417     }
418    
419     void Sampler::fireAudioDeviceCountChanged(int NewCount) {
420     for (int i = 0; i < llAudioDeviceCountListeners.GetListenerCount(); i++) {
421     llAudioDeviceCountListeners.GetListener(i)->AudioDeviceCountChanged(NewCount);
422     }
423     }
424    
425     void Sampler::AddMidiDeviceCountListener(MidiDeviceCountListener* l) {
426     llMidiDeviceCountListeners.AddListener(l);
427     }
428    
429     void Sampler::RemoveMidiDeviceCountListener(MidiDeviceCountListener* l) {
430     llMidiDeviceCountListeners.RemoveListener(l);
431     }
432    
433     void Sampler::fireMidiDeviceCountChanged(int NewCount) {
434     for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
435     llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCountChanged(NewCount);
436     }
437     }
438    
439 schoenebeck 1695 void Sampler::fireMidiDeviceToBeDestroyed(MidiInputDevice* pDevice) {
440     for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
441     llMidiDeviceCountListeners.GetListener(i)->MidiDeviceToBeDestroyed(pDevice);
442     }
443     }
444    
445     void Sampler::fireMidiDeviceCreated(MidiInputDevice* pDevice) {
446     for (int i = 0; i < llMidiDeviceCountListeners.GetListenerCount(); i++) {
447     llMidiDeviceCountListeners.GetListener(i)->MidiDeviceCreated(pDevice);
448     }
449     }
450    
451 iliev 1130 void Sampler::AddVoiceCountListener(VoiceCountListener* l) {
452     llVoiceCountListeners.AddListener(l);
453     }
454    
455     void Sampler::RemoveVoiceCountListener(VoiceCountListener* l) {
456     llVoiceCountListeners.RemoveListener(l);
457     }
458    
459     void Sampler::fireVoiceCountChanged(int ChannelId, int NewCount) {
460 iliev 1789 std::map<uint, uint>::iterator it = mOldVoiceCounts.find(ChannelId);
461     if (it != mOldVoiceCounts.end()) {
462     uint oldCount = it->second;
463     if (NewCount == oldCount) return;
464     }
465    
466     mOldVoiceCounts[ChannelId] = NewCount;
467    
468 iliev 1130 for (int i = 0; i < llVoiceCountListeners.GetListenerCount(); i++) {
469     llVoiceCountListeners.GetListener(i)->VoiceCountChanged(ChannelId, NewCount);
470     }
471     }
472    
473     void Sampler::AddStreamCountListener(StreamCountListener* l) {
474     llStreamCountListeners.AddListener(l);
475     }
476    
477     void Sampler::RemoveStreamCountListener(StreamCountListener* l) {
478     llStreamCountListeners.RemoveListener(l);
479     }
480    
481     void Sampler::fireStreamCountChanged(int ChannelId, int NewCount) {
482 iliev 1789 std::map<uint, uint>::iterator it = mOldStreamCounts.find(ChannelId);
483     if (it != mOldStreamCounts.end()) {
484     uint oldCount = it->second;
485     if (NewCount == oldCount) return;
486     }
487    
488     mOldStreamCounts[ChannelId] = NewCount;
489    
490 iliev 1130 for (int i = 0; i < llStreamCountListeners.GetListenerCount(); i++) {
491     llStreamCountListeners.GetListener(i)->StreamCountChanged(ChannelId, NewCount);
492     }
493     }
494    
495     void Sampler::AddBufferFillListener(BufferFillListener* l) {
496     llBufferFillListeners.AddListener(l);
497     }
498    
499     void Sampler::RemoveBufferFillListener(BufferFillListener* l) {
500     llBufferFillListeners.RemoveListener(l);
501     }
502    
503     void Sampler::fireBufferFillChanged(int ChannelId, String FillData) {
504     for (int i = 0; i < llBufferFillListeners.GetListenerCount(); i++) {
505     llBufferFillListeners.GetListener(i)->BufferFillChanged(ChannelId, FillData);
506     }
507     }
508    
509 iliev 1541 void Sampler::AddTotalStreamCountListener(TotalStreamCountListener* l) {
510     llTotalStreamCountListeners.AddListener(l);
511     }
512    
513     void Sampler::RemoveTotalStreamCountListener(TotalStreamCountListener* l) {
514     llTotalStreamCountListeners.RemoveListener(l);
515     }
516    
517     void Sampler::fireTotalStreamCountChanged(int NewCount) {
518 iliev 1789 if (NewCount == uiOldTotalStreamCount) return;
519     uiOldTotalStreamCount = NewCount;
520    
521 iliev 1541 for (int i = 0; i < llTotalStreamCountListeners.GetListenerCount(); i++) {
522     llTotalStreamCountListeners.GetListener(i)->TotalStreamCountChanged(NewCount);
523     }
524     }
525    
526 iliev 1130 void Sampler::AddTotalVoiceCountListener(TotalVoiceCountListener* l) {
527     llTotalVoiceCountListeners.AddListener(l);
528     }
529    
530     void Sampler::RemoveTotalVoiceCountListener(TotalVoiceCountListener* l) {
531     llTotalVoiceCountListeners.RemoveListener(l);
532     }
533    
534     void Sampler::fireTotalVoiceCountChanged(int NewCount) {
535 iliev 1789 if (NewCount == uiOldTotalVoiceCount) return;
536     uiOldTotalVoiceCount = NewCount;
537    
538 iliev 1130 for (int i = 0; i < llTotalVoiceCountListeners.GetListenerCount(); i++) {
539     llTotalVoiceCountListeners.GetListener(i)->TotalVoiceCountChanged(NewCount);
540     }
541     }
542    
543     void Sampler::AddFxSendCountListener(FxSendCountListener* l) {
544     llFxSendCountListeners.AddListener(l);
545     }
546    
547     void Sampler::RemoveFxSendCountListener(FxSendCountListener* l) {
548     llFxSendCountListeners.RemoveListener(l);
549     }
550    
551     void Sampler::fireFxSendCountChanged(int ChannelId, int NewCount) {
552     for (int i = 0; i < llFxSendCountListeners.GetListenerCount(); i++) {
553     llFxSendCountListeners.GetListener(i)->FxSendCountChanged(ChannelId, NewCount);
554     }
555     }
556    
557 schoenebeck 1686 void Sampler::EventHandler::EngineToBeChanged(int ChannelId) {
558     // nothing to do here
559     }
560    
561 iliev 1130 void Sampler::EventHandler::EngineChanged(int ChannelId) {
562     EngineChannel* engineChannel = pSampler->GetSamplerChannel(ChannelId)->GetEngineChannel();
563     if(engineChannel == NULL) return;
564     engineChannel->AddFxSendCountListener(this);
565     }
566    
567     void Sampler::EventHandler::FxSendCountChanged(int ChannelId, int NewCount) {
568     pSampler->fireFxSendCountChanged(ChannelId, NewCount);
569     }
570    
571    
572 schoenebeck 53 SamplerChannel* Sampler::AddSamplerChannel() {
573 schoenebeck 209 // if there's no sampler channel yet
574     if (!mSamplerChannels.size()) {
575     SamplerChannel* pChannel = new SamplerChannel(this);
576     mSamplerChannels[0] = pChannel;
577 schoenebeck 1686 fireChannelAdded(pChannel);
578 iliev 1130 fireChannelCountChanged(1);
579     pChannel->AddEngineChangeListener(&eventHandler);
580 schoenebeck 209 return pChannel;
581     }
582    
583     // get the highest used sampler channel index
584     uint lastIndex = (--(mSamplerChannels.end()))->first;
585    
586     // check if we reached the index limit
587     if (lastIndex + 1 < lastIndex) {
588     // search for an unoccupied sampler channel index starting from 0
589     for (uint i = 0; i < lastIndex; i++) {
590     if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue;
591     // we found an unused index, so insert the new channel there
592     SamplerChannel* pChannel = new SamplerChannel(this);
593     mSamplerChannels[i] = pChannel;
594 schoenebeck 1686 fireChannelAdded(pChannel);
595 iliev 1130 fireChannelCountChanged(SamplerChannels());
596     pChannel->AddEngineChangeListener(&eventHandler);
597 schoenebeck 209 return pChannel;
598     }
599 schoenebeck 880 throw Exception("Internal error: could not find unoccupied sampler channel index.");
600 schoenebeck 209 }
601    
602     // we have not reached the index limit so we just add the channel past the highest index
603 schoenebeck 53 SamplerChannel* pChannel = new SamplerChannel(this);
604 schoenebeck 209 mSamplerChannels[lastIndex + 1] = pChannel;
605 schoenebeck 1686 fireChannelAdded(pChannel);
606 iliev 1130 fireChannelCountChanged(SamplerChannels());
607     pChannel->AddEngineChangeListener(&eventHandler);
608 schoenebeck 53 return pChannel;
609     }
610    
611     SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
612 schoenebeck 209 return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
613 schoenebeck 53 }
614    
615 schoenebeck 209 std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
616     return mSamplerChannels;
617     }
618    
619 schoenebeck 53 void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
620 schoenebeck 209 SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
621     for (; iterChan != mSamplerChannels.end(); iterChan++) {
622     if (iterChan->second == pSamplerChannel) {
623 schoenebeck 1686 fireChannelToBeRemoved(pSamplerChannel);
624 iliev 1789 mOldVoiceCounts.erase(pSamplerChannel->Index());
625     mOldStreamCounts.erase(pSamplerChannel->Index());
626 iliev 1130 pSamplerChannel->RemoveAllEngineChangeListeners();
627 schoenebeck 209 mSamplerChannels.erase(iterChan);
628 schoenebeck 53 delete pSamplerChannel;
629 iliev 1130 fireChannelCountChanged(SamplerChannels());
630 schoenebeck 53 return;
631     }
632     }
633     }
634    
635     void Sampler::RemoveSamplerChannel(uint uiSamplerChannel) {
636     SamplerChannel* pChannel = GetSamplerChannel(uiSamplerChannel);
637     if (!pChannel) return;
638     RemoveSamplerChannel(pChannel);
639     }
640    
641 iliev 1835 void Sampler::RemoveAllSamplerChannels() {
642     /*
643     * In maps iterator invalidation occurs when the iterator point
644     * to the element that is being erased. So we need to copy the map
645     * by calling GetSamplerChannels() to prevent that.
646     */
647     SamplerChannelMap chns = GetSamplerChannels();
648     SamplerChannelMap::iterator iter = chns.begin();
649     for(; iter != chns.end(); iter++) {
650     RemoveSamplerChannel(iter->second);
651     }
652     }
653    
654 schoenebeck 123 std::vector<String> Sampler::AvailableAudioOutputDrivers() {
655     return AudioOutputDeviceFactory::AvailableDrivers();
656     }
657 schoenebeck 53
658 schoenebeck 900 std::vector<String> Sampler::AvailableMidiInputDrivers() {
659     return MidiInputDeviceFactory::AvailableDrivers();
660     }
661    
662     std::vector<String> Sampler::AvailableEngineTypes() {
663     return EngineFactory::AvailableEngineTypes();
664     }
665    
666 schoenebeck 880 AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) {
667 schoenebeck 53 // create new device
668 schoenebeck 123 AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
669 schoenebeck 53
670 iliev 1130 fireAudioDeviceCountChanged(AudioOutputDevices());
671 schoenebeck 53 return pDevice;
672     }
673    
674 schoenebeck 123 uint Sampler::AudioOutputDevices() {
675 schoenebeck 3054 return (uint) AudioOutputDeviceFactory::Devices().size();
676 schoenebeck 53 }
677    
678 senkov 155 uint Sampler::MidiInputDevices() {
679 schoenebeck 3054 return (uint) MidiInputDeviceFactory::Devices().size();
680 senkov 155 }
681    
682 schoenebeck 123 std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
683 schoenebeck 1934 return AudioOutputDeviceFactory::Devices();
684 schoenebeck 123 }
685    
686 senkov 155 std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
687 schoenebeck 1934 return MidiInputDeviceFactory::Devices();
688 senkov 155 }
689    
690 schoenebeck 880 void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
691 schoenebeck 1934 if (pDevice) {
692     // check if there are still sampler engines connected to this device
693     for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
694     iterChan != mSamplerChannels.end(); iterChan++
695     ) if (iterChan->second->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the audio output device.");
696 schoenebeck 123
697 schoenebeck 1934 //TODO: should we add fireAudioDeviceToBeDestroyed() here ?
698     AudioOutputDeviceFactory::Destroy(pDevice);
699     fireAudioDeviceCountChanged(AudioOutputDevices());
700 schoenebeck 123 }
701     }
702    
703 iliev 1835 void Sampler::DestroyAllAudioOutputDevices() throw (Exception) {
704     /*
705     * In maps iterator invalidation occurs when the iterator point
706     * to the element that is being erased. So we need to copy the map
707     * by calling GetAudioOutputDevices() to prevent that.
708     */
709 schoenebeck 1934 std::map<uint, AudioOutputDevice*> devs = GetAudioOutputDevices();
710     std::map<uint, AudioOutputDevice*>::iterator iter = devs.begin();
711     for (; iter != devs.end(); iter++) {
712     AudioOutputDevice* pDevice = iter->second;
713    
714     // skip non-autonomous devices
715     if (!pDevice->isAutonomousDevice()) continue;
716    
717     DestroyAudioOutputDevice(pDevice);
718 iliev 1835 }
719     }
720    
721 schoenebeck 880 void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
722 schoenebeck 1934 if (pDevice) {
723     // check if there are still sampler engines connected to this device
724     for (SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
725 schoenebeck 2500 iterChan != mSamplerChannels.end(); ++iterChan)
726     {
727     std::vector<MidiInputPort*> vPorts = iterChan->second->GetMidiInputPorts();
728     for (int k = 0; k < vPorts.size(); ++k)
729     if (vPorts[k]->GetDevice() == pDevice)
730     throw Exception("Sampler channel " + ToString(iterChan->first) + " is still connected to the midi input device.");
731     }
732 schoenebeck 53
733 schoenebeck 1934 fireMidiDeviceToBeDestroyed(pDevice);
734     MidiInputDeviceFactory::Destroy(pDevice);
735     fireMidiDeviceCountChanged(MidiInputDevices());
736 schoenebeck 53 }
737 senkov 155 }
738 schoenebeck 53
739 iliev 1835 void Sampler::DestroyAllMidiInputDevices() throw (Exception) {
740     /*
741     * In maps iterator invalidation occurs when the iterator point
742     * to the element that is being erased. So we need to copy the map
743     * by calling GetMidiInputDevices() to prevent that.
744     */
745 schoenebeck 1934 std::map<uint, MidiInputDevice*> devs = GetMidiInputDevices();
746     std::map<uint, MidiInputDevice*>::iterator iter = devs.begin();
747     for (; iter != devs.end(); iter++) {
748     MidiInputDevice* pDevice = iter->second;
749    
750     // skip non-autonomous devices
751     if (!pDevice->isAutonomousDevice()) continue;
752    
753     DestroyMidiInputDevice(pDevice);
754 iliev 1835 }
755     }
756    
757 schoenebeck 880 MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) {
758 senkov 155 // create new device
759 schoenebeck 551 MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
760 senkov 155
761 schoenebeck 1695 fireMidiDeviceCreated(pDevice);
762 iliev 1130 fireMidiDeviceCountChanged(MidiInputDevices());
763 schoenebeck 53 return pDevice;
764     }
765    
766 iliev 1541 int Sampler::GetDiskStreamCount() {
767     int count = 0;
768     std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
769    
770     for(; it != EngineFactory::EngineInstances().end(); it++) {
771     count += (*it)->DiskStreamCount();
772     }
773    
774     return count;
775     }
776    
777 iliev 778 int Sampler::GetVoiceCount() {
778     int count = 0;
779     std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
780    
781     for(; it != EngineFactory::EngineInstances().end(); it++) {
782     count += (*it)->VoiceCount();
783     }
784    
785     return count;
786     }
787    
788 schoenebeck 2375 int Sampler::GetGlobalMaxVoices() {
789     return GLOBAL_MAX_VOICES; // see common/global_private.cpp
790     }
791    
792     int Sampler::GetGlobalMaxStreams() {
793     return GLOBAL_MAX_STREAMS; // see common/global_private.cpp
794     }
795    
796     void Sampler::SetGlobalMaxVoices(int n) throw (Exception) {
797     if (n < 1) throw Exception("Maximum voices may not be less than 1");
798     GLOBAL_MAX_VOICES = n; // see common/global_private.cpp
799     const std::set<Engine*>& engines = EngineFactory::EngineInstances();
800     if (engines.size() > 0) {
801     std::set<Engine*>::iterator iter = engines.begin();
802     std::set<Engine*>::iterator end = engines.end();
803     for (; iter != end; ++iter) {
804     (*iter)->SetMaxVoices(n);
805     }
806     }
807     }
808    
809     void Sampler::SetGlobalMaxStreams(int n) throw (Exception) {
810     if (n < 0) throw Exception("Maximum disk streams may not be negative");
811     GLOBAL_MAX_STREAMS = n; // see common/global_private.cpp
812     const std::set<Engine*>& engines = EngineFactory::EngineInstances();
813     if (engines.size() > 0) {
814     std::set<Engine*>::iterator iter = engines.begin();
815     std::set<Engine*>::iterator end = engines.end();
816     for (; iter != end; ++iter) {
817     (*iter)->SetMaxDiskStreams(n);
818     }
819     }
820     }
821    
822 schoenebeck 212 void Sampler::Reset() {
823     // delete sampler channels
824     try {
825 schoenebeck 1934 RemoveAllSamplerChannels();
826 schoenebeck 212 }
827     catch(...) {
828     std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
829     exit(EXIT_FAILURE);
830     }
831    
832     // delete midi input devices
833     try {
834 schoenebeck 1934 DestroyAllMidiInputDevices();
835 schoenebeck 212 }
836     catch(...) {
837     std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
838     exit(EXIT_FAILURE);
839     }
840    
841     // delete audio output devices
842     try {
843 schoenebeck 1934 DestroyAllAudioOutputDevices();
844 schoenebeck 212 }
845     catch(...) {
846     std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
847     exit(EXIT_FAILURE);
848     }
849 schoenebeck 1008
850     // delete MIDI instrument maps
851     try {
852     MidiInstrumentMapper::RemoveAllMaps();
853     }
854     catch(...) {
855     std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI instrument maps, exiting.\n" << std::flush;
856     exit(EXIT_FAILURE);
857     }
858 schoenebeck 1212
859     // unload all instrument editor DLLs
860 schoenebeck 4021 /* FIXME: disabled for now as it caused an LSCP "RESET" command to unload the
861     * editor DLLs and (probably due to a race) prevented them from reloading
862     * subsequently (see comments in ClosePlugins())
863     * https://sourceforge.net/p/linuxsampler/mailman/message/37411956/
864 schoenebeck 1212 InstrumentEditorFactory::ClosePlugins();
865 schoenebeck 4021 */
866 schoenebeck 212 }
867    
868 schoenebeck 1723 bool Sampler::EnableDenormalsAreZeroMode() {
869     Features::detect();
870     return Features::enableDenormalsAreZeroMode();
871     }
872    
873 persson 1765 void Sampler::fireStatistics() {
874     static const LSCPEvent::event_t eventsArr[] = {
875     LSCPEvent::event_voice_count, LSCPEvent::event_stream_count,
876     LSCPEvent::event_buffer_fill, LSCPEvent::event_total_voice_count
877     };
878     static const std::list<LSCPEvent::event_t> events(eventsArr, eventsArr + 4);
879    
880     if (LSCPServer::EventSubscribers(events))
881     {
882 persson 2427 LockGuard lock(LSCPServer::RTNotifyMutex);
883 persson 1765 std::map<uint,SamplerChannel*> channels = GetSamplerChannels();
884     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
885     for (; iter != channels.end(); iter++) {
886     SamplerChannel* pSamplerChannel = iter->second;
887     EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
888     if (!pEngineChannel) continue;
889     Engine* pEngine = pEngineChannel->GetEngine();
890     if (!pEngine) continue;
891     fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount());
892     fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());
893     fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());
894     }
895 schoenebeck 1934
896 iliev 1789 fireTotalStreamCountChanged(GetDiskStreamCount());
897     fireTotalVoiceCountChanged(GetVoiceCount());
898 persson 1765 }
899     }
900    
901 persson 1897 #if defined(WIN32)
902     static HINSTANCE dllInstance = NULL;
903    
904     String Sampler::GetInstallDir() {
905     char buf[MAX_PATH + 1];
906     if (GetModuleFileName(dllInstance, buf, MAX_PATH)) {
907     String s(buf);
908     size_t n = s.rfind('\\');
909     if (n != String::npos) {
910     return s.substr(0, n);
911     }
912     }
913     return "";
914     }
915     #endif
916 schoenebeck 53 } // namespace LinuxSampler
917 persson 1897
918     #if defined(WIN32)
919     extern "C" {
920     BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
921     {
922     switch (reason) {
923     case DLL_PROCESS_ATTACH:
924     LinuxSampler::dllInstance = instance;
925     break;
926     }
927     return TRUE;
928     }
929     }
930     #endif

  ViewVC Help
Powered by ViewVC