Skip to content

Instantly share code, notes, and snippets.

@keedi
Created May 13, 2010 11:09
Show Gist options
  • Save keedi/399725 to your computer and use it in GitHub Desktop.
Save keedi/399725 to your computer and use it in GitHub Desktop.
static int
audio_linux_set_hwparams (AudioLinux * al)
{
snd_pcm_hw_params_t *params;
unsigned int rrate;
int err, dir;
snd_pcm_hw_params_malloc (&params);
/* choose all parameters */
err = snd_pcm_hw_params_any (al->handle, params);
if (err < 0)
{
log_warning (NULL,
"broken configuration, no configurations available: %s",
snd_strerror (err));
goto return_of_jedi;
}
/* set hardware resampling */
err =
snd_pcm_hw_params_set_rate_resample (al->handle, params,
audio_linux_resample);
if (err < 0)
{
log_warning (NULL, "resampling setup failed: %s", snd_strerror (err));
goto return_of_jedi;
}
/* set the interleaved read/write format */
err =
snd_pcm_hw_params_set_access (al->handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0)
{
log_warning (NULL, "access type not available: %s", snd_strerror (err));
goto return_of_jedi;
}
/* set the sample format */
err = snd_pcm_hw_params_set_format (al->handle, params, al->format);
if (err < 0)
{
log_warning (NULL, "sample format not available: %s",
snd_strerror (err));
goto return_of_jedi;
}
/* set the count of channels */
err = snd_pcm_hw_params_set_channels (al->handle, params, al->channels);
if (err < 0)
{
log_warning (NULL, "channels count (%i) not available: %s",
al->channels, snd_strerror (err));
goto return_of_jedi;
}
/* set the stream rate */
rrate = al->sample_rate;
err = snd_pcm_hw_params_set_rate_near (al->handle, params, &rrate, 0);
if (err < 0)
{
log_warning (NULL, "rate %iHz not available: %s", al->sample_rate,
snd_strerror (err));
goto return_of_jedi;
}
if (rrate != al->sample_rate)
{
log_warning (NULL, "rate doesn't match (requested %iHz, get %iHz)",
al->sample_rate, rrate);
err = -EINVAL;
goto return_of_jedi;
}
/* set the buffer time */
err =
snd_pcm_hw_params_set_buffer_time_near (al->handle, params,
&audio_linux_buffer_time, &dir);
if (err < 0)
{
log_warning (NULL, "unable to set buffer time %i: %s",
audio_linux_buffer_time, snd_strerror (err));
goto return_of_jedi;
}
/* set the period time */
err =
snd_pcm_hw_params_set_period_time_near (al->handle, params,
&audio_linux_period_time, &dir);
if (err < 0)
{
log_warning (NULL, "unable to set period time %i: %s",
audio_linux_period_time, snd_strerror (err));
goto return_of_jedi;
}
/* write the parameters to the device */
err = snd_pcm_hw_params (al->handle, params);
if (err < 0)
{
log_warning (NULL, "unable to set hw params: %s", snd_strerror (err));
goto return_of_jedi;
}
return_of_jedi:
snd_pcm_hw_params_free (params);
return err;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment