本文整理汇总了C++中snd_pcm_hw_params_set_channels函数的典型用法代码示例。如果您正苦于以下问题:C++ snd_pcm_hw_params_set_channels函数的具体用法?C++ snd_pcm_hw_params_set_channels怎么用?C++ snd_pcm_hw_params_set_channels使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snd_pcm_hw_params_set_channels函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: snd_pcm_hw_params_malloc
bool PokeLaunchApplication::sound() {
#if JUCE_LINUX
int err;
int freq = 44100, channels = 2;
snd_pcm_hw_params_t *hw_params;
snd_pcm_sw_params_t *sw_params;
snd_pcm_hw_params_malloc( &hw_params );
snd_pcm_sw_params_malloc( &sw_params );
err = snd_pcm_open( &g_alsa_playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0 );
if( err < 0 )
{
DBG( "ALSA ERROR: Can't open audio device: " << snd_strerror( err ) );
return false;
}
DBG("Opened Audio Device");
err = snd_pcm_hw_params_any( g_alsa_playback_handle, hw_params );
if( err < 0 )
{
DBG( "ALSA ERROR: Can't initialize hardware parameter structure: " << snd_strerror( err ) );
return false;
}
err = snd_pcm_hw_params_set_access( g_alsa_playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED );
if( err < 0 )
{
DBG( "ALSA ERROR: Can't set access type: " << snd_strerror( err ) );
return false;
}
//const UTF8_CHAR *sample_format = "";
err = snd_pcm_hw_params_set_format( g_alsa_playback_handle, hw_params, SND_PCM_FORMAT_S16_LE );
if( err < 0 )
{
DBG( "ALSA ERROR: Can't set sample format :" << snd_strerror( err ) );
return false;
}
err = snd_pcm_hw_params_set_rate_near( g_alsa_playback_handle, hw_params, (unsigned int*)&freq, 0 );
if( err < 0 )
{
DBG( "ALSA ERROR: Can't set sample rate: " << snd_strerror( err ) );
return false;
}
DBG( "ALSA Sample rate: "<< freq );
err = snd_pcm_hw_params_set_channels( g_alsa_playback_handle, hw_params, channels );
if( err < 0 )
{
DBG( "ALSA ERROR: Can't set channel count: " << snd_strerror( err ) );
return false;
}
snd_pcm_uframes_t frames;
frames = DEFAULT_BUFFER_SIZE;
err = snd_pcm_hw_params_set_buffer_size_near( g_alsa_playback_handle, hw_params, &frames );
if( err < 0 )
{
DBG( "ALSA ERROR: Can't set buffer size: " << snd_strerror( err ) );
return false;
}
snd_pcm_hw_params_get_buffer_size( hw_params, &frames );
DBG( "ALSA Buffer size: 4096 samples" );
err = snd_pcm_hw_params( g_alsa_playback_handle, hw_params );
if( err < 0 )
{
DBG( "ALSA ERROR: Can't set parameters: " << snd_strerror( err ) );
return false;
}
snd_pcm_hw_params_free( hw_params );
snd_pcm_sw_params_free( sw_params );
err = snd_pcm_prepare( g_alsa_playback_handle );
if( err < 0 )
{
DBG( "ALSA ERROR: Can't prepare audio interface for use: " << snd_strerror( err ) );
return false;
}
/* Stop PCM device and drop pending frames */
snd_pcm_drain(g_alsa_playback_handle);
#endif
return true;
}
开发者ID:brandonlanky,项目名称:PocketCHIP-pocket-home,代码行数:80,代码来源:Main.cpp
示例2: 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 recording (capture). */
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);
}
/* 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);
loops = 5000000 / val;
while (loops > 0) {
loops--;
rc = snd_pcm_readi(handle, buffer, frames);
if (rc == -EPIPE) {
/* EPIPE means overrun */
fprintf(stderr, "overrun occurred\n");
snd_pcm_prepare(handle);
} else if (rc < 0) {
fprintf(stderr,
"error from read: %s\n",
snd_strerror(rc));
} else if (rc != (int)frames) {
fprintf(stderr, "short read, read %d frames\n", rc);
}
rc = write(1, buffer, size);
if (rc != size)
fprintf(stderr,
"short write: wrote %d bytes\n", rc);
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
return 0;
}
开发者ID:windleos,项目名称:sound-card,代码行数:96,代码来源:demo5.c
示例3: pcm_open
static int pcm_open(struct alsa_pcm *alsa, const char *device_name,
snd_pcm_stream_t stream, int rate, int buffer_time)
{
int r, dir;
unsigned int p;
size_t bytes;
snd_pcm_hw_params_t *hw_params;
r = snd_pcm_open(&alsa->pcm, device_name, stream, SND_PCM_NONBLOCK);
if (r < 0) {
alsa_error("open", r);
return -1;
}
snd_pcm_hw_params_alloca(&hw_params);
r = snd_pcm_hw_params_any(alsa->pcm, hw_params);
if (r < 0) {
alsa_error("hw_params_any", r);
return -1;
}
r = snd_pcm_hw_params_set_access(alsa->pcm, hw_params,
SND_PCM_ACCESS_RW_INTERLEAVED);
if (r < 0) {
alsa_error("hw_params_set_access", r);
return -1;
}
r = snd_pcm_hw_params_set_format(alsa->pcm, hw_params, SND_PCM_FORMAT_S16);
if (r < 0) {
alsa_error("hw_params_set_format", r);
fprintf(stderr, "16-bit signed format is not available. "
"You may need to use a 'plughw' device.\n");
return -1;
}
r = snd_pcm_hw_params_set_rate(alsa->pcm, hw_params, rate, 0);
if (r < 0) {
alsa_error("hw_params_set_rate", r);
fprintf(stderr, "%dHz sample rate not available. You may need to use "
"a 'plughw' device.\n", rate);
return -1;
}
alsa->rate = rate;
r = snd_pcm_hw_params_set_channels(alsa->pcm, hw_params, DEVICE_CHANNELS);
if (r < 0) {
alsa_error("hw_params_set_channels", r);
fprintf(stderr, "%d channel audio not available on this device.\n",
DEVICE_CHANNELS);
return -1;
}
p = buffer_time * 1000; /* microseconds */
dir = -1;
r = snd_pcm_hw_params_set_buffer_time_max(alsa->pcm, hw_params, &p, &dir);
if (r < 0) {
alsa_error("hw_params_set_buffer_time_max", r);
fprintf(stderr, "Buffer of %dms may be too small for this hardware.\n",
buffer_time);
return -1;
}
p = 2; /* double buffering */
dir = 1;
r = snd_pcm_hw_params_set_periods_min(alsa->pcm, hw_params, &p, &dir);
if (r < 0) {
alsa_error("hw_params_set_periods_min", r);
fprintf(stderr, "Buffer of %dms may be too small for this hardware.\n",
buffer_time);
return -1;
}
r = snd_pcm_hw_params(alsa->pcm, hw_params);
if (r < 0) {
alsa_error("hw_params", r);
return -1;
}
r = snd_pcm_hw_params_get_period_size(hw_params, &alsa->period, &dir);
if (r < 0) {
alsa_error("get_period_size", r);
return -1;
}
bytes = alsa->period * DEVICE_CHANNELS * sizeof(signed short);
alsa->buf = malloc(bytes);
if (!alsa->buf) {
perror("malloc");
return -1;
}
/* snd_pcm_readi() returns uninitialised memory on first call,
* possibly caused by premature POLLIN. Keep valgrind happy. */
memset(alsa->buf, 0, bytes);
return 0;
}
开发者ID:ewanuno,项目名称:xwax,代码行数:100,代码来源:alsa.c
示例4: pcm_open
static int pcm_open(pcm_handle_t* pcm, const pcm_desc_t* desc)
{
const snd_pcm_format_t fmt = SND_PCM_FORMAT_S16_LE;
snd_pcm_stream_t stm;
int err;
if (desc->flags & PCM_FLAG_IN) stm = SND_PCM_STREAM_CAPTURE;
else stm = SND_PCM_STREAM_PLAYBACK;
err = snd_pcm_open
(&pcm->pcm, desc->name, stm, SND_PCM_NONBLOCK);
if (err) PERROR_GOTO(snd_strerror(err), on_error_0);
err = snd_pcm_hw_params_malloc(&pcm->hw_params);
if (err) PERROR_GOTO(snd_strerror(err), on_error_1);
err = snd_pcm_hw_params_any(pcm->pcm, pcm->hw_params);
if (err) PERROR_GOTO(snd_strerror(err), on_error_2);
err = snd_pcm_hw_params_set_access
(pcm->pcm, pcm->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (err) PERROR_GOTO(snd_strerror(err), on_error_2);
err = snd_pcm_hw_params_set_format(pcm->pcm, pcm->hw_params, fmt);
if (err) PERROR_GOTO(snd_strerror(err), on_error_2);
err = snd_pcm_hw_params_set_rate
(pcm->pcm, pcm->hw_params, desc->fsampl, 0);
if (err) PERROR_GOTO(snd_strerror(err), on_error_2);
pcm->nchan = desc->nchan;
pcm->wchan = (size_t)snd_pcm_format_physical_width(fmt) / 8;
pcm->scale = pcm->nchan * pcm->wchan;
err = snd_pcm_hw_params_set_channels
(pcm->pcm, pcm->hw_params, desc->nchan);
if (err) PERROR_GOTO(snd_strerror(err), on_error_2);
err = snd_pcm_hw_params(pcm->pcm, pcm->hw_params);
if (err) PERROR_GOTO(snd_strerror(err), on_error_2);
err = snd_pcm_sw_params_malloc(&pcm->sw_params);
if (err) PERROR_GOTO(snd_strerror(err), on_error_2);
err = snd_pcm_sw_params_current(pcm->pcm, pcm->sw_params);
if (err) PERROR_GOTO(snd_strerror(err), on_error_3);
#if 1
err = snd_pcm_sw_params_set_avail_min
(pcm->pcm, pcm->sw_params, 1024);
if (err) PERROR_GOTO(snd_strerror(err), on_error_3);
#endif
#if 1
err = snd_pcm_sw_params_set_start_threshold
(pcm->pcm, pcm->sw_params, 0U);
if (err) PERROR_GOTO(snd_strerror(err), on_error_3);
#endif
err = snd_pcm_sw_params(pcm->pcm, pcm->sw_params);
if (err) PERROR_GOTO(snd_strerror(err), on_error_3);
err = snd_pcm_prepare(pcm->pcm);
if (err) PERROR_GOTO(snd_strerror(err), on_error_3);
pcm->rpos = 0;
pcm->wpos = 0;
pcm->nsampl = (size_t)desc->fsampl * 10;
pcm->buf = malloc(pcm->nsampl * pcm->scale);
if (pcm->buf == NULL) goto on_error_3;
return 0;
on_error_3:
snd_pcm_sw_params_free(pcm->sw_params);
on_error_2:
snd_pcm_hw_params_free(pcm->hw_params);
on_error_1:
snd_pcm_close(pcm->pcm);
on_error_0:
return -1;
}
开发者ID:texane,项目名称:aspect,代码行数:83,代码来源:main.c
示例5: SetupSound
void SetupSound(void)
{
snd_pcm_hw_params_t *hwparams;
snd_pcm_sw_params_t *swparams;
snd_pcm_status_t *status;
int pspeed;
int pchannels;
int format;
int buffer_time;
int period_time;
int err;
if(iDisStereo) pchannels=1;
else pchannels=2;
pspeed=48000;
format=SND_PCM_FORMAT_S16_LE;
buffer_time=500000;
period_time=buffer_time/4;
if((err=snd_pcm_open(&handle, "default",
SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK))<0)
{
printf("Audio open error: %s\n", snd_strerror(err));
return;
}
if((err=snd_pcm_nonblock(handle, 0))<0)
{
printf("Can't set blocking moded: %s\n", snd_strerror(err));
return;
}
snd_pcm_hw_params_alloca(&hwparams);
snd_pcm_sw_params_alloca(&swparams);
if((err=snd_pcm_hw_params_any(handle, hwparams))<0)
{
printf("Broken configuration for this PCM: %s\n", snd_strerror(err));
return;
}
if((err=snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED))<0)
{
printf("Access type not available: %s\n", snd_strerror(err));
return;
}
if((err=snd_pcm_hw_params_set_format(handle, hwparams, format))<0)
{
printf("Sample format not available: %s\n", snd_strerror(err));
return;
}
if((err=snd_pcm_hw_params_set_channels(handle, hwparams, pchannels))<0)
{
printf("Channels count not available: %s\n", snd_strerror(err));
return;
}
if((err=snd_pcm_hw_params_set_rate_near(handle, hwparams, &pspeed, 0))<0)
{
printf("Rate not available: %s\n", snd_strerror(err));
return;
}
if((err=snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, 0))<0)
{
printf("Buffer time error: %s\n", snd_strerror(err));
return;
}
if((err=snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, 0))<0)
{
printf("Period time error: %s\n", snd_strerror(err));
return;
}
if((err=snd_pcm_hw_params(handle, hwparams))<0)
{
printf("Unable to install hw params: %s\n", snd_strerror(err));
return;
}
snd_pcm_status_alloca(&status);
if((err=snd_pcm_status(handle, status))<0)
{
printf("Unable to get status: %s\n", snd_strerror(err));
return;
}
buffer_size=snd_pcm_status_get_avail(status);
}
开发者ID:0xZERO3,项目名称:PCSX2-rr-lua,代码行数:92,代码来源:alsa.c
示例6: set_params_raw
static int set_params_raw(alsa_param_t *alsa_params)
{
snd_pcm_hw_params_t *hwparams;
snd_pcm_sw_params_t *swparams;
// snd_pcm_uframes_t buffer_size;
// snd_pcm_uframes_t boundary;
// unsigned int period_time = 0;
// unsigned int buffer_time = 0;
snd_pcm_uframes_t bufsize;
int err;
unsigned int rate;
snd_pcm_uframes_t start_threshold, stop_threshold;
snd_pcm_hw_params_alloca(&hwparams);
snd_pcm_sw_params_alloca(&swparams);
err = snd_pcm_hw_params_any(alsa_params->handle, hwparams);
if (err < 0) {
adec_print("Broken configuration for this PCM: no configurations available");
return err;
}
err = snd_pcm_hw_params_set_access(alsa_params->handle, hwparams,
SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) {
adec_print("Access type not available");
return err;
}
err = snd_pcm_hw_params_set_format(alsa_params->handle, hwparams, alsa_params->format);
if (err < 0) {
adec_print("Sample format non available");
return err;
}
err = snd_pcm_hw_params_set_channels(alsa_params->handle, hwparams, alsa_params->channelcount);
if (err < 0) {
adec_print("Channels count non available");
return err;
}
rate = alsa_params->rate;
err = snd_pcm_hw_params_set_rate_near(alsa_params->handle, hwparams, &alsa_params->rate, 0);
assert(err >= 0);
#if 0
err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
assert(err >= 0);
if (buffer_time > 500000) {
buffer_time = 500000;
}
period_time = buffer_time / 4;
err = snd_pcm_hw_params_set_period_time_near(handle, hwparams,
&period_time, 0);
assert(err >= 0);
err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams,
&buffer_time, 0);
assert(err >= 0);
#endif
alsa_params->bits_per_sample = snd_pcm_format_physical_width(alsa_params->format);
//bits_per_frame = bits_per_sample * hwparams.realchanl;
alsa_params->bits_per_frame = alsa_params->bits_per_sample * alsa_params->channelcount;
bufsize = PERIOD_NUM*PERIOD_SIZE*4;
err = snd_pcm_hw_params_set_buffer_size_near(alsa_params->handle, hwparams,&bufsize);
if (err < 0) {
adec_print("Unable to set buffer size \n");
return err;
}
err = snd_pcm_hw_params_set_period_size_near(alsa_params->handle, hwparams, &chunk_size, NULL);
if (err < 0) {
adec_print("Unable to set period size \n");
return err;
}
#if 0
err = snd_pcm_hw_params_set_periods_near(alsa_params->handle, hwparams, &fragcount, NULL);
if (err < 0) {
adec_print("Unable to set periods \n");
return err;
}
#endif
err = snd_pcm_hw_params(alsa_params->handle, hwparams);
if (err < 0) {
adec_print("Unable to install hw params:");
return err;
}
err = snd_pcm_hw_params_get_buffer_size(hwparams, &bufsize);
if (err < 0) {
adec_print("Unable to get buffersize \n");
return err;
}
adec_print("[%s::%d]--[alsa raw buffer frame size:%d]\n", __FUNCTION__, __LINE__,bufsize);
alsa_params->buffer_size = bufsize * alsa_params->bits_per_frame / 8;
#if 1
//.........这里部分代码省略.........
开发者ID:mdrjr,项目名称:c2_aml_libs,代码行数:101,代码来源:alsa-out-raw.c
示例7: snd_pcm_close
/**
* @brief Setup the ALSA handle to the audio device
*/
void AlsaPlayback::setupHandle()
{
if(alsa_handle)
{
snd_pcm_close(alsa_handle);
alsa_handle = NULL;
}
if(snd_pcm_open(&alsa_handle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0)
{
TRACE("Unable to open playback device!!\n");
alsa_handle = NULL;
}
else
{
int rc = -1;
snd_pcm_hw_params_t *params = NULL;
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_malloc(¶ms);
if(NULL != params)
{
int dir = 0;
/* Fill it in with default values. */
snd_pcm_hw_params_any(alsa_handle, params);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(alsa_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(alsa_handle, params, SND_PCM_FORMAT_S16_LE);
/* One channel (mono) */
snd_pcm_hw_params_set_channels(alsa_handle, params, 1);
/* set sampling rate */
snd_pcm_hw_params_set_rate_near(alsa_handle, params, &AlsaPlayback::_sample_rate, &dir);
/* Set period size to 128 frames (samples) */
frames = AlsaPlayback::FRAME_PERIOD;
snd_pcm_hw_params_set_period_size_near(alsa_handle, params, &frames, &dir);
snd_pcm_uframes_t buf_size = MINIMUM_SAMPLE_SET_SIZE;
snd_pcm_hw_params_set_buffer_size_near(alsa_handle, params, &buf_size);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(alsa_handle, params);
if(rc >= 0)
{
TRACE("AlsaPlayback init completed successfully..\n");
}
else
{
snd_pcm_close(alsa_handle);
alsa_handle = NULL;
TRACE("AlsaPlayback init failed\n");
}
snd_pcm_hw_params_free(params);
params = NULL;
}
else
{
TRACE("playAudio - snd_pcm_hw_params_alloc() failed\n");
}
}
}
开发者ID:AnXi-TieGuanYin-Tea,项目名称:Multi-Channel-Audio-Mixer,代码行数:74,代码来源:AlsaAudioPlayback.cpp
示例8: create_recorder
void create_recorder(int samp_rate,char* dev_name,int nchan,void **capture_handle_f)
{
int err;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
printf("From C: *capture_handle_f=%p\n",*capture_handle_f);
if ((err = snd_pcm_open ((snd_pcm_t**)capture_handle_f, dev_name,
SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
dev_name,
snd_strerror (err));
fprintf(stderr, "Hint: Use \"arecord -l\" to list recording devices.\n");
exit (1);
}
capture_handle = (snd_pcm_t*)(*capture_handle_f);
printf("made handle %p (&=%p) to %s at %d\n",capture_handle,&capture_handle,dev_name,samp_rate);
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
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));
exit (1);
}
if ((err = snd_pcm_hw_params_set_format (capture_handle,
hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_rate_resample(capture_handle,hw_params, 0)) < 0) {
fprintf(stderr, "failed attempting to prevent ALSA resampling. (%s)",
snd_strerror(err));
exit (1);
}
int dir = 0;
unsigned int val = samp_rate;
if ((err = snd_pcm_hw_params_set_rate_near (capture_handle,
hw_params, &val, &dir)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, nchan)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
exit (1);
}
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));
exit (1);
}
/*
int nbuf = 8000;
short *buf = (short*)malloc(sizeof(short)*nbuf);
int i;
for (i=0; i<2; ++i) {
if ((err = snd_pcm_readi(capture_handle, buf, nbuf)) != nbuf) {
fprintf (stderr, "read from audio interface failed (%s)\n",
snd_strerror(err));
exit (1);
}
else {
printf("read from C using %p into %p.\n",capture_handle,buf);
}
}
*/
}
开发者ID:lurobi,项目名称:pecanpi,代码行数:96,代码来源:alsa_pcm_read_simple.c
示例9: main
int main()
{
long loops;
int rc,i = 0;
int size;
FILE *fp ;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val,val2;
int dir;
snd_pcm_uframes_t frames;
char *buffer;
if( (fp =fopen("sound.wav","w")) < 0)
printf("open sound.wav fial\n");
/* Open PCM device for recording (capture). */
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);
}
/* 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 */
printf("size = %d\n",size);
buffer = (char *) malloc(size);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params, &val, &dir);
loops = 10000000 / val;
while (loops > 0)
{
loops--;
rc = snd_pcm_readi(handle, buffer, frames);
printf("%d\n",i++);
if (rc == -EPIPE)
{
/* EPIPE means overrun */
fprintf(stderr, "overrun occurred/n");
snd_pcm_prepare(handle);
}
else if (rc < 0)
{
fprintf(stderr,
"error from read: %s/n",
snd_strerror(rc));
}
else if (rc != (int)frames)
{
fprintf(stderr, "short read, read %d frames/n", rc);
}
//rc = fwrite( buffer,1, size,fp);
rc = write(1,buffer,size);
if (rc != size)
fprintf(stderr, "short write: wrote %d bytes/n", rc);
else printf("fwrite buffer success\n");
}
/******************打印参数*********************/
snd_pcm_hw_params_get_channels(params, &val);
printf("channels = %d\n", val);
snd_pcm_hw_params_get_rate(params, &val, &dir);
printf("rate = %d bps\n", val);
snd_pcm_hw_params_get_period_time(params,
&val, &dir);
printf("period time = %d us\n", val);
snd_pcm_hw_params_get_period_size(params,
&frames, &dir);
printf("period size = %d frames\n", (int)frames);
snd_pcm_hw_params_get_buffer_time(params,
&val, &dir);
printf("buffer time = %d us\n", val);
snd_pcm_hw_params_get_buffer_size(params,
(snd_pcm_uframes_t *) &val);
//.........这里部分代码省略.........
开发者ID:renhardly,项目名称:learngit,代码行数:101,代码来源:capture.c
示例10: main
int main(int argc, char *argv[])
{
ros::init(argc, argv, "cmd_control");
ros::NodeHandle n;
ros::Publisher motor_pub = n.advertise<std_msgs::Char>("motor_chatter", 1000);
std::string port;
ros::param::param<std::string>("~port", port, "/dev/ttyACM0");
int baud;
ros::param::param<int>("~baud", baud, 57600);
int byte_per_sample = bits_per_sample >> 3;
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;
char *speechbuf;
/* Open PCM device for recording (capture). */
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);
}
/* 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, channels);
/* 44100 bits/second sampling rate (CD quality) */
//val = 44100;
val = sample_rate;
snd_pcm_hw_params_set_rate_near(handle, params,
&val, &dir);
/* Set period size to 32 frames. */
frames = frames_per_period;
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 * channels * byte_per_sample; /* 2 bytes/sample, 1 channels */
speechbuf = (char *) malloc(size);
//printf("buffer size %d\n", size);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params,
&val, &dir);
int client_sockfd;
struct sockaddr_in remote_addr; //服务器端网络地址结构体
memset(&remote_addr,0,sizeof(remote_addr)); //数据初始化--清零
remote_addr.sin_family=AF_INET; //设置为IP通信
remote_addr.sin_addr.s_addr=inet_addr("10.0.1.0");//服务器IP地址
remote_addr.sin_port=htons(port); //服务器端口号
/*创建客户端套接字--IPv4协议,面向连接通信,TCP协议*/
if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
{
perror("socket failed\n");
return 1;
}
/*将套接字绑定到服务器的网络地址上*/
if(connect(client_sockfd,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0)
{
perror("connect failed\n");
return 1;
//.........这里部分代码省略.........
开发者ID:Yvaine,项目名称:speech-robot,代码行数:101,代码来源:recvcmd.cpp
示例11: AudioOutputDevice
/**
* Create and initialize Alsa audio output device with given parameters.
*
* @param Parameters - optional parameters
* @throws AudioOutputException if output device cannot be opened
*/
AudioOutputDeviceAlsa::AudioOutputDeviceAlsa(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(Parameters), Thread(true, true, 1, 0) {
pcm_handle = NULL;
stream = SND_PCM_STREAM_PLAYBACK;
this->uiAlsaChannels = ((DeviceCreationParameterInt*)Parameters["CHANNELS"])->ValueAsInt();
this->uiSamplerate = ((DeviceCreationParameterInt*)Parameters["SAMPLERATE"])->ValueAsInt();
this->FragmentSize = ((DeviceCreationParameterInt*)Parameters["FRAGMENTSIZE"])->ValueAsInt();
uint Fragments = ((DeviceCreationParameterInt*)Parameters["FRAGMENTS"])->ValueAsInt();
String Card = ((DeviceCreationParameterString*)Parameters["CARD"])->ValueAsString();
dmsg(2,("Checking if hw parameters supported...\n"));
if (HardwareParametersSupported(Card, uiAlsaChannels, uiSamplerate, Fragments, FragmentSize)) {
pcm_name = "hw:" + Card;
}
else {
fprintf(stderr, "Warning: your soundcard doesn't support chosen hardware parameters; ");
fprintf(stderr, "trying to compensate support lack with plughw...");
fflush(stdout);
pcm_name = "plughw:" + Card;
}
dmsg(2,("HW check completed.\n"));
int err;
snd_pcm_hw_params_alloca(&hwparams); // Allocate the snd_pcm_hw_params_t structure on the stack.
/* Open PCM. The last parameter of this function is the mode. */
/* If this is set to 0, the standard mode is used. Possible */
/* other values are SND_PCM_NONBLOCK and SND_PCM_ASYNC. */
/* If SND_PCM_NONBLOCK is used, read / write access to the */
/* PCM device will return immediately. If SND_PCM_ASYNC is */
/* specified, SIGIO will be emitted whenever a period has */
/* been completely processed by the soundcard. */
if ((err = snd_pcm_open(&pcm_handle, pcm_name.c_str(), stream, 0)) < 0) {
throw AudioOutputException(String("Error opening PCM device ") + pcm_name + ": " + snd_strerror(err));
}
if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0) {
throw AudioOutputException(String("Error, cannot initialize hardware parameter structure: ") + snd_strerror(err));
}
/* Set access type. This can be either */
/* SND_PCM_ACCESS_RW_INTERLEAVED or */
/* SND_PCM_ACCESS_RW_NONINTERLEAVED. */
if ((err = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
throw AudioOutputException(String("Error snd_pcm_hw_params_set_access: ") + snd_strerror(err));
}
/* Set sample format */
#if WORDS_BIGENDIAN
if ((err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_BE)) < 0)
#else // little endian
if ((err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE)) < 0)
#endif
{
throw AudioOutputException(String("Error setting sample format: ") + snd_strerror(err));
}
int dir = 0;
/* Set sample rate. If the exact rate is not supported */
/* by the hardware, use nearest possible rate. */
#if ALSA_MAJOR > 0
if((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &uiSamplerate, &dir)) < 0)
#else
if((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, uiSamplerate, &dir)) < 0)
#endif
{
throw AudioOutputException(String("Error setting sample rate: ") + snd_strerror(err));
}
if ((err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, uiAlsaChannels)) < 0) {
throw AudioOutputException(String("Error setting number of channels: ") + snd_strerror(err));
}
/* Set number of periods. Periods used to be called fragments. */
if ((err = snd_pcm_hw_params_set_periods(pcm_handle, hwparams, Fragments, dir)) < 0) {
throw AudioOutputException(String("Error setting number of ") + ToString(Fragments) + " periods: " + snd_strerror(err));
}
/* Set buffer size (in frames). The resulting latency is given by */
/* latency = periodsize * periods / (rate * bytes_per_frame) */
if ((err = snd_pcm_hw_params_set_buffer_size(pcm_handle, hwparams, (FragmentSize * Fragments))) < 0) {
throw AudioOutputException(String("Error setting buffersize: ") + snd_strerror(err));
}
/* Apply HW parameter settings to */
/* PCM device and prepare device */
if ((err = snd_pcm_hw_params(pcm_handle, hwparams)) < 0) {
throw AudioOutputException(String("Error setting HW params: ") + snd_strerror(err));
}
if (snd_pcm_sw_params_malloc(&swparams) != 0) {
throw AudioOutputException(String("Error in snd_pcm_sw_params_malloc: ") + snd_strerror(err));
}
//.........这里部分代码省略.........
开发者ID:svn2github,项目名称:linuxsampler,代码行数:101,代码来源:AudioOutputDeviceAlsa.cpp
示例12: 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 );
}
//.........这里部分代码省略.........
开发者ID:AHudon,项目名称:lmms,代码行数:101,代码来源:AudioAlsa.cpp
示例13: pcm_init
void pcm_init()
{
int n, m, err;
snd_pcm_hw_params_t *hw_params;
if (!sound)
{
pcm.hz = 11025;
pcm.len = 4096;
pcm.buf = malloc(pcm.len);
pcm.pos = 0;
playback_handle = NULL;
return;
}
if (!dsp_device) dsp_device = strdup(DSP_DEVICE);
if ((err = snd_pcm_open (&playback_handle, dsp_device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
dsp_device,
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_U8)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, samplerate, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, stereo ? 2 : 1)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
exit (1);
}
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (playback_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
pcm.stereo = stereo;
pcm.hz = samplerate;
pcm.len = 4096;
pcm.buf = malloc(pcm.len);
}
开发者ID:geekmaster,项目名称:fbgnuboy,代码行数:79,代码来源:alsa.c
示例14: init_alsa
int init_alsa(unsigned int channels, unsigned sample_rate,
snd_pcm_format_t format)
{
int ret;
snd_pcm_hw_params_t *hw_params;
playback_handle = NULL;
ret = snd_pcm_open(&playback_handle, "default", SND_PCM_STREAM_PLAYBACK,
0);
if (ret < 0) {
fprintf(stderr, "can NOT open soundcard\n");
goto fail;
}
ret = snd_pcm_hw_params_malloc(&hw_params);
if (ret < 0) {
fprintf(stderr, "can NOT allocate hardware paramter structure (%s)\n",
snd_strerror(ret));
goto fail;
}
ret = snd_pcm_hw_params_any(playback_handle, hw_params);
if (ret < 0) {
fprintf(stderr, "can NOT initialize hardware paramter structure (%s)\n",
snd_strerror(ret));
goto fail;
}
ret = snd_pcm_hw_params_set_access(playback_handle, hw_params,
SND_PCM_ACCESS_RW_INTERLEAVED);
if (ret < 0) {
fprintf(stderr, "can NOT set access type (%s)\n", snd_strerror(ret));
goto fail;
}
ret = snd_pcm_hw_params_set_format(playback_handle, hw_params, format);
if (ret < 0) {
fprintf(stderr, "can NOT set sample format (%s)\n", snd_strerror(ret));
goto fail;
}
ret = snd_pcm_hw_params_set_rate_near(playback_handle, hw_params,
&sample_rate, 0);
if (ret < 0) {
fprintf(stderr, "can NOT set sample rate (%s)\n", snd_strerror(ret));
goto fail;
}
ret = snd_pcm_hw_params_set_channels(playback_handle, hw_params, channels);
if (ret < 0) {
fprintf(stderr, "can NOT set channels (%s)\n", snd_strerror(ret));
goto fail;
}
snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
buffer_size = buffer_size < ALSA_BUFFER_SIZE_MAX ?
buffer_size : ALSA_BUFFER_SIZE_MAX;
ret = snd_pcm_hw_params_set_buffer_size_near(playback_handle, hw_params,
&buffer_size);
if (ret < 0) {
fprintf(stderr, "can NOT set alsa buffer size (%s)\n", snd_strerror(ret));
goto fail;
}
snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
if (!period_size)
period_size = buffer_size / 4;
ret = snd_pcm_hw_params_set_period_size_near(playback_handle, hw_params,
&period_size, NULL);
if (ret < 0) {
fprintf(stderr, "can NOT set alsa period size (%s)\n", snd_strerror(ret));
goto fail;
}
ret = snd_pcm_hw_params(playback_handle, hw_params);
if (ret < 0) {
fprintf(stderr, "can NOT set parameters (%s)\n", snd_strerror(ret));
goto fail;
}
snd_pcm_hw_params_free(hw_params);
audio_channels = channels;
audio_sample_rate = sample_rate;
audio_format = format;
ret = 0;
return ret;
fail:
if (playback_handle) {
snd_pcm_close(playback_handle);
playback_handle = NULL;
}
return ret;
}
开发者ID:fedoracdu,项目名称:tyl_player,代码行数:96,代码来源:alsa.c
示例15: sizeof
static void *alsa_init(const char *device, unsigned rate, unsigned latency)
{
alsa_t *alsa = (alsa_t*)calloc(1, sizeof(alsa_t));
if (!alsa)
return NULL;
snd_pcm_hw_params_t *params = NULL;
snd_pcm_sw_params_t *sw_params = NULL;
unsigned latency_usec = latency * 1000;
unsigned channels = 2;
unsigned periods = 4;
snd_pcm_format_t format;
const char *alsa_dev = "default";
if (device)
alsa_dev = device;
snd_pcm_uframes_t buffer_size;
TRY_ALSA(snd_pcm_open(&alsa->pcm, alsa_dev, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK));
TRY_ALSA(snd_pcm_hw_params_malloc(¶ms));
alsa->has_float = find_float_format(alsa->pcm, params);
format = alsa->has_float ? SND_PCM_FORMAT_FLOAT : SND_PCM_FORMAT_S16;
TRY_ALSA(snd_pcm_hw_params_any(alsa->pcm, params));
TRY_ALSA(snd_pcm_hw_params_set_access(alsa
|
请发表评论