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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 778 - (show annotations) (download)
Fri Sep 23 06:58:26 2005 UTC (18 years, 6 months ago) by iliev
File size: 16104 byte(s)
Added new LSCP commands: GET TOTAL_VOICE_COUNT,
GET TOTAL_VOICE_COUNT_MAX, SUBSCRIBE/UNSUBSCRIBE TOTAL_VOICE_COUNT

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 }
317 }
318
319 void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) {
320 MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
321 for (; iter != mMidiInputDevices.end(); iter++) {
322 if (iter->second == pDevice) {
323 // check if there are still sampler engines connected to this device
324 for (uint i = 0; i < SamplerChannels(); i++)
325 if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
326
327 // disable device
328 pDevice->StopListen();
329
330 // remove device from the device list
331 mMidiInputDevices.erase(iter);
332
333 // destroy and free device from memory
334 delete pDevice;
335 }
336 }
337 }
338
339 MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
340 // create new device
341 MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
342
343 // add new device to the midi device list
344 for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
345 if (!mMidiInputDevices[i]) {
346 mMidiInputDevices[i] = pDevice;
347 break;
348 }
349 }
350
351 return pDevice;
352 }
353
354 int Sampler::GetVoiceCount() {
355 int count = 0;
356 std::set<Engine*>::iterator it = EngineFactory::EngineInstances().begin();
357
358 for(; it != EngineFactory::EngineInstances().end(); it++) {
359 count += (*it)->VoiceCount();
360 }
361
362 return count;
363 }
364
365 void Sampler::Reset() {
366 // delete sampler channels
367 try {
368 while (true) {
369 SamplerChannelMap::iterator iter = mSamplerChannels.begin();
370 if (iter == mSamplerChannels.end()) break;
371 RemoveSamplerChannel(iter->second);
372 }
373 }
374 catch(...) {
375 std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
376 exit(EXIT_FAILURE);
377 }
378
379 // delete midi input devices
380 try {
381 while (true) {
382 MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
383 if (iter == mMidiInputDevices.end()) break;
384 DestroyMidiInputDevice(iter->second);
385 }
386 }
387 catch(...) {
388 std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
389 exit(EXIT_FAILURE);
390 }
391
392 // delete audio output devices
393 try {
394 while (true) {
395 AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
396 if (iter == mAudioOutputDevices.end()) break;
397 DestroyAudioOutputDevice(iter->second);
398 }
399 }
400 catch(...) {
401 std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
402 exit(EXIT_FAILURE);
403 }
404 }
405
406 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC