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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 730 - (hide annotations) (download)
Sat Jul 30 08:18:08 2005 UTC (18 years, 8 months ago) by iliev
File size: 15777 byte(s)
* Added CHANNEL_INFO notification when engine is assigned to
sampler channel with no midi and audio device set (Fixes bug #22)

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

  ViewVC Help
Powered by ViewVC