本文整理汇总了C++中snd_pcm_hw_params_set_buffer_time_near函数的典型用法代码示例。如果您正苦于以下问题:C++ snd_pcm_hw_params_set_buffer_time_near函数的具体用法?C++ snd_pcm_hw_params_set_buffer_time_near怎么用?C++ snd_pcm_hw_params_set_buffer_time_near使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snd_pcm_hw_params_set_buffer_time_near函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: set_hwparams
static
int set_hwparams(snd_pcm_t *handle,
snd_pcm_hw_params_t *params,
snd_pcm_access_t access)
{
int err, dir;
/* choose all parameters */
err = snd_pcm_hw_params_any(handle,params);
if (err < 0) {
printf("Access type not available for playback: %s\n", snd_strerror(err));
return err;
}
/* set the sample format */
err = snd_pcm_hw_params_set_format(handle, params, alsa_format);
if (err < 0) {
printf("Sample format not available for playback: %s\n", snd_strerror(err));
return err;
}
/* set the count of channels */
err = snd_pcm_hw_params_set_channels(handle, params, channels);
if (err < 0) {
printf("Channels count (%i) not available for playbacks: %s\n", channels, snd_strerror(err));
return err;
}
/* set the stream rate */
err = snd_pcm_hw_params_set_rate_near(handle, params, rate, 0);
if (err < 0) {
printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
return err;
}
if (err != rate) {
printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
return -EINVAL;
}
/* set buffer time */
err = snd_pcm_hw_params_set_buffer_time_near(handle, params, buffer_time, &dir);
if (err < 0) {
printf("Unable to set buffer time %i for playback: %s\n", buffer_time, snd_strerror(err));
return err;
}
buffer_size = snd_pcm_hw_params_get_buffer_size(params);
/* set period time */
err = snd_pcm_hw_params_set_period_time_near(handle, params, period_time, &dir);
if (err < 0) {
printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err));
return err;
}
period_size = snd_pcm_hw_params_get_period_size(params, &dir);
/* write the parameters to device */
err = snd_pcm_hw_params(handle, params);
if (err < 0) {
printf("Unable to set hw params for playback: %s\n", snd_strerror(err));
return err;
}
return 0;
}
开发者ID:Jay-Jay-OPL,项目名称:ps2sdk-ports,代码行数:57,代码来源:audio_alsa.c
示例2: set_snd_pcm_hw_params
static bool set_snd_pcm_hw_params(snd_pcm_t *snd, snd_pcm_hw_params_t *params, snd_pcm_format_t fmt, unsigned &channels, unsigned &sample_rate, unsigned &delay_us)
{
const bool ok = !snd_pcm_hw_params_set_access(snd, params, SND_PCM_ACCESS_RW_INTERLEAVED) && !snd_pcm_hw_params_set_format(snd, params, fmt) && !snd_pcm_hw_params_set_channels_near(snd, params, &channels) && !snd_pcm_hw_params_set_rate_near(snd, params, &sample_rate, NULL);
if (ok)
{
unsigned period_us = delay_us >> 2;
return (!snd_pcm_hw_params_set_buffer_time_near(snd, params, &delay_us, NULL) && !snd_pcm_hw_params_set_period_time_near(snd, params, &period_us, NULL)) || (!snd_pcm_hw_params_set_period_time_near(snd, params, &period_us, NULL) && !snd_pcm_hw_params_set_buffer_time_near(snd, params, &delay_us, NULL));
}
return false;
}
开发者ID:mitya57,项目名称:QMPlay2,代码行数:10,代码来源:ALSAWriter.cpp
示例3: set_params
static void set_params(void)
{
hwparams.format=SND_PCM_FORMAT_S16_LE;
hwparams.channels=2;
hwparams.rate=44100;
snd_pcm_hw_params_t *params;
snd_pcm_sw_params_t *swparams;
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_sw_params_alloca(&swparams);
snd_pcm_hw_params_any(handle, params);
snd_pcm_hw_params_set_format(handle, params, hwparams.format);
snd_pcm_hw_params_set_channels(handle, params, hwparams.channels);
snd_pcm_hw_params_set_rate_near(handle, params, &hwparams.rate, 0);
buffer_time=0;
snd_pcm_hw_params_get_buffer_time_max(params,&buffer_time, 0);
period_time=125000;
snd_pcm_hw_params_set_period_time_near(handle, params,&period_time, 0);
buffer_time = 500000;
snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, 0);
/*monotonic = */snd_pcm_hw_params_is_monotonic(params);
/*can_pause = */snd_pcm_hw_params_can_pause(params);
printf("sizeof(params) : %d\n",sizeof(params));
snd_pcm_hw_params(handle, params);
snd_pcm_uframes_t buffer_size;
snd_pcm_hw_params_get_period_size(params, &chunk_size, 0);
size_t n=chunk_size;
snd_pcm_sw_params_set_avail_min(handle, swparams, n);
snd_pcm_uframes_t start_threshold, stop_threshold;
start_threshold=22050;
snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
stop_threshold=22050;
snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
snd_pcm_format_physical_width(hwparams.format);
}
开发者ID:cty222,项目名称:Note,代码行数:51,代码来源:newPlayer1.c
示例4: init
static int init(struct xmp_context *ctx)
{
snd_pcm_hw_params_t *hwparams;
int ret;
char *token, **parm;
unsigned int channels, rate;
unsigned int btime = 2000000; /* 2s */
unsigned int ptime = 100000; /* 100ms */
char *card_name = "default";
struct xmp_options *o = &ctx->o;
parm_init();
chkparm1("buffer", btime = 1000 * strtoul(token, NULL, 0));
chkparm1("period", btime = 1000 * strtoul(token, NULL, 0));
chkparm1("card", card_name = token);
parm_end();
if ((ret = snd_pcm_open(&pcm_handle, card_name,
SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf(stderr, "Unable to initialize ALSA pcm device: %s\n",
snd_strerror(ret));
return XMP_ERR_DINIT;
}
channels = (o->outfmt & XMP_FMT_MONO) ? 1 : 2;
rate = o->freq;
snd_pcm_hw_params_alloca(&hwparams);
snd_pcm_hw_params_any(pcm_handle, hwparams);
snd_pcm_hw_params_set_access(pcm_handle, hwparams,
SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(pcm_handle, hwparams, to_fmt(o));
snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &rate, 0);
snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &channels);
snd_pcm_hw_params_set_buffer_time_near(pcm_handle, hwparams, &btime, 0);
snd_pcm_hw_params_set_period_time_near(pcm_handle, hwparams, &ptime, 0);
snd_pcm_nonblock(pcm_handle, 0);
if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0) {
fprintf(stderr, "Unable to set ALSA output parameters: %s\n",
snd_strerror(ret));
return XMP_ERR_DINIT;
}
if (prepare_driver() < 0)
return XMP_ERR_DINIT;
o->freq = rate;
return xmp_smix_on(ctx);
}
开发者ID:44kksharma,项目名称:AndEngineMODPlayerExtension,代码行数:51,代码来源:alsa.c
示例5: alsa_set_hwparams
static int alsa_set_hwparams()
{
snd_pcm_hw_params_t *hwp;
snd_pcm_sw_params_t *swp;
int dir = 1;
unsigned period_time;
snd_pcm_uframes_t buffer_size, period_size;
snd_pcm_hw_params_alloca(&hwp);
snd_pcm_sw_params_alloca(&swp);
// ALSA bug? If we request 44100 Hz, it rounds the value up to 48000...
alsa_hw.rate--;
if (alsa_error("hw_params_any", snd_pcm_hw_params_any(alsa_hw.handle, hwp))
|| alsa_error("hw_params_set_format", snd_pcm_hw_params_set_format(alsa_hw.handle, hwp, alsa_hw.format))
|| alsa_error("hw_params_set_channels",
snd_pcm_hw_params_set_channels(alsa_hw.handle, hwp, alsa_hw.num_channels))
|| alsa_error("hw_params_set_rate_near",
snd_pcm_hw_params_set_rate_near(alsa_hw.handle, hwp, &alsa_hw.rate, &dir))
|| alsa_error("hw_params_set_access",
snd_pcm_hw_params_set_access(alsa_hw.handle, hwp, SND_PCM_ACCESS_RW_INTERLEAVED))
|| alsa_error("hw_params_set_buffer_time_near",
snd_pcm_hw_params_set_buffer_time_near(alsa_hw.handle, hwp, &alsa_hw.buffer_time, 0)))
return -1;
/* How often to call our SIGIO handler (~40Hz) */
period_time = alsa_hw.buffer_time / 4;
if (alsa_error
("hw_params_set_period_time_near",
snd_pcm_hw_params_set_period_time_near(alsa_hw.handle, hwp, &period_time, &dir))
|| alsa_error("hw_params_get_buffer_size", snd_pcm_hw_params_get_buffer_size(hwp, &buffer_size))
|| alsa_error("hw_params_get_period_size", snd_pcm_hw_params_get_period_size(hwp, &period_size, 0))
|| alsa_error("hw_params", snd_pcm_hw_params(alsa_hw.handle, hwp)))
return -1;
snd_pcm_sw_params_current(alsa_hw.handle, swp);
if (alsa_error
("sw_params_set_start_threshold", snd_pcm_sw_params_set_start_threshold(alsa_hw.handle, swp, period_size))
|| alsa_error("sw_params_set_avail_min", snd_pcm_sw_params_set_avail_min(alsa_hw.handle, swp, period_size))
|| alsa_error("sw_params", snd_pcm_sw_params(alsa_hw.handle, swp)))
return -1;
return 0;
}
开发者ID:TryndamereStark,项目名称:lirc,代码行数:45,代码来源:hw_audio_alsa.c
示例6: capture
static int capture(lua_State *lstate) {
char *card;
snd_pcm_sw_params_t *sparams;
int ret, dir, i;
snd_pcm_format_t f, format;
unsigned int rate, buffer_time;
if (rbuf == NULL) {
rbuf = new_ringbuf(jack_sr, CHANNELS, BUFSECS,
0.333, 0.667);
}
getstring(lstate, "card", &card);
lua_pop(lstate, 1);
for (i = 0; i < 20; i++) {
ret = snd_pcm_open(&handle, card, SND_PCM_STREAM_CAPTURE, 0);
if (ret < 0) {
logmsg("can't open %s (%s)\n", card, snd_strerror(ret));
}
else {
break;
}
}
free(card);
if (ret < 0) return 0;
if (hparams != NULL) snd_pcm_hw_params_free(hparams);
snd_pcm_hw_params_malloc(&hparams);
snd_pcm_hw_params_any(handle, hparams);
for (f = format = 0; f < SND_PCM_FORMAT_LAST; f++) {
ret = snd_pcm_hw_params_test_format(handle, hparams, f);
if (ret == 0) {
logmsg("- %s\n", snd_pcm_format_name(f));
format = f;
}
}
ret = snd_pcm_hw_params_set_access(handle, hparams,
SND_PCM_ACCESS_RW_INTERLEAVED);
if (ret < 0) {
logmsg("access %s\n", snd_strerror(ret));
}
ret = snd_pcm_hw_params_set_format(handle, hparams, format);
logmsg("format: %s\n", snd_pcm_format_description(format));
if (ret < 0) {
logmsg("format error %s\n", snd_strerror(ret));
}
snd_pcm_hw_params_get_buffer_time_max(hparams, &buffer_time, 0);
rate = jack_sr;
ret = snd_pcm_hw_params_set_rate(handle, hparams, rate, 0);
logmsg("rate %d\n", rate);
if (ret < 0) logmsg("rate error %s\n", snd_strerror(ret));
snd_pcm_hw_params_set_channels(handle, hparams, CHANNELS);
blocksize = BLOCKSIZE;
snd_pcm_hw_params_set_period_size(handle, hparams, blocksize, 0);
logmsg("period %ld\n", blocksize);
snd_pcm_hw_params_set_buffer_time_near(handle, hparams, &buffer_time, &dir);
logmsg("buffer time %u\n", buffer_time);
if ((ret = snd_pcm_hw_params(handle, hparams)) < 0) {
logmsg("can't set hardware: %s\n",
snd_strerror(ret));
return 0;
}
else logmsg("hardware configd\n");
snd_pcm_sw_params_malloc(&sparams);
snd_pcm_sw_params_current(handle, sparams);
snd_pcm_sw_params_set_avail_min(handle, sparams, blocksize);
snd_pcm_sw_params_set_start_threshold(handle, sparams, 0U);
if ((ret = snd_pcm_sw_params(handle, sparams)) < 0) {
logmsg("can't set software: %s\n",
snd_strerror(ret));
}
else logmsg("software configd\n");
snd_pcm_sw_params_free(sparams);
pthread_create(&thread_id, NULL, dev_thread, NULL);
return 0;
}
开发者ID:pmyadlowsky,项目名称:mash,代码行数:73,代码来源:alsa.c
示例7: now
//.........这里部分代码省略.........
fatal = true;
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_rate_resample: err = %1").arg(err);
}
}
if ( !fatal ) {
err = snd_pcm_hw_params_set_access( handle, hwparams, access );
if ( err < 0 ) {
fatal = true;
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_access: err = %1").arg(err);
}
}
if ( !fatal ) {
err = setFormat();
if ( err < 0 ) {
fatal = true;
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_format: err = %1").arg(err);
}
}
if ( !fatal ) {
err = snd_pcm_hw_params_set_channels( handle, hwparams, (unsigned int)settings.channels() );
if ( err < 0 ) {
fatal = true;
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_channels: err = %1").arg(err);
}
}
if ( !fatal ) {
err = snd_pcm_hw_params_set_rate_near( handle, hwparams, &freakuency, 0 );
if ( err < 0 ) {
fatal = true;
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_rate_near: err = %1").arg(err);
}
}
if ( !fatal ) {
err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, &dir);
if ( err < 0 ) {
fatal = true;
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_buffer_time_near: err = %1").arg(err);
}
}
if ( !fatal ) {
err = snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, &dir);
if ( err < 0 ) {
fatal = true;
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_period_time_near: err = %1").arg(err);
}
}
if ( !fatal ) {
err = snd_pcm_hw_params_set_periods_near(handle, hwparams, &chunks, &dir);
if ( err < 0 ) {
fatal = true;
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params_set_periods_near: err = %1").arg(err);
}
}
if ( !fatal ) {
err = snd_pcm_hw_params(handle, hwparams);
if ( err < 0 ) {
fatal = true;
errMessage = QString::fromLatin1("QAudioOutput: snd_pcm_hw_params: err = %1").arg(err);
}
}
if( err < 0) {
qWarning()<<errMessage;
errorState = QAudio::OpenError;
deviceState = QAudio::StoppedState;
return false;
}
开发者ID:Nacto1,项目名称:qt-everywhere-opensource-src-4.6.2,代码行数:67,代码来源:qaudiooutput_alsa_p.cpp
示例8: drvHostALSAAudioOpen
//.........这里部分代码省略.........
break;
}
unsigned int period_size = pCfgReq->period_size;
unsigned int buffer_size = pCfgReq->buffer_size;
if ( !((fIn && s_ALSAConf.size_in_usec_in)
|| (!fIn && s_ALSAConf.size_in_usec_out)))
{
if (!buffer_size)
{
buffer_size = DEFAULT_BUFFER_SIZE;
period_size = DEFAULT_PERIOD_SIZE;
}
}
if (buffer_size)
{
if ( ( fIn && s_ALSAConf.size_in_usec_in)
|| (!fIn && s_ALSAConf.size_in_usec_out))
{
if (period_size)
{
err = snd_pcm_hw_params_set_period_time_near(phPCM, pHWParms,
&period_size, 0);
if (err < 0)
{
LogRel(("ALSA: Failed to set period time %d\n", pCfgReq->period_size));
rc = VERR_GENERAL_FAILURE; /** @todo Find a better rc. */
break;
}
}
err = snd_pcm_hw_params_set_buffer_time_near(phPCM, pHWParms,
&buffer_size, 0);
if (err < 0)
{
LogRel(("ALSA: Failed to set buffer time %d\n", pCfgReq->buffer_size));
rc = VERR_GENERAL_FAILURE; /** @todo Find a better rc. */
break;
}
}
else
{
snd_pcm_uframes_t period_size_f = (snd_pcm_uframes_t)period_size;
snd_pcm_uframes_t buffer_size_f = (snd_pcm_uframes_t)buffer_size;
snd_pcm_uframes_t minval;
if (period_size_f)
{
minval = period_size_f;
int dir = 0;
err = snd_pcm_hw_params_get_period_size_min(pHWParms,
&minval, &dir);
if (err < 0)
{
LogRel(("ALSA: Could not determine minimal period size\n"));
rc = VERR_GENERAL_FAILURE; /** @todo Find a better rc. */
break;
}
else
{
LogFunc(("Minimal period size is: %ld\n", minval));
if (period_size_f < minval)
开发者ID:stefano-garzarella,项目名称:virtualbox-org-svn-vbox-trunk,代码行数:67,代码来源:DrvHostALSAAudio.cpp
示例9: set_hwparams
static int
set_hwparams (GstAlsaSink * alsa)
{
guint rrate;
gint err;
snd_pcm_hw_params_t *params;
guint period_time, buffer_time;
snd_pcm_hw_params_malloc (¶ms);
GST_DEBUG_OBJECT (alsa, "Negotiating to %d channels @ %d Hz (format = %s) "
"SPDIF (%d)", alsa->channels, alsa->rate,
snd_pcm_format_name (alsa->format), alsa->iec958);
/* start with requested values, if we cannot configure alsa for those values,
* we set these values to -1, which will leave the default alsa values */
buffer_time = alsa->buffer_time;
period_time = alsa->period_time;
retry:
/* choose all parameters */
CHECK (snd_pcm_hw_params_any (alsa->handle, params), no_config);
/* set the interleaved read/write format */
CHECK (snd_pcm_hw_params_set_access (alsa->handle, params, alsa->access),
wrong_access);
/* set the sample format */
if (alsa->iec958) {
/* Try to use big endian first else fallback to le and swap bytes */
if (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format) < 0) {
alsa->format = SND_PCM_FORMAT_S16_LE;
alsa->need_swap = TRUE;
GST_DEBUG_OBJECT (alsa, "falling back to little endian with swapping");
} else {
alsa->need_swap = FALSE;
}
}
CHECK (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format),
no_sample_format);
/* set the count of channels */
CHECK (snd_pcm_hw_params_set_channels (alsa->handle, params, alsa->channels),
no_channels);
/* set the stream rate */
rrate = alsa->rate;
CHECK (snd_pcm_hw_params_set_rate_near (alsa->handle, params, &rrate, NULL),
no_rate);
#ifndef GST_DISABLE_GST_DEBUG
/* get and dump some limits */
{
guint min, max;
snd_pcm_hw_params_get_buffer_time_min (params, &min, NULL);
snd_pcm_hw_params_get_buffer_time_max (params, &max, NULL);
GST_DEBUG_OBJECT (alsa, "buffer time %u, min %u, max %u",
alsa->buffer_time, min, max);
snd_pcm_hw_params_get_period_time_min (params, &min, NULL);
snd_pcm_hw_params_get_period_time_max (params, &max, NULL);
GST_DEBUG_OBJECT (alsa, "period time %u, min %u, max %u",
alsa->period_time, min, max);
snd_pcm_hw_params_get_periods_min (params, &min, NULL);
snd_pcm_hw_params_get_periods_max (params, &max, NULL);
GST_DEBUG_OBJECT (alsa, "periods min %u, max %u", min, max);
}
#endif
/* now try to configure the buffer time and period time, if one
* of those fail, we fall back to the defaults and emit a warning. */
if (buffer_time != -1 && !alsa->iec958) {
/* set the buffer time */
if ((err = snd_pcm_hw_params_set_buffer_time_near (alsa->handle, params,
&buffer_time, NULL)) < 0) {
GST_ELEMENT_WARNING (alsa, RESOURCE, SETTINGS, (NULL),
("Unable to set buffer time %i for playback: %s",
buffer_time, snd_strerror (err)));
/* disable buffer_time the next round */
buffer_time = -1;
goto retry;
}
GST_DEBUG_OBJECT (alsa, "buffer time %u", buffer_time);
}
if (period_time != -1 && !alsa->iec958) {
/* set the period time */
if ((err = snd_pcm_hw_params_set_period_time_near (alsa->handle, params,
&period_time, NULL)) < 0) {
GST_ELEMENT_WARNING (alsa, RESOURCE, SETTINGS, (NULL),
("Unable to set period time %i for playback: %s",
period_time, snd_strerror (err)));
/* disable period_time the next round */
period_time = -1;
goto retry;
}
GST_DEBUG_OBJECT (alsa, "period time %u", period_time);
}
/* Set buffer size and period size manually for SPDIF */
//.........这里部分代码省略.........
开发者ID:pli3,项目名称:gst-plugins-base,代码行数:101,代码来源:gstalsasink.c
示例10: fprintf
static snd_pcm_t *alsa_open(snd_pcm_t **pcm_handle, int stream_type,
snd_pcm_uframes_t *buffer_size) {
char *pcm_name = "chumix";
snd_pcm_hw_params_t *hwparams;
snd_pcm_sw_params_t *swparams;
unsigned int buffer_time;
int status;
snd_pcm_uframes_t xfer_align;
// Open the device. Experimenting with duplex mode.
fprintf(stderr, "Opening PCM device\n");
status = snd_pcm_open(pcm_handle, pcm_name,
stream_type, 0);
if(status < 0) {
fprintf(stderr, "Unable to open audio device: %s\n",
snd_strerror(status));
goto error;
}
// Determine what the hardware can do.
fprintf(stderr, "Allocating hw params\n");
snd_pcm_hw_params_malloc(&hwparams);
status = snd_pcm_hw_params_any(*pcm_handle, hwparams);
if(status < 0) {
fprintf(stderr, "Unable to get hardware parameters: %s\n",
snd_strerror(status));
goto error;
}
// Set up interleaved audio.
fprintf(stderr, "Setting up interleaved audio\n");
status = snd_pcm_hw_params_set_access(*pcm_handle, hwparams,
SND_PCM_ACCESS_RW_INTERLEAVED);
if(status < 0) {
fprintf(stderr, "Unable to set interleaved mode: %s\n",
snd_strerror(status));
goto error;
}
// Set the audio format.
fprintf(stderr, "Setting format\n");
status = snd_pcm_hw_params_set_format(*pcm_handle, hwparams,
SND_PCM_FORMAT_S16_LE);
if(status<0) {
fprintf(stderr, "Unable to set audio format: %s\n",
snd_strerror(status));
goto error;
}
// Set stereo audio.
fprintf(stderr, "Setting channels\n");
status = snd_pcm_hw_params_set_channels(*pcm_handle, hwparams, 2);
if(status<0) {
fprintf(stderr, "Unable to set stereo mode: %s\n",
snd_strerror(status));
goto error;
}
// Set sample rate.
fprintf(stderr, "Setting audio rate\n");
int rate = 44100;
status = snd_pcm_hw_params_set_rate_near(*pcm_handle, hwparams,
&rate, NULL);
if(status<0) {
fprintf(stderr, "Unable to set sample rate to 44100: %s\n",
snd_strerror(status));
goto error;
}
// Set buffer time to a reasonable value.
// Probably won't work for playback, as it's fixed in /etc/asound.conf
buffer_time = BUFFER_TIME;
status = snd_pcm_hw_params_set_buffer_time_near(*pcm_handle, hwparams,
&buffer_time, 0);
snd_pcm_hw_params_get_buffer_size(hwparams, buffer_size);
// Write the settings out to the audio card.
fprintf(stderr, "Setting hw params\n");
status = snd_pcm_hw_params(*pcm_handle, hwparams);
if(status<0) {
fprintf(stderr, "Unable to set audio parameters: %s\n",
snd_strerror(status));
goto error;
}
snd_pcm_hw_params_free(hwparams);
status = snd_pcm_prepare(*pcm_handle);
if(status<0) {
fprintf(stderr, "Unable to prepare audio device: %s\n",
snd_strerror(status));
goto error;
}
//.........这里部分代码省略.........
开发者ID:xobs,项目名称:chumbradiod,代码行数:101,代码来源:crad_interface.c
示例11: puts
static void *_msynth_thread_main(void *arg)
{
int i;
int err;
int sample;
int processed;
struct sampleclock sc = {0, 0, 0.0f, 0.0f};
msynth_frame fb = NULL;
puts("synthread: started");
/* Begin initialization of ALSA */
/* snd_pcm_open(pcm_handle, device_name, stream_type, open_mode) */
err = snd_pcm_open(&pcm, config.device_name, SND_PCM_STREAM_PLAYBACK, 0);
ALSERT("opening audio device");
/* First ALSA hardware settings */
/* Allocate HW params */
err = snd_pcm_hw_params_malloc(&hw_p);
ALSERT("allocating hw params");
/* Get default HW parameters */
err = snd_pcm_hw_params_any(pcm, hw_p);
ALSERT("requesting hw default params");
/* Disable software resampling */
err = snd_pcm_hw_params_set_rate_resample(pcm, hw_p, config.resample);
ALSERT("disabling software resampling");
/* Configure sample rate */
if (config.srate != -1)
srate = config.srate;
else {
/* Get maximum hardware samplerate */
err = snd_pcm_hw_params_get_rate_max(hw_p, &srate, &dir);
ALSERT("requesting maximum hardware samplerate");
}
/* Set sample rate
* snd_pcm_hw_params_set_rate_near(pcn, hw_p, *rate,
* 0 (== set exact rate))
*/
err = snd_pcm_hw_params_set_rate_near(pcm, hw_p, &srate, 0);
ALSERT("setting samplerate");
printf("synthread: Detected samplerate of %u\n", srate);
/* RW interleaved access (means we will use the snd_pcm_writei function) */
err = snd_pcm_hw_params_set_access(pcm, hw_p,
SND_PCM_ACCESS_RW_INTERLEAVED);
ALSERT("setting access mode");
/* native-endian 16-bit signed sample format */
err = snd_pcm_hw_params_set_format(pcm, hw_p, SND_PCM_FORMAT_S16);
ALSERT("setting sample format");
/* Switch to 2.0ch audio */
err = snd_pcm_hw_params_set_channels(pcm, hw_p, 2);
ALSERT("switching to 2.0ch audio");
if (config.buffer_time != -1) {
/* Set configured buffer time */
err = snd_pcm_hw_params_set_buffer_time_near(pcm, hw_p,
&config.buffer_time, &dir);
ALSERT("setting buffer time");
/* Fetch resulting size */
err = snd_pcm_hw_params_get_buffer_size(hw_p, &buffer_size);
ALSERT("getting buffer size");
} else {
/* Since we want the interactive synthesizer to be very responsive
* select a minimum buffer size.
*/
err = snd_pcm_hw_params_set_buffer_size_first(pcm, hw_p, &buffer_size);
ALSERT("setting buffer size");
}
printf("synthread: Selected buffersize of %lu\n", buffer_size);
printf("synthread: Response delay is approximately %.2f ms\n",
(double)buffer_size / (double)srate * 1000.0);
if (config.period_time != -1) {
err = snd_pcm_hw_params_set_period_time_near(pcm, hw_p,
&config.period_time, &dir);
ALSERT("setting period time");
err = snd_pcm_hw_params_get_period_size(hw_p, &period_size, &dir);
ALSERT("getting period size");
} else {
/* Select a period time of half the buffer time
* since this improves processing performance
*/
err = snd_pcm_hw_params_set_period_size_last(pcm, hw_p, &period_size, &dir);
ALSERT("setting period size");
}
if (dir)
printf("synthread: Selected period size near %lu\n", period_size);
else
//.........这里部分代码省略.........
开发者ID:meridion,项目名称:microsynth,代码行数:101,代码来源:synth.c
示例12: init_audio
static void
init_audio(const char* devname)
{
snd_pcm_hw_params_t* hwparams = 0;
snd_pcm_sw_params_t* swparams = 0;
snd_pcm_uframes_t bufsize = 0;
unsigned int buftime = 1000000;
unsigned int pertime = 50000;
int dir = 0;
int rc;
if ((rc = snd_output_stdio_attach(&output, stderr, 0)) < 0)
exit_snd_error(rc, "log output");
if ((rc = snd_pcm_open(&audio, devname, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
exit_snd_error(rc, "opening device");
if ((rc = snd_pcm_hw_params_malloc(&hwparams)) < 0)
exit_snd_error(rc, "hardware parameters");
if ((rc = snd_pcm_hw_params_any(audio, hwparams)) < 0)
exit_snd_error(rc, "hardware parameters");
if ((rc = snd_pcm_hw_params_set_rate_resample(audio, hwparams, 0)) < 0)
exit_snd_error(rc, "hardware parameters");
if ((rc = snd_pcm_hw_params_set_access(audio, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
exit_snd_error(rc, "access type");
if ((rc = snd_pcm_hw_params_set_format(audio, hwparams, SND_PCM_FORMAT_S16)) < 0)
exit_snd_error(rc, "sample format");
if ((rc = snd_pcm_hw_params_set_channels_near(audio, hwparams, &n_channels)) < 0)
exit_snd_error(rc, "number of channels");
if ((rc = snd_pcm_hw_params_set_rate_near(audio, hwparams, &samplerate, &dir)) < 0)
exit_snd_error(rc, "sample rate");
if ((rc = snd_pcm_hw_params_set_buffer_time_near(audio, hwparams, &buftime, &dir)) < 0)
exit_snd_error(rc, "buffer time");
if ((rc = snd_pcm_hw_params_set_period_time_near(audio, hwparams, &pertime, &dir)) < 0)
exit_snd_error(rc, "period time");
if ((rc = snd_pcm_hw_params(audio, hwparams)) < 0)
exit_snd_error(rc, "applying hardware parameters");
if ((rc = snd_pcm_hw_params_get_buffer_size(hwparams, &bufsize)) < 0)
exit_snd_error(rc, "buffer size");
if ((rc = snd_pcm_hw_params_get_period_size(hwparams, &periodsize, &dir)) < 0)
exit_snd_error(rc, "period size");
snd_pcm_hw_params_free(hwparams);
if ((rc = snd_pcm_sw_params_malloc(&swparams)) < 0)
exit_snd_error(rc, "software parameters");
if ((rc = snd_pcm_sw_params_current(audio, swparams)) < 0)
exit_snd_error(rc, "software parameters");
if ((rc = snd_pcm_sw_params_set_start_threshold(audio, swparams,
bufsize / periodsize * periodsize)) < 0)
exit_snd_error(rc, "start threshold");
if ((rc = snd_pcm_sw_params(audio, swparams)) < 0)
exit_snd_error(rc, "applying software parameters");
snd_pcm_sw_params_free(swparams);
if ((rc = snd_pcm_prepare(audio)) < 0)
exit_snd_error(rc, "preparing device");
}
开发者ID:danielkitta,项目名称:kcio,代码行数:73,代码来源:kcplay.c
示例13: qWarning
//.........这里部分代码省略.........
if ((err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, alsa_format)) < 0)
{
qDebug("OutputALSA: Error setting format: %s", snd_strerror(err));
return false;
}
exact_rate = rate;
if ((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &exact_rate, 0)) < 0)
{
qWarning("OutputALSA: Error setting rate: %s", snd_strerror(err));
return false;
}
if (rate != exact_rate)
{
qWarning("OutputALSA: The rate %d Hz is not supported by your hardware.\n==> Using %d Hz instead.", rate, exact_rate);
rate = exact_rate;
}
uint c = map.count();
if ((err = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0)
{
qWarning("OutputALSA: Error setting channels: %s", snd_strerror(err));
return false;
}
if (c != (uint)map.count())
{
qWarning("OutputALSA: The channel number %d is not supported by your hardware", map.count());
qWarning("==> Using %d instead.", c);
}
if ((err = snd_pcm_hw_params_set_period_time_near(pcm_handle, hwparams, &period_time ,0)) < 0)
{
qWarning("OutputALSA: Error setting period time: %s", snd_strerror(err));
return false;
}
if ((err = snd_pcm_hw_params_set_buffer_time_near(pcm_handle, hwparams, &buffer_time ,0)) < 0)
{
qWarning("OutputALSA: Error setting buffer time: %s", snd_strerror(err));
return false;
}
if ((err = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
{
qWarning("OutputALSA: Error setting HW params: %s", snd_strerror(err));
return false;
}
//read some alsa parameters
snd_pcm_uframes_t buffer_size = 0;
snd_pcm_uframes_t period_size = 0;
if ((err = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size)) < 0)
{
qWarning("OutputALSA: Error reading buffer size: %s", snd_strerror(err));
return false;
}
if ((err = snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0)) < 0)
{
qWarning("OutputALSA: Error reading period size: %s", snd_strerror(err));
return false;
}
//swparams
snd_pcm_sw_params_alloca(&swparams);
snd_pcm_sw_params_current(pcm_handle, swparams);
if ((err = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams,
buffer_size - period_size)) < 0)
qWarning("OutputALSA: Error setting threshold: %s", snd_strerror(err));
if ((err = snd_pcm_sw_params(pcm_handle, swparams)) < 0)
{
qWarning("OutputALSA: Error setting SW params: %s", snd_strerror(err));
return false;
开发者ID:Greedysky,项目名称:qmmp,代码行数:67,代码来源:outputalsa.cpp
示例14: alsa_set_format
static RD_BOOL
alsa_set_format(snd_pcm_t * pcm, RD_WAVEFORMATEX * pwfx)
{
snd_pcm_hw_params_t *hwparams = NULL;
int err;
unsigned int buffertime;
short samplewidth;
int audiochannels;
unsigned int rate;
samplewidth = pwfx->wBitsPerSample / 8;
if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0)
{
error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
return False;
}
if ((err = snd_pcm_hw_params_any(pcm, hwparams)) < 0)
{
error("snd_pcm_hw_params_any: %s\n", snd_strerror(err));
return False;
}
if ((err = snd_pcm_hw_params_set_access(pcm, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{
error("snd_pcm_hw_params_set_access: %s\n", snd_strerror(err));
return False;
}
if (pwfx->wBitsPerSample == 16)
{
if ((err = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S16_LE)) < 0)
{
error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err));
return False;
}
}
else
{
if ((err = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S8)) < 0)
{
error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err));
return False;
}
}
#if 0
if ((err = snd_pcm_hw_params_set_rate_resample(pcm, hwparams, 1)) < 0)
{
error("snd_pcm_hw_params_set_rate_resample: %s\n", snd_strerror(err));
return False;
}
#endif
rate = pwfx->nSamplesPerSec;
if ((err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0)) < 0)
{
error("snd_pcm_hw_params_set_rate_near: %s\n", snd_strerror(err));
return False;
}
audiochannels = pwfx->nChannels;
if ((err = snd_pcm_hw_params_set_channels(pcm, hwparams, pwfx->nChannels)) < 0)
{
error("snd_pcm_hw_params_set_channels: %s\n", snd_strerror(err));
return False;
}
buffertime = 500000; /* microseconds */
if ((err = snd_pcm_hw_params_set_buffer_time_near(pcm, hwparams, &buffertime, 0)) < 0)
{
error("snd_pcm_hw_params_set_buffer_time_near: %s\n", snd_strerror(err));
return False;
}
if ((err = snd_pcm_hw_params(pcm, hwparams)) < 0)
{
error("snd_pcm_hw_params: %s\n", snd_strerror(err));
return False;
}
snd_pcm_hw_params_free(hwparams);
if ((err = snd_pcm_prepare(pcm)) < 0)
{
error("snd_pcm_prepare: %s\n", snd_strerror(err));
return False;
}
reopened = True;
return True;
}
开发者ID:AmesianX,项目名称:rdesktop-fuzzer,代码行数:95,代码来源:rdpsnd_alsa.c
示例15: set_hwparams
static int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, snd_pcm_access_t access, int rate, int channels, int period, int nperiods ) {
int err, dir=0;
unsigned int buffer_time;
unsigned int period_time;
unsigned int rrate;
unsigned int rchannels;
/* choose all parameters */
err = snd_pcm_hw_params_any(handle, params);
if (err < 0) {
printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
return err;
}
/* set the interleaved read/write format */
err = snd_pcm_hw_params_set_access(handle, params, access);
if (err < 0) {
printf("Access type not available for playback: %s\n", snd_strerror(err));
return err;
}
/* set the sample format */
err = set_hwformat(handle, params);
if (err < 0) {
printf("Sample format not available for playback: %s\n", snd_strerror(err));
return err;
}
/* set the count of channels */
rchannels = channels;
err = snd_pcm_hw_params_set_channels_near(handle, params, &rchannels);
if (err < 0) {
printf("Channels count (%i) not available for record: %s\n", channels, snd_strerror(err));
return err;
}
if (rchannels != channels) {
printf("WARNING: chennel count does not match (requested %d got %d)\n", channels, rchannels);
num_channels = rchannels;
}
/* set the stream rate */
rrate = rate;
err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
if (err < 0) {
printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
return err;
}
if (rrate != rate) {
printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, rrate);
return -EINVAL;
}
/* set the buffer time */
buffer_time = 1000000*(uint64_t)period*nperiods/rate;
err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);
if (err < 0) {
printf("Unable to set buffer time %i for playback: %s\n", 1000000*period*nperiods/rate, snd_strerror(err));
return err;
}
err = snd_pcm_hw_params_get_buffer_size( params, &real_buffer_size );
if (err < 0) {
printf("Unable to get buffer size back: %s\n", snd_strerror(err));
return err;
}
if( real_buffer_size != nperiods * period ) {
printf( "WARNING: buffer size does not match: (requested %d, got %d)\n", nperiods * period, (int) real_buffer_size );
}
/* set the period time */
period_time = 1000000*(uint64_t)period/rate;
err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);
if (err < 0) {
printf("Unable to set period time %i for playback: %s\n", 1000000*period/rate, snd_strerror(err));
return err;
}
err = snd_pcm_hw_params_get_period_size(params, &real_period_size, NULL );
if (err < 0) {
printf("Unable to get period size back: %s\n", snd_strerror(err));
return err;
}
if( real_period_size != period ) {
printf( "WARNING: period size does not match: (requested %i, got %i)\n", period, (int)real_period_size );
}
/* write the parameters to device */
err = snd_pcm_hw_params(handle, params);
if (err < 0) {
printf("Unable to set hw params for playback: %s\n", snd_strerror(err));
return err;
}
return 0;
}
开发者ID:recri,项目名称:keyer,代码行数:87,代码来源:alsa_out.c
示例16: open_alsa_output
static int open_alsa_output()
{
snd_pcm_hw_params_t *hw;
snd_pcm_sw_params_t *sw;
int err;
unsigned int alsa_buffer_time;
unsigned int alsa_period_time;
qDebug() << "open_alsa_output(): pcmname = " << pcmname;
if (!pcmname) {
pcmname = (char*) malloc( 8 );
strcpy(pcmname,"default\0");
}
if ((err = snd_pcm_open (&pcm, pcmname, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Error: audio open error: %s\r\n", snd_strerror (-err));
qDebug() << "Error: audio open error: " << QString::fromLocal8Bit(snd_strerror (-err)) << ", err = " << err;
return -1;
}
snd_pcm_hw_params_alloca (&hw);
if ((err = snd_pcm_hw_params_any(pcm, hw)) < 0) {
printf("ERROR: No configuration available for playback: %s\r\n", snd_strerror(-err));
return -1;
}
if ((err = snd_pcm_hw_params_set_access(pcm, hw, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
printf("Cannot set mmap'ed mode: %s.\r\n", snd_strerror(-err));
return -1;
}
if (snd_pcm_hw_params_set_format (pcm, hw, SND_PCM_FORMAT_S16_LE) < 0) {
printf("ALSA does not support 16bit signed audio for your soundcard\r\n");
close_alsa_output();
return -1;
}
if (snd_pcm_hw_params_set_channels (pcm, hw, 2) < 0) {
printf("ALSA does not support stereo for your soundcard\r\n");
close_alsa_output();
return -1;
}
if (snd_pcm_hw_params_set_rate_near(pcm, hw, &rate, 0) < 0) {
printf("ALSA does not support %iHz for your soundcard\r\n",rate);
close_alsa_output();
return -1;
}
alsa_buffer_time = 500000;
alsa_period_time = 50000;
if ((err = snd_pcm_hw_params_set_buffer_time_near(pcm, hw, &alsa_buffer_time, 0)) < 0)
{
printf("Set buffer time failed: %s.\r\n", snd_strerror(-err));
return -1;
}
if ((err = snd_pcm_hw_params_set_period_time_near(pcm, hw, &alsa_period_time, 0)) < 0)
{
printf("Set period time failed: %s.\r\n", snd_strerror(-err));
return -1;
}
if (snd_pcm_hw_params(pcm, hw) < 0)
{
printf("Unable to install hw params\r\n");
return -1;
}
snd_pcm_sw_params_alloca(&s
|
请发表评论