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 * |
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 * |
26 |
|
|
27 |
namespace LinuxSampler { |
namespace LinuxSampler { |
28 |
|
|
|
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); |
|
|
|
|
|
|
|
|
|
|
29 |
// *************** ParameterCard *************** |
// *************** ParameterCard *************** |
30 |
// * |
// * |
31 |
|
|
33 |
InitWithDefault(); // use default card |
InitWithDefault(); // use default card |
34 |
} |
} |
35 |
|
|
36 |
AudioOutputDeviceAlsa::ParameterCard::ParameterCard(String s) throw (LinuxSamplerException) : DeviceCreationParameterString(s) { |
AudioOutputDeviceAlsa::ParameterCard::ParameterCard(String s) throw (Exception) : DeviceCreationParameterString(s) { |
37 |
} |
} |
38 |
|
|
39 |
String AudioOutputDeviceAlsa::ParameterCard::Description() { |
String AudioOutputDeviceAlsa::ParameterCard::Description() { |
54 |
|
|
55 |
optional<String> AudioOutputDeviceAlsa::ParameterCard::DefaultAsString(std::map<String,String> Parameters) { |
optional<String> AudioOutputDeviceAlsa::ParameterCard::DefaultAsString(std::map<String,String> Parameters) { |
56 |
std::vector<String> cards = PossibilitiesAsString(Parameters); |
std::vector<String> cards = PossibilitiesAsString(Parameters); |
57 |
if (cards.empty()) throw LinuxSamplerException("AudioOutputDeviceAlsa: Can't find any card"); |
if (cards.empty()) throw Exception("AudioOutputDeviceAlsa: Can't find any card"); |
58 |
return cards[0]; // first card by default |
return cards[0]; // first card by default |
59 |
} |
} |
60 |
|
|
86 |
return CardNames; |
return CardNames; |
87 |
} |
} |
88 |
|
|
89 |
void AudioOutputDeviceAlsa::ParameterCard::OnSetValue(String s) throw (LinuxSamplerException) { |
void AudioOutputDeviceAlsa::ParameterCard::OnSetValue(String s) throw (Exception) { |
90 |
// not posssible, as parameter is fix |
// not posssible, as parameter is fix |
91 |
} |
} |
92 |
|
|
103 |
InitWithDefault(); |
InitWithDefault(); |
104 |
} |
} |
105 |
|
|
106 |
AudioOutputDeviceAlsa::ParameterFragments::ParameterFragments(String s) throw (LinuxSamplerException) : DeviceCreationParameterInt(s) { |
AudioOutputDeviceAlsa::ParameterFragments::ParameterFragments(String s) throw (Exception) : DeviceCreationParameterInt(s) { |
107 |
} |
} |
108 |
|
|
109 |
String AudioOutputDeviceAlsa::ParameterFragments::Description() { |
String AudioOutputDeviceAlsa::ParameterFragments::Description() { |
135 |
// obtain information from given sound card |
// obtain information from given sound card |
136 |
String pcm_name = "hw:" + Parameters["CARD"]; |
String pcm_name = "hw:" + Parameters["CARD"]; |
137 |
snd_pcm_t* pcm_handle = NULL; |
snd_pcm_t* pcm_handle = NULL; |
138 |
if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, 0) < 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; |
139 |
snd_pcm_hw_params_t* hwparams; |
snd_pcm_hw_params_t* hwparams; |
140 |
snd_pcm_hw_params_alloca(&hwparams); |
snd_pcm_hw_params_alloca(&hwparams); |
141 |
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
158 |
// obtain information from given sound card |
// obtain information from given sound card |
159 |
String pcm_name = "hw:" + Parameters["CARD"]; |
String pcm_name = "hw:" + Parameters["CARD"]; |
160 |
snd_pcm_t* pcm_handle = NULL; |
snd_pcm_t* pcm_handle = NULL; |
161 |
if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, 0) < 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; |
162 |
snd_pcm_hw_params_t* hwparams; |
snd_pcm_hw_params_t* hwparams; |
163 |
snd_pcm_hw_params_alloca(&hwparams); |
snd_pcm_hw_params_alloca(&hwparams); |
164 |
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
179 |
return std::vector<int>(); |
return std::vector<int>(); |
180 |
} |
} |
181 |
|
|
182 |
void AudioOutputDeviceAlsa::ParameterFragments::OnSetValue(int i) throw (LinuxSamplerException) { |
void AudioOutputDeviceAlsa::ParameterFragments::OnSetValue(int i) throw (Exception) { |
183 |
// not posssible, as parameter is fix |
// not posssible, as parameter is fix |
184 |
} |
} |
185 |
|
|
196 |
InitWithDefault(); |
InitWithDefault(); |
197 |
} |
} |
198 |
|
|
199 |
AudioOutputDeviceAlsa::ParameterFragmentSize::ParameterFragmentSize(String s) throw (LinuxSamplerException) : DeviceCreationParameterInt(s) { |
AudioOutputDeviceAlsa::ParameterFragmentSize::ParameterFragmentSize(String s) throw (Exception) : DeviceCreationParameterInt(s) { |
200 |
} |
} |
201 |
|
|
202 |
String AudioOutputDeviceAlsa::ParameterFragmentSize::Description() { |
String AudioOutputDeviceAlsa::ParameterFragmentSize::Description() { |
228 |
// obtain information from given sound card |
// obtain information from given sound card |
229 |
String pcm_name = "hw:" + Parameters["CARD"]; |
String pcm_name = "hw:" + Parameters["CARD"]; |
230 |
snd_pcm_t* pcm_handle = NULL; |
snd_pcm_t* pcm_handle = NULL; |
231 |
if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, 0) < 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; |
232 |
snd_pcm_hw_params_t* hwparams; |
snd_pcm_hw_params_t* hwparams; |
233 |
snd_pcm_hw_params_alloca(&hwparams); |
snd_pcm_hw_params_alloca(&hwparams); |
234 |
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
251 |
// obtain information from given sound card |
// obtain information from given sound card |
252 |
String pcm_name = "hw:" + Parameters["CARD"]; |
String pcm_name = "hw:" + Parameters["CARD"]; |
253 |
snd_pcm_t* pcm_handle = NULL; |
snd_pcm_t* pcm_handle = NULL; |
254 |
if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), SND_PCM_STREAM_PLAYBACK, 0) < 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; |
255 |
snd_pcm_hw_params_t* hwparams; |
snd_pcm_hw_params_t* hwparams; |
256 |
snd_pcm_hw_params_alloca(&hwparams); |
snd_pcm_hw_params_alloca(&hwparams); |
257 |
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
272 |
return std::vector<int>(); |
return std::vector<int>(); |
273 |
} |
} |
274 |
|
|
275 |
void AudioOutputDeviceAlsa::ParameterFragmentSize::OnSetValue(int i) throw (LinuxSamplerException) { |
void AudioOutputDeviceAlsa::ParameterFragmentSize::OnSetValue(int i) throw (Exception) { |
276 |
// not posssible, as parameter is fix |
// not posssible, as parameter is fix |
277 |
} |
} |
278 |
|
|
291 |
* @param Parameters - optional parameters |
* @param Parameters - optional parameters |
292 |
* @throws AudioOutputException if output device cannot be opened |
* @throws AudioOutputException if output device cannot be opened |
293 |
*/ |
*/ |
294 |
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) { |
295 |
pcm_handle = NULL; |
pcm_handle = NULL; |
296 |
stream = SND_PCM_STREAM_PLAYBACK; |
stream = SND_PCM_STREAM_PLAYBACK; |
297 |
this->uiAlsaChannels = ((DeviceCreationParameterInt*)Parameters["CHANNELS"])->ValueAsInt(); |
this->uiAlsaChannels = ((DeviceCreationParameterInt*)Parameters["CHANNELS"])->ValueAsInt(); |
300 |
uint Fragments = ((DeviceCreationParameterInt*)Parameters["FRAGMENTS"])->ValueAsInt(); |
uint Fragments = ((DeviceCreationParameterInt*)Parameters["FRAGMENTS"])->ValueAsInt(); |
301 |
String Card = ((DeviceCreationParameterString*)Parameters["CARD"])->ValueAsString(); |
String Card = ((DeviceCreationParameterString*)Parameters["CARD"])->ValueAsString(); |
302 |
|
|
303 |
dmsg(1,("Checking if hw parameters supported...\n")); |
dmsg(2,("Checking if hw parameters supported...\n")); |
304 |
if (HardwareParametersSupported(Card, uiAlsaChannels, uiSamplerate, Fragments, FragmentSize)) { |
if (HardwareParametersSupported(Card, uiAlsaChannels, uiSamplerate, Fragments, FragmentSize)) { |
305 |
pcm_name = "hw:" + Card; |
pcm_name = "hw:" + Card; |
306 |
} |
} |
307 |
else { |
else { |
308 |
printf("Warning: your soundcard doesn't support chosen hardware parameters; "); |
fprintf(stderr, "Warning: your soundcard doesn't support chosen hardware parameters; "); |
309 |
printf("trying to compensate support lack with plughw..."); |
fprintf(stderr, "trying to compensate support lack with plughw..."); |
310 |
fflush(stdout); |
fflush(stdout); |
311 |
pcm_name = "plughw:" + Card; |
pcm_name = "plughw:" + Card; |
312 |
} |
} |
313 |
dmsg(1,("HW check completed.\n")); |
dmsg(2,("HW check completed.\n")); |
314 |
|
|
315 |
int err; |
int err; |
316 |
|
|
431 |
* Checks if sound card supports the chosen parameters. |
* Checks if sound card supports the chosen parameters. |
432 |
* |
* |
433 |
* @returns true if hardware supports it |
* @returns true if hardware supports it |
434 |
|
* @throws AudioOutputException - if device cannot be accessed |
435 |
*/ |
*/ |
436 |
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) { |
437 |
pcm_name = "hw:" + card; |
pcm_name = "hw:" + card; |
438 |
if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), stream, 0) < 0) return false; |
int err; |
439 |
|
if ((err = snd_pcm_open(&pcm_handle, pcm_name.c_str(), stream, SND_PCM_NONBLOCK)) < 0) { |
440 |
|
throw AudioOutputException(String("Error opening PCM device ") + pcm_name + ": " + snd_strerror(err)); |
441 |
|
} |
442 |
snd_pcm_hw_params_alloca(&hwparams); |
snd_pcm_hw_params_alloca(&hwparams); |
443 |
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
444 |
snd_pcm_close(pcm_handle); |
snd_pcm_close(pcm_handle); |
517 |
} |
} |
518 |
|
|
519 |
String AudioOutputDeviceAlsa::Version() { |
String AudioOutputDeviceAlsa::Version() { |
520 |
String s = "$Revision: 1.15 $"; |
String s = "$Revision: 1.21 $"; |
521 |
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 |
522 |
} |
} |
523 |
|
|