本文整理汇总了C++中snd_pcm_frames_to_bytes函数的典型用法代码示例。如果您正苦于以下问题:C++ snd_pcm_frames_to_bytes函数的具体用法?C++ snd_pcm_frames_to_bytes怎么用?C++ snd_pcm_frames_to_bytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snd_pcm_frames_to_bytes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: write_alsa_output
static int
write_alsa_output (char * output_data, int output_size) {
int err;
snd_pcm_uframes_t frames;
while (output_size > 0) {
frames = snd_pcm_bytes_to_frames(pcm, output_size);
if ((err = snd_pcm_writei(pcm, output_data, frames)) < 0) {
if (snd_pcm_state(pcm) == SND_PCM_STATE_XRUN) {
if ((err = snd_pcm_prepare(pcm)) < 0)
printf("snd_pcm_prepare() failed.\r\n");
alsa_first_time = 1;
continue;
}
return err;
}
output_size -= snd_pcm_frames_to_bytes(pcm, err);
output_data += snd_pcm_frames_to_bytes(pcm, err);
if (alsa_first_time) {
alsa_first_time = 0;
snd_pcm_start(pcm);
}
}
return 0;
}
示例2: snd_pcm_file_hw_params
static int snd_pcm_file_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
{
snd_pcm_file_t *file = pcm->private_data;
unsigned int channel;
snd_pcm_t *slave = file->gen.slave;
int err = _snd_pcm_hw_params(slave, params);
if (err < 0)
return err;
file->buffer_bytes = snd_pcm_frames_to_bytes(slave, slave->buffer_size);
file->wbuf_size = slave->buffer_size * 2;
file->wbuf_size_bytes = snd_pcm_frames_to_bytes(slave, file->wbuf_size);
file->wbuf_used_bytes = 0;
assert(!file->wbuf);
file->wbuf = malloc(file->wbuf_size_bytes);
if (file->wbuf == NULL) {
snd_pcm_file_hw_free(pcm);
return -ENOMEM;
}
file->wbuf_areas = malloc(sizeof(*file->wbuf_areas) * slave->channels);
if (file->wbuf_areas == NULL) {
snd_pcm_file_hw_free(pcm);
return -ENOMEM;
}
file->appl_ptr = file->file_ptr_bytes = 0;
for (channel = 0; channel < slave->channels; ++channel) {
snd_pcm_channel_area_t *a = &file->wbuf_areas[channel];
a->addr = file->wbuf;
a->first = slave->sample_bits * channel;
a->step = slave->frame_bits;
}
return 0;
}
示例3: ALSANoMMapProc
static ALuint ALSANoMMapProc(ALvoid *ptr)
{
ALCdevice *Device = (ALCdevice*)ptr;
alsa_data *data = (alsa_data*)Device->ExtraData;
snd_pcm_sframes_t avail;
char *WritePtr;
SetRTPriority();
while(!data->killNow)
{
int state = verify_state(data->pcmHandle);
if(state < 0)
{
ERR("Invalid state detected: %s\n", snd_strerror(state));
aluHandleDisconnect(Device);
break;
}
WritePtr = data->buffer;
avail = data->size / snd_pcm_frames_to_bytes(data->pcmHandle, 1);
aluMixData(Device, WritePtr, avail);
while(avail > 0)
{
int ret = snd_pcm_writei(data->pcmHandle, WritePtr, avail);
switch (ret)
{
case -EAGAIN:
continue;
case -ESTRPIPE:
case -EPIPE:
case -EINTR:
ret = snd_pcm_recover(data->pcmHandle, ret, 1);
if(ret < 0)
avail = 0;
break;
default:
if (ret >= 0)
{
WritePtr += snd_pcm_frames_to_bytes(data->pcmHandle, ret);
avail -= ret;
}
break;
}
if (ret < 0)
{
ret = snd_pcm_prepare(data->pcmHandle);
if(ret < 0)
break;
}
}
}
return 0;
}
示例4: snd_pcm_bytes_to_frames
int AudioInputALSA::PcmRead(void* buf, uint nbytes)
{
unsigned char* bufptr = (unsigned char*)buf;
snd_pcm_uframes_t to_read = snd_pcm_bytes_to_frames(pcm_handle, nbytes);
snd_pcm_uframes_t nframes = to_read;
snd_pcm_sframes_t nread, avail;
int retries = 0;
while (nframes > 0 && retries < 3)
{
if (AlsaBad((avail = snd_pcm_avail_update(pcm_handle)),
"available update failed"))
{
if (!Recovery(avail))
{
++retries;
continue;
}
}
if ((nread = snd_pcm_readi(pcm_handle, bufptr, nframes)) < 0)
{
switch (nread)
{
case -EAGAIN:
break;
case -EBADFD:
LOG(VB_GENERAL, LOG_ERR, LOC_DEV +
QString("in a state unfit to read (%1): %2")
.arg(nread).arg(snd_strerror(nread)));
break;
case -EINTR:
case -EPIPE:
case -ESTRPIPE:
Recovery(nread);
break;
default:
LOG(VB_GENERAL, LOG_ERR, LOC_DEV +
QString("weird return from snd_pcm_readi: %1")
.arg(snd_strerror(nread)));
break;
}
}
else
{
nframes -= nread;
bufptr += snd_pcm_frames_to_bytes(pcm_handle, nread);
}
++retries;
}
if (nframes > 0)
LOG(VB_AUDIO, LOG_ERR, LOC_DEV +
QString("short pcm read, %1 of %2 frames, retries %3")
.arg(to_read - nframes).arg(to_read).arg(retries));
return snd_pcm_frames_to_bytes(pcm_handle, to_read - nframes);
}
示例5: bytesFree
bool QAudioOutputPrivate::deviceReady()
{
if(pullMode) {
int l = 0;
int chunks = bytesAvailable/period_size;
if(chunks==0) {
bytesAvailable = bytesFree();
return false;
}
#ifdef DEBUG_AUDIO
qDebug()<<"deviceReady() avail="<<bytesAvailable<<" bytes, period size="<<period_size<<" bytes";
qDebug()<<"deviceReady() no. of chunks that can fit ="<<chunks<<", chunks in bytes ="<<period_size*chunks;
#endif
int input = period_frames*chunks;
if(input > (int)buffer_frames)
input = buffer_frames;
l = audioSource->read(audioBuffer,snd_pcm_frames_to_bytes(handle, input));
if(l > 0) {
// Got some data to output
if(deviceState != QAudio::ActiveState)
return true;
write(audioBuffer,l);
bytesAvailable = bytesFree();
} else if(l == 0) {
// Did not get any data to output
bytesAvailable = bytesFree();
if(bytesAvailable > snd_pcm_frames_to_bytes(handle, buffer_frames-period_frames)) {
// Underrun
errorState = QAudio::UnderrunError;
deviceState = QAudio::IdleState;
emit stateChanged(deviceState);
}
} else if(l < 0) {
close();
errorState = QAudio::IOError;
emit stateChanged(deviceState);
}
} else
bytesAvailable = bytesFree();
if(deviceState != QAudio::ActiveState)
return true;
if((timeStamp.elapsed() + elapsedTimeOffset) > intervalTime) {
emit notify();
elapsedTimeOffset = timeStamp.elapsed() + elapsedTimeOffset - intervalTime;
timeStamp.restart();
}
return true;
}
示例6: alsa_stop_capture
static void alsa_stop_capture(ALCdevice *Device)
{
alsa_data *data = (alsa_data*)Device->ExtraData;
ALCuint avail;
int err;
/* OpenAL requires access to unread audio after stopping, but ALSA's
* snd_pcm_drain is unreliable and snd_pcm_drop drops it. Capture what's
* available now so it'll be available later after the drop. */
avail = alsa_available_samples(Device);
if(!data->ring && avail > 0)
{
/* The ring buffer implicitly captures when checking availability.
* Direct access needs to explicitly capture it into temp storage. */
ALsizei size;
void *ptr;
size = snd_pcm_frames_to_bytes(data->pcmHandle, avail);
ptr = realloc(data->buffer, size);
if(ptr)
{
data->buffer = ptr;
alsa_capture_samples(Device, data->buffer, avail);
data->size = size;
}
}
err = snd_pcm_drop(data->pcmHandle);
if(err < 0)
ERR("drop failed: %s\n", snd_strerror(err));
data->doCapture = AL_FALSE;
}
示例7: wodUpdatePlayedTotal
/**************************************************************************
* wodUpdatePlayedTotal [internal]
*
*/
static BOOL wodUpdatePlayedTotal(WINE_WAVEDEV* wwo, snd_pcm_status_t* ps)
{
snd_pcm_sframes_t delay;
snd_pcm_sframes_t avail;
snd_pcm_uframes_t buf_size = 0;
snd_pcm_state_t state;
state = snd_pcm_state(wwo->pcm);
avail = snd_pcm_avail_update(wwo->pcm);
snd_pcm_hw_params_get_buffer_size(wwo->hw_params, &buf_size);
delay = buf_size - avail;
if (state != SND_PCM_STATE_RUNNING && state != SND_PCM_STATE_PREPARED)
{
WARN("Unexpected state (%d) while updating Total Played, resetting\n", state);
wine_snd_pcm_recover(wwo->pcm, -EPIPE, 0);
delay=0;
}
/* A delay < 0 indicates an underrun; for our purposes that's 0. */
if (delay < 0)
{
WARN("Unexpected delay (%ld) while updating Total Played, resetting\n", delay);
delay=0;
}
InterlockedExchange((LONG*)&wwo->dwPlayedTotal, wwo->dwWrittenTotal - snd_pcm_frames_to_bytes(wwo->pcm, delay));
return TRUE;
}
示例8: snd_pcm_avail_update
int QAudioOutputPrivate::bytesFree() const
{
if(resuming)
return period_size;
if(deviceState != QAudio::ActiveState && deviceState != QAudio::IdleState)
return 0;
int frames = snd_pcm_avail_update(handle);
if (frames == -EPIPE) {
// Try and handle buffer underrun
int err = snd_pcm_recover(handle, frames, 0);
if (err < 0)
return 0;
else
frames = snd_pcm_avail_update(handle);
} else if (frames < 0) {
return 0;
}
if ((int)frames > (int)buffer_frames)
frames = buffer_frames;
return snd_pcm_frames_to_bytes(handle, frames);
}
示例9: while
EmErrorCode AlsaRenderer::Write(const char* buf, uint32_t len)
{
int off = 0;
int leftFrames = (len + m_FrameLength - 1) / m_FrameLength;
while (leftFrames > 0) {
int written = snd_pcm_writei(m_PcmHandle, buf+off, leftFrames);
if (written > 0) {
leftFrames -= written;
off += snd_pcm_frames_to_bytes(m_PcmHandle, written);
} else if (written == -EPIPE) {
if (snd_pcm_prepare(m_PcmHandle) < 0)
printf("FATAL: snd_pcm_prepare() failed!\n");
} else if (written == -ESTRPIPE) {
while ((written = snd_pcm_resume(m_PcmHandle)) == -EAGAIN)
usleep(100);
if (written < 0) {
if ((written = snd_pcm_prepare(m_PcmHandle)) < 0)
return ErrorCode::RendererFailedToWrite;
}
} else if (written <= 0) {
printf("writei error / short write\n");
}
}
return ErrorCode::Ok;
}
示例10: while
qint64 QAudioInputPrivate::read( char *data, qint64 maxlen )
{
int rc = -1, count=0;
if (handle != NULL)
{
while(count < 5) {
int frames = snd_pcm_bytes_to_frames(handle, (int)maxlen);
int readFrames = snd_pcm_readi(handle, data, frames);
if (readFrames >= 0) {
rc = snd_pcm_frames_to_bytes(handle, readFrames);
qLog(QAudioInput) << QString("read in bytes = %1 (frames=%2)").arg(rc).arg(readFrames).toLatin1().constData();
break;
}
else if ((readFrames == -EAGAIN) || (readFrames == -EINTR)) {
qLog(QAudioInput) << "ALSA: EAGAIN || EINTR error";
rc = 0;
break;
}
else {
if(readFrames == -EPIPE) {
qLog(QAudioInput) << "ALSA: underrun!!!";
rc = snd_pcm_prepare(handle);
} else if(readFrames == -ESTRPIPE) {
qLog(QAudioInput) << "ALSA: suspend recovery!!!!";
rc = snd_pcm_prepare(handle);
}
if(rc != 0) break;
}
count++;
}
}
return rc;
}
示例11: alsa_write_audio
/* transfer data to audio h/w via normal write */
static void
alsa_write_audio(char* data, int length) {
snd_pcm_sframes_t written_frames;
while(length > 0) {
int frames = snd_pcm_bytes_to_frames(alsa_pcm, length);
written_frames = snd_pcm_writei(alsa_pcm, data, frames);
if(written_frames > 0) {
int written = snd_pcm_frames_to_bytes(alsa_pcm,
written_frames);
length -= written;
data += written;
alsa_hw_written += written;
} else {
int err = alsa_handle_error((int) written_frames);
if(err < 0) {
printf("alsa_write_audio(): write error: %s\n",
snd_strerror(-err));
break;
}
}
}
}
示例12: snd_pcm_prepare
void QAudioOutputPrivate::resume()
{
if(deviceState == QAudio::SuspendedState) {
int err = 0;
if(handle) {
err = snd_pcm_prepare( handle );
if(err < 0)
xrun_recovery(err);
err = snd_pcm_start(handle);
if(err < 0)
xrun_recovery(err);
bytesAvailable = (int)snd_pcm_frames_to_bytes(handle, buffer_frames);
}
resuming = true;
if(pullMode)
deviceState = QAudio::ActiveState;
else
deviceState = QAudio::IdleState;
errorState = QAudio::NoError;
timer->start(period_time/1000);
emit stateChanged(deviceState);
}
}
示例13: gst_alsasink_write
static guint
gst_alsasink_write (GstAudioSink * asink, gpointer data, guint length)
{
GstAlsaSink *alsa;
gint err;
gint cptr;
gint16 *ptr = data;
alsa = GST_ALSA_SINK (asink);
if (alsa->iec958 && alsa->need_swap) {
guint i;
GST_DEBUG_OBJECT (asink, "swapping bytes");
for (i = 0; i < length / 2; i++) {
ptr[i] = GUINT16_SWAP_LE_BE (ptr[i]);
}
}
GST_LOG_OBJECT (asink, "received audio samples buffer of %u bytes", length);
cptr = length / alsa->bytes_per_sample;
GST_ALSA_SINK_LOCK (asink);
while (cptr > 0) {
/* start by doing a blocking wait for free space. Set the timeout
* to 4 times the period time */
err = snd_pcm_wait (alsa->handle, (4 * alsa->period_time / 1000));
if (err < 0) {
GST_DEBUG_OBJECT (asink, "wait error, %d", err);
} else {
GST_DELAY_SINK_LOCK (asink);
err = snd_pcm_writei (alsa->handle, ptr, cptr);
GST_DELAY_SINK_UNLOCK (asink);
}
GST_DEBUG_OBJECT (asink, "written %d frames out of %d", err, cptr);
if (err < 0) {
GST_DEBUG_OBJECT (asink, "Write error: %s", snd_strerror (err));
if (err == -EAGAIN) {
continue;
} else if (xrun_recovery (alsa, alsa->handle, err) < 0) {
goto write_error;
}
continue;
}
ptr += snd_pcm_frames_to_bytes (alsa->handle, err);
cptr -= err;
}
GST_ALSA_SINK_UNLOCK (asink);
return length - (cptr * alsa->bytes_per_sample);
write_error:
{
GST_ALSA_SINK_UNLOCK (asink);
return length; /* skip one period */
}
}
示例14: snd_pcm_file_add_frames
static void snd_pcm_file_add_frames(snd_pcm_t *pcm,
const snd_pcm_channel_area_t *areas,
snd_pcm_uframes_t offset,
snd_pcm_uframes_t frames)
{
snd_pcm_file_t *file = pcm->private_data;
while (frames > 0) {
snd_pcm_uframes_t n = frames;
snd_pcm_uframes_t cont = file->wbuf_size - file->appl_ptr;
snd_pcm_uframes_t avail = file->wbuf_size - snd_pcm_bytes_to_frames(pcm, file->wbuf_used_bytes);
if (n > cont)
n = cont;
if (n > avail)
n = avail;
snd_pcm_areas_copy(file->wbuf_areas, file->appl_ptr,
areas, offset,
pcm->channels, n, pcm->format);
frames -= n;
offset += n;
file->appl_ptr += n;
if (file->appl_ptr == file->wbuf_size)
file->appl_ptr = 0;
file->wbuf_used_bytes += snd_pcm_frames_to_bytes(pcm, n);
if (file->wbuf_used_bytes > file->buffer_bytes)
snd_pcm_file_write_bytes(pcm, file->wbuf_used_bytes - file->buffer_bytes);
assert(file->wbuf_used_bytes < file->wbuf_size_bytes);
}
}
示例15: sa_stream_drain
int
sa_stream_drain(sa_stream_t *s)
{
if (s == NULL || s->output_unit == NULL) {
return SA_ERROR_NO_INIT;
}
if (snd_pcm_state(s->output_unit) == SND_PCM_STATE_PREPARED) {
size_t min_samples = 0;
size_t min_bytes = 0;
void *buf;
if (sa_stream_get_min_write(s, &min_samples) < 0)
return SA_ERROR_SYSTEM;
min_bytes = snd_pcm_frames_to_bytes(s->output_unit, min_samples);
buf = malloc(min_bytes);
if (!buf)
return SA_ERROR_SYSTEM;
memset(buf, 0, min_bytes);
sa_stream_write(s, buf, min_bytes);
free(buf);
}
if (snd_pcm_state(s->output_unit) != SND_PCM_STATE_RUNNING) {
return SA_ERROR_INVALID;
}
snd_pcm_drain(s->output_unit);
return SA_SUCCESS;
}