There are possibilities to do this in the asoundrc
config file, but I need an application-level solution without the need to restart the app. So far, I've had this line of code in the init part of my app:
snd_pcm_hw_params_set_channels (cd::output_handle, cd::output_params, (unsigned int) 2);
The audio data stream comes from the CD drive, so that it always features 2 channels (stereo). Can I simply set the channel count to 1 on the fly? Thank you.
You can change the hw parameters only when the PCM stream is not running. See: https://www.alsa-project.org/alsa-doc/alsa-lib/pcm.html. You need to fill first the snd_pcm_hw_params_t
structure with the desired configuration, and then apply this configuration with snd_pcm_hw_params()
. For instance:
int err;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(¶ms);
err = snd_pcm_open(&handle, "default" //, ...
//...
err = snd_pcm_hw_params_any(handle, params);
//...
if((err = snd_pcm_hw_params_set_channels(handle, params, 2)) == 0) {
err = snd_pcm_hw_params(handle, params);
}