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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 778 - (hide 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 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 411 * Copyright (C) 2005 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 schoenebeck 675 MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : __GetMidiInputDevicePort(GetMidiInputChannel());
52 schoenebeck 411 if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
53 schoenebeck 420 if (pEngineChannel) {
54     if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
55 schoenebeck 660 EngineChannelFactory::Destroy(pEngineChannel);
56 schoenebeck 420 }
57 schoenebeck 53 }
58     }
59    
60 schoenebeck 411 void SamplerChannel::SetEngineType(String EngineType) throw (LinuxSamplerException) {
61     dmsg(2,("SamplerChannel: Assigning engine type..."));
62 schoenebeck 53
63 schoenebeck 411 // create new engine channel
64     EngineChannel* pNewEngineChannel = EngineChannelFactory::Create(EngineType);
65     if (!pNewEngineChannel) throw LinuxSamplerException("Unknown engine type");
66 schoenebeck 53
67 schoenebeck 660 //FIXME: hack to allow fast retrieval of engine channel's sampler channel index
68     pNewEngineChannel->iSamplerChannelIndex = Index();
69    
70 capela 159 // dereference midi input port.
71 schoenebeck 675 MidiInputPort* pMidiInputPort = __GetMidiInputDevicePort(GetMidiInputPort());
72     // disconnect old engine channel
73 schoenebeck 411 if (pEngineChannel) {
74     if (pMidiInputPort) pMidiInputPort->Disconnect(pEngineChannel);
75 schoenebeck 412 if (pAudioOutputDevice) pEngineChannel->DisconnectAudioOutputDevice();
76 schoenebeck 660 EngineChannelFactory::Destroy(pEngineChannel);
77 schoenebeck 53 }
78    
79 schoenebeck 411 // connect new engine channel
80 schoenebeck 675 if (pMidiInputPort) pMidiInputPort->Connect(pNewEngineChannel, GetMidiInputChannel());
81 schoenebeck 412 if (pAudioOutputDevice) {
82     pNewEngineChannel->Connect(pAudioOutputDevice);
83     pAudioOutputDevice->Connect(pNewEngineChannel->GetEngine());
84     }
85 schoenebeck 675 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 iliev 730 pEngineChannel->StatusChanged(true);
92    
93 schoenebeck 64 dmsg(2,("OK\n"));
94 schoenebeck 53 }
95    
96 schoenebeck 123 void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
97 schoenebeck 53 // disconnect old device
98 schoenebeck 412 if (pAudioOutputDevice && pEngineChannel) pEngineChannel->DisconnectAudioOutputDevice();
99 schoenebeck 53 // connect new device
100     pAudioOutputDevice = pDevice;
101 schoenebeck 412 if (pEngineChannel) {
102     pEngineChannel->Connect(pAudioOutputDevice);
103     pAudioOutputDevice->Connect(pEngineChannel->GetEngine());
104     }
105 schoenebeck 53 }
106    
107 capela 159 void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
108 schoenebeck 675 SetMidiInput(pDevice, 0, GetMidiInputChannel());
109 capela 159 }
110 schoenebeck 203
111 capela 159 void SamplerChannel::SetMidiInputPort(int MidiPort) {
112 schoenebeck 675 SetMidiInput(GetMidiInputDevice(), MidiPort, GetMidiInputChannel());
113 capela 159 }
114 schoenebeck 203
115 schoenebeck 675 void SamplerChannel::SetMidiInputChannel(midi_chan_t MidiChannel) {
116     SetMidiInput(GetMidiInputDevice(), GetMidiInputPort(), MidiChannel);
117 capela 159 }
118 schoenebeck 203
119 schoenebeck 675 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 capela 159 // disconnect old device port
127 schoenebeck 675 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 capela 159 // connect new device port
136 schoenebeck 675 if (pNewMidiInputPort && pEngineChannel) pNewMidiInputPort->Connect(pEngineChannel, MidiChannel);
137 capela 159 // Ooops.
138 schoenebeck 675 if (pNewMidiInputPort == NULL)
139 schoenebeck 221 throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
140 schoenebeck 53 }
141    
142 schoenebeck 411 EngineChannel* SamplerChannel::GetEngineChannel() {
143     return pEngineChannel;
144 schoenebeck 53 }
145    
146 schoenebeck 675 midi_chan_t SamplerChannel::GetMidiInputChannel() {
147     if (pEngineChannel) this->midiChannel = pEngineChannel->MidiChannel();
148 capela 159 return this->midiChannel;
149 schoenebeck 53 }
150    
151 capela 159 int SamplerChannel::GetMidiInputPort() {
152 schoenebeck 675 MidiInputPort* pMidiInputPort = (pEngineChannel) ? pEngineChannel->GetMidiInputPort() : NULL;
153     if (pMidiInputPort) this->iMidiPort = (int) pMidiInputPort->GetPortNumber();
154     return iMidiPort;
155 capela 159 }
156    
157 schoenebeck 53 AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
158     return pAudioOutputDevice;
159     }
160    
161 senkov 155 MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
162 schoenebeck 675 if (pEngineChannel)
163     pMidiInputDevice = (pEngineChannel->GetMidiInputPort()) ? pEngineChannel->GetMidiInputPort()->GetDevice() : NULL;
164 capela 159 return pMidiInputDevice;
165 senkov 155 }
166    
167 schoenebeck 53 uint SamplerChannel::Index() {
168     if (iIndex >= 0) return iIndex;
169    
170 schoenebeck 209 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 schoenebeck 53 }
176     }
177    
178 schoenebeck 209 throw LinuxSamplerException("Internal error: SamplerChannel index not found");
179 schoenebeck 53 }
180    
181 schoenebeck 675 MidiInputPort* SamplerChannel::__GetMidiInputDevicePort(int iMidiPort) {
182 schoenebeck 221 MidiInputPort* pMidiInputPort = NULL;
183 schoenebeck 675 MidiInputDevice* pMidiInputDevice = GetMidiInputDevice();
184 capela 159 if (pMidiInputDevice)
185 schoenebeck 221 pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
186 capela 159 return pMidiInputPort;
187     }
188 schoenebeck 53
189 schoenebeck 212
190    
191 schoenebeck 53 // ******************************************************************
192     // * Sampler
193    
194     Sampler::Sampler() {
195     }
196    
197     Sampler::~Sampler() {
198 schoenebeck 212 Reset();
199 schoenebeck 53 }
200    
201     uint Sampler::SamplerChannels() {
202 schoenebeck 209 return mSamplerChannels.size();
203 schoenebeck 53 }
204    
205     SamplerChannel* Sampler::AddSamplerChannel() {
206 schoenebeck 209 // if there's no sampler channel yet
207     if (!mSamplerChannels.size()) {
208     SamplerChannel* pChannel = new SamplerChannel(this);
209     mSamplerChannels[0] = pChannel;
210 schoenebeck 556 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, 1));
211 schoenebeck 209 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 schoenebeck 556 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, i));
226 schoenebeck 209 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 schoenebeck 53 SamplerChannel* pChannel = new SamplerChannel(this);
233 schoenebeck 209 mSamplerChannels[lastIndex + 1] = pChannel;
234 schoenebeck 556 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, lastIndex + 1));
235 schoenebeck 53 return pChannel;
236     }
237    
238     SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
239 schoenebeck 209 return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
240 schoenebeck 53 }
241    
242 schoenebeck 209 std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
243     return mSamplerChannels;
244     }
245    
246 schoenebeck 53 void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
247 schoenebeck 209 SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
248     for (; iterChan != mSamplerChannels.end(); iterChan++) {
249     if (iterChan->second == pSamplerChannel) {
250     mSamplerChannels.erase(iterChan);
251 schoenebeck 53 delete pSamplerChannel;
252 schoenebeck 556 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channel_count, mSamplerChannels.size()));
253 schoenebeck 53 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 schoenebeck 123 std::vector<String> Sampler::AvailableAudioOutputDrivers() {
265     return AudioOutputDeviceFactory::AvailableDrivers();
266     }
267 schoenebeck 53
268 schoenebeck 123 AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
269 schoenebeck 53 // create new device
270 schoenebeck 123 AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
271 schoenebeck 53
272 schoenebeck 64 // add new audio device to the audio device list
273 schoenebeck 123 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 schoenebeck 64
280 schoenebeck 53 return pDevice;
281     }
282    
283 schoenebeck 123 uint Sampler::AudioOutputDevices() {
284     return mAudioOutputDevices.size();
285 schoenebeck 53 }
286    
287 senkov 155 uint Sampler::MidiInputDevices() {
288     return mMidiInputDevices.size();
289     }
290    
291 schoenebeck 123 std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
292     return mAudioOutputDevices;
293     }
294    
295 senkov 155 std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
296     return mMidiInputDevices;
297     }
298    
299 schoenebeck 123 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 senkov 155 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 schoenebeck 53
327 senkov 155 // 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 schoenebeck 53 }
337 senkov 155 }
338 schoenebeck 53
339 senkov 155 MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
340     // create new device
341 schoenebeck 551 MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters, this);
342 senkov 155
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 schoenebeck 64
351 schoenebeck 53 return pDevice;
352     }
353    
354 iliev 778 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 schoenebeck 212 void Sampler::Reset() {
366     // delete sampler channels
367     try {
368 senkov 329 while (true) {
369     SamplerChannelMap::iterator iter = mSamplerChannels.begin();
370     if (iter == mSamplerChannels.end()) break;
371     RemoveSamplerChannel(iter->second);
372 schoenebeck 212 }
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 senkov 329 while (true) {
382     MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
383     if (iter == mMidiInputDevices.end()) break;
384     DestroyMidiInputDevice(iter->second);
385 schoenebeck 212 }
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 senkov 329 while (true) {
395     AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
396     if (iter == mAudioOutputDevices.end()) break;
397     DestroyAudioOutputDevice(iter->second);
398 schoenebeck 212 }
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 schoenebeck 53 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC