本文整理汇总了C++中snd_pcm_writei函数的典型用法代码示例。如果您正苦于以下问题:C++ snd_pcm_writei函数的具体用法?C++ snd_pcm_writei怎么用?C++ snd_pcm_writei使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snd_pcm_writei函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: channels
void AudioAlsa::run()
{
surroundSampleFrame * temp =
new surroundSampleFrame[mixer()->framesPerPeriod()];
int_sample_t * outbuf =
new int_sample_t[mixer()->framesPerPeriod() *
channels()];
int_sample_t * pcmbuf = new int_sample_t[m_periodSize * channels()];
int outbuf_size = mixer()->framesPerPeriod() * channels();
int outbuf_pos = 0;
int pcmbuf_size = m_periodSize * channels();
bool quit = false;
while( quit == false )
{
int_sample_t * ptr = pcmbuf;
int len = pcmbuf_size;
while( len )
{
if( outbuf_pos == 0 )
{
// frames depend on the sample rate
const fpp_t frames = getNextBuffer( temp );
if( !frames )
{
quit = true;
memset( ptr, 0, len
* sizeof( int_sample_t ) );
break;
}
outbuf_size = frames * channels();
convertToS16( temp, frames,
mixer()->masterGain(),
outbuf,
m_convertEndian );
}
int min_len = qMin( len, outbuf_size - outbuf_pos );
memcpy( ptr, outbuf + outbuf_pos,
min_len * sizeof( int_sample_t ) );
ptr += min_len;
len -= min_len;
outbuf_pos += min_len;
outbuf_pos %= outbuf_size;
}
f_cnt_t frames = m_periodSize;
ptr = pcmbuf;
while( frames )
{
int err = snd_pcm_writei( m_handle, ptr, frames );
if( err == -EAGAIN )
{
continue;
}
if( err < 0 )
{
if( handleError( err ) < 0 )
{
printf( "Write error: %s\n",
snd_strerror( err ) );
}
break; // skip this buffer
}
ptr += err * channels();
frames -= err;
}
}
delete[] temp;
delete[] outbuf;
delete[] pcmbuf;
}
示例2: rdpsnd_alsa_play
static void rdpsnd_alsa_play(rdpsndDevicePlugin* device, uint8* data, int size)
{
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
uint8* decoded_data;
int decoded_size;
uint8* src;
uint8* resampled_data;
int len;
int error;
int frames;
int rbytes_per_frame;
int sbytes_per_frame;
uint8* pindex;
uint8* end;
if (alsa->out_handle == 0)
return;
if (alsa->wformat == 0x11)
{
decoded_data = dsp_decode_ima_adpcm(&alsa->adpcm,
data, size, alsa->source_channels, alsa->block_size, &decoded_size);
size = decoded_size;
src = decoded_data;
}
else
{
decoded_data = NULL;
src = data;
}
sbytes_per_frame = alsa->source_channels * alsa->bytes_per_channel;
rbytes_per_frame = alsa->actual_channels * alsa->bytes_per_channel;
if ((size % sbytes_per_frame) != 0)
{
DEBUG_WARN("error len mod");
return;
}
if ((alsa->source_rate == alsa->actual_rate) &&
(alsa->source_channels == alsa->actual_channels))
{
resampled_data = NULL;
}
else
{
resampled_data = dsp_resample(src, alsa->bytes_per_channel,
alsa->source_channels, alsa->source_rate, size / sbytes_per_frame,
alsa->actual_channels, alsa->actual_rate, &frames);
DEBUG_SVC("resampled %d frames at %d to %d frames at %d",
size / sbytes_per_frame, alsa->source_rate, frames, alsa->actual_rate);
size = frames * rbytes_per_frame;
src = resampled_data;
}
pindex = src;
end = pindex + size;
while (pindex < end)
{
len = end - pindex;
frames = len / rbytes_per_frame;
error = snd_pcm_writei(alsa->out_handle, pindex, frames);
if (error == -EPIPE)
{
snd_pcm_recover(alsa->out_handle, error, 0);
error = 0;
}
else if (error < 0)
{
DEBUG_WARN("error %d", error);
snd_pcm_close(alsa->out_handle);
alsa->out_handle = 0;
rdpsnd_alsa_open(device, NULL, alsa->latency);
break;
}
pindex += error * rbytes_per_frame;
}
if (resampled_data)
xfree(resampled_data);
if (decoded_data)
xfree(decoded_data);
}
示例3: alsa_write_float
static int
alsa_write_float (snd_pcm_t *alsa_dev, float *data, int frames, int channels)
{ static int epipe_count = 0 ;
int total = 0 ;
int retval ;
if (epipe_count > 0)
epipe_count -- ;
while (total < frames)
{ retval = snd_pcm_writei (alsa_dev, data + total * channels, frames - total) ;
if (retval >= 0)
{ total += retval ;
if (total == frames)
return total ;
continue ;
} ;
switch (retval)
{ case -EAGAIN :
puts ("alsa_write_float: EAGAIN") ;
continue ;
break ;
case -EPIPE :
if (epipe_count > 0)
{ printf ("alsa_write_float: EPIPE %d\n", epipe_count) ;
if (epipe_count > 140)
return retval ;
} ;
epipe_count += 100 ;
#if 0
if (0)
{ snd_pcm_status_t *status ;
snd_pcm_status_alloca (&status) ;
if ((retval = snd_pcm_status (alsa_dev, status)) < 0)
fprintf (stderr, "alsa_out: xrun. can't determine length\n") ;
else if (snd_pcm_status_get_state (status) == SND_PCM_STATE_XRUN)
{ struct timeval now, diff, tstamp ;
gettimeofday (&now, 0) ;
snd_pcm_status_get_trigger_tstamp (status, &tstamp) ;
timersub (&now, &tstamp, &diff) ;
fprintf (stderr, "alsa_write_float xrun: of at least %.3f msecs. resetting stream\n",
diff.tv_sec * 1000 + diff.tv_usec / 1000.0) ;
}
else
fprintf (stderr, "alsa_write_float: xrun. can't determine length\n") ;
} ;
#endif
snd_pcm_prepare (alsa_dev) ;
break ;
case -EBADFD :
fprintf (stderr, "alsa_write_float: Bad PCM state.n") ;
return 0 ;
break ;
case -ESTRPIPE :
fprintf (stderr, "alsa_write_float: Suspend event.n") ;
return 0 ;
break ;
case -EIO :
puts ("alsa_write_float: EIO") ;
return 0 ;
default :
fprintf (stderr, "alsa_write_float: retval = %d\n", retval) ;
return 0 ;
break ;
} ; /* switch */
} ; /* while */
return total ;
} /* alsa_write_float */
示例4: 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;
}
示例5: 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) {
//.........这里部分代码省略.........
示例6: 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);
//.........这里部分代码省略.........
示例7: ags_devout_alsa_play
void
ags_devout_alsa_play(AgsDevout *devout,
GError **error)
{
/* */
if((AGS_DEVOUT_BUFFER0 & (devout->flags)) != 0){
memset(devout->buffer[3], 0, (size_t) devout->dsp_channels * devout->buffer_size * sizeof(signed short));
devout->out.alsa.rc = snd_pcm_writei(devout->out.alsa.handle,
devout->buffer[0],
(snd_pcm_uframes_t) (devout->buffer_size));
if((AGS_DEVOUT_NONBLOCKING & (devout->flags)) == 0){
if(devout->out.alsa.rc == -EPIPE){
/* EPIPE means underrun */
snd_pcm_prepare(devout->out.alsa.handle);
#ifdef AGS_DEBUG
g_message("underrun occurred\0");
#endif
}else if(devout->out.alsa.rc == -ESTRPIPE){
static const struct timespec idle = {
0,
4000,
};
int err;
while((err = snd_pcm_resume(devout->out.alsa.handle)) == -EAGAIN)
nanosleep(&idle, NULL); /* wait until the suspend flag is released */
if(err < 0){
err = snd_pcm_prepare(devout->out.alsa.handle);
}
}else if(devout->out.alsa.rc < 0){
g_message("error from writei: %s\0", snd_strerror(devout->out.alsa.rc));
}else if(devout->out.alsa.rc != (int) devout->buffer_size) {
g_message("short write, write %d frames\0", devout->out.alsa.rc);
}
}
// g_message("ags_devout_play 0\0");
}else if((AGS_DEVOUT_BUFFER1 & (devout->flags)) != 0){
memset(devout->buffer[0], 0, (size_t) devout->dsp_channels * devout->buffer_size * sizeof(signed short));
devout->out.alsa.rc = snd_pcm_writei(devout->out.alsa.handle,
devout->buffer[1],
(snd_pcm_uframes_t) (devout->buffer_size));
if((AGS_DEVOUT_NONBLOCKING & (devout->flags)) == 0){
if(devout->out.alsa.rc == -EPIPE){
/* EPIPE means underrun */
snd_pcm_prepare(devout->out.alsa.handle);
#ifdef AGS_DEBUG
g_message("underrun occurred\0");
#endif
}else if(devout->out.alsa.rc == -ESTRPIPE){
static const struct timespec idle = {
0,
4000,
};
int err;
while((err = snd_pcm_resume(devout->out.alsa.handle)) == -EAGAIN)
nanosleep(&idle, NULL); /* wait until the suspend flag is released */
if(err < 0){
err = snd_pcm_prepare(devout->out.alsa.handle);
}
}else if(devout->out.alsa.rc < 0){
g_message("error from writei: %s\0", snd_strerror(devout->out.alsa.rc));
}else if(devout->out.alsa.rc != (int) devout->buffer_size) {
g_message("short write, write %d frames\0", devout->out.alsa.rc);
}
}
// g_message("ags_devout_play 1\0");
}else if((AGS_DEVOUT_BUFFER2 & (devout->flags)) != 0){
memset(devout->buffer[1], 0, (size_t) devout->dsp_channels * devout->buffer_size * sizeof(signed short));
devout->out.alsa.rc = snd_pcm_writei(devout->out.alsa.handle,
devout->buffer[2],
(snd_pcm_uframes_t) (devout->buffer_size));
if((AGS_DEVOUT_NONBLOCKING & (devout->flags)) == 0){
if(devout->out.alsa.rc == -EPIPE){
/* EPIPE means underrun */
snd_pcm_prepare(devout->out.alsa.handle);
#ifdef AGS_DEBUG
g_message("underrun occurred\0");
#endif
}else if(devout->out.alsa.rc == -ESTRPIPE){
static const struct timespec idle = {
0,
4000,
};
int err;
while((err = snd_pcm_resume(devout->out.alsa.handle)) == -EAGAIN)
nanosleep(&idle, NULL); /* wait until the suspend flag is released */
//.........这里部分代码省略.........
示例8: alsa_write
static ssize_t alsa_write(void *data, const void *buf_, size_t size_)
{
alsa_t *alsa = (alsa_t*)data;
const uint8_t *buf = (const uint8_t*)buf_;
bool eagain_retry = true;
snd_pcm_sframes_t written = 0;
snd_pcm_sframes_t size = snd_pcm_bytes_to_frames(alsa->pcm, size_);
while (size)
{
if (!alsa->nonblock)
{
int rc = snd_pcm_wait(alsa->pcm, -1);
if (rc == -EPIPE || rc == -ESTRPIPE || rc == -EINTR)
{
if (snd_pcm_recover(alsa->pcm, rc, 1) < 0)
{
RARCH_ERR("[ALSA]: (#1) Failed to recover from error (%s)\n",
snd_strerror(rc));
return -1;
}
continue;
}
}
snd_pcm_sframes_t frames = snd_pcm_writei(alsa->pcm, buf, size);
if (frames == -EPIPE || frames == -EINTR || frames == -ESTRPIPE)
{
if (snd_pcm_recover(alsa->pcm, frames, 1) < 0)
{
RARCH_ERR("[ALSA]: (#2) Failed to recover from error (%s)\n",
snd_strerror(frames));
return -1;
}
break;
}
else if (frames == -EAGAIN && !alsa->nonblock) // Definitely not supposed to happen.
{
RARCH_WARN("[ALSA]: poll() was signaled, but EAGAIN returned from write.\n"
"Your ALSA driver might be subtly broken.\n");
if (eagain_retry)
{
eagain_retry = false;
continue;
}
else
return written;
}
else if (frames == -EAGAIN) // Expected if we're running nonblock.
{
return written;
}
else if (frames < 0)
{
RARCH_ERR("[ALSA]: Unknown error occured (%s).\n", snd_strerror(frames));
return -1;
}
written += frames;
buf += (frames << 1) * (alsa->has_float ? sizeof(float) : sizeof(int16_t));
size -= frames;
}
return written;
}
示例9: main
int main(int ac, char** av)
{
pcm_desc_t desc;
pcm_handle_t ipcm;
pcm_handle_t opcm;
mod_handle_t mod;
int err;
cmdline_t cmd;
size_t i;
err = -1;
if (get_cmdline(&cmd, ac - 1, av + 1)) goto on_error_0;
pcm_init_desc(&desc);
desc.flags |= PCM_FLAG_IN;
if (cmd.flags & CMDLINE_FLAG(IPCM)) desc.name = cmd.ipcm;
if (pcm_open(&ipcm, &desc)) goto on_error_0;
pcm_init_desc(&desc);
desc.flags |= PCM_FLAG_OUT;
if (cmd.flags & CMDLINE_FLAG(OPCM)) desc.name = cmd.opcm;
if (pcm_open(&opcm, &desc)) goto on_error_1;
if (mod_open(&mod, 512)) goto on_error_2;
if (pcm_start(&ipcm)) goto on_error_3;
if (pcm_start(&opcm)) goto on_error_3;
signal(SIGINT, on_sigint);
for (i = 0; is_sigint == 0; i += 1)
{
size_t nsampl;
size_t navail;
size_t off;
/* read ipcm */
err = snd_pcm_wait(ipcm.pcm, -1);
if (is_sigint) break ;
if (err < 0) goto on_ipcm_xrun;
navail = (size_t)snd_pcm_avail_update(ipcm.pcm);
if (ipcm.wpos >= ipcm.rpos) nsampl = ipcm.nsampl - ipcm.wpos;
else nsampl = ipcm.rpos - ipcm.wpos;
if (nsampl > navail) nsampl = navail;
off = ipcm.wpos * ipcm.scale;
err = snd_pcm_readi(ipcm.pcm, ipcm.buf + off, nsampl);
if (err < 0) goto on_ipcm_xrun;
ipcm.wpos += (size_t)err;
if (ipcm.wpos == ipcm.nsampl) ipcm.wpos = 0;
/* apply modifier */
redo_mod:
if (ipcm.wpos >= ipcm.rpos) nsampl = ipcm.wpos - ipcm.rpos;
else nsampl = ipcm.nsampl - ipcm.rpos + ipcm.wpos;
if (cmd.flags & CMDLINE_FLAG(FILT))
{
const size_t n = mod_apply
(&mod, ipcm.buf, ipcm.nsampl, ipcm.rpos, nsampl);
nsampl = n;
}
if (nsampl == 0) continue ;
if ((ipcm.rpos + nsampl) > ipcm.nsampl)
{
const size_t n = ipcm.nsampl - ipcm.rpos;
off = ipcm.rpos * ipcm.scale;
err = snd_pcm_writei(opcm.pcm, ipcm.buf + off, n);
if (err < 0) goto on_opcm_xrun;
nsampl -= n;
ipcm.rpos = 0;
}
off = ipcm.rpos * ipcm.scale;
err = snd_pcm_writei(opcm.pcm, ipcm.buf + off, nsampl);
if (err < 0) goto on_opcm_xrun;
ipcm.rpos += nsampl;
if (ipcm.rpos == ipcm.nsampl) ipcm.rpos = 0;
goto redo_mod;
continue ;
on_ipcm_xrun:
if (pcm_recover_xrun(&ipcm, err)) PERROR_GOTO("", on_error_2);
continue ;
on_opcm_xrun:
if (pcm_recover_xrun(&opcm, err)) PERROR_GOTO("", on_error_2);
continue ;
}
//.........这里部分代码省略.........
示例10: alsa_open_audio
//.........这里部分代码省略.........
SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
check_error(err, 1, "snd_pcm_open");
if (err < 0)
continue;
alsa_outdev[alsa_noutdev].a_devno = audiooutdev[iodev];
snd_pcm_nonblock(alsa_outdev[alsa_noutdev].a_handle, 1);
alsa_noutdev++;
}
if (!alsa_nindev && !alsa_noutdev)
goto blewit;
/* If all the open devices support mmap_noninterleaved, let's call
Wini's code in s_audio_alsamm.c */
alsa_usemmap = 1;
for (iodev = 0; iodev < alsa_nindev; iodev++)
if (!alsaio_canmmap(&alsa_indev[iodev]))
alsa_usemmap = 0;
for (iodev = 0; iodev < alsa_noutdev; iodev++)
if (!alsaio_canmmap(&alsa_outdev[iodev]))
alsa_usemmap = 0;
if (alsa_usemmap)
{
post("using mmap audio interface");
if (alsamm_open_audio(rate, blocksize))
goto blewit;
else return (0);
}
for (iodev = 0; iodev < alsa_nindev; iodev++)
{
int channels = chindev[iodev];
if (alsaio_setup(&alsa_indev[iodev], 0, &channels, &rate,
nfrags, frag_size) < 0)
goto blewit;
inchans += channels;
}
for (iodev = 0; iodev < alsa_noutdev; iodev++)
{
int channels = choutdev[iodev];
if (alsaio_setup(&alsa_outdev[iodev], 1, &channels, &rate,
nfrags, frag_size) < 0)
goto blewit;
outchans += channels;
}
if (!inchans && !outchans)
goto blewit;
for (iodev = 0; iodev < alsa_nindev; iodev++)
snd_pcm_prepare(alsa_indev[iodev].a_handle);
for (iodev = 0; iodev < alsa_noutdev; iodev++)
snd_pcm_prepare(alsa_outdev[iodev].a_handle);
/* if duplex we can link the channels so they start together */
for (iodev = 0; iodev < alsa_nindev; iodev++)
for (dev2 = 0; dev2 < alsa_noutdev; dev2++)
{
if (alsa_indev[iodev].a_devno == alsa_outdev[iodev].a_devno)
{
snd_pcm_link(alsa_indev[iodev].a_handle,
alsa_outdev[iodev].a_handle);
}
}
/* allocate the status variables */
if (!alsa_status)
{
err = snd_pcm_status_malloc(&alsa_status);
check_error(err, -1, "snd_pcm_status_malloc");
}
/* fill the buffer with silence and prime the output FIFOs. This
should automatically start the output devices. */
memset(alsa_snd_buf, 0, alsa_snd_bufsize);
if (outchans)
{
i = (frag_size * nfrags)/DEFDACBLKSIZE + 1;
while (i--)
{
for (iodev = 0; iodev < alsa_noutdev; iodev++)
snd_pcm_writei(alsa_outdev[iodev].a_handle, alsa_snd_buf,
DEFDACBLKSIZE);
}
}
if (inchans)
{
/* some of the ADC devices might already have been started by
starting the outputs above, but others might still need it. */
for (iodev = 0; iodev < alsa_nindev; iodev++)
if (snd_pcm_state(alsa_indev[iodev].a_handle)
!= SND_PCM_STATE_RUNNING)
if ((err = snd_pcm_start(alsa_indev[iodev].a_handle)) < 0)
check_error(err, -1, "input start failed");
}
return (0);
blewit:
STUFF->st_inchannels = 0;
STUFF->st_outchannels = 0;
alsa_close_audio();
return (1);
}
示例11: alsa_send_dacs
//.........这里部分代码省略.........
#endif
/* do output */
for (iodev = 0, fp1 = STUFF->st_soundout, ch = 0;
iodev < alsa_noutdev; iodev++)
{
int thisdevchans = alsa_outdev[iodev].a_channels;
int chans = (chansouttogo < thisdevchans ? chansouttogo : thisdevchans);
chansouttogo -= chans;
if (alsa_outdev[iodev].a_sampwidth == 4)
{
for (i = 0; i < chans; i++, ch++, fp1 += DEFDACBLKSIZE)
for (j = i, k = DEFDACBLKSIZE, fp2 = fp1; k--;
j += thisdevchans, fp2++)
{
float s1 = *fp2 * INT32_MAX;
((t_alsa_sample32 *)alsa_snd_buf)[j] = CLIP32(s1);
}
for (; i < thisdevchans; i++, ch++)
for (j = i, k = DEFDACBLKSIZE; k--; j += thisdevchans)
((t_alsa_sample32 *)alsa_snd_buf)[j] = 0;
}
else if (alsa_outdev[iodev].a_sampwidth == 3)
{
for (i = 0; i < chans; i++, ch++, fp1 += DEFDACBLKSIZE)
for (j = i, k = DEFDACBLKSIZE, fp2 = fp1; k--;
j += thisdevchans, fp2++)
{
int s = *fp2 * 8388352.;
if (s > 8388351)
s = 8388351;
else if (s < -8388351)
s = -8388351;
#if BYTE_ORDER == LITTLE_ENDIAN
((char *)(alsa_snd_buf))[3*j] = (s & 255);
((char *)(alsa_snd_buf))[3*j+1] = ((s>>8) & 255);
((char *)(alsa_snd_buf))[3*j+2] = ((s>>16) & 255);
#else
fprintf(stderr, "big endian 24-bit not supported");
#endif
}
for (; i < thisdevchans; i++, ch++)
for (j = i, k = DEFDACBLKSIZE; k--; j += thisdevchans)
((char *)(alsa_snd_buf))[3*j] =
((char *)(alsa_snd_buf))[3*j+1] =
((char *)(alsa_snd_buf))[3*j+2] = 0;
}
else /* 16 bit samples */
{
for (i = 0; i < chans; i++, ch++, fp1 += DEFDACBLKSIZE)
for (j = ch, k = DEFDACBLKSIZE, fp2 = fp1; k--;
j += thisdevchans, fp2++)
{
int s = *fp2 * 32767.;
if (s > 32767)
s = 32767;
else if (s < -32767)
s = -32767;
((t_alsa_sample16 *)alsa_snd_buf)[j] = s;
}
for (; i < thisdevchans; i++, ch++)
for (j = ch, k = DEFDACBLKSIZE; k--; j += thisdevchans)
((t_alsa_sample16 *)alsa_snd_buf)[j] = 0;
}
result = snd_pcm_writei(alsa_outdev[iodev].a_handle, alsa_snd_buf,
transfersize);
if (result != (int)transfersize)
{
#ifdef DEBUG_ALSA_XFER
if (result >= 0 || errno == EAGAIN)
post("ALSA: write returned %d of %d\n",
result, transfersize);
else post("ALSA: write: %s\n",
snd_strerror(errno));
#endif
sys_log_error(ERR_DATALATE);
if (result == -EPIPE)
{
result = snd_pcm_prepare(alsa_indev[iodev].a_handle);
if (result < 0)
fprintf(stderr, "read reset error %d\n", result);
}
else fprintf(stderr, "read other error %d\n", result);
resync = 1;
}
/* zero out the output buffer */
memset(STUFF->st_soundout, 0, DEFDACBLKSIZE * sizeof(*STUFF->st_soundout) *
STUFF->st_outchannels);
if (sys_getrealtime() - timenow > 0.002)
{
#ifdef DEBUG_ALSA_XFER
post("output %d took %d msec\n",
callno, (int)(1000 * (timenow - timelast))), fflush(stderr);
#endif
timenow = sys_getrealtime();
sys_log_error(ERR_DACSLEPT);
}
}
示例12: alsa_play_play
int alsa_play_play(alsa_play_t alsa_play, glc_audio_data_header_t *audio_hdr, char *data)
{
snd_pcm_uframes_t frames, rem;
snd_pcm_sframes_t ret = 0;
unsigned int c;
if (audio_hdr->id != alsa_play->id)
return 0;
if (!alsa_play->pcm) {
glc_log(alsa_play->glc, GLC_ERROR, "alsa_play", "broken stream %d",
alsa_play->id);
return EINVAL;
}
frames = snd_pcm_bytes_to_frames(alsa_play->pcm, audio_hdr->size);
glc_utime_t time = glc_state_time(alsa_play->glc);
glc_utime_t duration = ((glc_utime_t) 1000000000 * (glc_utime_t) frames) /
(glc_utime_t) alsa_play->rate;
if (time + alsa_play->silence_threshold + duration < audio_hdr->time) {
struct timespec ts = {
.tv_sec = (audio_hdr->time - time - duration - alsa_play->silence_threshold)/1000000000,
.tv_nsec = (audio_hdr->time - time - duration - alsa_play->silence_threshold)%1000000000 };
nanosleep(&ts,NULL);
}
/*
* This condition determine what will be the initial audio packet.
* it is preferable to be ahead by < duration/2 than behind
* the video by > duration/2
*/
else if (time > audio_hdr->time + duration/2) {
glc_log(alsa_play->glc, GLC_DEBUG, "alsa_play",
"dropped packet. now %" PRId64 " ts %" PRId64,
time, audio_hdr->time);
return 0;
}
rem = frames;
while (rem > 0) {
/* alsa is horrible... */
/*snd_pcm_wait(alsa_play->pcm, duration);*/
if (alsa_play->flags & GLC_AUDIO_INTERLEAVED)
ret = snd_pcm_writei(alsa_play->pcm,
&data[snd_pcm_frames_to_bytes(alsa_play->pcm, frames - rem)],
rem);
else {
for (c = 0; c < alsa_play->channels; c++)
alsa_play->bufs[c] =
&data[snd_pcm_samples_to_bytes(alsa_play->pcm, frames)
* c + snd_pcm_samples_to_bytes(alsa_play->pcm, frames - rem)];
ret = snd_pcm_writen(alsa_play->pcm, alsa_play->bufs, rem);
}
if (ret == 0)
break;
if ((ret == -EBUSY) || (ret == -EAGAIN))
break;
else if (ret < 0) {
if ((ret = alsa_play_xrun(alsa_play, ret))) {
glc_log(alsa_play->glc, GLC_ERROR, "alsa_play",
"xrun recovery failed: %s", snd_strerror(-ret));
return ret;
}
} else
rem -= ret;
}
return 0;
}
示例13: playback
void playback() {
int err;
unsigned int i;
snd_pcm_t *handle;
// for (i = 0; i < sizeof(buffer); i++)
// buffer[i] = random() & 0xff;
if ((err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
initialize(handle);
printBuffer(buffer, 0, 10);
// for (i = 0; i < sizeof(buffer); i++)
// buffer[i] = random() & 0xff;
for (i = 0; i < NUM_FRAMES; i++) {
if ((err = snd_pcm_writei (handle, buffer+FRAME_SIZE*i*mult, FRAME_SIZE)) != FRAME_SIZE) {
fprintf (stderr, "write from audio interface failed (%s)\n",
err, snd_strerror (err));
exit (1);
}
fprintf(stdout, "write %d done\n", i);
printBuffer(buffer, FRAME_SIZE*i*mult, 10);
}
snd_pcm_close(handle);
// int err;
// unsigned int i;
// snd_pcm_t *handle;
// snd_pcm_sframes_t frames;
// for (i = 0; i < sizeof(buffer); i++)
// buffer[i] = random() & 0xff;
// if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
// printf("Playback open error: %s\n", snd_strerror(err));
// exit(EXIT_FAILURE);
// }
// if ((err = snd_pcm_set_params(handle,
// SND_PCM_FORMAT_U8,
// SND_PCM_ACCESS_RW_INTERLEAVED,
// 1,
// 48000,
// 1,
// 500000)) < 0) { /* 0.5sec */
// printf("Playback open error: %s\n", snd_strerror(err));
// exit(EXIT_FAILURE);
// }
// for (i = 0; i < 16; i++) {
// frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
// if (frames < 0)
// frames = snd_pcm_recover(handle, frames, 0);
// if (frames < 0) {
// printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
// break;
// }
// if (frames > 0 && frames < (long)sizeof(buffer))
// printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
// }
// snd_pcm_close(handle);
}
示例14: memset
/**
* @brief Playback AudioSample_t's pointed to by samples
*
* @param samples A pointer to AudioSample_t's to be played
* @param num_samples The number of samples pointed to by samples
*/
void AlsaPlayback::playAudio(const AudioSample_t *samples, const size_t num_samples)
{
if(samples && (num_samples > 0) && alsa_handle)
{
bool error_cond = false;
size_t frames_sent = 0;
size_t pad_out = num_samples % AlsaPlayback::FRAME_PERIOD;
size_t frames_to_send = num_samples + pad_out;
AudioSample_t *send_samples = new AudioSample_t[frames_to_send];
if(send_samples)
{
int rc = 0;
memset(send_samples, 0, sizeof(AudioSample_t)*frames_to_send);
memcpy(send_samples, samples, sizeof(AudioSample_t)*num_samples);
for(frames_sent = 0, error_cond = false; (frames_sent < frames_to_send) && !error_cond; frames_sent += (size_t)frames)
{
if(0 != AlsaPlayback::_volume)
{
rc = snd_pcm_writei(alsa_handle, &(send_samples[frames_sent]), frames);
if(-EPIPE == rc)
{
TRACE("playAudio() - underrun occurred in snd_pcm_writei()\n");
snd_pcm_prepare(alsa_handle);
}
else if(-EBADFD == rc)
{
TRACE("playAudio() - PCM is not in the right state\n");
AlsaPlayback::setupHandle();
}
else if (-ESTRPIPE == rc)
{
TRACE("playAudio() - stream is suspended, attempting recovery\n");
}
else if(rc < 0)
{
TRACEF("playAudio() - error occurred in snd_pcm_writei() [%d]\n", rc);
error_cond = true;
}
else if(rc != (int)frames)
{
TRACE("playAudio() - short write occurred in snd_pcm_writei()\n");
}
else
{
_num_samples_played += rc;
}
}
}
// rc = snd_pcm_drain(alsa_handle);
delete[] send_samples;
send_samples = NULL;
}
}
}
示例15: record
int record(int size,char *serverString)
{
unsigned int pcm, tmp, dir;
int rate, channels, seconds;
snd_pcm_t *pcm_handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
char *buff,*buf;
int buff_size, loops;
int fp;
/*if (argc < 4)
{
printf("Usage: %s <sample_rate> <channels> <seconds>\n",
argv[0]);
return -1;
}*/
rate = 8000;//atoi(argv[1]);
channels = 1;//atoi(argv[2]);
seconds = 10;//atoi(argv[3]);
/* Open the PCM device in playback mode */
if (pcm = snd_pcm_open(&pcm_handle, "default" , SND_PCM_STREAM_PLAYBACK, 0) < 0)
printf("ERROR: Can't open \"%s\" PCM device. %s\n", "default", snd_strerror(pcm));
/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcm_handle, params);
/* Set parameters */
if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params,SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params,SND_PCM_FORMAT_U8) < 0)
printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0)
printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0)
printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
/* Write parameters */
if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0)
printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
/* Resume information */
printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
snd_pcm_hw_params_get_channels(params, &tmp);
printf("channels: %i ", tmp);
if (tmp == 1)
printf("(mono)\n");
else if (tmp == 2)
printf("(stereo)\n");
snd_pcm_hw_params_get_rate(params, &tmp, 0);
printf("rate: %d bps\n", tmp);
printf("seconds: %d\n", seconds);
/* Allocate buffer to hold single period */
snd_pcm_hw_params_get_period_size(params, &frames, 0);
buff_size = frames * channels * 2 /* 2 -> sample size */;
buff = (char *) malloc(buff_size*3);
size = frames * channels * 2 /* 2 -> sample size */;
buf = (char *) realloc((void*)serverString,(size*3));
snd_pcm_hw_params_get_period_time(params, &tmp, NULL);
//fp=open("222.wav",O_RDONLY);
for (loops = (seconds * 1000000) / tmp; loops > 0; loops--)
{
/*
if (pcm = read(fp, buff, buff_size) == 0)
{
printf("Early end of file.\n");
//return 0;
}
*/
if (pcm = snd_pcm_writei(pcm_handle, buf, frames) == -EPIPE)
{
printf("XRUN.\n");
snd_pcm_prepare(pcm_handle);
}
else
if(pcm < 0)
{
printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
}
//.........这里部分代码省略.........