RADLib
RADical C++ application framework
RADMedia::RADAudioDevice Class Reference

ALSA-backed input or output audio device implementing RADIODevice. More...

#include <RADMedia.h>

+ Inheritance diagram for RADMedia::RADAudioDevice:
+ Collaboration diagram for RADMedia::RADAudioDevice:

Public Member Functions

 RADAudioDevice (RADCore::EventLoop &loop, AudioDeviceDirection direction)
 Creates an input or output audio device using loop for events. More...
 
 ~RADAudioDevice () override
 Closes the device if still open. More...
 
AudioDeviceDirection direction () const
 Returns whether this device captures input or plays output. More...
 
void setDeviceId (std::string deviceId)
 Sets the ALSA device id, usually "default" or an enumerated id. More...
 
const std::string & deviceId () const
 Returns the configured ALSA device id. More...
 
void setFormat (WavFormat format)
 Sets the desired audio format. More...
 
const WavFormatformat () const
 Returns the configured audio format. More...
 
void setFramesPerChunk (size_t frames)
 Sets how many frames are transferred per low-level chunk. More...
 
size_t framesPerChunk () const
 Returns frames transferred per low-level chunk. More...
 
void setTargetLatencyUsec (unsigned int usec)
 Sets the requested ALSA device latency in microseconds before opening. More...
 
unsigned int targetLatencyUsec () const
 Returns the requested ALSA device latency in microseconds. More...
 
void setPeriodFrames (size_t frames)
 Sets the requested ALSA hardware period size in frames. Zero uses framesPerChunk(). More...
 
size_t periodFrames () const
 Returns the requested ALSA hardware period size in frames, or zero for automatic. More...
 
void setBufferFrames (size_t frames)
 Sets the requested ALSA hardware buffer size in frames. Zero uses targetLatencyUsec(). More...
 
size_t bufferFrames () const
 Returns the requested ALSA hardware buffer size in frames, or zero for automatic. More...
 
void setMaxBufferedBytes (size_t bytes)
 Sets the maximum queued input bytes retained for reads. More...
 
size_t maxBufferedBytes () const
 Returns the maximum queued input bytes. More...
 
size_t bytesAvailable () const
 Returns queued readable bytes for input devices. More...
 
bool open (RADCore::OpenMode mode) override
 Opens the device for read or write depending on direction. More...
 
void close () override
 Closes the ALSA handle and worker thread. More...
 
bool isOpen () const override
 Returns true when the ALSA device is open. More...
 
ssize_t readData (void *buffer, size_t maxBytes) override
 Reads captured bytes from the input buffer. More...
 
ssize_t writeData (const void *data, size_t size) override
 Writes bytes to the output device. More...
 
ssize_t writeFrames (const void *data, size_t frames)
 Writes complete frames to the output device. More...
 
size_t delayFrames () const
 Returns ALSA's current playback/capture delay in frames, or zero when unavailable. More...
 
void drain ()
 Drains queued playback frames. More...
 
void drop ()
 Drops queued playback/capture frames. More...
 
- Public Member Functions inherited from RADCore::RADIODevice
virtual ~RADIODevice ()=default
 Destroys the IO device. More...
 
std::vector< uint8_t > read (size_t maxBytes)
 Reads up to maxBytes into a byte vector. More...
 
std::vector< uint8_t > readAll (size_t chunkSize=8192)
 Reads all available data in chunks. More...
 
ssize_t write (const std::vector< uint8_t > &data)
 Writes a byte vector. More...
 
ssize_t write (const std::string &text)
 Writes string bytes. More...
 
- Public Member Functions inherited from RADCore::RADObject
 RADObject ()
 Creates a RADObject bound to the current thread. More...
 
virtual ~RADObject ()
 Disconnects tracked event links and releases lifetime token. More...
 
void registerLink (const ConnectionRecord &record)
 Registers a connection for cleanup when this object is destroyed. More...
 
std::thread::id threadId () const
 Returns the thread id this object is currently associated with. More...
 
EventLoopassociatedEventLoop () const
 Returns the associated event loop, if any. More...
 
ThreadAffinity threadAffinity () const
 Returns both thread id and associated loop atomically. More...
 
std::weak_ptr< void > lifetimeToken () const
 Returns a weak token used to drop queued callbacks after destruction. More...
 
void moveToThread (std::thread::id tId, EventLoop *loop=nullptr)
 Moves the object's affinity to tId and optional loop. More...
 

Static Public Member Functions

static bool isFormatSupported (const std::string &deviceId, AudioDeviceDirection direction, const WavFormat &format)
 Returns true when ALSA reports deviceId can use format for direction. More...
 

Public Attributes

RADCore::Event< void() > started
 Raised when the device starts capture or playback. More...
 
RADCore::Event< void() > stopped
 Raised when the device stops. More...
 
RADCore::Event< void() > underrun
 Raised when playback underruns. More...
 
- Public Attributes inherited from RADCore::RADIODevice
RADCore::Event< void() > readyRead
 Raised when data is available to read. More...
 
RADCore::Event< void(size_t) > bytesWritten
 Raised after bytes are written. More...
 
RADCore::Event< void(std::string) > errorOccurred
 Raised when an IO error occurs. More...
 

Detailed Description

ALSA-backed input or output audio device implementing RADIODevice.

Constructor & Destructor Documentation

◆ RADAudioDevice()

RADMedia::RADAudioDevice::RADAudioDevice ( RADCore::EventLoop loop,
AudioDeviceDirection  direction 
)

Creates an input or output audio device using loop for events.

◆ ~RADAudioDevice()

RADMedia::RADAudioDevice::~RADAudioDevice ( )
override

Closes the device if still open.

Member Function Documentation

◆ bufferFrames()

size_t RADMedia::RADAudioDevice::bufferFrames ( ) const

Returns the requested ALSA hardware buffer size in frames, or zero for automatic.

◆ bytesAvailable()

size_t RADMedia::RADAudioDevice::bytesAvailable ( ) const

Returns queued readable bytes for input devices.

◆ close()

void RADMedia::RADAudioDevice::close ( )
overridevirtual

Closes the ALSA handle and worker thread.

Implements RADCore::RADIODevice.

◆ delayFrames()

size_t RADMedia::RADAudioDevice::delayFrames ( ) const

Returns ALSA's current playback/capture delay in frames, or zero when unavailable.

◆ deviceId()

const std::string& RADMedia::RADAudioDevice::deviceId ( ) const

Returns the configured ALSA device id.

◆ direction()

AudioDeviceDirection RADMedia::RADAudioDevice::direction ( ) const

Returns whether this device captures input or plays output.

◆ drain()

void RADMedia::RADAudioDevice::drain ( )

Drains queued playback frames.

◆ drop()

void RADMedia::RADAudioDevice::drop ( )

Drops queued playback/capture frames.

◆ format()

const WavFormat& RADMedia::RADAudioDevice::format ( ) const

Returns the configured audio format.

◆ framesPerChunk()

size_t RADMedia::RADAudioDevice::framesPerChunk ( ) const

Returns frames transferred per low-level chunk.

◆ isFormatSupported()

static bool RADMedia::RADAudioDevice::isFormatSupported ( const std::string &  deviceId,
AudioDeviceDirection  direction,
const WavFormat format 
)
static

Returns true when ALSA reports deviceId can use format for direction.

◆ isOpen()

bool RADMedia::RADAudioDevice::isOpen ( ) const
overridevirtual

Returns true when the ALSA device is open.

Implements RADCore::RADIODevice.

◆ maxBufferedBytes()

size_t RADMedia::RADAudioDevice::maxBufferedBytes ( ) const

Returns the maximum queued input bytes.

◆ open()

bool RADMedia::RADAudioDevice::open ( RADCore::OpenMode  mode)
overridevirtual

Opens the device for read or write depending on direction.

Implements RADCore::RADIODevice.

◆ periodFrames()

size_t RADMedia::RADAudioDevice::periodFrames ( ) const

Returns the requested ALSA hardware period size in frames, or zero for automatic.

◆ readData()

ssize_t RADMedia::RADAudioDevice::readData ( void *  buffer,
size_t  maxBytes 
)
overridevirtual

Reads captured bytes from the input buffer.

Implements RADCore::RADIODevice.

◆ setBufferFrames()

void RADMedia::RADAudioDevice::setBufferFrames ( size_t  frames)

Sets the requested ALSA hardware buffer size in frames. Zero uses targetLatencyUsec().

◆ setDeviceId()

void RADMedia::RADAudioDevice::setDeviceId ( std::string  deviceId)

Sets the ALSA device id, usually "default" or an enumerated id.

◆ setFormat()

void RADMedia::RADAudioDevice::setFormat ( WavFormat  format)

Sets the desired audio format.

◆ setFramesPerChunk()

void RADMedia::RADAudioDevice::setFramesPerChunk ( size_t  frames)

Sets how many frames are transferred per low-level chunk.

◆ setMaxBufferedBytes()

void RADMedia::RADAudioDevice::setMaxBufferedBytes ( size_t  bytes)

Sets the maximum queued input bytes retained for reads.

◆ setPeriodFrames()

void RADMedia::RADAudioDevice::setPeriodFrames ( size_t  frames)

Sets the requested ALSA hardware period size in frames. Zero uses framesPerChunk().

◆ setTargetLatencyUsec()

void RADMedia::RADAudioDevice::setTargetLatencyUsec ( unsigned int  usec)

Sets the requested ALSA device latency in microseconds before opening.

◆ targetLatencyUsec()

unsigned int RADMedia::RADAudioDevice::targetLatencyUsec ( ) const

Returns the requested ALSA device latency in microseconds.

◆ writeData()

ssize_t RADMedia::RADAudioDevice::writeData ( const void *  data,
size_t  size 
)
overridevirtual

Writes bytes to the output device.

Implements RADCore::RADIODevice.

◆ writeFrames()

ssize_t RADMedia::RADAudioDevice::writeFrames ( const void *  data,
size_t  frames 
)

Writes complete frames to the output device.

Member Data Documentation

◆ started

RADCore::Event<void () > RADMedia::RADAudioDevice::started

Raised when the device starts capture or playback.

◆ stopped

RADCore::Event<void () > RADMedia::RADAudioDevice::stopped

Raised when the device stops.

◆ underrun

RADCore::Event<void () > RADMedia::RADAudioDevice::underrun

Raised when playback underruns.


The documentation for this class was generated from the following file: