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 * |
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 |
|
|
34 |
} |
} |
35 |
|
|
36 |
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 |
|
37 |
} |
} |
38 |
|
|
39 |
String AudioOutputDeviceAlsa::ParameterCard::Description() { |
String AudioOutputDeviceAlsa::ParameterCard::Description() { |
91 |
} |
} |
92 |
|
|
93 |
String AudioOutputDeviceAlsa::ParameterCard::Name() { |
String AudioOutputDeviceAlsa::ParameterCard::Name() { |
94 |
return "card"; |
return "CARD"; |
95 |
} |
} |
96 |
|
|
97 |
|
|
130 |
} |
} |
131 |
|
|
132 |
optional<int> AudioOutputDeviceAlsa::ParameterFragments::RangeMinAsInt(std::map<String,String> Parameters) { |
optional<int> AudioOutputDeviceAlsa::ParameterFragments::RangeMinAsInt(std::map<String,String> Parameters) { |
133 |
return optional<int>::nothing; |
if (!Parameters.count("CARD")) return optional<int>::nothing; |
134 |
|
|
135 |
|
// obtain information from given sound card |
136 |
|
String pcm_name = "hw:" + Parameters["CARD"]; |
137 |
|
snd_pcm_t* pcm_handle = NULL; |
138 |
|
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; |
140 |
|
snd_pcm_hw_params_alloca(&hwparams); |
141 |
|
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
142 |
|
snd_pcm_close(pcm_handle); |
143 |
|
return optional<int>::nothing; |
144 |
|
} |
145 |
|
int dir = 0; |
146 |
|
uint periods_min; |
147 |
|
if (snd_pcm_hw_params_get_periods_min(hwparams, &periods_min, &dir) < 0) { |
148 |
|
snd_pcm_close(pcm_handle); |
149 |
|
return optional<int>::nothing; |
150 |
|
} |
151 |
|
snd_pcm_close(pcm_handle); |
152 |
|
return (int) periods_min; |
153 |
} |
} |
154 |
|
|
155 |
optional<int> AudioOutputDeviceAlsa::ParameterFragments::RangeMaxAsInt(std::map<String,String> Parameters) { |
optional<int> AudioOutputDeviceAlsa::ParameterFragments::RangeMaxAsInt(std::map<String,String> Parameters) { |
156 |
return optional<int>::nothing; |
if (!Parameters.count("CARD")) return optional<int>::nothing; |
157 |
|
|
158 |
|
// obtain information from given sound card |
159 |
|
String pcm_name = "hw:" + Parameters["CARD"]; |
160 |
|
snd_pcm_t* pcm_handle = NULL; |
161 |
|
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; |
163 |
|
snd_pcm_hw_params_alloca(&hwparams); |
164 |
|
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
165 |
|
snd_pcm_close(pcm_handle); |
166 |
|
return optional<int>::nothing; |
167 |
|
} |
168 |
|
int dir = 0; |
169 |
|
uint periods_max; |
170 |
|
if (snd_pcm_hw_params_get_periods_max(hwparams, &periods_max, &dir) < 0) { |
171 |
|
snd_pcm_close(pcm_handle); |
172 |
|
return optional<int>::nothing; |
173 |
|
} |
174 |
|
snd_pcm_close(pcm_handle); |
175 |
|
return (int) periods_max; |
176 |
} |
} |
177 |
|
|
178 |
std::vector<int> AudioOutputDeviceAlsa::ParameterFragments::PossibilitiesAsInt(std::map<String,String> Parameters) { |
std::vector<int> AudioOutputDeviceAlsa::ParameterFragments::PossibilitiesAsInt(std::map<String,String> Parameters) { |
184 |
} |
} |
185 |
|
|
186 |
String AudioOutputDeviceAlsa::ParameterFragments::Name() { |
String AudioOutputDeviceAlsa::ParameterFragments::Name() { |
187 |
return "fragments"; |
return "FRAGMENTS"; |
188 |
} |
} |
189 |
|
|
190 |
|
|
223 |
} |
} |
224 |
|
|
225 |
optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::RangeMinAsInt(std::map<String,String> Parameters) { |
optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::RangeMinAsInt(std::map<String,String> Parameters) { |
226 |
return optional<int>::nothing; |
if (!Parameters.count("CARD")) return optional<int>::nothing; |
227 |
|
|
228 |
|
// obtain information from given sound card |
229 |
|
String pcm_name = "hw:" + Parameters["CARD"]; |
230 |
|
snd_pcm_t* pcm_handle = NULL; |
231 |
|
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; |
233 |
|
snd_pcm_hw_params_alloca(&hwparams); |
234 |
|
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
235 |
|
snd_pcm_close(pcm_handle); |
236 |
|
return optional<int>::nothing; |
237 |
|
} |
238 |
|
int dir = 0; |
239 |
|
unsigned long period_size_min; |
240 |
|
if (snd_pcm_hw_params_get_period_size_min(hwparams, &period_size_min, &dir) < 0) { |
241 |
|
snd_pcm_close(pcm_handle); |
242 |
|
return optional<int>::nothing; |
243 |
|
} |
244 |
|
snd_pcm_close(pcm_handle); |
245 |
|
return (int) period_size_min; |
246 |
} |
} |
247 |
|
|
248 |
optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::RangeMaxAsInt(std::map<String,String> Parameters) { |
optional<int> AudioOutputDeviceAlsa::ParameterFragmentSize::RangeMaxAsInt(std::map<String,String> Parameters) { |
249 |
return optional<int>::nothing; |
if (!Parameters.count("CARD")) return optional<int>::nothing; |
250 |
|
|
251 |
|
// obtain information from given sound card |
252 |
|
String pcm_name = "hw:" + Parameters["CARD"]; |
253 |
|
snd_pcm_t* pcm_handle = NULL; |
254 |
|
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; |
256 |
|
snd_pcm_hw_params_alloca(&hwparams); |
257 |
|
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) { |
258 |
|
snd_pcm_close(pcm_handle); |
259 |
|
return optional<int>::nothing; |
260 |
|
} |
261 |
|
int dir = 0; |
262 |
|
unsigned long period_size_max; |
263 |
|
if (snd_pcm_hw_params_get_period_size_max(hwparams, &period_size_max, &dir) < 0) { |
264 |
|
snd_pcm_close(pcm_handle); |
265 |
|
return optional<int>::nothing; |
266 |
|
} |
267 |
|
snd_pcm_close(pcm_handle); |
268 |
|
return (int) period_size_max; //FIXME: might overflow int limit |
269 |
} |
} |
270 |
|
|
271 |
std::vector<int> AudioOutputDeviceAlsa::ParameterFragmentSize::PossibilitiesAsInt(std::map<String,String> Parameters) { |
std::vector<int> AudioOutputDeviceAlsa::ParameterFragmentSize::PossibilitiesAsInt(std::map<String,String> Parameters) { |
277 |
} |
} |
278 |
|
|
279 |
String AudioOutputDeviceAlsa::ParameterFragmentSize::Name() { |
String AudioOutputDeviceAlsa::ParameterFragmentSize::Name() { |
280 |
return "fragmentsize"; |
return "FRAGMENTSIZE"; |
281 |
} |
} |
282 |
|
|
283 |
|
|
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(); |
298 |
this->uiSamplerate = ((DeviceCreationParameterInt*)Parameters["samplerate"])->ValueAsInt(); |
this->uiSamplerate = ((DeviceCreationParameterInt*)Parameters["SAMPLERATE"])->ValueAsInt(); |
299 |
this->FragmentSize = ((DeviceCreationParameterInt*)Parameters["fragmentsize"])->ValueAsInt(); |
this->FragmentSize = ((DeviceCreationParameterInt*)Parameters["FRAGMENTSIZE"])->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 |
|
|
406 |
pAlsaOutputBuffer = new int16_t[uiAlsaChannels * FragmentSize]; |
pAlsaOutputBuffer = new int16_t[uiAlsaChannels * FragmentSize]; |
407 |
|
|
408 |
// 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 |
409 |
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)); |
410 |
|
|
411 |
if (((DeviceCreationParameterBool*)Parameters["active"])->ValueAsBool()) { |
if (((DeviceCreationParameterBool*)Parameters["ACTIVE"])->ValueAsBool()) { |
412 |
Play(); |
Play(); |
413 |
} |
} |
414 |
} |
} |
421 |
//FIXME: currently commented out due to segfault |
//FIXME: currently commented out due to segfault |
422 |
//snd_pcm_close(pcm_handle); |
//snd_pcm_close(pcm_handle); |
423 |
|
|
|
// destroy all audio channels |
|
|
for (int c = 0; c < Channels.size(); c++) delete Channels[c]; |
|
|
|
|
424 |
if (pAlsaOutputBuffer) { |
if (pAlsaOutputBuffer) { |
425 |
//FIXME: currently commented out due to segfault |
//FIXME: currently commented out due to segfault |
426 |
//delete[] pOutputBuffer; |
//delete[] pOutputBuffer; |
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); |
491 |
StopThread(); |
StopThread(); |
492 |
} |
} |
493 |
|
|
494 |
void AudioOutputDeviceAlsa::AcquireChannels(uint Channels) { |
AudioChannel* AudioOutputDeviceAlsa::CreateChannel(uint ChannelNr) { |
495 |
if (Channels > uiAlsaChannels) { |
// just create a mix channel |
496 |
// 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); |
|
|
} |
|
|
} |
|
497 |
} |
} |
498 |
|
|
499 |
uint AudioOutputDeviceAlsa::MaxSamplesPerCycle() { |
uint AudioOutputDeviceAlsa::MaxSamplesPerCycle() { |
505 |
} |
} |
506 |
|
|
507 |
String AudioOutputDeviceAlsa::Name() { |
String AudioOutputDeviceAlsa::Name() { |
508 |
return "Alsa"; |
return "ALSA"; |
509 |
} |
} |
510 |
|
|
511 |
String AudioOutputDeviceAlsa::Driver() { |
String AudioOutputDeviceAlsa::Driver() { |
517 |
} |
} |
518 |
|
|
519 |
String AudioOutputDeviceAlsa::Version() { |
String AudioOutputDeviceAlsa::Version() { |
520 |
String s = "$Revision: 1.12 $"; |
String s = "$Revision: 1.20 $"; |
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 |
|
|
568 |
} |
} |
569 |
|
|
570 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |
|
|
|