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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 412 - (hide annotations) (download)
Sat Feb 26 22:44:51 2005 UTC (19 years, 1 month ago) by schoenebeck
File size: 14683 byte(s)
* gig::Engine: fixed silence (engine channels' events were not imported
  into the engine, fixed undesired creation of new gig::Engine instances
  (and disk threads)
* AudioOutputDevice: reverted behavior to render per Engine instance (and
  not per EngineChannel instance)

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

  ViewVC Help
Powered by ViewVC