ethrbunny
ethrbunny

Reputation: 10469

alsa - managing non-blocking stream

Working with a usb audio device (its a HID with multiple channels) that constantly outputs data.

What I'm hoping to achieve is to ignore the audio until a signal comes in from the device. At that point I would start monitoring the feed. A second signal from the device would indicate that I can go back to ignoring the data. I have opened said device in non-blocking mode so it won't interfere with other USB signals coming in.

This is working fine except that when I start reading the data (via snd_pcm_readi) I get an EPIPE error indicating a buffer overrun. This can be fixed by calling snd_pcm_prepare every time but I'm hoping there is a way to let the buffer empty while Im ignoring it.

I've looked at snd_pcm_drain and snd_pcm_drop but these stop the PCM and I'd rather keep it open.

Suggestions?

Upvotes: 0

Views: 2271

Answers (1)

CL.
CL.

Reputation: 180162

To ignore buffer overruns, change the PCM device's software parameters: set the stop threshold to the same value as the boundary. With that configuration, overruns will not cause the device to stop, but will let it continue to fill the buffer. (Other errors will still stop the device; it would be hard to continue when a USB device is unplugged ...)

When an overrun has happened, the buffer will contain more data than actually can fit into it, i.e., snd_pcm_avail will report more available frames than the buffer size.

When you want to actually start recording, you should call snd_pcm_forward to throw away all those invalid frames.

Upvotes: 3

Related Questions