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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 837 - (show annotations) (download)
Thu Feb 9 20:03:03 2006 UTC (18 years, 1 month ago) by persson
File size: 16152 byte(s)
* fix of silly bug introduced in recent memory fix: all devices were
  destroyed instead of just the one asked for in the Sampler::Destroy
  functions

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

  ViewVC Help
Powered by ViewVC