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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 900 - (hide annotations) (download)
Wed Jul 5 17:53:22 2006 UTC (17 years, 9 months ago) by schoenebeck
File size: 17349 byte(s)
* src/Sampler.h, src/Sampler.cpp: allow 3rd party applications to
  retrieve available drivers and sampler engines
* src/linuxsampler.cpp: show available engines on startup (on the console)

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 880 * Copyright (C) 2005, 2006 Christian Schoenebeck *
7 schoenebeck 53 * *
8     * This program is free software; you can redistribute it and/or modify *
9     * 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     * This program is distributed in the hope that it will be useful, *
14     * 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     * along with this program; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24 schoenebeck 123 #include <sstream>
25    
26 schoenebeck 53 #include "Sampler.h"
27    
28 iliev 778 #include "engines/EngineFactory.h"
29 schoenebeck 411 #include "engines/EngineChannelFactory.h"
30 schoenebeck 203 #include "drivers/audio/AudioOutputDeviceFactory.h"
31     #include "drivers/midi/MidiInputDeviceFactory.h"
32 senkov 359 #include "network/lscpserver.h"
33 schoenebeck 53
34     namespace LinuxSampler {
35    
36     // ******************************************************************
37     // * SamplerChannel
38    
39     SamplerChannel::SamplerChannel(Sampler* pS) {
40     pSampler = pS;
41 schoenebeck 411 pEngineChannel = NULL;
42 schoenebeck 675 pAudioOutputDevice = NULL;
43 capela 159 pMidiInputDevice = NULL;
44 schoenebeck 675 iMidiPort = 0;
45     midiChannel = midi_chan_all;
46 schoenebeck 53 iIndex = -1;
47     }
48    
49     SamplerChannel::~SamplerChannel() {
50 schoenebeck 411 if (pEngineChannel) {
51 persson 840 Engine* engine = pEngineChannel->GetEngine();
52     if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
53    
54 schoenebeck 675 MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel());
55 schoenebeck 411 if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
56 schoenebeck 420 if (pEngineChannel) {
57     if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
58 schoenebeck 660 EngineChannelFactory::Destroy(pEngineChannel);
59 persson 840
60     // reconnect engine if it still exists
61     const std::set<Engine*>& engines = EngineFactory::EngineInstances();
62     if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
63 schoenebeck 420 }
64 schoenebeck 53 }
65     }
66    
67 schoenebeck 880 void SamplerChannel::SetEngineType(String EngineType) throw (Exception) {
68 schoenebeck 411 dmsg(2,("SamplerChannel: Assigning engine type..."));
69 schoenebeck 53
70 schoenebeck 411 // create new engine channel
71     EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
72 schoenebeck 880 if (!pNewEngineChannel) throw Exception("Unknown engine type");
73 schoenebeck 53
74 schoenebeck 660 //FIXME: hack to allow fast retrieval of engine channel's sampler channel index
75     pNewEngineChannel->iSamplerChannelIndex = Index();
76    
77 capela 159 // dereference midi input port.
78 schoenebeck 675 MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
79     // disconnect old engine channel
80 schoenebeck 411 if (pEngineChannel) {
81 persson 840 Engine* engine = pEngineChannel->GetEngine();
82     if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(engine);
83    
84 schoenebeck 411 if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
85 schoenebeck 412 if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
86 schoenebeck 660 EngineChannelFactory::Destroy(pEngineChannel);
87 persson 840
88     // reconnect engine if it still exists
89     const std::set<Engine*>& engines = EngineFactory::EngineInstances();
90     if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
91 schoenebeck 53 }
92    
93 schoenebeck 411 // connect new engine channel
94 schoenebeck 412 if (pAudioOutputDevice) {
95     pNewEngineChannel->Connect(pAudioOutputDevice);
96     pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
97     }
98 persson 846 if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());
99 schoenebeck 675 pEngineChannel = pNewEngineChannel;
100    
101     // from now on get MIDI device and port from EngineChannel object
102     this->pMidiInputDevice = NULL;
103     this->iMidiPort = 0;
104    
105 iliev 730 pEngineChannel->StatusChanged(true);
106    
107 schoenebeck 64 dmsg(2,("OK\n"));
108 schoenebeck 53 }
109    
110 schoenebeck 123 void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
111 schoenebeck 53 // disconnect old device
112 persson 840 if (pAudioOutputDevice && pEngineChannel) {
113     Engine* engine = pEngineChannel->GetEngine();
114     pAudioOutputDevice->Disconnect(engine);
115    
116     pEngineChannel->DisconnectAudioOutputDevice();
117    
118     // reconnect engine if it still exists
119     const std::set<Engine*>& engines = EngineFactory::EngineInstances();
120     if (engines.find(engine) != engines.end()) pAudioOutputDevice->Connect(engine);
121     }
122    
123 schoenebeck 53 // connect new device
124     pAudioOutputDevice = pDevice;
125 schoenebeck 412 if (pEngineChannel) {
126     pEngineChannel->Connect(pAudioOutputDevice);
127     pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
128     }
129 schoenebeck 53 }
130    
131 capela 159 void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
132 schoenebeck 675 SetMidiInput(pDevice, 0, GetMidiInputChannel());
133 capela 159 }
134 schoenebeck 203
135 capela 159 void SamplerChannel::SetMidiInputPort(int MidiPort) {
136 schoenebeck 675 SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
137 capela 159 }
138 schoenebeck 203
139 schoenebeck 675 void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
140     SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
141 capela 159 }
142 schoenebeck 203
143 schoenebeck 675 void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel) {
144 schoenebeck 880 if (!pDevice) throw Exception("No MIDI input device assigned.");
145 schoenebeck 675
146     // get old and new midi input port
147     MidiInputPort* pOldMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
148     MidiInputPort* pNewMidiInputPort = pDevice->GetPort(iMidiPort);
149    
150 capela 159 // disconnect old device port
151 schoenebeck 675 if (pOldMidiInputPort && pEngineChannel) pOldMidiInputPort->Disconnect(pEngineChannel);
152     // remember new device, port and channel if not engine channel yet created
153     if (!pEngineChannel) {
154     this->pMidiInputDevice = pDevice;
155     this->iMidiPort = iMidiPort;
156     this->midiChannel = MidiChannel;
157     }
158    
159 capela 159 // connect new device port
160 schoenebeck 675 if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);
161 capela 159 // Ooops.
162 schoenebeck 675 if (pNewMidiInputPort == NULL)
163 schoenebeck 880 throw Exception("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
164 schoenebeck 53 }
165    
166 schoenebeck 411 EngineChannel* SamplerChannel::GetEngineChannel() {
167     return pEngineChannel;
168 schoenebeck 53 }
169    
170 schoenebeck 675 midi_chan_t SamplerChannel::GetMidiInputChannel() {
171     if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
172 capela 159 return this->midiChannel;
173 schoenebeck 53 }
174    
175 capela 159 int SamplerChannel::GetMidiInputPort() {
176 schoenebeck 675 MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;
177     if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
178     return iMidiPort;
179 capela 159 }
180    
181 schoenebeck 53 AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
182     return pAudioOutputDevice;
183     }
184    
185 senkov 155 MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
186 schoenebeck 675 if (pEngineChannel)
187     pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;
188 capela 159 return pMidiInputDevice;
189 senkov 155 }
190    
191 schoenebeck 53 uint SamplerChannel::Index() {
192     if (iIndex >= 0) return iIndex;
193    
194 schoenebeck 209 Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
195     for (; iter != pSampler->mSamplerChannels.end(); iter++) {
196     if (iter->second == this) {
197     iIndex = iter->first;
198     return iIndex;
199 schoenebeck 53 }
200     }
201    
202 schoenebeck 880 throw Exception("Internal error: SamplerChannel index not found");
203 schoenebeck 53 }
204    
205 schoenebeck 675 MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {
206 schoenebeck 221 MidiInputPort* pMidiInputPort = NULL;
207 schoenebeck 675 MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
208 capela 159 if (pMidiInputDevice)
209 schoenebeck 221 pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
210 capela 159 return pMidiInputPort;
211     }
212 schoenebeck 53
213 schoenebeck 212
214    
215 schoenebeck 53 // ******************************************************************
216     // * Sampler
217    
218     Sampler::Sampler() {
219     }
220    
221     Sampler::~Sampler() {
222 schoenebeck 212 Reset();
223 schoenebeck 53 }
224    
225     uint Sampler::SamplerChannels() {
226 schoenebeck 209 return mSamplerChannels.size();
227 schoenebeck 53 }
228    
229     SamplerChannel* Sampler::AddSamplerChannel() {
230 schoenebeck 209 // if there's no sampler channel yet
231     if (!mSamplerChannels.size()) {
232     SamplerChannel* pChannel = new SamplerChannel(this);
233     mSamplerChannels[0] = pChannel;
234 schoenebeck 556 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1));
235 schoenebeck 209 return pChannel;
236     }
237    
238     // get the highest used sampler channel index
239     uint lastIndex = (--(mSamplerChannels.end()))->first;
240    
241     // check if we reached the index limit
242     if (lastIndex + 1 < lastIndex) {
243     // search for an unoccupied sampler channel index starting from 0
244     for (uint i = 0; i < lastIndex; i++) {
245     if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue;
246     // we found an unused index, so insert the new channel there
247     SamplerChannel* pChannel = new SamplerChannel(this);
248     mSamplerChannels[i] = pChannel;
249 schoenebeck 556 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i));
250 schoenebeck 209 return pChannel;
251     }
252 schoenebeck 880 throw Exception("Internal error: could not find unoccupied sampler channel index.");
253 schoenebeck 209 }
254    
255     // we have not reached the index limit so we just add the channel past the highest index
256 schoenebeck 53 SamplerChannel* pChannel = new SamplerChannel(this);
257 schoenebeck 209 mSamplerChannels[lastIndex + 1] = pChannel;
258 schoenebeck 556 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1));
259 schoenebeck 53 return pChannel;
260     }
261    
262     SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
263 schoenebeck 209 return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
264 schoenebeck 53 }
265    
266 schoenebeck 209 std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
267     return mSamplerChannels;
268     }
269    
270 schoenebeck 53 void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
271 schoenebeck 209 SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
272     for (; iterChan != mSamplerChannels.end(); iterChan++) {
273     if (iterChan->second == pSamplerChannel) {
274     mSamplerChannels.erase(iterChan);
275 schoenebeck 53 delete pSamplerChannel;
276 schoenebeck 556 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size()));
277 schoenebeck 53 return;
278     }
279     }
280     }
281    
282     void Sampler::RemoveSamplerChannel(uint uiSamplerChannel) {
283     SamplerChannel* pChannel = GetSamplerChannel(uiSamplerChannel);
284     if (!pChannel) return;
285     RemoveSamplerChannel(pChannel);
286     }
287    
288 schoenebeck 123 std::vector<String> Sampler::AvailableAudioOutputDrivers() {
289     return AudioOutputDeviceFactory::AvailableDrivers();
290     }
291 schoenebeck 53
292 schoenebeck 900 std::vector<String> Sampler::AvailableMidiInputDrivers() {
293     return MidiInputDeviceFactory::AvailableDrivers();
294     }
295    
296     std::vector<String> Sampler::AvailableEngineTypes() {
297     return EngineFactory::AvailableEngineTypes();
298     }
299    
300 schoenebeck 880 AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception) {
301 schoenebeck 53 // create new device
302 schoenebeck 123 AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
303 schoenebeck 53
304 schoenebeck 64 // add new audio device to the audio device list
305 schoenebeck 123 for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
306     if (!mAudioOutputDevices[i]) {
307     mAudioOutputDevices[i] = pDevice;
308     break;
309     }
310     }
311 schoenebeck 64
312 schoenebeck 53 return pDevice;
313     }
314    
315 schoenebeck 123 uint Sampler::AudioOutputDevices() {
316     return mAudioOutputDevices.size();
317 schoenebeck 53 }
318    
319 senkov 155 uint Sampler::MidiInputDevices() {
320     return mMidiInputDevices.size();
321     }
322    
323 schoenebeck 123 std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
324     return mAudioOutputDevices;
325     }
326    
327 senkov 155 std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
328     return mMidiInputDevices;
329     }
330    
331 schoenebeck 880 void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception) {
332 schoenebeck 123 AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
333     for (; iter != mAudioOutputDevices.end(); iter++) {
334     if (iter->second == pDevice) {
335     // check if there are still sampler engines connected to this device
336     for (uint i = 0; i < SamplerChannels(); i++)
337 schoenebeck 880 if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the audio output device.");
338 schoenebeck 123
339     // disable device
340     pDevice->Stop();
341    
342 persson 837 // remove device from the device list
343     mAudioOutputDevices.erase(iter);
344    
345 schoenebeck 123 // destroy and free device from memory
346     delete pDevice;
347 persson 837
348     break;
349 schoenebeck 123 }
350     }
351     }
352    
353 schoenebeck 880 void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception) {
354 senkov 155 MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
355     for (; iter != mMidiInputDevices.end(); iter++) {
356     if (iter->second == pDevice) {
357     // check if there are still sampler engines connected to this device
358     for (uint i = 0; i < SamplerChannels(); i++)
359 schoenebeck 880 if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw Exception("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
360 schoenebeck 53
361 senkov 155 // disable device
362     pDevice->StopListen();
363    
364 persson 837 // remove device from the device list
365     mMidiInputDevices.erase(iter);
366    
367 senkov 155 // destroy and free device from memory
368     delete pDevice;
369 persson 837
370     break;
371 senkov 155 }
372 schoenebeck 53 }
373 senkov 155 }
374 schoenebeck 53
375 schoenebeck 880 MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception) {
376 senkov 155 // create new device
377 schoenebeck 551 MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
378 senkov 155
379     // add new device to the midi device list
380     for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
381     if (!mMidiInputDevices[i]) {
382     mMidiInputDevices[i] = pDevice;
383     break;
384     }
385     }
386 schoenebeck 64
387 schoenebeck 53 return pDevice;
388     }
389    
390 iliev 778 int Sampler::GetVoiceCount() {
391     int count = 0;
392     std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
393    
394     for(; it != EngineFactory::EngineInstances().end(); it++) {
395     count += (*it)->VoiceCount();
396     }
397    
398     return count;
399     }
400    
401 schoenebeck 212 void Sampler::Reset() {
402     // delete sampler channels
403     try {
404 senkov 329 while (true) {
405     SamplerChannelMap::iterator iter = mSamplerChannels.begin();
406     if (iter == mSamplerChannels.end()) break;
407     RemoveSamplerChannel(iter->second);
408 schoenebeck 212 }
409     }
410     catch(...) {
411     std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
412     exit(EXIT_FAILURE);
413     }
414    
415     // delete midi input devices
416     try {
417 senkov 329 while (true) {
418     MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
419     if (iter == mMidiInputDevices.end()) break;
420     DestroyMidiInputDevice(iter->second);
421 schoenebeck 212 }
422     }
423     catch(...) {
424     std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
425     exit(EXIT_FAILURE);
426     }
427    
428     // delete audio output devices
429     try {
430 senkov 329 while (true) {
431     AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
432     if (iter == mAudioOutputDevices.end()) break;
433     DestroyAudioOutputDevice(iter->second);
434 schoenebeck 212 }
435     }
436     catch(...) {
437     std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
438     exit(EXIT_FAILURE);
439     }
440     }
441    
442 schoenebeck 53 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC