/[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 880 by schoenebeck, Tue Jun 27 22:57:37 2006 UTC revision 1049 by schoenebeck, Wed Feb 28 06:53:42 2007 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, 2006 Christian Schoenebeck                        *   *   Copyright (C) 2005 - 2007 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 96  namespace LinuxSampler { Line 96  namespace LinuxSampler {
96    
97    
98    
99    // *************** ParameterSampleRate ***************
100    // *
101    
102        AudioOutputDeviceAlsa::ParameterSampleRate::ParameterSampleRate() : AudioOutputDevice::ParameterSampleRate::ParameterSampleRate() {
103        }
104    
105        AudioOutputDeviceAlsa::ParameterSampleRate::ParameterSampleRate(String s) : AudioOutputDevice::ParameterSampleRate::ParameterSampleRate(s) {
106        }
107    
108        std::map<String,DeviceCreationParameter*> AudioOutputDeviceAlsa::ParameterSampleRate::DependsAsParameters() {
109            static ParameterCard card;
110            std::map<String,DeviceCreationParameter*> dependencies;
111            dependencies[card.Name()] = &card;
112            return dependencies;
113        }
114    
115        optional<int> AudioOutputDeviceAlsa::ParameterSampleRate::DefaultAsInt(std::map<String,String> Parameters) {
116            if (!Parameters.count("CARD")) return optional<int>::nothing;
117    
118            // obtain information from given sound card
119            ParameterCard card(Parameters["CARD"]);
120            String pcm_name = "hw:" + card.ValueAsString();
121            snd_pcm_t* pcm_handle = NULL;
122            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
123            snd_pcm_hw_params_t* hwparams;
124            snd_pcm_hw_params_alloca(&hwparams);
125            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
126                snd_pcm_close(pcm_handle);
127                return optional<int>::nothing;
128            }
129            uint rate = 44100;
130            if (snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &rate, NULL) < 0) {
131                snd_pcm_close(pcm_handle);
132                return optional<int>::nothing;
133            }
134            snd_pcm_close(pcm_handle);
135            return rate;
136        }
137    
138        optional<int> AudioOutputDeviceAlsa::ParameterSampleRate::RangeMinAsInt(std::map<String,String> Parameters) {
139            if (!Parameters.count("CARD")) return optional<int>::nothing;
140    
141            // obtain information from given sound card
142            ParameterCard card(Parameters["CARD"]);
143            String pcm_name = "hw:" + card.ValueAsString();
144            snd_pcm_t* pcm_handle = NULL;
145            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
146            snd_pcm_hw_params_t* hwparams;
147            snd_pcm_hw_params_alloca(&hwparams);
148            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
149                snd_pcm_close(pcm_handle);
150                return optional<int>::nothing;
151            }
152            uint rate;
153            if (snd_pcm_hw_params_get_rate_min(hwparams, &rate, NULL) < 0) {
154                snd_pcm_close(pcm_handle);
155                return optional<int>::nothing;
156            }
157            snd_pcm_close(pcm_handle);
158            return rate;
159        }
160    
161        optional<int> AudioOutputDeviceAlsa::ParameterSampleRate::RangeMaxAsInt(std::map<String,String> Parameters) {
162            if (!Parameters.count("CARD")) return optional<int>::nothing;
163    
164            // obtain information from given sound card
165            String pcm_name       = "hw:" + Parameters["CARD"];
166            snd_pcm_t* pcm_handle = NULL;
167            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
168            snd_pcm_hw_params_t* hwparams;
169            snd_pcm_hw_params_alloca(&hwparams);
170            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
171                snd_pcm_close(pcm_handle);
172                return optional<int>::nothing;
173            }
174            uint rate;
175            if (snd_pcm_hw_params_get_rate_max(hwparams, &rate, NULL) < 0) {
176                snd_pcm_close(pcm_handle);
177                return optional<int>::nothing;
178            }
179            snd_pcm_close(pcm_handle);
180            return rate;
181        }
182    
183    
184    
185    // *************** ParameterChannels ***************
186    // *
187    
188        AudioOutputDeviceAlsa::ParameterChannels::ParameterChannels() : AudioOutputDevice::ParameterChannels::ParameterChannels() {
189            //InitWithDefault();
190            // could not determine default value? ...
191            //if (ValueAsInt() == 0) SetValue(2); // ... then (try) a common value
192        }
193    
194        AudioOutputDeviceAlsa::ParameterChannels::ParameterChannels(String s) : AudioOutputDevice::ParameterChannels::ParameterChannels(s) {
195        }
196    
197        std::map<String,DeviceCreationParameter*> AudioOutputDeviceAlsa::ParameterChannels::DependsAsParameters() {
198            static ParameterCard card;
199            std::map<String,DeviceCreationParameter*> dependencies;
200            dependencies[card.Name()] = &card;
201            return dependencies;
202        }
203    
204        optional<int> AudioOutputDeviceAlsa::ParameterChannels::DefaultAsInt(std::map<String,String> Parameters) {
205            if (!Parameters.count("CARD")) return optional<int>::nothing;
206    
207            // obtain information from given sound card
208            ParameterCard card(Parameters["CARD"]);
209            String pcm_name = "hw:" + card.ValueAsString();
210            snd_pcm_t* pcm_handle = NULL;
211            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
212            snd_pcm_hw_params_t* hwparams;
213            snd_pcm_hw_params_alloca(&hwparams);
214            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
215                snd_pcm_close(pcm_handle);
216                return optional<int>::nothing;
217            }
218            uint channels = 2;
219            if (snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &channels) < 0) {
220                snd_pcm_close(pcm_handle);
221                return optional<int>::nothing;
222            }
223            snd_pcm_close(pcm_handle);
224            return channels;
225        }
226    
227        optional<int> AudioOutputDeviceAlsa::ParameterChannels::RangeMinAsInt(std::map<String,String> Parameters) {
228            if (!Parameters.count("CARD")) return optional<int>::nothing;
229    
230            // obtain information from given sound card
231            ParameterCard card(Parameters["CARD"]);
232            String pcm_name = "hw:" + card.ValueAsString();
233            snd_pcm_t* pcm_handle = NULL;
234            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
235            snd_pcm_hw_params_t* hwparams;
236            snd_pcm_hw_params_alloca(&hwparams);
237            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
238                snd_pcm_close(pcm_handle);
239                return optional<int>::nothing;
240            }
241            uint channels;
242            if (snd_pcm_hw_params_get_channels_min(hwparams, &channels) < 0) {
243                snd_pcm_close(pcm_handle);
244                return optional<int>::nothing;
245            }
246            snd_pcm_close(pcm_handle);
247            return channels;
248        }
249    
250        optional<int> AudioOutputDeviceAlsa::ParameterChannels::RangeMaxAsInt(std::map<String,String> Parameters) {
251            if (!Parameters.count("CARD")) return optional<int>::nothing;
252    
253            // obtain information from given sound card
254            String pcm_name       = "hw:" + Parameters["CARD"];
255            snd_pcm_t* pcm_handle = NULL;
256            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
257            snd_pcm_hw_params_t* hwparams;
258            snd_pcm_hw_params_alloca(&hwparams);
259            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
260                snd_pcm_close(pcm_handle);
261                return optional<int>::nothing;
262            }
263            uint channels;
264            if (snd_pcm_hw_params_get_channels_max(hwparams, &channels) < 0) {
265                snd_pcm_close(pcm_handle);
266                return optional<int>::nothing;
267            }
268            snd_pcm_close(pcm_handle);
269            return channels;
270        }
271    
272    
273    
274  // *************** ParameterFragments ***************  // *************** ParameterFragments ***************
275  // *  // *
276    
# Line 126  namespace LinuxSampler { Line 301  namespace LinuxSampler {
301      }      }
302    
303      optional<int> AudioOutputDeviceAlsa::ParameterFragments::DefaultAsInt(std::map<String,String> Parameters) {      optional<int> AudioOutputDeviceAlsa::ParameterFragments::DefaultAsInt(std::map<String,String> Parameters) {
304          return 2; // until done          if (!Parameters.count("CARD")) return optional<int>::nothing;
305    
306            // obtain information from given sound card
307            ParameterCard card(Parameters["CARD"]);
308            String pcm_name = "hw:" + card.ValueAsString();
309            snd_pcm_t* pcm_handle = NULL;
310            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
311            snd_pcm_hw_params_t* hwparams;
312            snd_pcm_hw_params_alloca(&hwparams);
313            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
314                snd_pcm_close(pcm_handle);
315                return optional<int>::nothing;
316            }
317            uint segs = 2;
318            if (snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &segs, NULL) < 0) {
319                snd_pcm_close(pcm_handle);
320                return optional<int>::nothing;
321            }
322            snd_pcm_close(pcm_handle);
323            return segs;
324      }      }
325    
326      optional<int> AudioOutputDeviceAlsa::ParameterFragments::RangeMinAsInt(std::map<String,String> Parameters) {      optional<int> AudioOutputDeviceAlsa::ParameterFragments::RangeMinAsInt(std::map<String,String> Parameters) {
327          if (!Parameters.count("CARD")) return optional<int>::nothing;          if (!Parameters.count("CARD")) return optional<int>::nothing;
328    
329          // obtain information from given sound card          // obtain information from given sound card
330          String pcm_name       = "hw:" + Parameters["CARD"];          ParameterCard card(Parameters["CARD"]);
331            String pcm_name = "hw:" + card.ValueAsString();
332          snd_pcm_t* pcm_handle = NULL;          snd_pcm_t* pcm_handle = NULL;
333          if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;          if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
334          snd_pcm_hw_params_t* hwparams;          snd_pcm_hw_params_t* hwparams;
# Line 156  namespace LinuxSampler { Line 351  namespace LinuxSampler {
351          if (!Parameters.count("CARD")) return optional<int>::nothing;          if (!Parameters.count("CARD")) return optional<int>::nothing;
352    
353          // obtain information from given sound card          // obtain information from given sound card
354          String pcm_name       = "hw:" + Parameters["CARD"];          ParameterCard card(Parameters["CARD"]);
355            String pcm_name = "hw:" + card.ValueAsString();
356          snd_pcm_t* pcm_handle = NULL;          snd_pcm_t* pcm_handle = NULL;
357          if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;          if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
358          snd_pcm_hw_params_t* hwparams;          snd_pcm_hw_params_t* hwparams;
# Line 219  namespace LinuxSampler { Line 415  namespace LinuxSampler {
415      }      }
416    
417      optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::DefaultAsInt(std::map<String,String> Parameters) {      optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::DefaultAsInt(std::map<String,String> Parameters) {
418          return 128; // until done          if (!Parameters.count("CARD")) return optional<int>::nothing;
419    
420            // obtain information from given sound card
421            ParameterCard card(Parameters["CARD"]);
422            String pcm_name = "hw:" + card.ValueAsString();
423            snd_pcm_t* pcm_handle = NULL;
424            if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
425            snd_pcm_hw_params_t* hwparams;
426            snd_pcm_hw_params_alloca(&hwparams);
427            if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
428                snd_pcm_close(pcm_handle);
429                return optional<int>::nothing;
430            }
431            snd_pcm_uframes_t size = 128;
432            if (snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &size, NULL) < 0) {
433                snd_pcm_close(pcm_handle);
434                return optional<int>::nothing;
435            }
436            snd_pcm_close(pcm_handle);
437            return size;
438      }      }
439    
440      optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::RangeMinAsInt(std::map<String,String> Parameters) {      optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::RangeMinAsInt(std::map<String,String> Parameters) {
441          if (!Parameters.count("CARD")) return optional<int>::nothing;          if (!Parameters.count("CARD")) return optional<int>::nothing;
442    
443          // obtain information from given sound card          // obtain information from given sound card
444          String pcm_name       = "hw:" + Parameters["CARD"];          ParameterCard card(Parameters["CARD"]);
445            String pcm_name = "hw:" + card.ValueAsString();
446          snd_pcm_t* pcm_handle = NULL;          snd_pcm_t* pcm_handle = NULL;
447          if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;          if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
448          snd_pcm_hw_params_t* hwparams;          snd_pcm_hw_params_t* hwparams;
# Line 249  namespace LinuxSampler { Line 465  namespace LinuxSampler {
465          if (!Parameters.count("CARD")) return optional<int>::nothing;          if (!Parameters.count("CARD")) return optional<int>::nothing;
466    
467          // obtain information from given sound card          // obtain information from given sound card
468          String pcm_name       = "hw:" + Parameters["CARD"];          ParameterCard card(Parameters["CARD"]);
469            String pcm_name = "hw:" + card.ValueAsString();
470          snd_pcm_t* pcm_handle = NULL;          snd_pcm_t* pcm_handle = NULL;
471          if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;          if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) return optional<int>::nothing;
472          snd_pcm_hw_params_t* hwparams;          snd_pcm_hw_params_t* hwparams;
# Line 367  namespace LinuxSampler { Line 584  namespace LinuxSampler {
584    
585          /* Set number of periods. Periods used to be called fragments. */          /* Set number of periods. Periods used to be called fragments. */
586          if ((err = snd_pcm_hw_params_set_periods(pcm_handle, hwparams, Fragments, dir)) < 0) {          if ((err = snd_pcm_hw_params_set_periods(pcm_handle, hwparams, Fragments, dir)) < 0) {
587              throw AudioOutputException(String("Error setting number of periods: ") + snd_strerror(err));              throw AudioOutputException(String("Error setting number of ") + ToString(Fragments) + " periods: " + snd_strerror(err));
588          }          }
589    
590          /* Set buffer size (in frames). The resulting latency is given by */          /* Set buffer size (in frames). The resulting latency is given by */
# Line 517  namespace LinuxSampler { Line 734  namespace LinuxSampler {
734      }      }
735    
736      String AudioOutputDeviceAlsa::Version() {      String AudioOutputDeviceAlsa::Version() {
737         String s = "$Revision: 1.21 $";         String s = "$Revision: 1.22 $";
738         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
739      }      }
740    

Legend:
Removed from v.880  
changed lines
  Added in v.1049

  ViewVC Help
Powered by ViewVC