/[svn]/linuxsampler/trunk/src/drivers/audio/AudioOutputDevice.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/drivers/audio/AudioOutputDevice.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 554 by schoenebeck, Thu May 19 19:25:14 2005 UTC revision 1722 by schoenebeck, Thu Apr 10 17:41:32 2008 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6   *   Copyright (C) 2005 Christian Schoenebeck                              *   *   Copyright (C) 2005 - 2008 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
# Line 23  Line 23 
23    
24  #include "AudioOutputDeviceFactory.h"  #include "AudioOutputDeviceFactory.h"
25  #include "AudioOutputDevice.h"  #include "AudioOutputDevice.h"
26    #include "../../common/global_private.h"
27    
28  namespace LinuxSampler {  namespace LinuxSampler {
29    
# Line 56  namespace LinuxSampler { Line 57  namespace LinuxSampler {
57          return true;          return true;
58      }      }
59    
60      void AudioOutputDevice::ParameterActive::OnSetValue(bool b) throw (LinuxSamplerException) {      void AudioOutputDevice::ParameterActive::OnSetValue(bool b) throw (Exception) {
61          if (b) ((AudioOutputDevice*)pDevice)->Play();          if (b) ((AudioOutputDevice*)pDevice)->Play();
62          else ((AudioOutputDevice*)pDevice)->Stop();          else ((AudioOutputDevice*)pDevice)->Stop();
63      }      }
# Line 109  namespace LinuxSampler { Line 110  namespace LinuxSampler {
110          return std::vector<int>();          return std::vector<int>();
111      }      }
112    
113      void AudioOutputDevice::ParameterSampleRate::OnSetValue(int i) throw (LinuxSamplerException) {      int AudioOutputDevice::ParameterSampleRate::ValueAsInt() {
114            return (pDevice) ? (int) ((AudioOutputDevice*)pDevice)->SampleRate()
115                             : DeviceCreationParameterInt::ValueAsInt();
116        }
117    
118        void AudioOutputDevice::ParameterSampleRate::OnSetValue(int i) throw (Exception) {
119          /* cannot happen, as parameter is fix */          /* cannot happen, as parameter is fix */
120      }      }
121    
# Line 150  namespace LinuxSampler { Line 156  namespace LinuxSampler {
156      }      }
157    
158      optional<int> AudioOutputDevice::ParameterChannels::RangeMinAsInt(std::map<String,String> Parameters) {      optional<int> AudioOutputDevice::ParameterChannels::RangeMinAsInt(std::map<String,String> Parameters) {
159          return optional<int>::nothing;          return 1;
160      }      }
161    
162      optional<int> AudioOutputDevice::ParameterChannels::RangeMaxAsInt(std::map<String,String> Parameters) {      optional<int> AudioOutputDevice::ParameterChannels::RangeMaxAsInt(std::map<String,String> Parameters) {
# Line 161  namespace LinuxSampler { Line 167  namespace LinuxSampler {
167          return std::vector<int>();          return std::vector<int>();
168      }      }
169    
170      void AudioOutputDevice::ParameterChannels::OnSetValue(int i) throw (LinuxSamplerException) {      void AudioOutputDevice::ParameterChannels::OnSetValue(int i) throw (Exception) {
171          ((AudioOutputDevice*)pDevice)->AcquireChannels(i);          ((AudioOutputDevice*)pDevice)->AcquireChannels(i);
172      }      }
173    
# Line 174  namespace LinuxSampler { Line 180  namespace LinuxSampler {
180  // *************** AudioOutputDevice ***************  // *************** AudioOutputDevice ***************
181  // *  // *
182    
183      AudioOutputDevice::AudioOutputDevice(std::map<String,DeviceCreationParameter*> DriverParameters) {      AudioOutputDevice::AudioOutputDevice(std::map<String,DeviceCreationParameter*> DriverParameters)
184            : EnginesReader(Engines) {
185          this->Parameters = DriverParameters;          this->Parameters = DriverParameters;
186      }      }
187    
# Line 183  namespace LinuxSampler { Line 190  namespace LinuxSampler {
190          {          {
191              std::vector<AudioChannel*>::iterator iter = Channels.begin();              std::vector<AudioChannel*>::iterator iter = Channels.begin();
192              while (iter != Channels.end()) {              while (iter != Channels.end()) {
                 Channels.erase(iter);  
193                  delete *iter;                  delete *iter;
194                  iter++;                  iter++;
195              }              }
196                Channels.clear();
197          }          }
198    
199          // delete all device parameters          // delete all device parameters
200          {          {
201              std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin();              std::map<String,DeviceCreationParameter*>::iterator iter = Parameters.begin();
202              while (iter != Parameters.end()) {              while (iter != Parameters.end()) {
                 Parameters.erase(iter);  
203                  delete iter->second;                  delete iter->second;
204                  iter++;                  iter++;
205              }              }
206                Parameters.clear();
207            }
208    
209            // delete all master effect chains
210            {
211                std::vector<EffectChain*>::iterator iter = vEffectChains.begin();
212                while (iter != vEffectChains.end()) {
213                    delete *iter;
214                    iter++;
215                }
216                vEffectChains.clear();
217          }          }
218      }      }
219    
220      void AudioOutputDevice::Connect(Engine* pEngine) {      void AudioOutputDevice::Connect(Engine* pEngine) {
221          if (Engines.find(pEngine) == Engines.end()) {          std::set<Engine*>& engines = Engines.GetConfigForUpdate();
222              Engines.insert(pEngine);          if (engines.find(pEngine) == engines.end()) {
223                engines.insert(pEngine);
224                Engines.SwitchConfig().insert(pEngine);
225              // make sure the engine knows about the connection              // make sure the engine knows about the connection
226              //pEngine->Connect(this);              //pEngine->Connect(this);
227          }          }
228      }      }
229    
230      void AudioOutputDevice::Disconnect(Engine* pEngine) {      void AudioOutputDevice::Disconnect(Engine* pEngine) {
231          if (Engines.find(pEngine) != Engines.end()) { // if clause to prevent disconnect loop          std::set<Engine*>& engines = Engines.GetConfigForUpdate();
232              Engines.erase(pEngine);          if (engines.find(pEngine) != engines.end()) { // if clause to prevent disconnect loop
233                engines.erase(pEngine);
234                Engines.SwitchConfig().erase(pEngine);
235              // make sure the engine knows about the disconnection              // make sure the engine knows about the disconnection
236              //pEngine->DisconnectAudioOutputDevice();              //pEngine->DisconnectAudioOutputDevice();
237          }          }
# Line 229  namespace LinuxSampler { Line 249  namespace LinuxSampler {
249          }          }
250      }      }
251    
252        uint AudioOutputDevice::ChannelCount() {
253            return Channels.size();
254        }
255    
256      std::map<String,DeviceCreationParameter*> AudioOutputDevice::DeviceParameters() {      std::map<String,DeviceCreationParameter*> AudioOutputDevice::DeviceParameters() {
257          return Parameters;          return Parameters;
258      }      }
259    
260        EffectChain* AudioOutputDevice::AddMasterEffectChain() {
261            EffectChain* pChain = new EffectChain(this);
262            vEffectChains.push_back(pChain);
263            return pChain;
264        }
265    
266        void AudioOutputDevice::RemoveMasterEffectChain(uint iChain) throw (Exception) {
267            if (iChain >= vEffectChains.size())
268                throw Exception(
269                    "Could not remove master effect chain " + ToString(iChain) +
270                    ", index out of bounds"
271                );
272            std::vector<EffectChain*>::iterator iter = vEffectChains.begin();
273            for (int i = 0; i < iChain; ++i) ++iter;
274            vEffectChains.erase(iter);
275        }
276    
277        EffectChain* AudioOutputDevice::MasterEffectChain(uint iChain) const {
278            if (iChain >= vEffectChains.size()) return NULL;
279            return vEffectChains[iChain];
280        }
281    
282        uint AudioOutputDevice::MasterEffectChainCount() const {
283            return vEffectChains.size();
284        }
285    
286      int AudioOutputDevice::RenderAudio(uint Samples) {      int AudioOutputDevice::RenderAudio(uint Samples) {
287          if (Channels.empty()) return 0;          if (Channels.empty()) return 0;
288    
# Line 243  namespace LinuxSampler { Line 293  namespace LinuxSampler {
293              for (; iterChannels != end; iterChannels++)              for (; iterChannels != end; iterChannels++)
294                  (*iterChannels)->Clear(); // zero out audio buffer                  (*iterChannels)->Clear(); // zero out audio buffer
295          }          }
296            // do the same for master effects
297            {
298                std::vector<EffectChain*>::iterator iterChains = vEffectChains.begin();
299                std::vector<EffectChain*>::iterator end        = vEffectChains.end();
300                for (; iterChains != end; ++iterChains)
301                    (*iterChains)->ClearAllChannels(); // zero out audio buffers
302            }
303    
304          int result = 0;          int result = 0;
305    
306          // let all connected engines render audio for the current audio fragment cycle          // let all connected engines render audio for the current audio fragment cycle
307            const std::set<Engine*>& engines = EnginesReader.Lock();
308          #if CONFIG_RT_EXCEPTIONS          #if CONFIG_RT_EXCEPTIONS
309          try          try
310          #endif // CONFIG_RT_EXCEPTIONS          #endif // CONFIG_RT_EXCEPTIONS
311          {          {
312              std::set<Engine*>::iterator iterEngine = Engines.begin();              std::set<Engine*>::iterator iterEngine = engines.begin();
313              std::set<Engine*>::iterator end        = Engines.end();              std::set<Engine*>::iterator end        = engines.end();
314              for (; iterEngine != end; iterEngine++) {              for (; iterEngine != end; iterEngine++) {
315                  int res = (*iterEngine)->RenderAudio(Samples);                  int res = (*iterEngine)->RenderAudio(Samples);
316                  if (res != 0) result = res;                  if (res != 0) result = res;
# Line 264  namespace LinuxSampler { Line 322  namespace LinuxSampler {
322              exit(EXIT_FAILURE);              exit(EXIT_FAILURE);
323          }          }
324          #endif // CONFIG_RT_EXCEPTIONS          #endif // CONFIG_RT_EXCEPTIONS
325            EnginesReader.Unlock();
326    
327            // now that the engines (might) have left fx send signals for master
328            // effects, render all master effects
329            {
330                std::vector<EffectChain*>::iterator iterChains = vEffectChains.begin();
331                std::vector<EffectChain*>::iterator end        = vEffectChains.end();
332                for (; iterChains != end; ++iterChains) {
333                    if (!(*iterChains)->EffectCount()) continue;
334                    (*iterChains)->RenderAudio(Samples);
335                    // mix the result of the last effect in the chain to the audio
336                    // output device channel(s)
337                    Effect* pLastEffect =
338                        (*iterChains)->GetEffect((*iterChains)->EffectCount() - 1);
339                    for (int iChan = 0; iChan < pLastEffect->OutputChannelCount() && iChan < ChannelCount(); ++iChan)
340                        pLastEffect->OutputChannel(iChan)->MixTo(Channel(iChan), Samples);
341                }
342            }
343    
344          return result;          return result;
345      }      }

Legend:
Removed from v.554  
changed lines
  Added in v.1722

  ViewVC Help
Powered by ViewVC