arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
USBAudioStreamBase Class Referenceabstract

Role-independent front end shared by USB Audio devices (USBAudioDeviceBase, aliased as USBAudioStream) and USB Audio hosts (USBAudioHostBase, aliased as USBAudioHostStream). More...

#include <USBAudioStreamBase.h>

Inheritance diagram for USBAudioStreamBase:
AudioStream VolumeSupport BaseStream AudioInfoSupport AudioInfoSource Stream Print USBAudioDeviceBase USBAudioHostBase USBAudioDeviceESP32 USBAudioDeviceRenesas USBAudioDeviceTinyUSB USBAudioHostESP32 USBAudioHostTinyUSB USBAudioDeviceTinyUSBFreeRTOS USBAudioHostTinyUSBFreeRTOS

Public Member Functions

virtual void addNotifyAudioChange (AudioInfoSupport &bi)
 Adds target to be notified about audio changes.
 
virtual AudioInfo audioInfo () override
 provides the actual input AudioInfo
 
virtual AudioInfo audioInfoOut ()
 
int available () override
 Bytes of received audio waiting in the RX buffer.
 
int availableForWrite () override
 Bytes of free space in the TX buffer.
 
virtual bool begin ()
 
virtual BaseBuffer< uint8_t > & bufferRx ()=0
 Returns the RX audio buffer. Must be overridden by subclasses.
 
virtual BaseBuffer< uint8_t > & bufferTx ()=0
 Returns the TX audio buffer. Must be overridden by subclasses.
 
virtual void clearNotifyAudioChange ()
 Deletes all change notify subscriptions.
 
const USBAudioConfig & config () const
 Provides the active configuration.
 
virtual void end ()
 
virtual void flush () override
 
virtual int getActualCore () const
 Returns the actual core on which the call is running (for RP2040)
 
bool isEpInEnabled () const
 Returns true if the IN endpoint is enabled.
 
bool isEpOutEnabled () const
 Returns true if the OUT endpoint is enabled.
 
bool isMute (uint8_t channel=0) const
 Returns the current mute state for the given channel.
 
bool isNotifyActive ()
 Checks if the automatic AudioInfo update is active.
 
bool isStreamingActive () const
 Returns true if either the TX or RX stream is open.
 
virtual bool isStreamingActiveRx () const =0
 Returns true if the stream used by readBytes() is open.
 
virtual bool isStreamingActiveTx () const =0
 Returns true if the stream used by write() is open.
 
virtual bool mounted ()=0
 Returns true if the USB connection is established (device: enumerated by the host; host: an audio device is attached).
 
 operator bool () override
 
size_t readBytes (uint8_t *buffer, size_t bufsize)
 Receive audio data from the USB peer. Until bufferRx() has filled past config.rx_start_fill_percent at least once after begin(), no data is returned. This is a one-shot startup prime only – once reached, readBytes() keeps draining normally for the rest of the session and is not gated again by later underruns.
 
virtual size_t readSilence (uint8_t *buffer, size_t length)
 Source to generate silence: just sets the buffer to 0.
 
virtual bool removeNotifyAudioChange (AudioInfoSupport &bi)
 Removes a target in order not to be notified about audio changes.
 
virtual void serviceUSB ()=0
 Process pending USB events on platforms where the application drives the stack. No-op where a dedicated task runs the stack.
 
virtual void setAudioInfo (AudioInfo newInfo) override
 Defines the input AudioInfo.
 
bool setMute (bool m, uint8_t channel=0)
 Set the mute state for a channel and notify the USB peer.
 
void setMuteCallback (std::function< void(bool, uint8_t)> cb)
 Register a callback invoked when the mute state changes.
 
void setNotifyActive (bool flag)
 Deactivate/Reactivate automatic AudioInfo updates: (default is active)
 
void setSampleRateCallback (std::function< void(uint32_t)> cb)
 Register a callback invoked when the sample rate changes.
 
void setStreamingStateCallback (std::function< void(bool, bool)> cb)
 Register a callback invoked when the streaming state changes (a streaming interface was opened or closed).
 
bool setVolume (float vol, uint8_t channel)
 Set the volume for a channel and notify the USB peer.
 
bool setVolume (float volume) override
 sets the volume for the master channel (channel 0)
 
void setVolumeCallback (std::function< void(float, uint8_t)> cb)
 Register a callback invoked when the volume changes.
 
void setWriteBufferSize (int size)
 
float volume () override
 gets the volume for the master channel (channel 0)
 
float volume (uint8_t channel)
 Returns the current volume for the given channel.
 
size_t write (const uint8_t *data, size_t len)
 Send audio data to the USB peer. Silently discards data while the TX stream is not open so StreamCopy doesn't report write errors before streaming starts.
 
virtual size_t write (uint8_t ch) override
 
virtual void writeSilence (size_t len)
 Writes len bytes of silence (=0).
 

Protected Member Functions

virtual bool beginUSB ()=0
 Override in platform subclasses to start the USB stack.
 
bool configChanged (const USBAudioConfig &n)
 
float getVolumeExt (uint8_t channel) const
 Returns the effective volume for a per-channel index (1-based). Combines master volume (index 0) with per-channel volume and mute.
 
virtual int not_supported (int out, const char *msg="")
 
void notifyAudioChange (AudioInfo info)
 
void notifyStreamingState ()
 
virtual void onMuteChanged (uint8_t)
 
virtual void onVolumeChanged (uint8_t)
 
template<typename T >
void processVolume (T *data, size_t sample_count)
 
void processVolume (uint8_t *data, size_t len)
 Process audio data for volume control.
 
void refillReadBuffer ()
 Refill small read buffer (e.g. 8 bytes) to avoid single byte reads when calling read()
 
virtual void resizeBuffers ()=0
 Resize the platform audio buffers.
 
void setConfig (const USBAudioConfig &cfg)
 Set the USB audio configuration (use begin(cfg) instead).
 
void setupVolumeState ()
 (Re)sizes the volume and mute state: master (index 0) + one per channel
 

Static Protected Member Functions

static int16_t floatToUac2 (float vol)
 Convert linear volume (0.0–1.0) to UAC2 int16 (1/256 dB). Linear mapping: 0.0 → -100 dB (min), 1.0 → 0 dB (max).
 
static bool isValidBitsPerSample (uint8_t bps)
 
static float uac2ToFloat (int16_t v)
 Convert UAC2 int16 (1/256 dB) to linear volume (0.0–1.0). Linear mapping within the -100..0 dB range reported by GET_RANGE.
 

Protected Attributes

int _timeout = 10
 
USBAudioConfig config_
 
int core = -1
 
AudioInfo info
 
bool is_active_ = false
 
bool is_notify_active = true
 
bool is_started_ = false
 
std::vector< bool > mute_
 
std::function< void(bool, uint8_t)> mute_cb_
 
Vector< AudioInfoSupport * > notify_vector
 
bool rx_primed_ = false
 
std::function< void(uint32_t)> sample_rate_cb_
 
std::function< void(bool, bool)> streaming_state_cb_
 
RingBuffer< uint8_t > tmp_in {0}
 
RingBuffer< uint8_t > tmp_out {0}
 
bool usb_task_active_ = false
 
std::vector< float > volume_
 
std::function< void(float, uint8_t)> volume_cb_
 
float volume_value = 1.0f
 
int write_buffer_size = MAX_SINGLE_CHARS
 

Static Protected Attributes

static constexpr int16_t kVolumeMinDb256 = -25600
 Convert AudioTools volume (0.0–1.0) to UAC2 int16 (1/256 dB). 0.0 maps to 0x8000 (silence), 1.0 maps to 0 (0 dB).
 

Detailed Description

Role-independent front end shared by USB Audio devices (USBAudioDeviceBase, aliased as USBAudioStream) and USB Audio hosts (USBAudioHostBase, aliased as USBAudioHostStream).

It provides the identical public Arduino stream API for both roles: write() sends audio to the USB peer, readBytes() receives audio from it, plus volume/mute state, callbacks and the one-shot RX startup prime. The USB stack specific parts are provided by the subclasses via the pure virtual methods.

Author
Phil Schatzmann

Member Function Documentation

◆ addNotifyAudioChange()

virtual void addNotifyAudioChange ( AudioInfoSupport &  bi)
inlinevirtualinherited

◆ audioInfo()

virtual AudioInfo audioInfo ( )
inlineoverridevirtualinherited

◆ audioInfoOut()

virtual AudioInfo audioInfoOut ( )
inlinevirtualinherited

◆ available()

int available ( )
inlineoverridevirtual

Bytes of received audio waiting in the RX buffer.

Reimplemented from BaseStream.

◆ availableForWrite()

int availableForWrite ( )
inlineoverridevirtual

Bytes of free space in the TX buffer.

Reimplemented from BaseStream.

◆ begin()

virtual bool begin ( )
inlinevirtualinherited

◆ beginUSB()

virtual bool beginUSB ( )
protectedpure virtual

◆ bufferRx()

virtual BaseBuffer< uint8_t > & bufferRx ( )
pure virtual

◆ bufferTx()

virtual BaseBuffer< uint8_t > & bufferTx ( )
pure virtual

◆ clearNotifyAudioChange()

virtual void clearNotifyAudioChange ( )
inlinevirtualinherited

Deletes all change notify subscriptions.

Reimplemented in RTSPClient< TcpClient, UdpSocket >.

◆ config()

const USBAudioConfig & config ( ) const
inline

Provides the active configuration.

◆ configChanged()

bool configChanged ( const USBAudioConfig &  n)
inlineprotected

◆ end()

virtual void end ( )
inlinevirtualinherited

◆ floatToUac2()

static int16_t floatToUac2 ( float  vol)
inlinestaticprotected

Convert linear volume (0.0–1.0) to UAC2 int16 (1/256 dB). Linear mapping: 0.0 → -100 dB (min), 1.0 → 0 dB (max).

◆ flush()

virtual void flush ( )
inlineoverridevirtualinherited

◆ getActualCore()

virtual int getActualCore ( ) const
inlinevirtual

Returns the actual core on which the call is running (for RP2040)

Reimplemented in USBAudioDeviceBase, USBAudioDeviceESP32, USBAudioDeviceTinyUSB, and USBAudioHostESP32.

◆ getVolumeExt()

float getVolumeExt ( uint8_t  channel) const
inlineprotected

Returns the effective volume for a per-channel index (1-based). Combines master volume (index 0) with per-channel volume and mute.

◆ isEpInEnabled()

bool isEpInEnabled ( ) const
inline

Returns true if the IN endpoint is enabled.

◆ isEpOutEnabled()

bool isEpOutEnabled ( ) const
inline

Returns true if the OUT endpoint is enabled.

◆ isMute()

bool isMute ( uint8_t  channel = 0) const
inline

Returns the current mute state for the given channel.

Parameters
channel0 = master, 1..N = per-channel.

◆ isNotifyActive()

bool isNotifyActive ( )
inlineinherited

Checks if the automatic AudioInfo update is active.

◆ isStreamingActive()

bool isStreamingActive ( ) const
inline

Returns true if either the TX or RX stream is open.

◆ isStreamingActiveRx()

virtual bool isStreamingActiveRx ( ) const
pure virtual

Returns true if the stream used by readBytes() is open.

Implemented in USBAudioDeviceBase, and USBAudioHostBase.

◆ isStreamingActiveTx()

virtual bool isStreamingActiveTx ( ) const
pure virtual

Returns true if the stream used by write() is open.

Implemented in USBAudioDeviceBase, and USBAudioHostBase.

◆ isValidBitsPerSample()

static bool isValidBitsPerSample ( uint8_t  bps)
inlinestaticprotected

◆ mounted()

virtual bool mounted ( )
pure virtual

Returns true if the USB connection is established (device: enumerated by the host; host: an audio device is attached).

Implemented in USBAudioDeviceBase, and USBAudioHostBase.

◆ not_supported()

virtual int not_supported ( int  out,
const char *  msg = "" 
)
inlineprotectedvirtualinherited

◆ notifyAudioChange()

void notifyAudioChange ( AudioInfo  info)
inlineprotectedinherited

◆ notifyStreamingState()

void notifyStreamingState ( )
inlineprotected

◆ onMuteChanged()

virtual void onMuteChanged ( uint8_t  )
inlineprotectedvirtual

Called by setMute() after the state was updated: propagate the change to the USB peer.

Reimplemented in USBAudioDeviceBase, and USBAudioHostBase.

◆ onVolumeChanged()

virtual void onVolumeChanged ( uint8_t  )
inlineprotectedvirtual

Called by setVolume() after the state was updated: propagate the change to the USB peer.

Reimplemented in USBAudioDeviceBase, and USBAudioHostBase.

◆ operator bool()

operator bool ( )
inlineoverridevirtual

Returns true when begin() has been called and the USB connection is established.

Reimplemented from AudioStream.

◆ processVolume() [1/2]

template<typename T >
void processVolume ( T *  data,
size_t  sample_count 
)
inlineprotected

◆ processVolume() [2/2]

void processVolume ( uint8_t *  data,
size_t  len 
)
inlineprotected

Process audio data for volume control.

◆ readBytes()

size_t readBytes ( uint8_t *  buffer,
size_t  bufsize 
)
inlinevirtual

Receive audio data from the USB peer. Until bufferRx() has filled past config.rx_start_fill_percent at least once after begin(), no data is returned. This is a one-shot startup prime only – once reached, readBytes() keeps draining normally for the rest of the session and is not gated again by later underruns.

Reimplemented from AudioStream.

◆ readSilence()

virtual size_t readSilence ( uint8_t *  buffer,
size_t  length 
)
inlinevirtualinherited

Source to generate silence: just sets the buffer to 0.

◆ refillReadBuffer()

void refillReadBuffer ( )
inlineprotectedinherited

Refill small read buffer (e.g. 8 bytes) to avoid single byte reads when calling read()

◆ removeNotifyAudioChange()

virtual bool removeNotifyAudioChange ( AudioInfoSupport &  bi)
inlinevirtualinherited

Removes a target in order not to be notified about audio changes.

Reimplemented in RTSPClient< TcpClient, UdpSocket >.

◆ resizeBuffers()

virtual void resizeBuffers ( )
protectedpure virtual

◆ serviceUSB()

virtual void serviceUSB ( )
pure virtual

Process pending USB events on platforms where the application drives the stack. No-op where a dedicated task runs the stack.

Implemented in USBAudioDeviceESP32, USBAudioDeviceRenesas, USBAudioDeviceTinyUSB, USBAudioHostESP32, USBAudioHostTinyUSB, and USBAudioDeviceBase.

◆ setAudioInfo()

virtual void setAudioInfo ( AudioInfo  info)
inlineoverridevirtualinherited

◆ setConfig()

void setConfig ( const USBAudioConfig &  cfg)
inlineprotected

Set the USB audio configuration (use begin(cfg) instead).

◆ setMute()

bool setMute ( bool  m,
uint8_t  channel = 0 
)
inline

Set the mute state for a channel and notify the USB peer.

Parameters
mtrue = muted, false = unmuted.
channel0 = master, 1..N = per-channel.
Returns
true if the channel index was valid.

◆ setMuteCallback()

void setMuteCallback ( std::function< void(bool, uint8_t)>  cb)
inline

Register a callback invoked when the mute state changes.

Parameters
cbCallback receiving (bool muted, uint8_t channel).

◆ setNotifyActive()

void setNotifyActive ( bool  flag)
inlineinherited

Deactivate/Reactivate automatic AudioInfo updates: (default is active)

◆ setSampleRateCallback()

void setSampleRateCallback ( std::function< void(uint32_t)>  cb)
inline

Register a callback invoked when the sample rate changes.

Parameters
cbCallback receiving the new rate in Hz.

◆ setStreamingStateCallback()

void setStreamingStateCallback ( std::function< void(bool, bool)>  cb)
inline

Register a callback invoked when the streaming state changes (a streaming interface was opened or closed).

Parameters
cbCallback receiving (bool active_tx, bool active_rx).

◆ setupVolumeState()

void setupVolumeState ( )
inlineprotected

(Re)sizes the volume and mute state: master (index 0) + one per channel

◆ setVolume() [1/2]

bool setVolume ( float  vol,
uint8_t  channel 
)
inline

Set the volume for a channel and notify the USB peer.

Parameters
volVolume as a float 0.0 (silence) to 1.0 (0 dB).
channel0 = master, 1..N = per-channel.
Returns
true if the channel index was valid.

◆ setVolume() [2/2]

bool setVolume ( float  volume)
inlineoverridevirtual

sets the volume for the master channel (channel 0)

Reimplemented from VolumeSupport.

◆ setVolumeCallback()

void setVolumeCallback ( std::function< void(float, uint8_t)>  cb)
inline

Register a callback invoked when the volume changes.

Parameters
cbCallback receiving (float volume, uint8_t channel).

◆ setWriteBufferSize()

void setWriteBufferSize ( int  size)
inlineinherited

◆ uac2ToFloat()

static float uac2ToFloat ( int16_t  v)
inlinestaticprotected

Convert UAC2 int16 (1/256 dB) to linear volume (0.0–1.0). Linear mapping within the -100..0 dB range reported by GET_RANGE.

◆ volume() [1/2]

float volume ( )
inlineoverridevirtual

gets the volume for the master channel (channel 0)

Reimplemented from VolumeSupport.

◆ volume() [2/2]

float volume ( uint8_t  channel)
inline

Returns the current volume for the given channel.

Parameters
channel0 = master, 1..N = per-channel.
Returns
Volume as a float in the range 0.0 (silence) to 1.0 (0 dB).

◆ write() [1/2]

size_t write ( const uint8_t *  data,
size_t  len 
)
inlinevirtual

Send audio data to the USB peer. Silently discards data while the TX stream is not open so StreamCopy doesn't report write errors before streaming starts.

Reimplemented from AudioStream.

◆ write() [2/2]

virtual size_t write ( uint8_t  ch)
inlineoverridevirtualinherited

◆ writeSilence()

virtual void writeSilence ( size_t  len)
inlinevirtualinherited

Writes len bytes of silence (=0).

Member Data Documentation

◆ _timeout

int _timeout = 10
protectedinherited

◆ config_

USBAudioConfig config_
protected

◆ core

int core = -1
protected

◆ info

AudioInfo info
protectedinherited

◆ is_active_

bool is_active_ = false
protected

◆ is_notify_active

bool is_notify_active = true
protectedinherited

◆ is_started_

bool is_started_ = false
protected

◆ kVolumeMinDb256

constexpr int16_t kVolumeMinDb256 = -25600
staticconstexprprotected

Convert AudioTools volume (0.0–1.0) to UAC2 int16 (1/256 dB). 0.0 maps to 0x8000 (silence), 1.0 maps to 0 (0 dB).

◆ mute_

std::vector<bool> mute_
protected

◆ mute_cb_

std::function<void(bool, uint8_t)> mute_cb_
protected

◆ notify_vector

Vector<AudioInfoSupport*> notify_vector
protectedinherited

◆ rx_primed_

bool rx_primed_ = false
protected

◆ sample_rate_cb_

std::function<void(uint32_t)> sample_rate_cb_
protected

◆ streaming_state_cb_

std::function<void(bool, bool)> streaming_state_cb_
protected

◆ tmp_in

RingBuffer<uint8_t> tmp_in {0}
protectedinherited

◆ tmp_out

RingBuffer<uint8_t> tmp_out {0}
protectedinherited

◆ usb_task_active_

bool usb_task_active_ = false
protected

◆ volume_

std::vector<float> volume_
protected

◆ volume_cb_

std::function<void(float, uint8_t)> volume_cb_
protected

◆ volume_value

float volume_value = 1.0f
protectedinherited

◆ write_buffer_size

int write_buffer_size = MAX_SINGLE_CHARS
protectedinherited

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