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

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

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

revision 221 by schoenebeck, Fri Aug 20 17:25:19 2004 UTC revision 485 by schoenebeck, Thu Mar 24 21:06:09 2005 UTC
# Line 25  Line 25 
25    
26  namespace LinuxSampler {  namespace LinuxSampler {
27    
     REGISTER_AUDIO_OUTPUT_DRIVER(AudioOutputDeviceAlsa);  
   
     /* Common parameters for now they'll have to be registered here. */  
     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceAlsa, ParameterActive);  
     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceAlsa, ParameterSampleRate);  
     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceAlsa, ParameterChannels);  
   
     /* Driver specific parameters */  
     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceAlsa, ParameterCard);  
     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceAlsa, ParameterFragments);  
     REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceAlsa, ParameterFragmentSize);  
   
   
   
28  // *************** ParameterCard ***************  // *************** ParameterCard ***************
29  // *  // *
30    
# Line 47  namespace LinuxSampler { Line 33  namespace LinuxSampler {
33      }      }
34    
35      AudioOutputDeviceAlsa::ParameterCard::ParameterCard(String s) throw (LinuxSamplerException) : DeviceCreationParameterString(s) {      AudioOutputDeviceAlsa::ParameterCard::ParameterCard(String s) throw (LinuxSamplerException) : DeviceCreationParameterString(s) {
         SetValue(s); // try to use given card  
36      }      }
37    
38      String AudioOutputDeviceAlsa::ParameterCard::Description() {      String AudioOutputDeviceAlsa::ParameterCard::Description() {
# Line 144  namespace LinuxSampler { Line 129  namespace LinuxSampler {
129      }      }
130    
131      optional<int> AudioOutputDeviceAlsa::ParameterFragments::RangeMinAsInt(std::map<String,String> Parameters) {      optional<int> AudioOutputDeviceAlsa::ParameterFragments::RangeMinAsInt(std::map<String,String> Parameters) {
132          return optional<int>::nothing;          if (!Parameters.count("CARD")) return optional<int>::nothing;
133    
134            // obtain information from given sound card
135            String pcm_name       = "hw:" + Parameters["CARD"];
136            snd_pcm_t* pcm_handle = NULL;
137            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
138            snd_pcm_hw_params_t* hwparams;
139            snd_pcm_hw_params_alloca(&hwparams);
140            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
141                snd_pcm_close(pcm_handle);
142                return optional<int>::nothing;
143            }
144            int dir = 0;
145            uint periods_min;
146            if (snd_pcm_hw_params_get_periods_min(hwparams, &periods_min, &dir) < 0) {
147                snd_pcm_close(pcm_handle);
148                return optional<int>::nothing;
149            }
150            snd_pcm_close(pcm_handle);
151            return (int) periods_min;
152      }      }
153    
154      optional<int> AudioOutputDeviceAlsa::ParameterFragments::RangeMaxAsInt(std::map<String,String> Parameters) {      optional<int> AudioOutputDeviceAlsa::ParameterFragments::RangeMaxAsInt(std::map<String,String> Parameters) {
155          return optional<int>::nothing;          if (!Parameters.count("CARD")) return optional<int>::nothing;
156    
157            // obtain information from given sound card
158            String pcm_name       = "hw:" + Parameters["CARD"];
159            snd_pcm_t* pcm_handle = NULL;
160            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
161            snd_pcm_hw_params_t* hwparams;
162            snd_pcm_hw_params_alloca(&hwparams);
163            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
164                snd_pcm_close(pcm_handle);
165                return optional<int>::nothing;
166            }
167            int dir = 0;
168            uint periods_max;
169            if (snd_pcm_hw_params_get_periods_max(hwparams, &periods_max, &dir) < 0) {
170                snd_pcm_close(pcm_handle);
171                return optional<int>::nothing;
172            }
173            snd_pcm_close(pcm_handle);
174            return (int) periods_max;
175      }      }
176    
177      std::vector<int> AudioOutputDeviceAlsa::ParameterFragments::PossibilitiesAsInt(std::map<String,String> Parameters) {      std::vector<int> AudioOutputDeviceAlsa::ParameterFragments::PossibilitiesAsInt(std::map<String,String> Parameters) {
# Line 199  namespace LinuxSampler { Line 222  namespace LinuxSampler {
222      }      }
223    
224      optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::RangeMinAsInt(std::map<String,String> Parameters) {      optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::RangeMinAsInt(std::map<String,String> Parameters) {
225          return optional<int>::nothing;          if (!Parameters.count("CARD")) return optional<int>::nothing;
226    
227            // obtain information from given sound card
228            String pcm_name       = "hw:" + Parameters["CARD"];
229            snd_pcm_t* pcm_handle = NULL;
230            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
231            snd_pcm_hw_params_t* hwparams;
232            snd_pcm_hw_params_alloca(&hwparams);
233            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
234                snd_pcm_close(pcm_handle);
235                return optional<int>::nothing;
236            }
237            int dir = 0;
238            unsigned long period_size_min;
239            if (snd_pcm_hw_params_get_period_size_min(hwparams, &period_size_min, &dir) < 0) {
240                snd_pcm_close(pcm_handle);
241                return optional<int>::nothing;
242            }
243            snd_pcm_close(pcm_handle);
244            return (int) period_size_min;
245      }      }
246    
247      optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::RangeMaxAsInt(std::map<String,String> Parameters) {      optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::RangeMaxAsInt(std::map<String,String> Parameters) {
248          return optional<int>::nothing;          if (!Parameters.count("CARD")) return optional<int>::nothing;
249    
250            // obtain information from given sound card
251            String pcm_name       = "hw:" + Parameters["CARD"];
252            snd_pcm_t* pcm_handle = NULL;
253            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
254            snd_pcm_hw_params_t* hwparams;
255            snd_pcm_hw_params_alloca(&hwparams);
256            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
257                snd_pcm_close(pcm_handle);
258                return optional<int>::nothing;
259            }
260            int dir = 0;
261            unsigned long period_size_max;
262            if (snd_pcm_hw_params_get_period_size_max(hwparams, &period_size_max, &dir) < 0) {
263                snd_pcm_close(pcm_handle);
264                return optional<int>::nothing;
265            }
266            snd_pcm_close(pcm_handle);
267            return (int) period_size_max; //FIXME: might overflow int limit
268      }      }
269    
270      std::vector<int> AudioOutputDeviceAlsa::ParameterFragmentSize::PossibilitiesAsInt(std::map<String,String> Parameters) {      std::vector<int> AudioOutputDeviceAlsa::ParameterFragmentSize::PossibilitiesAsInt(std::map<String,String> Parameters) {
# Line 229  namespace LinuxSampler { Line 290  namespace LinuxSampler {
290       * @param Parameters - optional parameters       * @param Parameters - optional parameters
291       * @throws AudioOutputException  if output device cannot be opened       * @throws AudioOutputException  if output device cannot be opened
292       */       */
293      AudioOutputDeviceAlsa::AudioOutputDeviceAlsa(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(Parameters), Thread(true, 1, 0) {      AudioOutputDeviceAlsa::AudioOutputDeviceAlsa(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(Parameters), Thread(true, true, 1, 0) {
294          pcm_handle           = NULL;          pcm_handle           = NULL;
295          stream               = SND_PCM_STREAM_PLAYBACK;          stream               = SND_PCM_STREAM_PLAYBACK;
296          this->uiAlsaChannels = ((DeviceCreationParameterInt*)Parameters["CHANNELS"])->ValueAsInt();          this->uiAlsaChannels = ((DeviceCreationParameterInt*)Parameters["CHANNELS"])->ValueAsInt();
# Line 344  namespace LinuxSampler { Line 405  namespace LinuxSampler {
405          pAlsaOutputBuffer = new int16_t[uiAlsaChannels * FragmentSize];          pAlsaOutputBuffer = new int16_t[uiAlsaChannels * FragmentSize];
406    
407          // create audio channels for this audio device to which the sampler engines can write to          // create audio channels for this audio device to which the sampler engines can write to
408          for (int i = 0; i < uiAlsaChannels; i++) this->Channels.push_back(new AudioChannel(FragmentSize));          for (int i = 0; i < uiAlsaChannels; i++) this->Channels.push_back(new AudioChannel(i, FragmentSize));
409    
410          if (((DeviceCreationParameterBool*)Parameters["ACTIVE"])->ValueAsBool()) {          if (((DeviceCreationParameterBool*)Parameters["ACTIVE"])->ValueAsBool()) {
411                  Play();                  Play();
# Line 359  namespace LinuxSampler { Line 420  namespace LinuxSampler {
420          //FIXME: currently commented out due to segfault          //FIXME: currently commented out due to segfault
421          //snd_pcm_close(pcm_handle);          //snd_pcm_close(pcm_handle);
422    
         // destroy all audio channels  
         for (int c = 0; c < Channels.size(); c++) delete Channels[c];  
   
423          if (pAlsaOutputBuffer) {          if (pAlsaOutputBuffer) {
424              //FIXME: currently commented out due to segfault              //FIXME: currently commented out due to segfault
425              //delete[] pOutputBuffer;              //delete[] pOutputBuffer;
# Line 372  namespace LinuxSampler { Line 430  namespace LinuxSampler {
430       *  Checks if sound card supports the chosen parameters.       *  Checks if sound card supports the chosen parameters.
431       *       *
432       *  @returns  true if hardware supports it       *  @returns  true if hardware supports it
433         *  @throws AudioOutputException - if device cannot be accessed
434       */       */
435      bool AudioOutputDeviceAlsa::HardwareParametersSupported(String card, uint channels, int samplerate, uint numfragments, uint fragmentsize) {      bool AudioOutputDeviceAlsa::HardwareParametersSupported(String card, uint channels, int samplerate, uint numfragments, uint fragmentsize) throw (AudioOutputException) {
436          pcm_name = "hw:" + card;          pcm_name = "hw:" + card;
437          if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), stream, 0) < 0) return false;          int err;
438            if ((err = snd_pcm_open(&pcm_handle, pcm_name.c_str(), stream, 0)) < 0) {
439                throw AudioOutputException(String("Error opening PCM device ") + pcm_name + ": " + snd_strerror(err));
440            }
441          snd_pcm_hw_params_alloca(&hwparams);          snd_pcm_hw_params_alloca(&hwparams);
442          if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {          if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
443              snd_pcm_close(pcm_handle);              snd_pcm_close(pcm_handle);
# Line 428  namespace LinuxSampler { Line 490  namespace LinuxSampler {
490          StopThread();          StopThread();
491      }      }
492    
493      void AudioOutputDeviceAlsa::AcquireChannels(uint Channels) {      AudioChannel* AudioOutputDeviceAlsa::CreateChannel(uint ChannelNr) {
494          if (Channels > uiAlsaChannels) {          // just create a mix channel
495              // just create mix channel(s)          return new AudioChannel(ChannelNr, Channel(ChannelNr % uiAlsaChannels));
             for (int i = uiAlsaChannels; i < Channels; i++) {  
                 AudioChannel* pNewChannel = new AudioChannel(this->Channels[i % uiAlsaChannels]);  
                 this->Channels.push_back(pNewChannel);  
             }  
         }  
496      }      }
497    
498      uint AudioOutputDeviceAlsa::MaxSamplesPerCycle() {      uint AudioOutputDeviceAlsa::MaxSamplesPerCycle() {
# Line 459  namespace LinuxSampler { Line 516  namespace LinuxSampler {
516      }      }
517    
518      String AudioOutputDeviceAlsa::Version() {      String AudioOutputDeviceAlsa::Version() {
519         String s = "$Revision: 1.13 $";         String s = "$Revision: 1.18 $";
520         return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword         return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
521      }      }
522    

Legend:
Removed from v.221  
changed lines
  Added in v.485

  ViewVC Help
Powered by ViewVC