本文整理汇总了C++中snd_pcm_hw_params_set_buffer_size_near函数的典型用法代码示例。如果您正苦于以下问题:C++ snd_pcm_hw_params_set_buffer_size_near函数的具体用法?C++ snd_pcm_hw_params_set_buffer_size_near怎么用?C++ snd_pcm_hw_params_set_buffer_size_near使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snd_pcm_hw_params_set_buffer_size_near函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: rdpsnd_alsa_set_params
static void rdpsnd_alsa_set_params(rdpsndAlsaPlugin* alsa)
{
snd_pcm_hw_params_t* hw_params;
snd_pcm_sw_params_t* sw_params;
int error;
snd_pcm_uframes_t frames;
snd_pcm_uframes_t start_threshold;
snd_pcm_drop(alsa->out_handle);
error = snd_pcm_hw_params_malloc(&hw_params);
if (error < 0)
{
DEBUG_WARN("snd_pcm_hw_params_malloc failed");
return;
}
snd_pcm_hw_params_any(alsa->out_handle, hw_params);
snd_pcm_hw_params_set_access(alsa->out_handle, hw_params,
SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(alsa->out_handle, hw_params,
alsa->format);
snd_pcm_hw_params_set_rate_near(alsa->out_handle, hw_params,
&alsa->actual_rate, NULL);
snd_pcm_hw_params_set_channels_near(alsa->out_handle, hw_params,
&alsa->actual_channels);
if (alsa->latency < 0)
frames = alsa->actual_rate * 4; /* Default to 4-second buffer */
else
frames = alsa->latency * alsa->actual_rate * 2 / 1000; /* Double of the latency */
if (frames < alsa->actual_rate / 2)
frames = alsa->actual_rate / 2; /* Minimum 0.5-second buffer */
snd_pcm_hw_params_set_buffer_size_near(alsa->out_handle, hw_params,
&frames);
snd_pcm_hw_params(alsa->out_handle, hw_params);
snd_pcm_hw_params_free(hw_params);
error = snd_pcm_sw_params_malloc(&sw_params);
if (error < 0)
{
DEBUG_WARN("snd_pcm_sw_params_malloc failed");
return;
}
snd_pcm_sw_params_current(alsa->out_handle, sw_params);
if (alsa->latency == 0)
start_threshold = 0;
else
start_threshold = frames / 2;
snd_pcm_sw_params_set_start_threshold(alsa->out_handle, sw_params, start_threshold);
snd_pcm_sw_params(alsa->out_handle, sw_params);
snd_pcm_sw_params_free(sw_params);
snd_pcm_prepare(alsa->out_handle);
DEBUG_SVC("hardware buffer %d frames, playback buffer %.2g seconds",
(int)frames, (double)frames / 2.0 / (double)alsa->actual_rate);
if ((alsa->actual_rate != alsa->source_rate) ||
(alsa->actual_channels != alsa->source_channels))
{
DEBUG_SVC("actual rate %d / channel %d is different from source rate %d / channel %d, resampling required.",
alsa->actual_rate, alsa->actual_channels, alsa->source_rate, alsa->source_channels);
}
}
示例2: drvHostALSAAudioOpen
//.........这里部分代码省略.........
rc = VERR_GENERAL_FAILURE; /** @todo Find a better rc. */
break;
}
}
/* Calculate default buffer size here since it might have been changed
* in the _near functions */
buffer_size_f = 4 * period_size_f;
minval = buffer_size_f;
err = snd_pcm_hw_params_get_buffer_size_min(pHWParms, &minval);
if (err < 0)
{
LogRel(("ALSA: Could not retrieve minimal buffer size\n"));
rc = VERR_GENERAL_FAILURE; /** @todo Find a better rc. */
break;
}
else
{
LogFunc(("Minimal buffer size is: %RU32\n", minval));
if (buffer_size_f < minval)
{
if ( ( fIn && s_ALSAConf.buffer_size_in_overriden)
|| (!fIn && s_ALSAConf.buffer_size_out_overriden))
{
LogFunc(("Buffer size %RU32 is less than minimal buffer size %RU32\n",
buffer_size_f, minval));
}
buffer_size_f = minval;
}
}
err = snd_pcm_hw_params_set_buffer_size_near(phPCM,
pHWParms, &buffer_size_f);
LogFunc(("Buffer size is: %RU32\n", buffer_size_f));
if (err < 0)
{
LogRel(("ALSA: Failed to set buffer size %d: %s\n",
buffer_size_f, snd_strerror(err)));
rc = VERR_GENERAL_FAILURE; /** @todo Find a better rc. */
break;
}
}
}
else
LogFunc(("Warning: Buffer size is not set\n"));
err = snd_pcm_hw_params(phPCM, pHWParms);
if (err < 0)
{
LogRel(("ALSA: Failed to apply audio parameters\n"));
rc = VERR_GENERAL_FAILURE; /** @todo Find a better rc. */
break;
}
err = snd_pcm_hw_params_get_buffer_size(pHWParms, &obt_buffer_size);
if (err < 0)
{
LogRel(("ALSA: Failed to get buffer size\n"));
rc = VERR_GENERAL_FAILURE; /** @todo Find a better rc. */
break;
}
snd_pcm_uframes_t obt_period_size;
int dir = 0;
示例3: printf
eBool tfAlsa::openDevice(const eChar *device, eU32 freq, eU32 bits, eU32 channels)
{
eInt err = 0;
if (device == eNULL)
return eFALSE;
printf ("opening device '%s' for playback\n", device);
if ((err = snd_pcm_open (&m_handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
{
fprintf (stderr, "cannot open audio device '%s' for playback(%s)\n", device, snd_strerror (err));
return eFALSE;
}
snd_pcm_hw_params_t *hw_params;
snd_pcm_format_t format;
snd_pcm_uframes_t buffer_size = BUFFERSIZE;
snd_pcm_uframes_t period_size = PERIODSIZE;
printf("setting device to %i hz %i bits %i channels\n", freq, bits, channels);
if (bits == 16)
format = SND_PCM_FORMAT_S16_LE;
else
format = SND_PCM_FORMAT_S8;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
{
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err));
return false;
}
if ((err = snd_pcm_hw_params_any (m_handle, hw_params)) < 0)
{
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err));
return false;
}
if ((err = snd_pcm_hw_params_set_access (m_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{
fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err));
return false;
}
if ((err = snd_pcm_hw_params_set_format (m_handle, hw_params, format)) < 0)
{
fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err));
return false;
}
if ((err = snd_pcm_hw_params_set_rate_near (m_handle, hw_params, (unsigned int *)&freq, 0)) < 0)
{
fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err));
return false;
}
if ((err = snd_pcm_hw_params_set_channels (m_handle, hw_params, channels)) < 0)
{
fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err));
return false;
}
if ((err = snd_pcm_hw_params_set_buffer_size_near (m_handle, hw_params, &buffer_size)) < 0)
{
fprintf (stderr, "cannot set buffer size (%s)\n", snd_strerror (err));
return false;
}
if ((err = snd_pcm_hw_params_set_period_size_near (m_handle, hw_params, &period_size, NULL)) < 0)
{
fprintf (stderr, "cannot set period size (%s)\n", snd_strerror (err));
return false;
}
if ((err = snd_pcm_hw_params (m_handle, hw_params)) < 0)
{
fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err));
return false;
}
snd_pcm_hw_params_free (hw_params);
return eTRUE;
}
示例4: sound_init_alsa
int sound_init_alsa() {
int err;
snd_pcm_hw_params_t *hw_params;
unsigned int resample,samplerate;
snd_pcm_uframes_t bufferSize;
err = snd_pcm_open( &_soundDevice, "plughw:0,0", SND_PCM_STREAM_PLAYBACK, 0 );
if (err<0) {
return -1;
}
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
snd_pcm_close (_soundDevice);
return -2;
}
if ((err = snd_pcm_hw_params_any (_soundDevice, hw_params)) < 0) {
snd_pcm_close (_soundDevice);
return -2;
}
if ((err = snd_pcm_hw_params_set_access (_soundDevice, hw_params,SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
snd_pcm_close (_soundDevice);
return -3;
}
if (snd_pcm_hw_params_set_format (_soundDevice, hw_params,SND_PCM_FORMAT_U8) >= 0) {
ordenador.sign=0;
ordenador.format=0;
} else if (snd_pcm_hw_params_set_format (_soundDevice, hw_params,SND_PCM_FORMAT_S8) >= 0) {
ordenador.sign=-128;
ordenador.format=0;
} else if (snd_pcm_hw_params_set_format (_soundDevice, hw_params,SND_PCM_FORMAT_U16_LE) >= 0) {
ordenador.sign=0;
ordenador.format=1;
} else if (snd_pcm_hw_params_set_format (_soundDevice, hw_params,SND_PCM_FORMAT_S16_LE) >= 0) {
ordenador.sign=-128;
ordenador.format=1;
} else if (snd_pcm_hw_params_set_format (_soundDevice, hw_params,SND_PCM_FORMAT_U16_BE) >= 0) {
ordenador.sign=0;
ordenador.format=2;
} else if (snd_pcm_hw_params_set_format (_soundDevice, hw_params,SND_PCM_FORMAT_S16_BE) >= 0) {
ordenador.sign=-128;
ordenador.format=2;
} else {
snd_pcm_close (_soundDevice);
return -3;
}
// Disable resampling.
resample = 0;
err = snd_pcm_hw_params_set_rate_resample(_soundDevice, hw_params, resample);
if (err < 0) {
snd_pcm_close (_soundDevice);
return -3;
}
if ((err = snd_pcm_hw_params_set_channels (_soundDevice, hw_params, 1)) >= 0) {
ordenador.channels=1;
} else if ((err = snd_pcm_hw_params_set_channels (_soundDevice, hw_params, 2)) >= 0) {
ordenador.channels=2;
} else {
snd_pcm_close (_soundDevice);
return -3;
}
samplerate=48000;
if ((err = snd_pcm_hw_params_set_rate_near (_soundDevice, hw_params, &samplerate, 0)) < 0) {
snd_pcm_close (_soundDevice);
return -3;
}
bufferSize=4096;
if (snd_pcm_hw_params_set_buffer_size_near(_soundDevice, hw_params, &bufferSize) < 0) {
fprintf(stderr, "Error setting buffersize.\n");
return -3;
}
ordenador.freq=samplerate;
err = snd_pcm_hw_params (_soundDevice, hw_params);
if (err<0) {
return -3;
}
//snd_pcm_hw_params_get_buffer_size( hw_params, &bufferSize );
ordenador.buffer_len=bufferSize;
started_sound=0;
return 0;
err = snd_pcm_prepare (_soundDevice);
if (err<0) {
return -5;
}
return 0;
}
示例5: set_params
static void set_params(void)
{
snd_pcm_hw_params_t *params;
snd_pcm_sw_params_t *swparams;
snd_pcm_uframes_t buffer_size;
int err;
size_t n;
snd_pcm_uframes_t xfer_align;
unsigned int rate;
snd_pcm_uframes_t start_threshold, stop_threshold;
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_sw_params_alloca(&swparams);
err = snd_pcm_hw_params_any(handle, params);
if (err < 0) {
error(_("Broken configuration for this PCM: no configurations available"));
exit(EXIT_FAILURE);
}
err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) {
error(_("Access type not available"));
exit(EXIT_FAILURE);
}
err = snd_pcm_hw_params_set_format(handle, params, hwparams.format);
if (err < 0) {
error(_("Sample format non available"));
exit(EXIT_FAILURE);
}
err = snd_pcm_hw_params_set_channels(handle, params, hwparams.channels);
if (err < 0) {
error(_("Channels count non available"));
exit(EXIT_FAILURE);
}
#if 0
err = snd_pcm_hw_params_set_periods_min(handle, params, 2);
assert(err >= 0);
#endif
rate = hwparams.rate;
err = snd_pcm_hw_params_set_rate_near(handle, params, &hwparams.rate, 0);
assert(err >= 0);
if ((float)rate * 1.05 < hwparams.rate || (float)rate * 0.95 > hwparams.rate) {
if (!quiet_mode) {
char plugex[64];
const char *pcmname = snd_pcm_name(handle);
fprintf(stderr, _("Warning: rate is not accurate (requested = %iHz, got = %iHz)\n"), rate, hwparams.rate);
if (! pcmname || strchr(snd_pcm_name(handle), ':'))
*plugex = 0;
else
snprintf(plugex, sizeof(plugex), "(-Dplug:%s)",
snd_pcm_name(handle));
fprintf(stderr, _(" please, try the plug plugin %s\n"),
plugex);
}
}
rate = hwparams.rate;
if (buffer_time == 0 && buffer_frames == 0) {
err = snd_pcm_hw_params_get_buffer_time_max(params,
&buffer_time, 0);
assert(err >= 0);
if (buffer_time > 500000)
buffer_time = 500000;
}
if (period_time == 0 && period_frames == 0) {
if (buffer_time > 0)
period_time = buffer_time / 4;
else
period_frames = buffer_frames / 4;
}
if (period_time > 0)
err = snd_pcm_hw_params_set_period_time_near(handle, params,
&period_time, 0);
else
err = snd_pcm_hw_params_set_period_size_near(handle, params,
&period_frames, 0);
assert(err >= 0);
if (buffer_time > 0) {
err = snd_pcm_hw_params_set_buffer_time_near(handle, params,
&buffer_time, 0);
} else {
err = snd_pcm_hw_params_set_buffer_size_near(handle, params,
&buffer_frames);
}
assert(err >= 0);
err = snd_pcm_hw_params(handle, params);
if (err < 0) {
error(_("Unable to install hw params:"));
snd_pcm_hw_params_dump(params, log);
exit(EXIT_FAILURE);
}
snd_pcm_hw_params_get_period_size(params, &chunk_size, 0);
snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
if (chunk_size == buffer_size) {
error(_("Can't use period equal to buffer size (%lu == %lu)"),
chunk_size, buffer_size);
exit(EXIT_FAILURE);
}
snd_pcm_sw_params_current(handle, swparams);
err = snd_pcm_sw_params_get_xfer_align(swparams, &xfer_align);
if (err < 0) {
error(_("Unable to obtain xfer align\n"));
//.........这里部分代码省略.........
示例6: GLOBAL_DEF
Error AudioDriverALSA::init_device() {
mix_rate = GLOBAL_DEF("audio/mix_rate", DEFAULT_MIX_RATE);
speaker_mode = SPEAKER_MODE_STEREO;
channels = 2;
// If there is a specified device check that it is really present
if (device_name != "Default") {
Array list = get_device_list();
if (list.find(device_name) == -1) {
device_name = "Default";
new_device = "Default";
}
}
int status;
snd_pcm_hw_params_t *hwparams;
snd_pcm_sw_params_t *swparams;
#define CHECK_FAIL(m_cond) \
if (m_cond) { \
fprintf(stderr, "ALSA ERR: %s\n", snd_strerror(status)); \
if (pcm_handle) { \
snd_pcm_close(pcm_handle); \
pcm_handle = NULL; \
} \
ERR_FAIL_COND_V(m_cond, ERR_CANT_OPEN); \
}
//todo, add
//6 chans - "plug:surround51"
//4 chans - "plug:surround40";
if (device_name == "Default") {
status = snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
} else {
String device = device_name;
int pos = device.find(";");
if (pos != -1) {
device = device.substr(0, pos);
}
status = snd_pcm_open(&pcm_handle, device.utf8().get_data(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
}
ERR_FAIL_COND_V(status < 0, ERR_CANT_OPEN);
snd_pcm_hw_params_alloca(&hwparams);
status = snd_pcm_hw_params_any(pcm_handle, hwparams);
CHECK_FAIL(status < 0);
status = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
CHECK_FAIL(status < 0);
//not interested in anything else
status = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE);
CHECK_FAIL(status < 0);
//todo: support 4 and 6
status = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, 2);
CHECK_FAIL(status < 0);
status = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &mix_rate, NULL);
CHECK_FAIL(status < 0);
// In ALSA the period size seems to be the one that will determine the actual latency
// Ref: https://www.alsa-project.org/main/index.php/FramesPeriods
unsigned int periods = 2;
int latency = GLOBAL_DEF("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
buffer_size = buffer_frames * periods;
period_size = buffer_frames;
// set buffer size from project settings
status = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size);
CHECK_FAIL(status < 0);
status = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &period_size, NULL);
CHECK_FAIL(status < 0);
if (OS::get_singleton()->is_stdout_verbose()) {
print_line("audio buffer frames: " + itos(period_size) + " calculated latency: " + itos(period_size * 1000 / mix_rate) + "ms");
}
status = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &periods, NULL);
CHECK_FAIL(status < 0);
status = snd_pcm_hw_params(pcm_handle, hwparams);
CHECK_FAIL(status < 0);
//snd_pcm_hw_params_free(&hwparams);
snd_pcm_sw_params_alloca(&swparams);
status = snd_pcm_sw_params_current(pcm_handle, swparams);
CHECK_FAIL(status < 0);
status = snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, period_size);
CHECK_FAIL(status < 0);
status = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1);
//.........这里部分代码省略.........
示例7: alloca
static snd_pcm_t *alsa_open(char *dev, int rate, int channels)
{
snd_pcm_hw_params_t *hwp;
snd_pcm_sw_params_t *swp;
snd_pcm_t *h;
int r;
int dir;
snd_pcm_uframes_t period_size_min;
snd_pcm_uframes_t period_size_max;
snd_pcm_uframes_t buffer_size_min;
snd_pcm_uframes_t buffer_size_max;
snd_pcm_uframes_t period_size;
snd_pcm_uframes_t buffer_size;
if ((r = snd_pcm_open(&h, dev, SND_PCM_STREAM_PLAYBACK, 0) < 0))
return NULL;
hwp = alloca(snd_pcm_hw_params_sizeof());
memset(hwp, 0, snd_pcm_hw_params_sizeof());
snd_pcm_hw_params_any(h, hwp);
snd_pcm_hw_params_set_access(h, hwp, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(h, hwp, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_rate(h, hwp, rate, 0);
snd_pcm_hw_params_set_channels(h, hwp, channels);
/* Configurue period */
dir = 0;
snd_pcm_hw_params_get_period_size_min(hwp, &period_size_min, &dir);
dir = 0;
snd_pcm_hw_params_get_period_size_max(hwp, &period_size_max, &dir);
period_size = 1024;
dir = 0;
r = snd_pcm_hw_params_set_period_size_near(h, hwp, &period_size, &dir);
if (r < 0) {
fprintf(stderr, "audio: Unable to set period size %lu (%s)\n",
period_size, snd_strerror(r));
snd_pcm_close(h);
return NULL;
}
dir = 0;
r = snd_pcm_hw_params_get_period_size(hwp, &period_size, &dir);
if (r < 0) {
fprintf(stderr, "audio: Unable to get period size (%s)\n",
snd_strerror(r));
snd_pcm_close(h);
return NULL;
}
/* Configurue buffer size */
snd_pcm_hw_params_get_buffer_size_min(hwp, &buffer_size_min);
snd_pcm_hw_params_get_buffer_size_max(hwp, &buffer_size_max);
buffer_size = period_size * 4;
dir = 0;
r = snd_pcm_hw_params_set_buffer_size_near(h, hwp, &buffer_size);
if (r < 0) {
fprintf(stderr, "audio: Unable to set buffer size %lu (%s)\n",
buffer_size, snd_strerror(r));
snd_pcm_close(h);
return NULL;
}
r = snd_pcm_hw_params_get_buffer_size(hwp, &buffer_size);
if (r < 0) {
fprintf(stderr, "audio: Unable to get buffer size (%s)\n",
snd_strerror(r));
snd_pcm_close(h);
return NULL;
}
/* write the hw params */
r = snd_pcm_hw_params(h, hwp);
if (r < 0) {
fprintf(stderr, "audio: Unable to configure hardware parameters (%s)\n",
snd_strerror(r));
snd_pcm_close(h);
return NULL;
}
/*
* Software parameters
*/
swp = alloca(snd_pcm_sw_params_sizeof());
memset(hwp, 0, snd_pcm_sw_params_sizeof());
snd_pcm_sw_params_current(h, swp);
r = snd_pcm_sw_params_set_avail_min(h, swp, period_size);
//.........这里部分代码省略.........
示例8: alsaio_setup
static int alsaio_setup(t_alsa_dev *dev, int out, int *channels, int *rate,
int nfrags, int frag_size)
{
int bufsizeforthis, err;
snd_pcm_hw_params_t* hw_params;
unsigned int tmp_uint;
snd_pcm_uframes_t tmp_snd_pcm_uframes;
if (sys_verbose)
{
if (out)
post("configuring sound output...");
else post("configuring sound input...");
}
/* set hardware parameters... */
snd_pcm_hw_params_alloca(&hw_params);
/* get the default params */
err = snd_pcm_hw_params_any(dev->a_handle, hw_params);
check_error(err, "snd_pcm_hw_params_any");
/* try to set interleaved access */
err = snd_pcm_hw_params_set_access(dev->a_handle,
hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0)
return (-1);
check_error(err, "snd_pcm_hw_params_set_access");
#if 0 /* enable this to print out which formats are available */
{
int i;
for (i = 0; i <= SND_PCM_FORMAT_LAST; i++)
fprintf(stderr, "%d -> %d\n",
i, snd_pcm_hw_params_test_format(dev->a_handle, hw_params, i));
}
#endif
/* Try to set 32 bit format first */
err = snd_pcm_hw_params_set_format(dev->a_handle,
hw_params, SND_PCM_FORMAT_S32);
if (err < 0)
{
/* fprintf(stderr,
"PD-ALSA: 32 bit format not available - trying 24\n"); */
err = snd_pcm_hw_params_set_format(dev->a_handle, hw_params,
SND_PCM_FORMAT_S24_3LE);
if (err < 0)
{
/* fprintf(stderr,
"PD-ALSA: 32/24 bit format not available - using 16\n"); */
err = snd_pcm_hw_params_set_format(dev->a_handle, hw_params,
SND_PCM_FORMAT_S16);
check_error(err, "snd_pcm_hw_params_set_format");
dev->a_sampwidth = 2;
}
else dev->a_sampwidth = 3;
}
else dev->a_sampwidth = 4;
if (sys_verbose)
post("Sample width set to %d bytes", dev->a_sampwidth);
/* set the subformat */
err = snd_pcm_hw_params_set_subformat(dev->a_handle,
hw_params, SND_PCM_SUBFORMAT_STD);
check_error(err, "snd_pcm_hw_params_set_subformat");
/* set the number of channels */
tmp_uint = *channels;
err = snd_pcm_hw_params_set_channels_min(dev->a_handle,
hw_params, &tmp_uint);
check_error(err, "snd_pcm_hw_params_set_channels");
if (tmp_uint != (unsigned)*channels)
post("ALSA: set %s channels to %d", (out?"output":"input"), tmp_uint);
*channels = tmp_uint;
dev->a_channels = *channels;
/* set the sampling rate */
err = snd_pcm_hw_params_set_rate_min(dev->a_handle, hw_params,
(unsigned int *)rate, 0);
check_error(err, "snd_pcm_hw_params_set_rate_min (input)");
#if 0
err = snd_pcm_hw_params_get_rate(hw_params, &subunitdir);
post("input sample rate %d", err);
#endif
/* post("frag size %d, nfrags %d", frag_size, nfrags); */
/* set "period size" */
#ifdef ALSAAPI9
err = snd_pcm_hw_params_set_period_size_near(dev->a_handle,
hw_params, (snd_pcm_uframes_t)frag_size, 0);
#else
tmp_snd_pcm_uframes = frag_size;
err = snd_pcm_hw_params_set_period_size_near(dev->a_handle,
hw_params, &tmp_snd_pcm_uframes, 0);
#endif
check_error(err, "snd_pcm_hw_params_set_period_size_near (input)");
/* set the buffer size */
#ifdef ALSAAPI9
err = snd_pcm_hw_params_set_buffer_size_near(dev->a_handle,
//.........这里部分代码省略.........
示例9: snd_pcm_hw_params_alloca
bool CAESinkALSA::InitializeHW(AEAudioFormat &format)
{
snd_pcm_hw_params_t *hw_params;
snd_pcm_hw_params_alloca(&hw_params);
memset(hw_params, 0, snd_pcm_hw_params_sizeof());
snd_pcm_hw_params_any(m_pcm, hw_params);
snd_pcm_hw_params_set_access(m_pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
unsigned int sampleRate = format.m_sampleRate;
unsigned int channelCount = format.m_channelLayout.Count();
snd_pcm_hw_params_set_rate_near (m_pcm, hw_params, &sampleRate, NULL);
snd_pcm_hw_params_set_channels_near(m_pcm, hw_params, &channelCount);
/* ensure we opened X channels or more */
if (format.m_channelLayout.Count() > channelCount)
{
CLog::Log(LOGERROR, "CAESinkALSA::InitializeHW - Unable to open the required number of channels");
return false;
}
/* update the channelLayout to what we managed to open */
format.m_channelLayout.Reset();
for (unsigned int i = 0; i < channelCount; ++i)
format.m_channelLayout += ALSAChannelMap[i];
snd_pcm_format_t fmt = AEFormatToALSAFormat(format.m_dataFormat);
if (fmt == SND_PCM_FORMAT_UNKNOWN)
{
/* if we dont support the requested format, fallback to float */
format.m_dataFormat = AE_FMT_FLOAT;
fmt = SND_PCM_FORMAT_FLOAT;
}
/* try the data format */
if (snd_pcm_hw_params_set_format(m_pcm, hw_params, fmt) < 0)
{
/* if the chosen format is not supported, try each one in decending order */
CLog::Log(LOGINFO, "CAESinkALSA::InitializeHW - Your hardware does not support %s, trying other formats", CAEUtil::DataFormatToStr(format.m_dataFormat));
for (enum AEDataFormat i = AE_FMT_MAX; i > AE_FMT_INVALID; i = (enum AEDataFormat)((int)i - 1))
{
if (AE_IS_RAW(i) || i == AE_FMT_MAX)
continue;
if (m_passthrough && i != AE_FMT_S16BE && i != AE_FMT_S16LE)
continue;
fmt = AEFormatToALSAFormat(i);
if (fmt == SND_PCM_FORMAT_UNKNOWN || snd_pcm_hw_params_set_format(m_pcm, hw_params, fmt) < 0)
{
fmt = SND_PCM_FORMAT_UNKNOWN;
continue;
}
int fmtBits = CAEUtil::DataFormatToBits(i);
int bits = snd_pcm_hw_params_get_sbits(hw_params);
if (bits != fmtBits)
{
/* if we opened in 32bit and only have 24bits, pack into 24 */
if (fmtBits == 32 && bits == 24)
i = AE_FMT_S24NE4;
else
continue;
}
/* record that the format fell back to X */
format.m_dataFormat = i;
CLog::Log(LOGINFO, "CAESinkALSA::InitializeHW - Using data format %s", CAEUtil::DataFormatToStr(format.m_dataFormat));
break;
}
/* if we failed to find a valid output format */
if (fmt == SND_PCM_FORMAT_UNKNOWN)
{
CLog::Log(LOGERROR, "CAESinkALSA::InitializeHW - Unable to find a suitable output format");
return false;
}
}
unsigned int periods;
snd_pcm_uframes_t periodSize, bufferSize;
snd_pcm_hw_params_get_buffer_size_max(hw_params, &bufferSize);
bufferSize = std::min(bufferSize, (snd_pcm_uframes_t)8192);
periodSize = bufferSize / ALSA_PERIODS;
periods = ALSA_PERIODS;
CLog::Log(LOGDEBUG, "CAESinkALSA::InitializeHW - Request: periodSize %lu, periods %u, bufferSize %lu", periodSize, periods, bufferSize);
/* work on a copy of the hw params */
snd_pcm_hw_params_t *hw_params_copy;
snd_pcm_hw_params_alloca(&hw_params_copy);
/* try to set the buffer size then the period size */
snd_pcm_hw_params_copy(hw_params_copy, hw_params);
snd_pcm_hw_params_set_buffer_size_near(m_pcm, hw_params_copy, &bufferSize);
snd_pcm_hw_params_set_period_size_near(m_pcm, hw_params_copy, &periodSize, NULL);
//.........这里部分代码省略.........
示例10: sndout_alsa_start
int sndout_alsa_start(int rate_, int stereo)
{
snd_pcm_hw_params_t *hwparams = NULL;
unsigned int rate = rate_;
int samples, shift;
int ret;
samples = rate * 40 / 1000;
for (shift = 8; (1 << shift) < samples; shift++)
;
period_size = 1 << shift;
buffer_size = 8 * period_size;
snd_pcm_hw_params_alloca(&hwparams);
ret = snd_pcm_hw_params_any(handle, hwparams);
ret |= snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
ret |= snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16_LE);
ret |= snd_pcm_hw_params_set_channels(handle, hwparams, stereo ? 2 : 1);
ret |= snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, 0);
ret |= snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer_size);
ret |= snd_pcm_hw_params_set_period_size_near(handle, hwparams, &period_size, NULL);
if (ret != 0) {
fprintf(stderr, PFX "failed to set hwparams\n");
goto fail;
}
ret = snd_pcm_hw_params(handle, hwparams);
if (ret != 0) {
fprintf(stderr, PFX "failed to apply hwparams: %d\n", ret);
goto fail;
}
snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
snd_pcm_hw_params_get_period_size(hwparams, &period_size, NULL);
snd_pcm_hw_params_get_channels(hwparams, &channels);
silent_period = realloc(silent_period, period_size * 2 * channels);
if (silent_period != NULL)
memset(silent_period, 0, period_size * 2 * channels);
ret = snd_pcm_prepare(handle);
if (ret != 0) {
fprintf(stderr, PFX "snd_pcm_prepare failed: %d\n", ret);
goto fail;
}
ret = snd_pcm_start(handle);
if (ret != 0) {
fprintf(stderr, PFX "snd_pcm_start failed: %d\n", ret);
goto fail;
}
failure_counter = 0;
return 0;
fail:
// to flush out redirected logs
fflush(stdout);
fflush(stderr);
return -1;
}
示例11: alsa_open
static snd_pcm_t *
alsa_open (int channels, unsigned samplerate, int realtime)
{ const char * device = "default" ;
snd_pcm_t *alsa_dev = NULL ;
snd_pcm_hw_params_t *hw_params ;
snd_pcm_uframes_t buffer_size ;
snd_pcm_uframes_t alsa_period_size, alsa_buffer_frames ;
snd_pcm_sw_params_t *sw_params ;
int err ;
if (realtime)
{ alsa_period_size = 256 ;
alsa_buffer_frames = 3 * alsa_period_size ;
}
else
{ alsa_period_size = 1024 ;
alsa_buffer_frames = 4 * alsa_period_size ;
} ;
if ((err = snd_pcm_open (&alsa_dev, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
{ fprintf (stderr, "cannot open audio device \"%s\" (%s)\n", device, snd_strerror (err)) ;
goto catch_error ;
} ;
snd_pcm_nonblock (alsa_dev, 0) ;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
{ fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_any (alsa_dev, hw_params)) < 0)
{ fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_access (alsa_dev, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{ fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_format (alsa_dev, hw_params, SND_PCM_FORMAT_FLOAT)) < 0)
{ fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_rate_near (alsa_dev, hw_params, &samplerate, 0)) < 0)
{ fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_channels (alsa_dev, hw_params, channels)) < 0)
{ fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_buffer_size_near (alsa_dev, hw_params, &alsa_buffer_frames)) < 0)
{ fprintf (stderr, "cannot set buffer size (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_period_size_near (alsa_dev, hw_params, &alsa_period_size, 0)) < 0)
{ fprintf (stderr, "cannot set period size (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params (alsa_dev, hw_params)) < 0)
{ fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
/* extra check: if we have only one period, this code won't work */
snd_pcm_hw_params_get_period_size (hw_params, &alsa_period_size, 0) ;
snd_pcm_hw_params_get_buffer_size (hw_params, &buffer_size) ;
if (alsa_period_size == buffer_size)
{ fprintf (stderr, "Can't use period equal to buffer size (%lu == %lu)", alsa_period_size, buffer_size) ;
goto catch_error ;
} ;
snd_pcm_hw_params_free (hw_params) ;
if ((err = snd_pcm_sw_params_malloc (&sw_params)) != 0)
{ fprintf (stderr, "%s: snd_pcm_sw_params_malloc: %s", __func__, snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_sw_params_current (alsa_dev, sw_params)) != 0)
{ fprintf (stderr, "%s: snd_pcm_sw_params_current: %s", __func__, snd_strerror (err)) ;
goto catch_error ;
} ;
/* note: set start threshold to delay start until the ring buffer is full */
snd_pcm_sw_params_current (alsa_dev, sw_params) ;
if ((err = snd_pcm_sw_params_set_start_threshold (alsa_dev, sw_params, buffer_size)) < 0)
{ fprintf (stderr, "cannot set start threshold (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
//.........这里部分代码省略.........
示例12: alsa_init
//.........这里部分代码省略.........
if (rc < 0)
{
device = "pulse";
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
}
if (rc < 0)
printf("ALSA: unable to automatically determine audio device.\n");
}
else {
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
}
if (rc < 0)
{
fprintf(stderr, "ALSA: unable to open PCM device %s: %s\n", device.c_str(), snd_strerror(rc));
return;
}
printf("ALSA: Successfully initialized \"%s\"\n", device.c_str());
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
rc=snd_pcm_hw_params_any(handle, params);
if (rc < 0)
{
fprintf(stderr, "ALSA: Error:snd_pcm_hw_params_any %s\n", snd_strerror(rc));
return;
}
/* Set the desired hardware parameters. */
/* Interleaved mode */
rc=snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (rc < 0)
{
fprintf(stderr, "ALSA: Error:snd_pcm_hw_params_set_access %s\n", snd_strerror(rc));
return;
}
/* Signed 16-bit little-endian format */
rc=snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
if (rc < 0)
{
fprintf(stderr, "ALSA: Error:snd_pcm_hw_params_set_format %s\n", snd_strerror(rc));
return;
}
/* Two channels (stereo) */
rc=snd_pcm_hw_params_set_channels(handle, params, 2);
if (rc < 0)
{
fprintf(stderr, "ALSA: Error:snd_pcm_hw_params_set_channels %s\n", snd_strerror(rc));
return;
}
/* 44100 bits/second sampling rate (CD quality) */
val = 44100;
rc=snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
if (rc < 0)
{
fprintf(stderr, "ALSA: Error:snd_pcm_hw_params_set_rate_near %s\n", snd_strerror(rc));
return;
}
/* Set period size to settings.aica.BufferSize frames. */
period_size = settings.aica.BufferSize;
rc=snd_pcm_hw_params_set_period_size_near(handle, params, &period_size, &dir);
if (rc < 0)
{
fprintf(stderr, "ALSA: Error:snd_pcm_hw_params_set_buffer_size_near %s\n", snd_strerror(rc));
return;
}
else
{
printf("ALSA: period size set to %ld\n", period_size);
}
buffer_size = (44100 * 100 /* settings.omx.Audio_Latency */ / 1000 / period_size + 1) * period_size;
rc=snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer_size);
if (rc < 0)
{
fprintf(stderr, "ALSA: Error:snd_pcm_hw_params_set_buffer_size_near %s\n", snd_strerror(rc));
return;
}
else
{
printf("ALSA: buffer size set to %ld\n", buffer_size);
}
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0)
{
fprintf(stderr, "ALSA: Unable to set hw parameters: %s\n", snd_strerror(rc));
return;
}
}
示例13: alsa_open_capture
static ALCenum alsa_open_capture(ALCdevice *Device, const ALCchar *deviceName)
{
const char *driver = NULL;
snd_pcm_hw_params_t *hp;
snd_pcm_uframes_t bufferSizeInFrames;
snd_pcm_uframes_t periodSizeInFrames;
ALboolean needring = AL_FALSE;
snd_pcm_format_t format;
const char *funcerr;
alsa_data *data;
int err;
if(deviceName)
{
size_t idx;
if(!allCaptureDevNameMap)
allCaptureDevNameMap = probe_devices(SND_PCM_STREAM_CAPTURE, &numCaptureDevNames);
for(idx = 0;idx < numCaptureDevNames;idx++)
{
if(strcmp(deviceName, allCaptureDevNameMap[idx].name) == 0)
{
driver = allCaptureDevNameMap[idx].device;
break;
}
}
if(idx == numCaptureDevNames)
return ALC_INVALID_VALUE;
}
else
{
deviceName = alsaDevice;
driver = GetConfigValue("alsa", "capture", "default");
}
data = (alsa_data*)calloc(1, sizeof(alsa_data));
err = snd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
if(err < 0)
{
ERR("Could not open capture device '%s': %s\n", driver, snd_strerror(err));
free(data);
return ALC_INVALID_VALUE;
}
format = -1;
switch(Device->FmtType)
{
case DevFmtByte:
format = SND_PCM_FORMAT_S8;
break;
case DevFmtUByte:
format = SND_PCM_FORMAT_U8;
break;
case DevFmtShort:
format = SND_PCM_FORMAT_S16;
break;
case DevFmtUShort:
format = SND_PCM_FORMAT_U16;
break;
case DevFmtInt:
format = SND_PCM_FORMAT_S32;
break;
case DevFmtUInt:
format = SND_PCM_FORMAT_U32;
break;
case DevFmtFloat:
format = SND_PCM_FORMAT_FLOAT;
break;
}
funcerr = NULL;
bufferSizeInFrames = maxu(Device->UpdateSize*Device->NumUpdates,
100*Device->Frequency/1000);
periodSizeInFrames = minu(bufferSizeInFrames, 25*Device->Frequency/1000);
snd_pcm_hw_params_malloc(&hp);
#define CHECK(x) if((funcerr=#x),(err=(x)) < 0) goto error
CHECK(snd_pcm_hw_params_any(data->pcmHandle, hp));
/* set interleaved access */
CHECK(snd_pcm_hw_params_set_access(data->pcmHandle, hp, SND_PCM_ACCESS_RW_INTERLEAVED));
/* set format (implicitly sets sample bits) */
CHECK(snd_pcm_hw_params_set_format(data->pcmHandle, hp, format));
/* set channels (implicitly sets frame bits) */
CHECK(snd_pcm_hw_params_set_channels(data->pcmHandle, hp, ChannelsFromDevFmt(Device->FmtChans)));
/* set rate (implicitly constrains period/buffer parameters) */
CHECK(snd_pcm_hw_params_set_rate(data->pcmHandle, hp, Device->Frequency, 0));
/* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
if(snd_pcm_hw_params_set_buffer_size_min(data->pcmHandle, hp, &bufferSizeInFrames) < 0)
{
TRACE("Buffer too large, using intermediate ring buffer\n");
needring = AL_TRUE;
CHECK(snd_pcm_hw_params_set_buffer_size_near(data->pcmHandle, hp, &bufferSizeInFrames));
}
/* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
CHECK(snd_pcm_hw_params_set_period_size_near(data->pcmHandle, hp, &periodSizeInFrames, NULL));
/* install and prepare hardware configuration */
CHECK(snd_pcm_hw_params(data->pcmHandle, hp));
/* retrieve configuration info */
//.........这里部分代码省略.........
示例14: printf
int AudioAlsa::setHWParams( const ch_cnt_t _channels, snd_pcm_access_t _access )
{
int err, dir;
// choose all parameters
if( ( err = snd_pcm_hw_params_any( m_handle, m_hwParams ) ) < 0 )
{
printf( "Broken configuration for playback: no configurations "
"available: %s\n", snd_strerror( err ) );
return err;
}
// set the interleaved read/write format
if( ( err = snd_pcm_hw_params_set_access( m_handle, m_hwParams,
_access ) ) < 0 )
{
printf( "Access type not available for playback: %s\n",
snd_strerror( err ) );
return err;
}
// set the sample format
if( ( snd_pcm_hw_params_set_format( m_handle, m_hwParams,
SND_PCM_FORMAT_S16_LE ) ) < 0 )
{
if( ( snd_pcm_hw_params_set_format( m_handle, m_hwParams,
SND_PCM_FORMAT_S16_BE ) ) < 0 )
{
printf( "Neither little- nor big-endian available for "
"playback: %s\n", snd_strerror( err ) );
return err;
}
m_convertEndian = isLittleEndian();
}
else
{
m_convertEndian = !isLittleEndian();
}
// set the count of channels
if( ( err = snd_pcm_hw_params_set_channels( m_handle, m_hwParams,
_channels ) ) < 0 )
{
printf( "Channel count (%i) not available for playbacks: %s\n"
"(Does your soundcard not support surround?)\n",
_channels, snd_strerror( err ) );
return err;
}
// set the sample rate
if( ( err = snd_pcm_hw_params_set_rate( m_handle, m_hwParams,
sampleRate(), 0 ) ) < 0 )
{
if( ( err = snd_pcm_hw_params_set_rate( m_handle, m_hwParams,
mixer()->baseSampleRate(), 0 ) ) < 0 )
{
printf( "Could not set sample rate: %s\n",
snd_strerror( err ) );
return err;
}
}
m_periodSize = mixer()->framesPerPeriod();
m_bufferSize = m_periodSize * 8;
dir = 0;
err = snd_pcm_hw_params_set_period_size_near( m_handle, m_hwParams,
&m_periodSize, &dir );
if( err < 0 )
{
printf( "Unable to set period size %lu for playback: %s\n",
m_periodSize, snd_strerror( err ) );
return err;
}
dir = 0;
err = snd_pcm_hw_params_get_period_size( m_hwParams, &m_periodSize,
&dir );
if( err < 0 )
{
printf( "Unable to get period size for playback: %s\n",
snd_strerror( err ) );
}
dir = 0;
err = snd_pcm_hw_params_set_buffer_size_near( m_handle, m_hwParams,
&m_bufferSize );
if( err < 0 )
{
printf( "Unable to set buffer size %lu for playback: %s\n",
m_bufferSize, snd_strerror( err ) );
return ( err );
}
err = snd_pcm_hw_params_get_buffer_size( m_hwParams, &m_bufferSize );
if( 2 * m_periodSize > m_bufferSize )
{
printf( "buffer to small, could not use\n" );
return ( err );
}
//.........这里部分代码省略.........
示例15: initialize_device
static int initialize_device(struct audio_info_struct *ai)
{
snd_pcm_hw_params_t *hw;
int i;
snd_pcm_format_t format;
unsigned int rate;
snd_pcm_uframes_t buffer_size;
snd_pcm_uframes_t period_size;
snd_pcm_sw_params_t *sw;
snd_pcm_uframes_t boundary;
snd_pcm_hw_params_alloca(&hw);
if (snd_pcm_hw_params_any(ai->handle, hw) < 0) {
fprintf(stderr, "initialize_device(): no configuration available\n");
return -1;
}
if (snd_pcm_hw_params_set_access(ai->handle, hw, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
fprintf(stderr, "initialize_device(): device does not support interleaved access\n");
return -1;
}
format = SND_PCM_FORMAT_UNKNOWN;
for (i = 0; i < NUM_FORMATS; ++i) {
if (ai->format == format_map[i].mpg123) {
format = format_map[i].alsa;
break;
}
}
if (format == SND_PCM_FORMAT_UNKNOWN) {
fprintf(stderr, "initialize_device(): invalid sample format %d\n", ai->format);
errno = EINVAL;
return -1;
}
if (snd_pcm_hw_params_set_format(ai->handle, hw, format) < 0) {
fprintf(stderr, "initialize_device(): cannot set format %s\n", snd_pcm_format_name(format));
return -1;
}
if (snd_pcm_hw_params_set_channels(ai->handle, hw, ai->channels) < 0) {
fprintf(stderr, "initialize_device(): cannot set %d channels\n", ai->channels);
return -1;
}
rate = ai->rate;
if (snd_pcm_hw_params_set_rate_near(ai->handle, hw, &rate, NULL) < 0) {
fprintf(stderr, "initialize_device(): cannot set rate %u\n", rate);
return -1;
}
if (!rates_match(ai->rate, rate)) {
fprintf(stderr, "initialize_device(): rate %ld not available, using %u\n", ai->rate, rate);
/* return -1; */
}
buffer_size = rate * BUFFER_LENGTH;
if (snd_pcm_hw_params_set_buffer_size_near(ai->handle, hw, &buffer_size) < 0) {
fprintf(stderr, "initialize_device(): cannot set buffer size\n");
return -1;
}
period_size = buffer_size / 4;
if (snd_pcm_hw_params_set_period_size_near(ai->handle, hw, &period_size, NULL) < 0) {
fprintf(stderr, "initialize_device(): cannot set period size\n");
return -1;
}
if (snd_pcm_hw_params(ai->handle, hw) < 0) {
fprintf(stderr, "initialize_device(): cannot set hw params\n");
return -1;
}
snd_pcm_sw_params_alloca(&sw);
if (snd_pcm_sw_params_current(ai->handle, sw) < 0) {
fprintf(stderr, "initialize_device(): cannot get sw params\n");
return -1;
}
/* start playing after the first write */
if (snd_pcm_sw_params_set_start_threshold(ai->handle, sw, 1) < 0) {
fprintf(stderr, "initialize_device(): cannot set start threshold\n");
return -1;
}
if (snd_pcm_sw_params_get_boundary(sw, &boundary) < 0) {
fprintf(stderr, "initialize_device(): cannot get boundary\n");
return -1;
}
/* never stop on underruns */
if (snd_pcm_sw_params_set_stop_threshold(ai->handle, sw, boundary) < 0) {
fprintf(stderr, "initialize_device(): cannot set stop threshold\n");
return -1;
}
/* wake up on every interrupt */
if (snd_pcm_sw_params_set_avail_min(ai->handle, sw, 1) < 0) {
fprintf(stderr, "initialize_device(): cannot set min avail\n");
return -1;
}
#if 0
/* always write as many frames as possible */
if (snd_pcm_sw_params_set_xfer_align(ai->handle, sw, 1) < 0) {
fprintf(stderr, "initialize_device(): cannot set transfer alignment\n");
return -1;
}
#endif
/* play silence when there is an underrun */
if (snd_pcm_sw_params_set_silence_size(ai->handle, sw, boundary) < 0) {
fprintf(stderr, "initialize_device(): cannot set silence size\n");
return -1;
}
//.........这里部分代码省略.........