本文整理汇总了C++中snd_pcm_prepare函数的典型用法代码示例。如果您正苦于以下问题:C++ snd_pcm_prepare函数的具体用法?C++ snd_pcm_prepare怎么用?C++ snd_pcm_prepare使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snd_pcm_prepare函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: an_configure_capture_card
//.........这里部分代码省略.........
// 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 )
{
loggerf("Unable to open sound device");
return;
}
unsigned int rate = 48000;
int r;
r = snd_pcm_hw_params_malloc(&hw_params);
r = snd_pcm_hw_params_any(m_audio_handle, hw_params);
r = snd_pcm_hw_params_set_access(m_audio_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
r = snd_pcm_hw_params_set_format(m_audio_handle, hw_params, SND_PCM_FORMAT_S16_LE);
r = snd_pcm_hw_params_set_rate_near(m_audio_handle, hw_params, &rate, 0);
r = snd_pcm_hw_params_set_channels(m_audio_handle, hw_params, 2);
r = snd_pcm_hw_params(m_audio_handle, hw_params);
snd_pcm_hw_params_free(hw_params);
r = snd_pcm_prepare(m_audio_handle);
an_init_codecs();
m_capturing = true;
an_setup_video_capturing( m_i_fd );
an_setup_audio_capturing();
}
示例2: 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;
}
示例3: main
int main()
{
int fp;
unsigned int pcm, tmp, dir;
int buff_size;
long loops;
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
snd_pcm_uframes_t frames;
char *buff;
int rate, channels, seconds;
/* 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_U8);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 1);
/* 44100 bits/second sampling rate (CD quality) */
val = 8000;
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 buff large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
size = frames * 4; /* 2 bytes/sample, 2 channels */
buff = (char *) malloc(size);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params, &val, &dir);
loops = 50000000 / val;
fp=open("222.wav",O_WRONLY);
while (loops > 0)
{
loops--;
bzero(buff,sizeof(buff));
rc = snd_pcm_readi(handle, buff, frames);
write(fp,buff,128);
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, buff, size);
if (rc != size)
fprintf(stderr,"short write: wrote %d bytes\n", rc);
*/
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
//.........这里部分代码省略.........
示例4: main
// alsa
main (int argc, char *argv[])
{
int i;
int err;
short buf[128];
snd_pcm_t *playback_handle;
snd_pcm_hw_params_t *hw_params;
if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
argv[1],
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_S16_LE)) < 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, 44100, 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, 2)) < 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);
}
for (i = 0; i < 10; ++i) {
if ((err = snd_pcm_writei (playback_handle, buf, 128)) != 128) {
fprintf (stderr, "write to audio interface failed (%s)\n",
snd_strerror (err));
exit (1);
}
}
snd_pcm_close (playback_handle);
exit (0);
}
示例5: sound_thread
static void* sound_thread(void* context)
{
struct SPU* c = (struct SPU*)context;
int div;
snd_pcm_t* snd;
snd_pcm_hw_params_t* hwp;
int rate = c->sampling;
int periods = 3;
snd_pcm_hw_params_alloca(&hwp);
if (snd_pcm_open(&snd, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
return NULL;
}
if (snd_pcm_hw_params_any(snd, hwp) < 0) {
return NULL;
}
div = c->bit / 8;
switch (c->bit) {
case 8:
if (snd_pcm_hw_params_set_format(snd, hwp, SND_PCM_FORMAT_U8) < 0) {
return NULL;
}
break;
case 16:
if (snd_pcm_hw_params_set_format(snd, hwp, SND_PCM_FORMAT_S16_LE) < 0) {
return NULL;
}
break;
case 24:
if (snd_pcm_hw_params_set_format(snd, hwp, SND_PCM_FORMAT_S24_LE) < 0) {
return NULL;
}
break;
case 32:
if (snd_pcm_hw_params_set_format(snd, hwp, SND_PCM_FORMAT_S32_LE) < 0) {
return NULL;
}
break;
default:
return NULL;
}
if (snd_pcm_hw_params_set_rate_near(snd, hwp, &rate, 0) < 0) {
return NULL;
}
if (rate != SAMPLE_RATE) {
return NULL;
}
if (snd_pcm_hw_params_set_channels(snd, hwp, c->ch) < 0) {
return NULL;
}
if (snd_pcm_hw_params_set_periods(snd, hwp, periods, 0) < 0) {
return NULL;
}
if (snd_pcm_hw_params_set_buffer_size(snd, hwp, (periods * c->size) / 4) < 0) {
return NULL;
}
if (snd_pcm_hw_params(snd, hwp) < 0) {
return NULL;
}
while (c->alive) {
c->callback(c->buffer, c->size);
while (c->alive) {
if (snd_pcm_writei(snd, c->buffer, c->size / div) < 0) {
snd_pcm_prepare(snd);
} else {
break;
}
}
}
snd_pcm_drain(snd);
snd_pcm_close(snd);
return NULL;
}
示例6: main
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;
unsigned char *buffer;
int i=0;
/* socket setting */
int sd;
struct sockaddr_in s_addr;
int n, n_send, status;
int on = 1;
sd = socket (AF_INET, SOCK_DGRAM, 0);
bzero(&s_addr, sizeof(s_addr));
s_addr.sin_family = AF_INET;
s_addr.sin_addr.s_addr = inet_addr("192.168.42.255");
s_addr.sin_port = htons(2007);
if((status = setsockopt(sd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on))) != 0 )
{
printf("setsockopt error\n");
exit(-1);
}
/* Open PCM device for recording (capture). */
rc = snd_pcm_open(&handle, "plughw:1,0",
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_U8);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 1);
/* 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 =4800;
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 *1; /* 2 bytes/sample, 2 channels */
buffer = (unsigned char*) malloc(size);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params,
&val, &dir);
while (1) {
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",
//.........这里部分代码省略.........
示例7: play
void play(int sec, int freq, char *file)
{
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;
int fd;
/* Check ranges */
if (sec < 1 || sec > 4000) {
printf("WARNING: Incorrect time to play range [1,4000] s\n");
printf("\tSetting time to play to 5s...\n");
sec = 5;
}
if (freq < 1000 || freq > 100000) {
printf("ERROR: Incorrect frequency range [1000,100000] Hz\n");
printf("\tSetting frequency to 44.1 kHz...\n");
freq = 44100;
}
/* Open file */
fd = open(file, O_RDONLY);
if (fd < 0) {
/* There was an error opening the file */
printf("ERROR: Couldn't open file to play\n");
printf("\tPlease make sure file exists\n");
} else {
/* Print that the file is playing with its parameters */
printf("Playing file %s for %d seconds", file, sec);
printf(" and frequency %d...\n", freq);
/* 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 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);
/* freq bits/second sampling rate (CD quality) */
val = freq;
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 sec seconds */
snd_pcm_hw_params_get_period_time(params, &val, &dir);
/* sec seconds in microseconds divided by period time */
loops = sec*1000000 / val;
while (loops > 0) {
loops--;
rc = read(fd, 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) {
//.........这里部分代码省略.........
示例8: AlsaOutput_Write
/*----------------------------------------------------------------------
| AlsaOutput_Write
+---------------------------------------------------------------------*/
static BLT_Result
AlsaOutput_Write(AlsaOutput* self, void* buffer, BLT_Size size)
{
int watchdog = 5;
int io_result;
unsigned int sample_count;
unsigned int sample_size;
BLT_Result result;
/* ensure that the device is prepared */
result = AlsaOutput_Prepare(self);
if (BLT_FAILED(result)) return result;
/* compute the number of samples */
sample_size = self->media_type.channel_count*self->media_type.bits_per_sample/8;
sample_count = size / sample_size;
/* write samples to the device and handle underruns */
do {
while (sample_count) {
io_result = snd_pcm_writei(self->device_handle,
buffer, sample_count);
if (io_result > 0) {
buffer = (void*)((char*)buffer + io_result*sample_size);
if ((unsigned int)io_result <= sample_count) {
sample_count -= io_result;
} else {
/* strange, snd_pcm_writei returned more than we wrote */
sample_count = 0;
}
} else {
break;
}
}
if (sample_count == 0) return BLT_SUCCESS;
/* we reach this point if the first write failed */
if (io_result < 0) {
snd_pcm_status_t* status;
snd_pcm_state_t state;
snd_pcm_status_alloca_no_assert(&status);
io_result = snd_pcm_status(self->device_handle, status);
if (io_result != 0) {
return BLT_FAILURE;
}
state = snd_pcm_status_get_state(status);
if (state == SND_PCM_STATE_XRUN) {
ATX_LOG_FINE("**** UNDERRUN *****");
/* re-prepare the channel */
io_result = snd_pcm_prepare(self->device_handle);
if (io_result != 0) {
return BLT_FAILURE;
}
} else {
ATX_LOG_WARNING_1("**** STATE = %d ****", state);
}
} else {
ATX_LOG_WARNING_1("snd_pcm_writei() returned %d", io_result);
}
ATX_LOG_FINE("**** RETRY *****");
} while(watchdog--);
ATX_LOG_SEVERE("**** THE WATCHDOG BIT US ****");
return BLT_FAILURE;
}
示例9: pthread_getschedparam
void *audiothread(void *v) {
int policy;
sched_param sched;
int err;
alsaaudio *dev;
pthread_getschedparam(pthread_self(),&policy,&sched);
sched.sched_priority++;//policy=SCHED_RR;
pthread_setschedparam(pthread_self(),policy,&sched);
dev=(alsaaudio*)v;
unsigned int val;
snd_pcm_t *fd;
snd_pcm_uframes_t periodsize;
snd_pcm_hw_params_t *hwparams;
snd_pcm_hw_params_alloca(&hwparams);
int output_rate;
int channels;
int fragment_size;
int fragment_count;
err=snd_pcm_open(&fd, strdup("default"), SND_PCM_STREAM_PLAYBACK, 0);
if (err<0) return -1;
fragment_size=LINUXFRAG; //overall buffer size
fragment_count=2; //2 - 16 fragment count - 2 minimum, the lower it is potentially the lower the latency
//configure device
if (snd_pcm_hw_params_any(fd, hwparams) < 0) {
//printf("linuxaudio failed at params any\n");
return -1;
}
if (snd_pcm_hw_params_set_access(fd, hwparams,SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
//printf("linuxaudio failed at set access\n");
return -1;
}
if (snd_pcm_hw_params_set_format(fd, hwparams,SND_PCM_FORMAT_S16_LE) < 0) {
//printf("linuxaudio failed at set format\n");
return -1;
}
val = 44100;
if (snd_pcm_hw_params_set_rate_near(fd, hwparams,&val, 0) < 0) {
// Try 48KHZ too
//printf("linuxaudio - %d HZ not available, trying 48000HZ\n", output_rate);
val = 48000;
if (snd_pcm_hw_params_set_rate_near(fd, hwparams,&val, 0) < 0) {
//printf("linuxaudio failed at setting output rate (%d)\n", output_rate);
return -1;
}
dev->mix->freq=val;
}
channels=2;
if (snd_pcm_hw_params_set_channels(fd, hwparams, channels) < 0) {
//printf("linuxaudio failed at set channels (%d)\n", channels);
return -1;
}
periodsize = (fragment_size) / 4; // bytes -> frames for 16-bit,stereo - should be a minimum of 512
if (snd_pcm_hw_params_set_period_size_near(fd, hwparams,&periodsize, 0) < 0) {
//printf("linuxaudio failed at set period size (%d)\n", (int)periodsize);
return -1;
}
val = fragment_count;
if (snd_pcm_hw_params_set_periods_near(fd, hwparams,&val, 0) < 0) {
//printf("linuxaudio failed at set periods (%d)\n", val);
//should attempt a one by one period increase up to 16?
return -1;
}
if (snd_pcm_hw_params(fd, hwparams) < 0) {
//printf("linuxaudio failed at installing hw params\n");
return -1;
}
//loop while playing sound
dev->playing=1;
while (dev->playing)
{
dev->mix->mix16(dev->buffer);
if ((snd_pcm_writei (fd, dev->buffer,LINUXFRAG/2)) < 0) { //Half buffer for two channels?
//printf ("linuxaudio warning: buffer underrun occurred\n");
if (snd_pcm_prepare(fd) < 0) {
//printf ("linuxaudio failed at preparing pcm\n");
dev->playing=0; //die gracefully
}
}
}
snd_pcm_drop(fd);
snd_pcm_close (fd);
return 0;
}
示例10: alsa_open
//.........这里部分代码省略.........
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 %d", 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));
snd_pcm_hw_params_free (hw_params);
return 0;
}
if ((err = snd_pcm_hw_params_set_buffer_time_near(handle, hw_params,
&buffer_time, 0)) < 0) {
error ("Can't set buffer time: %s", snd_strerror(err));
snd_pcm_hw_params_free (hw_params);
return 0;
}
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
error ("Can't set audio parameters: %s", snd_strerror(err));
snd_pcm_hw_params_free (hw_params);
return 0;
}
snd_pcm_hw_params_get_period_size (hw_params, &chunk_frames, 0);
snd_pcm_hw_params_get_buffer_size (hw_params, &buffer_frames);
bytes_per_frame = sound_params->channels
* sfmt_Bps(sound_params->fmt);
logit ("Buffer time: %ldus", buffer_frames * bytes_per_frame);
if (chunk_frames == buffer_frames) {
error ("Can't use period equal to buffer size (%lu == %lu)",
chunk_frames, buffer_frames);
snd_pcm_hw_params_free (hw_params);
return 0;
}
chunk_size = chunk_frames * bytes_per_frame;
debug ("Chunk size: %d", chunk_size);
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare(handle)) < 0) {
error ("Can't prepare audio interface for use: %s",
snd_strerror(err));
return 0;
}
debug ("ALSA device initialized");
params.channels = sound_params->channels;
alsa_buf_fill = 0;
return 1;
}
示例11: ca_thread_func
static int ca_thread_func (void *arg)
{
struct alsa_stream* stream = (struct alsa_stream*) arg;
snd_pcm_t* pcm = stream->ca_pcm;
int size = stream->ca_buf_size;
snd_pcm_uframes_t nframes = stream->ca_frames;
void* user_data = stream->user_data;
char* buf = stream->ca_buf;
pj_timestamp tstamp;
int result;
struct sched_param param;
pthread_t* thid;
thid = (pthread_t*) pj_thread_get_os_handle (pj_thread_this());
param.sched_priority = sched_get_priority_max (SCHED_RR);
PJ_LOG (5,(THIS_FILE, "ca_thread_func(%u): Set thread priority "
"for audio capture thread.",
(unsigned)syscall(SYS_gettid)));
result = pthread_setschedparam (*thid, SCHED_RR, ¶m);
if (result) {
if (result == EPERM)
PJ_LOG (5,(THIS_FILE, "Unable to increase thread priority, "
"root access needed."));
else
PJ_LOG (5,(THIS_FILE, "Unable to increase thread priority, "
"error: %d",
result));
}
pj_bzero (buf, size);
tstamp.u64 = 0;
TRACE_((THIS_FILE, "ca_thread_func(%u): Started",
(unsigned)syscall(SYS_gettid)));
snd_pcm_prepare (pcm);
while (!stream->quit) {
pjmedia_frame frame;
pj_bzero (buf, size);
result = snd_pcm_readi (pcm, buf, nframes);
if (result == -EPIPE) {
PJ_LOG (4,(THIS_FILE, "ca_thread_func: overrun!"));
snd_pcm_prepare (pcm);
continue;
} else if (result < 0) {
PJ_LOG (4,(THIS_FILE, "ca_thread_func: error reading data!"));
}
if (stream->quit)
break;
frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
frame.buf = (void*) buf;
frame.size = size;
frame.timestamp.u64 = tstamp.u64;
frame.bit_info = 0;
result = stream->ca_cb (user_data, &frame);
if (result != PJ_SUCCESS || stream->quit)
break;
tstamp.u64 += nframes;
}
snd_pcm_drain (pcm);
TRACE_((THIS_FILE, "ca_thread_func: Stopped"));
return PJ_SUCCESS;
}
示例12: main
//.........这里部分代码省略.........
snd_pcm_subformat_name((snd_pcm_subformat_t)val),
snd_pcm_subformat_description(
(snd_pcm_subformat_t)val));
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);
printf("buffer size = %d frames\n", val);
snd_pcm_hw_params_get_periods(params, &val, &dir);
printf("periods per buffer = %d frames\n", val);
snd_pcm_hw_params_get_rate_numden(params,
&val, &val2);
printf("exact rate = %d/%d bps\n", val, val2);
val = snd_pcm_hw_params_get_sbits(params);
printf("significant bits = %d\n", val);
val = snd_pcm_hw_params_is_batch(params);
printf("is batch = %d\n", val);
val = snd_pcm_hw_params_is_block_transfer(params);
printf("is block transfer = %d\n", val);
val = snd_pcm_hw_params_is_double(params);
printf("is double = %d\n", val);
val = snd_pcm_hw_params_is_half_duplex(params);
printf("is half duplex = %d\n", val);
val = snd_pcm_hw_params_is_joint_duplex(params);
printf("is joint duplex = %d\n", val);
val = snd_pcm_hw_params_can_overrange(params);
printf("can overrange = %d\n", val);
val = snd_pcm_hw_params_can_mmap_sample_resolution(params);
printf("can mmap = %d\n", val);
val = snd_pcm_hw_params_can_pause(params);
printf("can pause = %d\n", val);
val = snd_pcm_hw_params_can_resume(params);
printf("can resume = %d\n", val);
val = snd_pcm_hw_params_can_sync_start(params);
printf("can sync start = %d\n", val);
settings = open_pitch_tracker();
while(1){
rc = snd_pcm_readi(handle, buf, 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);
}
for( i = 0 ; i< BUFSIZE ; i++){
d_buffer[i] = (double) buf[i];
}
pitch = compute_pitch(d_buffer, BUFSIZE, S16, settings ,ACCURACY);
if( pitch != 0.0 )
printf("frequency -> %f\n",pitch);
memset(buf,0,BUFSIZE);
}
close_pitch_tracker(&settings);
snd_pcm_drain(handle);
snd_pcm_close(handle);
return 0;
}
示例13: snd_pcm_drop
void WaveRecorder::stop()
{
snd_pcm_drop(_pcm);
snd_pcm_prepare(_pcm);
}
示例14: GetChannelLayout
bool CAESinkALSA::Initialize(AEAudioFormat &format, std::string &device)
{
m_initDevice = device;
m_initFormat = format;
/* if we are raw, correct the data format */
if (AE_IS_RAW(format.m_dataFormat))
{
m_channelLayout = GetChannelLayout(format);
format.m_dataFormat = AE_FMT_S16NE;
m_passthrough = true;
}
else
{
m_channelLayout = GetChannelLayout(format);
m_passthrough = false;
}
#if defined(HAS_AMLPLAYER) || defined(HAS_LIBAMCODEC)
if (aml_present())
{
aml_set_audio_passthrough(m_passthrough);
device = "default";
}
#endif
if (m_channelLayout.Count() == 0)
{
CLog::Log(LOGERROR, "CAESinkALSA::Initialize - Unable to open the requested channel layout");
return false;
}
format.m_channelLayout = m_channelLayout;
AEDeviceType devType = AEDeviceTypeFromName(device);
std::string AESParams;
/* digital interfaces should have AESx set, though in practice most
* receivers don't care */
if (m_passthrough || devType == AE_DEVTYPE_HDMI || devType == AE_DEVTYPE_IEC958)
GetAESParams(format, AESParams);
CLog::Log(LOGINFO, "CAESinkALSA::Initialize - Attempting to open device \"%s\"", device.c_str());
/* get the sound config */
snd_config_t *config;
snd_config_copy(&config, snd_config);
if (!OpenPCMDevice(device, AESParams, m_channelLayout.Count(), &m_pcm, config))
{
CLog::Log(LOGERROR, "CAESinkALSA::Initialize - failed to initialize device \"%s\"", device.c_str());
snd_config_delete(config);
return false;
}
/* get the actual device name that was used */
device = snd_pcm_name(m_pcm);
m_device = device;
CLog::Log(LOGINFO, "CAESinkALSA::Initialize - Opened device \"%s\"", device.c_str());
/* free the sound config */
snd_config_delete(config);
if (!InitializeHW(format) || !InitializeSW(format))
return false;
snd_pcm_nonblock(m_pcm, 1);
snd_pcm_prepare (m_pcm);
m_format = format;
m_formatSampleRateMul = 1.0 / (double)m_format.m_sampleRate;
return true;
}
示例15: main
//.........这里部分代码省略.........
return -1;
}
*/
/*
avail=snd_pcm_avail_update(pcm_handle);
printf("\nAvailable frames = %d \n\n",(int)avail);
struct timeval tvbefore,tvafter;
gettimeofday(&tvbefore,NULL);
*/
unsigned char *ptr[2];
int i;
//unsigned int steps=0;
for(i=0;i<2;i++)
ptr[i] = (unsigned char*)areas[i].addr + (areas[i].first/8) + offset*(areas[i].step/8);
printf("\nOffset of the first sample : %u \n",areas[0].first);
printf("\n\nOffset of the memory map is : %u \n",areas[0].step/8);
printf("\nAreas start address is : %u \n",areas[0].addr);
printf("\nNumber of frames ; %u \n",frames);
printf("\n\nPointer before ; %u \n",ptr[0]);
long pl;
for(pl=0;pl<100000;pl++);
if(ptr[1]==NULL)
printf("\nNull pointer 1 allocated\n");
if(ptr[0]==NULL)
printf("\nNull pointer 0 allocated \n");
int check2=1;
snd_pcm_sframes_t size2 =30;
while(1)
{
for(i=0;i<2;i++)
{
//ptr[i] = (unsigned char*)areas[i].addr + (areas[i].first/8) + offset*(areas[i].step/8);
if(read(dest,ptr[i],120)!=0)
{
if(ptr[i]!=NULL)
{
if(pcmreturn = snd_pcm_mmap_writei(pcm_handle,ptr[i],size2)<0)
{
snd_pcm_prepare(pcm_handle);
printf("\n<<<<<<<Buffer Underrun>>>>>>>>\n");
break;
}
// printf("\n%d\n",pcmreturn);
ptr[i]+=0;
printf("\n%lu\n",ptr[i]);
}
}
else {
check2=0;
break;
}
}
if(check2==0)
break;
// l:break;
}
commitres = snd_pcm_mmap_commit(pcm_handle,offset,frames);
if(commitres<0)
{
printf("\nFrames not committed to memory\n");
}
}