本文整理汇总了C++中snd_pcm_avail_update函数的典型用法代码示例。如果您正苦于以下问题:C++ snd_pcm_avail_update函数的具体用法?C++ snd_pcm_avail_update怎么用?C++ snd_pcm_avail_update使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snd_pcm_avail_update函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: alsa_available_samples
static ALCuint alsa_available_samples(ALCdevice *Device)
{
alsa_data *data = (alsa_data*)Device->ExtraData;
snd_pcm_sframes_t avail;
avail = (Device->Connected ? snd_pcm_avail_update(data->pcmHandle) : 0);
if(avail < 0)
{
ERR("avail update failed: %s\n", snd_strerror(avail));
if((avail=snd_pcm_recover(data->pcmHandle, avail, 1)) >= 0)
{
if(data->doCapture)
avail = snd_pcm_start(data->pcmHandle);
if(avail >= 0)
avail = snd_pcm_avail_update(data->pcmHandle);
}
if(avail < 0)
{
ERR("restore error: %s\n", snd_strerror(avail));
aluHandleDisconnect(Device);
}
}
while(avail > 0)
{
snd_pcm_sframes_t amt;
amt = snd_pcm_bytes_to_frames(data->pcmHandle, data->size);
if(avail < amt) amt = avail;
amt = snd_pcm_readi(data->pcmHandle, data->buffer, amt);
if(amt < 0)
{
ERR("read error: %s\n", snd_strerror(amt));
if(amt == -EAGAIN)
continue;
if((amt=snd_pcm_recover(data->pcmHandle, amt, 1)) >= 0)
{
if(data->doCapture)
amt = snd_pcm_start(data->pcmHandle);
if(amt >= 0)
amt = snd_pcm_avail_update(data->pcmHandle);
}
if(amt < 0)
{
ERR("restore error: %s\n", snd_strerror(amt));
aluHandleDisconnect(Device);
break;
}
avail = amt;
continue;
}
WriteRingBuffer(data->ring, data->buffer, amt);
avail -= amt;
}
return RingBufferSize(data->ring);
}
示例2: drvHostALSAAudioGetAvail
static int drvHostALSAAudioGetAvail(snd_pcm_t *phPCM, snd_pcm_sframes_t *pFramesAvail)
{
AssertPtrReturn(phPCM, VERR_INVALID_POINTER);
AssertPtrReturn(pFramesAvail, VERR_INVALID_POINTER);
int rc;
snd_pcm_sframes_t framesAvail;
framesAvail = snd_pcm_avail_update(phPCM);
if (framesAvail < 0)
{
if (framesAvail == -EPIPE)
{
rc = drvHostALSAAudioRecover(phPCM);
if (RT_SUCCESS(rc))
framesAvail = snd_pcm_avail_update(phPCM);
}
else
rc = VERR_ACCESS_DENIED; /** @todo Find a better rc. */
}
else
rc = VINF_SUCCESS;
if (framesAvail >= 0)
*pFramesAvail = framesAvail;
return rc;
}
示例3: 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);
}
示例4: write
void write( const char *data, qint64 len )
{
if ( !handle )
return;
int count=0;
qLog(QAudioOutput)<<"frames to write out = "<<
snd_pcm_bytes_to_frames( handle, (int)len )<<" ("<<len<<") bytes";
while ( len > 0 ) {
int err=0;
int frames = snd_pcm_bytes_to_frames( handle, (int)len );
#ifdef ALSA_USE_AVAILABLE
if(frames < (int)period_size)
return;
int available = snd_pcm_avail_update(handle);
qLog(QAudioOutput) <<"available space = "<<available;
if(available == 0) {
while(available < frames) {
snd_pcm_wait(handle,period_size/1000);
usleep(period_size*10);
available = snd_pcm_avail_update(handle);
qLog(QAudioOutput) <<"->available space = "<<available;
count++;
if((count > 5)||(available < 0))
return;
}
}
#endif
err = snd_pcm_writei( handle, data, frames );
// Handle errors
if ( err >= 0 ) {
if(err == 0) count++;
int bytes = snd_pcm_frames_to_bytes( handle, err );
qLog(QAudioOutput) << QString("write out = %1").arg(bytes).toLatin1().constData();
data += bytes;
len -= bytes;
} else {
count++;
qLog(QAudioOutput) <<"err = "<<err;
err = xrun_recovery(err);
}
if(count > 5) {
qLog(QAudioOutput) <<"failing to write, close() and re-open() to try and recover!";
close();
open();
snd_pcm_prepare(handle);
break;
}
}
}
示例5: free
void *speaker_thread(void* ptr){
audiobuffer* buf = ((spk_pcm_package*)ptr)->buffer; //cast pointer, get buffer struct
snd_pcm_t* speaker_handle = ((spk_pcm_package*)ptr)->pcm_handle; //cast pointer, get device pointer
free(ptr); //free message memory
snd_pcm_nonblock(speaker_handle, SND_PCM_NONBLOCK); //set in nonblocking mode
char started = 0; //track when to start reading data
while(!global_kill && !speaker_kill_flag) { //loop until program stops us
//wait until adequate buffer is achieved and can be written
if((!started && BUFFER_SIZE(*buf) < (MIN_BUFFER)) \
|| BUFFER_EMPTY(*buf) \
|| !snd_pcm_avail_update(speaker_handle)) {
//printf("Speaker Waiting\n");
usleep(PERIOD_UTIME*2); //wait to reduce CPU usage
continue; //don't start yet
} else {
if(!started) snd_pcm_prepare(speaker_handle); //reset speaker
started = 1; //indicate that we've startd
}
//write data to speaker buffer, check responses
int write_count = MIN(BUFFER_SIZE(*buf), snd_pcm_avail_update(speaker_handle)/(buf->period));
#ifdef DEBUG_MODE
printf("Writing %d packets to speaker\n", write_count);
#endif
//loop over avaliable buffer entries
while(write_count-- > 0 && started){
/* Note: This call performs a syscall to copy data to kernel space
so it would be better to write multiple entries in one operation,
but using the audiobuffer without the abstraction was something
I didn't want to do at the time of writing. */
int rc = snd_pcm_writei(speaker_handle, GET_QUEUE_HEAD(*buf), buf->period);
INC_QUEUE_HEAD(*buf);
if (rc == -EPIPE){ //Catch underruns (not enough data)
fprintf(stderr, "underrun occurred\n");
started = 0; //stop and wait for buffer to buildup
} else if (rc < 0) fprintf(stderr, "error from writei: %s\n", snd_strerror(rc)); //other errors
else if (rc != (int)buf->period) fprintf(stderr, "short write, write %d frames\n", rc);
//else fprintf(stderr, "audio written correctly\n");
//snd_pcm_wait(speaker_handle, 1000); //wait for IO to be ready
}
#ifdef DEBUG_MODE
printf("%d unwritten\n", write_count);
#endif
}
// notify kernel to empty/close the speakers
snd_pcm_drain(speaker_handle); //finish transferring the audio
snd_pcm_close(speaker_handle); //close the device
printf("Audio Controller: Speaker Thread shutdown\n");
pthread_exit(NULL); //exit thread safetly
}
示例6: wodPlayer_FeedDSP
/**************************************************************************
* wodPlayer_FeedDSP [internal]
* Feed as much sound data as we can into the DSP and return the number of
* milliseconds before it will be necessary to feed the DSP again.
*/
static DWORD wodPlayer_FeedDSP(WINE_WAVEDEV* wwo)
{
DWORD availInQ;
wodUpdatePlayedTotal(wwo, NULL);
availInQ = snd_pcm_avail_update(wwo->pcm);
/* no more room... no need to try to feed */
if (availInQ > 0) {
/* Feed from partial wavehdr */
if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
wodPlayer_WriteMaxFrags(wwo, &availInQ);
}
/* Feed wavehdrs until we run out of wavehdrs or DSP space */
if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
do {
TRACE("Setting time to elapse for %p to %u\n",
wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
/* note the value that dwPlayedTotal will return when this wave finishes playing */
wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
} while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
}
}
return wodPlayer_DSPWait(wwo);
}
示例7: alsa_safe_avail
/* from PA */
static snd_pcm_sframes_t alsa_safe_avail(snd_pcm_t *pcm, size_t hwbuf_size, const StreamSpec& ss)
{
snd_pcm_sframes_t n;
size_t k;
/* Some ALSA drivers expose weird bugs, let's inform the user about what is going on */
n = snd_pcm_avail_update(pcm);
if (n <= 0)
return n;
k = (size_t) n * ss.FrameBytes();
if (k >= hwbuf_size * 5 ||
k >= ss.BytesPerSecond() * 10)
{
// Print this on console to allow users to report their driver.
printf("snd_pcm_avail_update() returned a value that is exceptionally large: %lu bytes (%lu ms).\n"
"Most likely this is a bug in the ALSA driver. Please report this issue to the ALSA developers.\n",
(unsigned long) k,
(unsigned long) ss.FramesToMs(k / ss.FrameBytes()));
/* Mhmm, let's try not to fail completely */
n = (snd_pcm_sframes_t) (hwbuf_size / ss.FrameBytes());
}
return n;
}
示例8: alsa_can_read
static int alsa_can_read(snd_pcm_t *dev)
{
snd_pcm_sframes_t avail;
int err;
alsa_resume(dev);
avail = snd_pcm_avail_update(dev);
/* A buggy driver does not return an error while being in Xrun */
if (avail >= 0 && snd_pcm_state(dev) == SND_PCM_STATE_XRUN) avail=-EPIPE;
if (avail < 0) {
ms_error("snd_pcm_avail_update: %s", snd_strerror(avail)); // most probably -EPIPE
/* overrun occured, snd_pcm_state() would return SND_PCM_STATE_XRUN
FIXME: handle other error conditions*/
ms_error("*** alsa_can_read fixup, trying to recover");
snd_pcm_drain(dev); /* Ignore possible error, at least -EAGAIN.*/
err = snd_pcm_recover(dev, avail, 0);
if (err){
ms_error("snd_pcm_recover() failed with err %d: %s", err, snd_strerror(err));
return -1;
}
err = snd_pcm_start(dev);
if (err){
ms_error("snd_pcm_start() failed with err %d: %s", err, snd_strerror(err));
return -1;
}
ms_message("Recovery done");
}
return avail;
}
示例9: IDsDriverBufferImpl_Stop
static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
{
const snd_pcm_channel_area_t *areas;
snd_pcm_uframes_t avail;
snd_pcm_format_t format;
IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
TRACE("(%p)\n",iface);
/* **** */
EnterCriticalSection(&This->pcm_crst);
avail = This->mmap_buflen_frames;
snd_pcm_drop(This->pcm);
snd_pcm_prepare(This->pcm);
avail = snd_pcm_avail_update(This->pcm);
snd_pcm_hw_params_get_format(This->hw_params, &format);
if (This->mmap)
{
snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &avail);
snd_pcm_format_set_silence(format, areas->addr, This->mmap_buflen_frames);
snd_pcm_mmap_commit(This->pcm, This->mmap_pos, 0);
}
else
{
snd_pcm_format_set_silence(format, This->mmap_buffer, This->mmap_buflen_frames);
snd_pcm_writei(This->pcm, This->mmap_buffer, This->mmap_buflen_frames);
This->mmap_pos = 0;
}
/* **** */
LeaveCriticalSection(&This->pcm_crst);
return DS_OK;
}
示例10: 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;
}
示例11: _InternalCallback
// Invoked by the static ExternalCallback method below.
void _InternalCallback()
{
snd_pcm_sframes_t avail;
fprintf(stderr, "* SPU2-X:Iz in your internal callback.\n");
avail = snd_pcm_avail_update(handle);
while (avail >= (int)period_time) {
StereoOut16 buff[PacketsPerBuffer * SndOutPacketSize];
StereoOut16 *p1 = buff;
for (int p = 0; p < PacketsPerBuffer; p++, p1 += SndOutPacketSize)
SndBuffer::ReadSamples(p1);
snd_pcm_writei(handle, buff, period_time);
avail = snd_pcm_avail_update(handle);
}
}
示例12: snd_pcm_avail_update
eU32 tfAlsa::getPlayBufferlen()
{
if (m_handle == NULL)
return 0;
eU32 len = snd_pcm_avail_update(m_handle);
//printf("%i\n", len);
return len;
}
示例13: 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);
}
示例14: CommitAll
/** Fill buffers, for starting and stopping
* Alsa won't start playing until everything is filled up
* This also updates mmap_pos
*
* Returns: Amount of periods in use so snd_pcm_avail_update
* doesn't have to be called up to 4x in GetPosition()
*/
static snd_pcm_uframes_t CommitAll(IDsDriverBufferImpl *This)
{
const snd_pcm_channel_area_t *areas;
snd_pcm_sframes_t used;
const snd_pcm_uframes_t commitahead = This->mmap_commitahead;
used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
if (used < 0) used = 0;
TRACE("%p needs to commit to %lu, used: %ld\n", This, commitahead, used);
if (used < commitahead)
{
snd_pcm_sframes_t done;
snd_pcm_uframes_t putin = commitahead - used;
if (This->mmap)
{
snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
}
else
{
if (putin + This->mmap_pos > This->mmap_buflen_frames)
putin = This->mmap_buflen_frames - This->mmap_pos;
done = snd_pcm_writei(This->pcm, This->mmap_buffer + snd_pcm_frames_to_bytes(This->pcm, This->mmap_pos), putin);
if (done < putin) WARN("Short write %ld/%ld\n", putin, done);
}
if (done < 0) done = 0;
This->mmap_pos += done;
used += done;
putin = commitahead - used;
if (This->mmap_pos == This->mmap_buflen_frames && (snd_pcm_sframes_t)putin > 0)
{
if (This->mmap)
{
snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
This->mmap_pos += done;
}
else
{
done = snd_pcm_writei(This->pcm, This->mmap_buffer, putin);
if (done < putin) WARN("Short write %ld/%ld\n", putin, done);
if (done < 0) done = 0;
This->mmap_pos = done;
}
used += done;
}
}
if (This->mmap_pos == This->mmap_buflen_frames)
This->mmap_pos = 0;
return used;
}
示例15: sound_get_buffer
unsigned int
sound_get_buffer ()
{
int ret;
snd_pcm_uframes_t buffer_size, period_size;
ret = snd_pcm_get_params (handle, &buffer_size, &period_size);
if (ret < 0)
return 0;
return buffer_size - snd_pcm_avail_update (handle);
}