本文整理汇总了C++中snd_pcm_hw_params函数的典型用法代码示例。如果您正苦于以下问题:C++ snd_pcm_hw_params函数的具体用法?C++ snd_pcm_hw_params怎么用?C++ snd_pcm_hw_params使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snd_pcm_hw_params函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: snd_pcm_hw_params_alloca
void PCMThread::init()
{
snd_pcm_t* pcm_handle;
snd_pcm_hw_params_t* hwparams;
snd_pcm_uframes_t buffersize_return;
unsigned int tmp;
int err;
std::unique_ptr<PCMHandle> spPCMHandle {new PCMHandle {spSettings_->pcmName_, SND_PCM_STREAM_CAPTURE}};
QObject::connect (spPCMHandle.get(), &PCMThread::PCMHandle::sigDebug, this, &PCMThread::slotDebug);
snd_pcm_hw_params_alloca (&hwparams);
if ( (err = snd_pcm_hw_params_any (*spPCMHandle, hwparams)) < 0)
throw std::runtime_error (snd_strerror (err));
if ( (err = snd_pcm_hw_params_set_access (*spPCMHandle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
throw std::runtime_error (snd_strerror (err));
if ( (err = snd_pcm_hw_params_set_format (*spPCMHandle, hwparams, SND_PCM_FORMAT_FLOAT)) < 0)
throw std::runtime_error (snd_strerror (err));
tmp = spSettings_->rate_;
if ( (err = snd_pcm_hw_params_set_rate_near (*spPCMHandle, hwparams, &tmp, 0)) < 0)
throw std::runtime_error (snd_strerror (err));
tmp = spSettings_->channels_;
if ( (err = snd_pcm_hw_params_set_channels (*spPCMHandle, hwparams, tmp)) < 0)
throw std::runtime_error (snd_strerror (err));
tmp = spSettings_->periods_;
if ( (err = snd_pcm_hw_params_set_periods (*spPCMHandle, hwparams, tmp, 0)) < 0)
throw std::runtime_error (snd_strerror (err));
buffersize_return = spSettings_->periodSize_ * spSettings_->periods_;
if ( (err = snd_pcm_hw_params_set_buffer_size_near (*spPCMHandle, hwparams, &buffersize_return)) < 0)
throw std::runtime_error (snd_strerror (err));
if (buffersize_return != static_cast<snd_pcm_uframes_t> (spSettings_->periodSize_ * spSettings_->periods_))
{
DebugHelper dbgHelper;
dbgHelper << "Period size " << spSettings_->periodSize_ << " not available, using " <<
buffersize_return / spSettings_->periods_;
emit sigDebug (dbgHelper.string());
periodSize_ = buffersize_return / spSettings_->periods_;
}
else
{
periodSize_ = spSettings_->periodSize_;
}
if ( (err = snd_pcm_hw_params (*spPCMHandle, hwparams)) < 0)
throw std::runtime_error (snd_strerror (err));
spPCMHandle_ = std::move (spPCMHandle);
emit sigDebug ("Initialized : " + QString::fromStdString (spSettings_->pcmName_));
}
示例2: QLatin1String
bool QAudioDeviceInfoInternal::testSettings(const QAudioFormat& format) const
{
// Set nearest to closest settings that do work.
// See if what is in settings will work (return value).
int err = 0;
snd_pcm_t* handle;
snd_pcm_hw_params_t *params;
QString dev = device;
QList<QByteArray> devices = QAudioDeviceInfoInternal::availableDevices(QAudio::AudioOutput);
if(dev.compare(QLatin1String("default")) == 0) {
#if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14)
dev = QLatin1String(devices.first().constData());
#else
dev = QLatin1String("hw:0,0");
#endif
} else {
#if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14)
dev = device;
#else
int idx = 0;
char *name;
QString shortName = device.mid(device.indexOf(QLatin1String("="),0)+1);
while(snd_card_get_name(idx,&name) == 0) {
if(shortName.compare(QLatin1String(name)) == 0)
break;
idx++;
}
dev = QString(QLatin1String("hw:%1,0")).arg(idx);
#endif
}
if(mode == QAudio::AudioOutput) {
err=snd_pcm_open( &handle,dev.toLocal8Bit().constData(),SND_PCM_STREAM_PLAYBACK,0);
} else {
err=snd_pcm_open( &handle,dev.toLocal8Bit().constData(),SND_PCM_STREAM_CAPTURE,0);
}
if(err < 0) {
handle = 0;
return false;
}
bool testChannel = false;
bool testCodec = false;
bool testFreq = false;
bool testType = false;
bool testSize = false;
int dir = 0;
snd_pcm_nonblock( handle, 0 );
snd_pcm_hw_params_alloca( ¶ms );
snd_pcm_hw_params_any( handle, params );
// set the values!
snd_pcm_hw_params_set_channels(handle,params,format.channels());
snd_pcm_hw_params_set_rate(handle,params,format.frequency(),dir);
err = -1;
switch(format.sampleSize()) {
case 8:
if(format.sampleType() == QAudioFormat::SignedInt)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S8);
else if(format.sampleType() == QAudioFormat::UnSignedInt)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U8);
break;
case 16:
if(format.sampleType() == QAudioFormat::SignedInt) {
if(format.byteOrder() == QAudioFormat::LittleEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S16_LE);
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S16_BE);
} else if(format.sampleType() == QAudioFormat::UnSignedInt) {
if(format.byteOrder() == QAudioFormat::LittleEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U16_LE);
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U16_BE);
}
break;
case 32:
if(format.sampleType() == QAudioFormat::SignedInt) {
if(format.byteOrder() == QAudioFormat::LittleEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S32_LE);
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S32_BE);
} else if(format.sampleType() == QAudioFormat::UnSignedInt) {
if(format.byteOrder() == QAudioFormat::LittleEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U32_LE);
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U32_BE);
}
}
// For now, just accept only audio/pcm codec
if(!format.codec().startsWith(QLatin1String("audio/pcm"))) {
err=-1;
} else
//.........这里部分代码省略.........
示例3: alsa_set_hwparams
/* setup alsa data format and buffer geometry */
static inline int alsa_set_hwparams(ao_device *device,
ao_sample_format *format)
{
ao_alsa_internal *internal = (ao_alsa_internal *) device->internal;
snd_pcm_hw_params_t *params;
int err;
unsigned int rate = internal->sample_rate = format->rate;
/* allocate the hardware parameter structure */
snd_pcm_hw_params_alloca(¶ms);
/* fetch all possible hardware parameters */
err = snd_pcm_hw_params_any(internal->pcm_handle, params);
if (err < 0){
adebug("snd_pcm_hw_params_any() failed.\n"
" Device exists but no matching hardware?\n");
return err;
}
/* set the access type */
err = snd_pcm_hw_params_set_access(internal->pcm_handle,
params, internal->access_mask);
if (err < 0){
adebug("snd_pcm_hw_params_set_access() failed.\n");
return err;
}
/* set the sample bitformat */
err = snd_pcm_hw_params_set_format(internal->pcm_handle,
params, internal->bitformat);
if (err < 0){
/* the device may support a greater bit-depth than the one
requested. */
switch(internal->bitformat){
case SND_PCM_FORMAT_U8:
if (!snd_pcm_hw_params_set_format(internal->pcm_handle,
params, SND_PCM_FORMAT_S16)){
adebug("snd_pcm_hw_params_set_format() unable to open %d bit playback.\n",format->bits);
adebug("snd_pcm_hw_params_set_format() using 16 bit playback instead.\n");
format->bits = 16;
break;
}
case SND_PCM_FORMAT_S16:
if (!snd_pcm_hw_params_set_format(internal->pcm_handle,
params, SND_PCM_FORMAT_S24)){
adebug("snd_pcm_hw_params_set_format() unable to open %d bit playback.\n",format->bits);
adebug("snd_pcm_hw_params_set_format() using 24 bit playback instead.\n");
format->bits = 24;
break;
}
case SND_PCM_FORMAT_S24:
if (!snd_pcm_hw_params_set_format(internal->pcm_handle,
params, SND_PCM_FORMAT_S32)){
adebug("snd_pcm_hw_params_set_format() unable to open %d bit playback.\n",format->bits);
adebug("snd_pcm_hw_params_set_format() using 32 bit playback instead.\n");
format->bits = 32;
break;
}
case SND_PCM_FORMAT_S32:
adebug("snd_pcm_hw_params_set_format() failed.\n");
return err;
}
}
/* set the number of channels */
err = snd_pcm_hw_params_set_channels(internal->pcm_handle,
params, (unsigned int)device->output_channels);
if (err < 0){
adebug("snd_pcm_hw_params_set_channels() failed.\n");
return err;
}
/* set the sample rate */
err = snd_pcm_hw_params_set_rate_near(internal->pcm_handle,
params, &rate, 0);
if (err < 0){
adebug("snd_pcm_hw_params_set_rate_near() failed.\n");
return err;
}
if (rate > 1.05 * format->rate || rate < 0.95 * format->rate) {
awarn("sample rate %i not supported "
"by the hardware, using %u\n", format->rate, rate);
}
/* set the time per hardware sample transfer */
if(internal->period_time==0)
internal->period_time=internal->buffer_time/4;
err = snd_pcm_hw_params_set_period_time_near(internal->pcm_handle,
params, &(internal->period_time), 0);
if (err < 0){
adebug("snd_pcm_hw_params_set_period_time_near() failed.\n");
return err;
}
/* set the length of the hardware sample buffer in microseconds */
/* some plug devices have very high minimum periods; don't
allow a buffer size small enough that it's ~ guaranteed to
//.........这里部分代码省略.........
示例4: an_configure_capture_card
//
// Set up both the audio and video capturing on the card
//
void an_configure_capture_card( void )
{
struct v4l2_cropcap cropcap;
struct v4l2_crop crop;
struct v4l2_format fmt;
sys_config info;
int input;
u32 pix_fmt;
dvb_config_get( &info );
CLEAR(cropcap);
CLEAR(crop);
CLEAR(fmt);
m_width = PAL_WIDTH_CAPTURE;
m_height = PAL_HEIGHT_CAPTURE;
// Set the cropping, ignore any errors
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl(m_i_fd, VIDIOC_CROPCAP, &cropcap)==0) {
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
crop.c = cropcap.defrect; /* reset to default */
ioctl(m_i_fd, VIDIOC_S_CROP, &crop);
}
//
// Analogue Video capture
//
pix_fmt = V4L2_PIX_FMT_YUV420;
m_sws = NULL;
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = m_width;
fmt.fmt.pix.height = m_height;
fmt.fmt.pix.pixelformat = pix_fmt;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
if (ioctl(m_i_fd, VIDIOC_S_FMT, &fmt) == 0)
{
an_set_image_size( AV_PIX_FMT_YUV420P );
}
else
{
pix_fmt = V4L2_PIX_FMT_YUYV; // capture format
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = m_width;
fmt.fmt.pix.height = m_height;
fmt.fmt.pix.pixelformat = pix_fmt;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
if (ioctl(m_i_fd, VIDIOC_S_FMT, &fmt) == 0)
{
// Format conversion will be required
m_sws = sws_getContext( m_width, m_height, AV_PIX_FMT_YUYV422,
m_width, m_height, AV_PIX_FMT_YUV420P,
SWS_BICUBIC, NULL,NULL, NULL);
an_set_image_size( AV_PIX_FMT_YUYV422 );
}
else
{
logger("CAP ANALOGUE FORMAT NOT SUPPORTED");
}
}
if(ioctl(m_i_fd, VIDIOC_G_FMT, &fmt)<0 )
logger("can't get format");
// input = V4L2_INPUT_TYPE_CAMERA;
input = info.video_capture_device_input;
if( ioctl( m_i_fd, VIDIOC_S_INPUT, &input) < 0 )
{
loggerf("CAP Error VIDIOC_S_INPUT %d",input);
}
/*
v4l2_streamparm parm;
memset(&parm,0,sizeof(v4l2_streamparm));
parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
parm.parm.capture.capturemode = 0;
parm.parm.capture.readbuffers = 0;
if( ioctl( m_i_fd, VIDIOC_S_PARM, &parm) < 0 )
{
loggerf("CAP Error VIDIOC_S_PARM");
}
*/
info.video_bitrate = calculate_video_bitrate();
//
// Analogue sound capture
//
snd_pcm_hw_params_t *hw_params;
if(snd_pcm_open(&m_audio_handle, "pulse", SND_PCM_STREAM_CAPTURE, 0)< 0 )
{
//.........这里部分代码省略.........
示例5: initalsa
static int initalsa(char *filename)
{
snd_pcm_hw_params_t *hw_params;
int err;
if ((err =
snd_pcm_open(&capture_handle, filename,
SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf(stderr, "cannot open audio device %s (%s)\n",
filename, snd_strerror(err));
return 0;
}
if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
fprintf(stderr,
"cannot allocate hardware parameter structure (%s)\n",
snd_strerror(err));
return 0;
}
if ((err = snd_pcm_hw_params_any(capture_handle, hw_params)) < 0) {
fprintf(stderr,
"cannot initialize hardware parameter structure (%s)\n",
snd_strerror(err));
return 0;
}
if ((err =
snd_pcm_hw_params_set_access(capture_handle, hw_params,
SND_PCM_ACCESS_RW_INTERLEAVED)) <
0) {
fprintf(stderr, "cannot set access type (%s)\n",
snd_strerror(err));
return 0;
}
if ((err =
snd_pcm_hw_params_set_format(capture_handle, hw_params,
SND_PCM_FORMAT_S16)) < 0) {
fprintf(stderr, "cannot set sample format (%s)\n",
snd_strerror(err));
return 0;
}
if ((err =
snd_pcm_hw_params_set_rate(capture_handle, hw_params, 48000,
0)) < 0) {
fprintf(stderr, "cannot set sample rate (%s)\n",
snd_strerror(err));
return 0;
}
for(nbch=2;nbch>0;nbch--) {
if (snd_pcm_hw_params_set_channels(capture_handle, hw_params, nbch)==0)
break;
}
if (nbch ==0) {
fprintf(stderr, "cannot set number of channels\n");
return 0;
}
if ((err = snd_pcm_hw_params(capture_handle, hw_params)) < 0) {
fprintf(stderr, "cannot set parameters (%s)\n",
snd_strerror(err));
return 0;
}
snd_pcm_hw_params_free(hw_params);
if ((err = snd_pcm_prepare(capture_handle)) < 0) {
fprintf(stderr,
"cannot prepare audio interface for use (%s)\n",
snd_strerror(err));
return 0;
}
return nbch;
}
示例6: init_audio_device
static int init_audio_device(common_data_t *p_common_data)
{
int ret;
int dir = 0;
unsigned int val;
snd_pcm_hw_params_t *p_params;
snd_pcm_uframes_t buffer_size;
unsigned int buffer_time;
unsigned int period_time;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
size_t bits_per_sample;
size_t bits_per_frame;
ret = snd_pcm_open(&p_common_data->handle, "plughw:0,0", SND_PCM_STREAM_PLAYBACK, 0);
if (ret < 0) {
dbg_alsa("unable to open pcm device: %s\n", snd_strerror(ret));
return -1;
}
snd_pcm_hw_params_alloca(&p_params);
snd_pcm_hw_params_any(p_common_data->handle, p_params);
snd_pcm_hw_params_set_access(p_common_data->handle, p_params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(p_common_data->handle, p_params, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(p_common_data->handle, p_params, 2);
val = 44100;
snd_pcm_hw_params_set_rate_near(p_common_data->handle, p_params, &val, &dir);
snd_pcm_hw_params_get_buffer_time_max(p_params, &buffer_time, 0);
// if (buffer_time > 500000)
// buffer_time = 500000;
//
// period_time = buffer_time / 4;
//
// snd_pcm_hw_params_set_period_time_near(p_common_data->handle, p_params, &period_time, 0);
// snd_pcm_hw_params_set_buffer_time_near(p_common_data->handle, p_params, &buffer_time, 0);
p_common_data->period_size = 2048;
snd_pcm_hw_params_set_period_size_near(p_common_data->handle, p_params, &p_common_data->period_size, &dir);
ret = snd_pcm_hw_params(p_common_data->handle, p_params);
if (ret < 0){
dbg_alsa("unable to set hw parameters: %s\n", snd_strerror(ret));
exit(1);
}
ret = snd_pcm_state(p_common_data->handle);
dbg("state is %d\n", ret);
snd_pcm_hw_params_get_period_size(p_params, &p_common_data->period_size, &dir);
snd_pcm_hw_params_get_buffer_size(p_params, &buffer_size);
if (p_common_data->period_size == buffer_size) {
dbg_alsa("Can't use period size equal to buffer size (%lu == %lu)\n", p_common_data->period_size, buffer_size);
}
dbg("period size is : %lu frames\n", p_common_data->period_size);
dbg("buffer size is : %lu frames\n", buffer_size);
bits_per_sample = snd_pcm_format_physical_width(format);
bits_per_frame = bits_per_sample * 2;
p_common_data->chunk_bytes = p_common_data->period_size * bits_per_frame / 8;
//g_buf = (msg_t *)malloc(sizeof(msg_t)+(char)p_common_data->chunk_bytes);
dbg("sample rate is %d\n", val);
dbg("bits_per_sample %d\n", bits_per_sample);
dbg("bits_per_frame %d\n", bits_per_frame);
dbg("chunk_bytes %d\n", p_common_data->chunk_bytes);
dbg("PCM handle name = '%s'\n",snd_pcm_name(p_common_data->handle));
return 0;
}
示例7: set_hwparams
static int
set_hwparams (GstAlsaSrc * alsa)
{
guint rrate;
gint err;
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_malloc (¶ms);
/* 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 */
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);
if (rrate != alsa->rate)
goto rate_match;
#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
if (alsa->buffer_time != -1) {
/* set the buffer time */
CHECK (snd_pcm_hw_params_set_buffer_time_near (alsa->handle, params,
&alsa->buffer_time, NULL), buffer_time);
GST_DEBUG_OBJECT (alsa, "buffer time %u", alsa->buffer_time);
}
if (alsa->period_time != -1) {
/* set the period time */
CHECK (snd_pcm_hw_params_set_period_time_near (alsa->handle, params,
&alsa->period_time, NULL), period_time);
GST_DEBUG_OBJECT (alsa, "period time %u", alsa->period_time);
}
/* write the parameters to device */
CHECK (snd_pcm_hw_params (alsa->handle, params), set_hw_params);
CHECK (snd_pcm_hw_params_get_buffer_size (params, &alsa->buffer_size),
buffer_size);
CHECK (snd_pcm_hw_params_get_period_size (params, &alsa->period_size, NULL),
period_size);
snd_pcm_hw_params_free (params);
return 0;
/* ERRORS */
no_config:
{
GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
("Broken configuration for recording: no configurations available: %s",
snd_strerror (err)));
snd_pcm_hw_params_free (params);
return err;
}
wrong_access:
{
GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
("Access type not available for recording: %s", snd_strerror (err)));
snd_pcm_hw_params_free (params);
return err;
}
no_sample_format:
{
GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
("Sample format not available for recording: %s", snd_strerror (err)));
snd_pcm_hw_params_free (params);
return err;
}
no_channels:
//.........这里部分代码省略.........
示例8: main
int main() {
long loops;
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir;
snd_pcm_uframes_t frames;
char *buffer;
/* Open PCM device for playback. */
rc = snd_pcm_open(&handle, "default",
SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
fprintf(stderr,
"unable to open pcm device: %s\n",
snd_strerror(rc));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params,
SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 2);
/* 44100 bits/second sampling rate (CD quality) */
val = 44100;
snd_pcm_hw_params_set_rate_near(handle, params,
&val, &dir);
/* Set period size to 32 frames. */
frames = 32;
snd_pcm_hw_params_set_period_size_near(handle,
params, &frames, &dir);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
fprintf(stderr,
"unable to set hw parameters: %s\n",
snd_strerror(rc));
exit(1);
}
/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames,
&dir);
size = frames * 4; /* 2 bytes/sample, 2 channels */
buffer = (char *) malloc(size);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params,
&val, &dir);
/* 5 seconds in microseconds divided by
* period time */
loops = 5000000 / val;
while (loops > 0) {
loops--;
rc = read(0, buffer, size);
if (rc == 0) {
fprintf(stderr, "end of file on input\n");
break;
} else if (rc != size) {
fprintf(stderr,
"short read: read %d bytes\n", rc);
}
rc = snd_pcm_writei(handle, buffer, frames);
if (rc == -EPIPE) {
/* EPIPE means underrun */
fprintf(stderr, "underrun occurred\n");
snd_pcm_prepare(handle);
} else if (rc < 0) {
fprintf(stderr,
"error from writei: %s\n",
snd_strerror(rc));
} else if (rc != (int)frames) {
fprintf(stderr,
"short write, write %d frames\n", rc);
}
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
//.........这里部分代码省略.........
示例9: snd_pcm_hw_params_alloca
bool CAESinkALSA::InitializeHW(const ALSAConfig &inconfig, ALSAConfig &outconfig)
{
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 = inconfig.sampleRate;
unsigned int channelCount = inconfig.channels;
#if defined(HAS_AMLPLAYER) || defined(HAS_LIBAMCODEC)
// alsa/kernel lies, so map everything to 44100 or 48000
switch(sampleRate)
{
case 11025:
case 22050:
case 88200:
case 176400:
sampleRate = 44100;
break;
case 8000:
case 16000:
case 24000:
case 32000:
case 96000:
case 192000:
case 384000:
sampleRate = 48000;
break;
}
#endif
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 (inconfig.channels > channelCount)
{
CLog::Log(LOGINFO, "CAESinkALSA::InitializeHW - Unable to open the required number of channels");
}
/* update outconfig */
outconfig.channels = channelCount;
snd_pcm_format_t fmt = AEFormatToALSAFormat(inconfig.format);
outconfig.format = inconfig.format;
if (fmt == SND_PCM_FORMAT_UNKNOWN)
{
/* if we dont support the requested format, fallback to float */
fmt = SND_PCM_FORMAT_FLOAT;
outconfig.format = AE_FMT_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(outconfig.format));
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 */
outconfig.format = i;
CLog::Log(LOGINFO, "CAESinkALSA::InitializeHW - Using data format %s", CAEUtil::DataFormatToStr(outconfig.format));
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;
}
//.........这里部分代码省略.........
示例10: tsmf_alsa_set_format
static boolean tsmf_alsa_set_format(ITSMFAudioDevice* audio,
uint32 sample_rate, uint32 channels, uint32 bits_per_sample)
{
int error;
snd_pcm_uframes_t frames;
snd_pcm_hw_params_t* hw_params;
snd_pcm_sw_params_t* sw_params;
TSMFALSAAudioDevice* alsa = (TSMFALSAAudioDevice*) audio;
if (!alsa->out_handle)
return false;
snd_pcm_drop(alsa->out_handle);
alsa->actual_rate = alsa->source_rate = sample_rate;
alsa->actual_channels = alsa->source_channels = channels;
alsa->bytes_per_sample = bits_per_sample / 8;
error = snd_pcm_hw_params_malloc(&hw_params);
if (error < 0)
{
DEBUG_WARN("snd_pcm_hw_params_malloc failed");
return false;
}
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,
SND_PCM_FORMAT_S16_LE);
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);
frames = sample_rate;
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");
return false;
}
snd_pcm_sw_params_current(alsa->out_handle, sw_params);
snd_pcm_sw_params_set_start_threshold(alsa->out_handle, sw_params,
frames / 2);
snd_pcm_sw_params(alsa->out_handle, sw_params);
snd_pcm_sw_params_free(sw_params);
snd_pcm_prepare(alsa->out_handle);
DEBUG_DVC("sample_rate %d channels %d bits_per_sample %d",
sample_rate, channels, bits_per_sample);
DEBUG_DVC("hardware buffer %d frames", (int)frames);
if ((alsa->actual_rate != alsa->source_rate) ||
(alsa->actual_channels != alsa->source_channels))
{
DEBUG_DVC("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);
}
return true;
}
示例11: setparams_set
int setparams_set(snd_pcm_t *handle,
snd_pcm_hw_params_t *params,
snd_pcm_sw_params_t *swparams,
const char *id)
{
int err;
snd_pcm_uframes_t val;
unsigned int sleep_min = 0;
err = snd_pcm_hw_params(handle, params);
if (err < 0) {
printf("Unable to set hw params for %s: %s\n", id, snd_strerror(err));
return err;
}
err = snd_pcm_sw_params_current(handle, swparams);
if (err < 0) {
printf("Unable to determine current swparams for %s: %s\n", id, snd_strerror(err));
return err;
}
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 0x7fffffff);
if (err < 0) {
printf("Unable to set start threshold mode for %s: %s\n", id, snd_strerror(err));
return err;
}
tick_time_ok = 0;
if (tick_time > 0) {
int time, ttime;
snd_pcm_hw_params_get_period_time(params, &time, NULL);
snd_pcm_hw_params_get_tick_time(params, &ttime, NULL);
if (time < ttime) {
printf("Skipping to set minimal sleep: period time < tick time\n");
} else if (ttime <= 0) {
printf("Skipping to set minimal sleep: tick time <= 0 (%i)\n", ttime);
} else {
sleep_min = tick_time / ttime;
if (sleep_min <= 0)
sleep_min = 1;
err = snd_pcm_sw_params_set_sleep_min(handle, swparams, sleep_min);
if (err < 0) {
printf("Unable to set minimal sleep %i for %s: %s\n", sleep_min, id, snd_strerror(err));
return err;
}
tick_time_ok = sleep_min * ttime;
}
}
if (!block)
val = 4;
else
snd_pcm_hw_params_get_period_size(params, &val, NULL);
if (tick_time_ok > 0)
val = 16;
err = snd_pcm_sw_params_set_avail_min(handle, swparams, val);
if (err < 0) {
printf("Unable to set avail min for %s: %s\n", id, snd_strerror(err));
return err;
}
val = !block ? 4 : 1;
err = snd_pcm_sw_params_set_xfer_align(handle, swparams, val);
if (err < 0) {
printf("Unable to set transfer align for %s: %s\n", id, snd_strerror(err));
return err;
}
err = snd_pcm_sw_params(handle, swparams);
if (err < 0) {
printf("Unable to set sw params for %s: %s\n", id, snd_strerror(err));
return err;
}
return 0;
}
示例12: main
int main() {
long loops;
int rc, rc2;
int size;
snd_pcm_t *handle;
snd_pcm_t *handle2;
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_t *params2;
unsigned int val, val2;
int dir, dir2;
snd_pcm_uframes_t frames, frames2;
char *buffer;
/* Open PCM device for recording (capture). */
rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE | SND_PCM_NONBLOCK, 0);
// rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE, 0);
if (rc < 0) {
fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc));
exit(1);
}
/* Open PCM device for playback. */
rc2 = snd_pcm_open(&handle2, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (rc2 < 0) {
fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc2));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms2);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle2, params2);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(handle2, params2, SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle2, params2, SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle2, params2, 2);
/* 44100 bits/second sampling rate (CD quality) */
val2 = 96000;
snd_pcm_hw_params_set_rate_near(handle2, params2, &val2, &dir2);
/* Set period size to 32 frames. */
frames2 = 8;
snd_pcm_hw_params_set_period_size_near(handle2, params2, &frames2, &dir2);
/* Write the parameters to the driver */
rc2 = snd_pcm_hw_params(handle2, params2);
if (rc2 < 0) {
fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc2));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 2);
/* 44100 bits/second sampling rate (CD quality) */
val = 96000;
//val = 48000;
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
/* Set period size to 32 frames. */
frames = 8;
snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc));
exit(1);
}
/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
//.........这里部分代码省略.........
示例13: 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(LOGINFO, "CAESinkALSA::InitializeHW - Unable to open the required number of channels");
}
/* 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;
}
}
snd_pcm_uframes_t periodSize, bufferSize;
snd_pcm_hw_params_get_buffer_size_max(hw_params, &bufferSize);
snd_pcm_hw_params_get_period_size_max(hw_params, &periodSize, NULL);
/*
We want to make sure, that we have max 200 ms Buffer with
a periodSize of approx 50 ms. Choosing a higher bufferSize
will cause problems with menu sounds. Buffer will be increased
after those are fixed.
*/
periodSize = std::min(periodSize, (snd_pcm_uframes_t) sampleRate / 20);
bufferSize = std::min(bufferSize, (snd_pcm_uframes_t) sampleRate / 5);
/*
According to upstream we should set buffer size first - so make sure it is always at least
4x period size to not get underruns (some systems seem to have issues with only 2 periods)
*/
periodSize = std::min(periodSize, bufferSize / 4);
CLog::Log(LOGDEBUG, "CAESinkALSA::InitializeHW - Request: periodSize %lu, bufferSize %lu", periodSize, bufferSize);
//.........这里部分代码省略.........
示例14: ags_devout_alsa_init
void
ags_devout_alsa_init(AgsDevout *devout,
GError **error)
{
static unsigned int period_time = 100000;
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;
int rc;
snd_pcm_t *handle;
snd_pcm_hw_params_t *hwparams;
unsigned int val;
snd_pcm_uframes_t frames;
unsigned int rate;
unsigned int rrate;
unsigned int channels;
snd_pcm_uframes_t size;
snd_pcm_sframes_t buffer_size;
snd_pcm_sframes_t period_size;
snd_pcm_sw_params_t *swparams;
int period_event = 0;
int err, dir;
/* Open PCM device for playback. */
if ((err = snd_pcm_open(&handle, devout->out.alsa.device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
return;
}
snd_pcm_hw_params_alloca(&hwparams);
snd_pcm_sw_params_alloca(&swparams);
/* choose all parameters */
err = snd_pcm_hw_params_any(handle, hwparams);
if (err < 0) {
printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
return;
}
/* set hardware resampling */
err = snd_pcm_hw_params_set_rate_resample(handle, hwparams, 1);
if (err < 0) {
printf("Resampling setup failed for playback: %s\n", snd_strerror(err));
return;
}
/* set the interleaved read/write format */
err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) {
printf("Access type not available for playback: %s\n", snd_strerror(err));
return;
}
/* set the sample format */
err = snd_pcm_hw_params_set_format(handle, hwparams, format);
if (err < 0) {
printf("Sample format not available for playback: %s\n", snd_strerror(err));
return;
}
/* set the count of channels */
channels = devout->dsp_channels;
err = snd_pcm_hw_params_set_channels(handle, hwparams, channels);
if (err < 0) {
printf("Channels count (%i) not available for playbacks: %s\n", channels, snd_strerror(err));
return;
}
/* set the stream rate */
rate = devout->frequency;
rrate = rate;
err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rrate, 0);
if (err < 0) {
printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
return;
}
if (rrate != rate) {
printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
exit(-EINVAL);
}
/* set the buffer size */
size = devout->buffer_size;
err = snd_pcm_hw_params_set_buffer_size(handle, hwparams, size);
if (err < 0) {
printf("Unable to set buffer size %i for playback: %s\n", size, snd_strerror(err));
return;
}
buffer_size = size;
/* set the period time */
err = snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, &dir);
if (err < 0) {
printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err));
return;
}
err = snd_pcm_hw_params_get_period_size(hwparams, &size, &dir);
if (err < 0) {
//.........这里部分代码省略.........
示例15: alsa_open
static int alsa_open (struct sound_params *sound_params)
{
snd_pcm_hw_params_t *hw_params;
int err;
unsigned int period_time;
unsigned int buffer_time;
snd_pcm_uframes_t chunk_frames;
snd_pcm_uframes_t buffer_frames;
char fmt_name[128];
switch (sound_params->fmt & SFMT_MASK_FORMAT) {
case SFMT_S8:
params.format = SND_PCM_FORMAT_S8;
break;
case SFMT_U8:
params.format = SND_PCM_FORMAT_U8;
break;
case SFMT_S16:
params.format = SND_PCM_FORMAT_S16;
break;
case SFMT_U16:
params.format = SND_PCM_FORMAT_U16;
break;
case SFMT_S32:
params.format = SND_PCM_FORMAT_S32;
break;
case SFMT_U32:
params.format = SND_PCM_FORMAT_U32;
break;
default:
error ("Unknown sample format: %s",
sfmt_str(sound_params->fmt, fmt_name, sizeof(fmt_name)));
params.format = SND_PCM_FORMAT_UNKNOWN;
return 0;
}
if ((err = snd_pcm_open(&handle, options_get_str("AlsaDevice"),
SND_PCM_STREAM_PLAYBACK,
SND_PCM_NONBLOCK)) < 0) {
error ("Can't open audio: %s", snd_strerror(err));
return 0;
}
if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
error ("Can't allocate alsa hardware parameters structure: %s",
snd_strerror(err));
return 0;
}
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
error ("Can't initialize hardware parameters structure: %s",
snd_strerror(err));
snd_pcm_hw_params_free (hw_params);
return 0;
}
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
error ("Can't set alsa access type: %s", snd_strerror(err));
snd_pcm_hw_params_free (hw_params);
return 0;
}
if ((err = snd_pcm_hw_params_set_format (handle, hw_params,
params.format)) < 0) {
error ("Can't set sample format: %s", snd_strerror(err));
snd_pcm_hw_params_free (hw_params);
return 0;
}
params.rate = sound_params->rate;
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params,
¶ms.rate, 0)) < 0) {
error ("Can't set sample rate: %s", snd_strerror(err));
snd_pcm_hw_params_free (hw_params);
return 0;
}
logit ("Set rate to %u", params.rate);
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params,
sound_params->channels)) < 0) {
error ("Can't set number of channels: %s", snd_strerror(err));
snd_pcm_hw_params_free (hw_params);
return 0;
}
if ((err = snd_pcm_hw_params_get_buffer_time_max(hw_params,
&buffer_time, 0)) < 0) {
error ("Can't get maximum buffer time: %s", snd_strerror(err));
snd_pcm_hw_params_free (hw_params);
return 0;
}
if (buffer_time > BUFFER_MAX_USEC)
buffer_time = BUFFER_MAX_USEC;
period_time = buffer_time / 4;
if ((err = snd_pcm_hw_params_set_period_time_near(handle, hw_params,
&period_time, 0)) < 0) {
error ("Can't set period time: %s", snd_strerror(err));
//.........这里部分代码省略.........