PebbleOS v4.47.0-512-g5285d4236
Loading...
Searching...
No Matches
Audio

Audio stream class. More...

Modules

 GPIO amplifier
 Amplifier switched by an enable GPIO, with an optional supply.
 
 DA7212 codec
 Audio codec: Dialog DA7212, playing to its line output.
 
 Digital microphone
 Digital microphones on a PDM interface, as a codec without a gain: powered from their reference supply while recording, the gain left to the interface.
 
 nRF5 audio
 Audio devices of the nRF52: the PDM and I2S interfaces.
 
 Emulated audio
 Audio devices of the emulators.
 
 SF32LB52 audio
 Audio DAIs of the SF32LB52: the audio codec DAC and the PDM interface.
 
 Simulator
 Simulated microphone, recording the audio written to it.
 

Data Structures

struct  pbl_audio_pos
 Stream position at a period boundary. More...
 
struct  pbl_audio_stream
 A stream. More...
 
struct  pbl_audio_caps
 What a device can do. More...
 
struct  pbl_audio_dai_ops
 DAI driver operations. More...
 
struct  pbl_audio_codec_cfg
 Codec configuration for a stream. More...
 
struct  pbl_audio_codec_ops
 Codec driver operations, called with the lock of the card held. More...
 
struct  pbl_audio_codec
 A codec: a separate chip converting between the link and the analog side. More...
 
struct  pbl_audio_amp_ops
 Amplifier driver operations, called with the lock of the linked device held. More...
 
struct  pbl_audio_amp
 An amplifier between the output and the speaker. More...
 
struct  pbl_audio_dai
 A digital audio interface of the SoC, streaming one direction. More...
 
struct  pbl_audio
 An audio device: a card, a DAI and what it is wired to. More...
 

Macros

#define PBL_AUDIO_BUF_ALIGN   32U
 Alignment, in bytes, of rings and their size: the largest data cache line.
 
#define PBL_AUDIO_MAX_PERIODS   32U
 Most periods in a ring.
 
#define PBL_AUDIO_CODEC_INIT(sym, _name, _init, _parent, _ops)
 Initializer of a struct pbl_audio_codec.
 
#define PBL_AUDIO_AMP_INIT(sym, _name, _init, _parent, _ops)
 Initializer of a struct pbl_audio_amp.
 
#define PBL_AUDIO_DAI_STATE_DEFINE(sym)
 Define the class state of a DAI.
 
#define PBL_AUDIO_DAI_INIT(sym, _name, _parent, _ops, _dir)
 Initializer for a struct pbl_audio_dai.
 
#define PBL_AUDIO_CARD_DEFINE(sym, _name, _dai, _codec, _amp, _fmt, _clocks)
 Define a card.
 

Typedefs

typedef void(* pbl_audio_period_cb) (const struct pbl_audio *audio, void *period, const struct pbl_audio_pos *pos, void *arg)
 Period callback, called from the driver's interrupt.
 

Enumerations

enum  pbl_audio_dir { PBL_AUDIO_PLAYBACK , PBL_AUDIO_CAPTURE }
 Stream direction. More...
 

Functions

static uint8_t pbl_audio_dir (const struct pbl_audio *audio)
 Get the direction of a card.
 
static const struct pbl_audio * pbl_audio_dai_card (const struct pbl_audio_dai *dai)
 Get the card of a DAI, for drivers.
 
void pbl_audio_period_elapsed (const struct pbl_audio *audio)
 Report the end of the period the hardware was working on.
 
void pbl_audio_report_xrun (const struct pbl_audio *audio)
 Report a hardware overrun or underrun, flagged in the next callback.
 
void * pbl_audio_period (const struct pbl_audio *audio, uint8_t idx)
 Get a period of the running stream, for drivers.
 
int pbl_audio_get_caps (const struct pbl_audio *audio, struct pbl_audio_caps *caps)
 Get what a device can do, including its codec.
 
int pbl_audio_start (const struct pbl_audio *audio, const struct pbl_audio_stream *stream)
 Start streaming.
 
int pbl_audio_period_ready (const struct pbl_audio *audio, void *period)
 Give a period back to the hardware: refilled for playback, read for capture.
 
int pbl_audio_stop (const struct pbl_audio *audio)
 Stop streaming.
 
int pbl_audio_set_gain (const struct pbl_audio *audio, int32_t gain_mdb)
 Set the gain.
 
int pbl_audio_set_mute (const struct pbl_audio *audio, bool mute)
 Mute or unmute.
 
int pbl_audio_get_position (const struct pbl_audio *audio, struct pbl_audio_pos *pos)
 Get the current stream position.
 
bool pbl_audio_is_running (const struct pbl_audio *audio)
 Check whether a device is streaming.
 
size_t pbl_audio_ring_bytes (const struct pbl_audio_stream *stream)
 Get the bytes of a ring.
 
void * pbl_audio_buf_alloc (size_t size)
 Allocate a ring: aligned and padded to PBL_AUDIO_BUF_ALIGN, reachable by DMA.
 
void pbl_audio_buf_free (void *buf)
 Free a ring from pbl_audio_buf_alloc().
 
uint32_t pbl_audio_rate_bit (uint32_t rate)
 Get the bit of a sample rate.
 
static int32_t pbl_audio_gain_to_step (int32_t gain_mdb, int32_t min_mdb, int32_t step_mdb)
 Convert a gain in mdB to the register steps of a hardware gain stage.
 

Variables

const struct pbl_audio *const SPEAKER
 The board's speaker, a playback device.
 
const struct pbl_audio *const MIC
 The board's microphone, a capture device.
 

Sample rates

Bits of struct pbl_audio_caps::rates.

#define PBL_AUDIO_RATE_8000   PBL_BIT(0)
 8 kHz.
 
#define PBL_AUDIO_RATE_11025   PBL_BIT(1)
 11.025 kHz.
 
#define PBL_AUDIO_RATE_12000   PBL_BIT(2)
 12 kHz.
 
#define PBL_AUDIO_RATE_16000   PBL_BIT(3)
 16 kHz.
 
#define PBL_AUDIO_RATE_22050   PBL_BIT(4)
 22.05 kHz.
 
#define PBL_AUDIO_RATE_24000   PBL_BIT(5)
 24 kHz.
 
#define PBL_AUDIO_RATE_32000   PBL_BIT(6)
 32 kHz.
 
#define PBL_AUDIO_RATE_44100   PBL_BIT(7)
 44.1 kHz.
 
#define PBL_AUDIO_RATE_48000   PBL_BIT(8)
 48 kHz.
 

Link formats

Flags of struct pbl_audio_link::fmt.

#define PBL_AUDIO_FMT_I2S   PBL_BIT(0)
 I2S framing.
 
#define PBL_AUDIO_FMT_CODEC_MASTER   PBL_BIT(1)
 The codec drives the bit and frame clocks.
 

Detailed Description

Audio stream class.

An audio device is a struct pbl_audio streaming one direction, playback or capture, between a ring of periods in memory and the hardware. The client owns the ring: interleaved signed 16-bit samples, periods periods of period_frames frames each, aligned and padded to PBL_AUDIO_BUF_ALIGN (pbl_audio_buf_alloc() returns such a buffer). The hardware works through the periods in order and wraps around.

Each time the hardware finishes a period, the class calls the client's period_done callback from the driver's interrupt with that period and the stream position. The client then owns the period until it gives it back with pbl_audio_period_ready(), from the callback or later:

static void prv_period_done(const struct pbl_audio *audio, void *period,
const struct pbl_audio_pos *pos, void *arg) {
fill(period);
(void)pbl_audio_period_ready(audio, period);
}
const struct pbl_audio_stream stream = {
.rate = 16000,
.channels = 1,
.buf = ring,
.period_frames = 512,
.periods = 2,
.period_done = prv_period_done,
};
ret = pbl_audio_start(SPEAKER, &stream);
...
uint32_t rate
Sample rate, in Hz.
Definition audio.h:155
An audio device: a card, a DAI and what it is wired to.
Definition audio.h:365
A stream.
Definition audio.h:153
Stream position at a period boundary.
Definition audio.h:132
int pbl_audio_start(const struct pbl_audio *audio, const struct pbl_audio_stream *stream)
Start streaming.
int pbl_audio_period_ready(const struct pbl_audio *audio, void *period)
Give a period back to the hardware: refilled for playback, read for capture.
int pbl_audio_stop(const struct pbl_audio *audio)
Stop streaming.
const struct pbl_audio *const SPEAKER
The board's speaker, a playback device.

Positions count the frames the hardware has transferred since the stream started, stamped with the uptime at the period boundary, on the same timebase for every device. A playback period handed out at frame f starts playing at frame f + (periods - 1) * period_frames; a capture period handed out at frame f holds frames f - period_frames to f.

Gain is in millibels (mdB) and mute is separate from it. Both take effect at once while streaming and are kept for the next start otherwise.

An audio device is a card, as in Linux: a digital audio interface (DAI) of the SoC, the struct pbl_audio_dai a driver implements, linked to a codec and an amplifier by a struct pbl_audio_link. The class powers them around the stream: the amplifier on before the stream and off after it, the codec up once the stream runs (it may need its clocks) and down before the stream stops, and routes gain and mute to the codec.

Calls other than pbl_audio_period_ready() must not be made from interrupts. They return 0 or a negative errno, -ENODEV if the device is not ready.


Data Structure Documentation

◆ pbl_audio_pos

struct pbl_audio_pos

Stream position at a period boundary.

Data Fields
uint64_t frame Frames the hardware has transferred since the stream started.
uint64_t ts_us Uptime when it reached frame, in microseconds.
bool xrun A period was missed since the previous callback: played as silence, or overwritten.

◆ pbl_audio_stream

struct pbl_audio_stream

A stream.

Data Fields
void * arg Callback argument.
void * buf Ring, periods periods of period_frames frames.
uint8_t channels Channels per frame.
pbl_audio_period_cb period_done Period callback.
size_t period_frames Frames per period.
uint8_t periods Periods in the ring, at least 2.
uint32_t rate Sample rate, in Hz.

◆ pbl_audio_caps

struct pbl_audio_caps

What a device can do.

Data Fields
int32_t gain_default_mdb Gain after initialization, in mdB.
int32_t gain_max_mdb Highest gain, in mdB.
int32_t gain_min_mdb Lowest gain, in mdB.

Equal to gain_max_mdb if the gain is fixed.

int32_t gain_step_mdb Gain step, in mdB, 0 if the gain is fixed.
uint8_t max_channels Most channels per frame.
uint8_t max_periods Most periods in a ring, 0 for PBL_AUDIO_MAX_PERIODS.
uint8_t min_channels Fewest channels per frame.
uint32_t rates Supported rates, PBL_AUDIO_RATE_* bits.

◆ pbl_audio_codec_cfg

struct pbl_audio_codec_cfg

Codec configuration for a stream.

Data Fields
uint8_t channels Channels per frame on the link.
uint32_t fmt Link format, PBL_AUDIO_FMT_* flags.
int32_t gain_mdb Gain, in mdB.
bool mute Mute.
uint32_t rate Sample rate, in Hz.

◆ pbl_audio_codec

struct pbl_audio_codec

A codec: a separate chip converting between the link and the analog side.

Data Fields
struct pbl_device dev Device.
const struct pbl_audio_codec_ops * ops Driver operations.

◆ pbl_audio_amp

struct pbl_audio_amp

An amplifier between the output and the speaker.

Data Fields
struct pbl_device dev Device.
const struct pbl_audio_amp_ops * ops Driver operations.

◆ pbl_audio_link

struct pbl_audio_link

What a device is wired to.

Data Fields
const struct pbl_audio_amp * amp Amplifier, or NULL.
const struct pbl_audio_codec * codec Codec on the link, or NULL.
uint32_t fmt Link format, PBL_AUDIO_FMT_* flags.

◆ pbl_audio_dai

struct pbl_audio_dai

A digital audio interface of the SoC, streaming one direction.

Data Fields
struct pbl_device dev Device.
uint8_t dir Direction, one of pbl_audio_dir.
const struct pbl_audio_dai_ops * ops Driver operations.
struct pbl_audio_dai_state * state Runtime state.

◆ pbl_audio

struct pbl_audio

An audio device: a card, a DAI and what it is wired to.

Data Fields
const struct pbl_audio_dai * dai DAI.
struct pbl_device dev Device.
const struct pbl_audio_link * link What the DAI is wired to.
struct pbl_audio_state * state Runtime state.

Macro Definition Documentation

◆ PBL_AUDIO_AMP_INIT

#define PBL_AUDIO_AMP_INIT (   sym,
  _name,
  _init,
  _parent,
  _ops 
)
Value:
{ \
.dev = PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, &pbl_audio_amp_class), \
.ops = (_ops), \
}
#define PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, _cls)
Initializer for the struct pbl_device of device instance sym, in class _cls.
Definition device.h:153

Initializer of a struct pbl_audio_amp.

Parameters
symSymbol of the amplifier.
_nameName.
_initInit function, or NULL.
_parentParent device, or NULL.
_opsDriver operations.

◆ PBL_AUDIO_BUF_ALIGN

#define PBL_AUDIO_BUF_ALIGN   32U

Alignment, in bytes, of rings and their size: the largest data cache line.

◆ PBL_AUDIO_CARD_DEFINE

#define PBL_AUDIO_CARD_DEFINE (   sym,
  _name,
  _dai,
  _codec,
  _amp,
  _fmt,
  _clocks 
)
Value:
static struct pbl_audio_state sym##_audio_state; \
static const struct pbl_audio_link sym##_link = { \
.codec = (_codec), \
.amp = (_amp), \
.fmt = (_fmt) | (_clocks), \
}; \
const struct pbl_audio sym = { \
.dev = PBL_DEVICE_INIT_CLASS(sym, _name, pbl_audio_dev_init, &(_dai)->dev, &pbl_audio_class), \
.dai = (_dai), \
.state = &sym##_audio_state, \
.link = &sym##_link, \
}; \
PBL_DEVICE_REGISTER(sym, &sym.dev)
struct pbl_audio_state * state
Runtime state.
Definition audio.h:371
const struct pbl_audio_codec * codec
Codec on the link, or NULL.
Definition audio.h:322
const struct pbl_audio_link * link
What the DAI is wired to.
Definition audio.h:373
const struct pbl_audio_amp * amp
Amplifier, or NULL.
Definition audio.h:324
struct pbl_device dev
Device.
Definition audio.h:367
#define PBL_DEVICE_STATE_DEFINE(sym)
Define the runtime state of device instance sym.
Definition device.h:132

Define a card.

Parameters
symSymbol of the card, a struct pbl_audio.
_nameDevice name.
_daiDAI.
_codecCodec on the link, or NULL.
_ampAmplifier, or NULL.
_fmtLink framing, PBL_AUDIO_FMT_I2S or 0.
_clocksWho drives the clocks, PBL_AUDIO_FMT_CODEC_MASTER or 0.

◆ PBL_AUDIO_CODEC_INIT

#define PBL_AUDIO_CODEC_INIT (   sym,
  _name,
  _init,
  _parent,
  _ops 
)
Value:
{ \
.dev = PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, &pbl_audio_codec_class), \
.ops = (_ops), \
}

Initializer of a struct pbl_audio_codec.

Parameters
symSymbol of the codec.
_nameName.
_initInit function, or NULL.
_parentParent device, or NULL.
_opsDriver operations.

◆ PBL_AUDIO_DAI_INIT

#define PBL_AUDIO_DAI_INIT (   sym,
  _name,
  _parent,
  _ops,
  _dir 
)
Value:
{ \
.dev = \
PBL_DEVICE_INIT_CLASS(sym, _name, pbl_audio_dai_dev_init, _parent, &pbl_audio_dai_class), \
.ops = (_ops), \
.state = &sym##_dai_state, \
.dir = (_dir), \
}

Initializer for a struct pbl_audio_dai.

Parameters
symSymbol of the instance, as passed to PBL_AUDIO_DAI_STATE_DEFINE().
_nameDevice name.
_parentParent device, or NULL.
_opsDriver operations.
_dirDirection, one of pbl_audio_dir.

◆ PBL_AUDIO_DAI_STATE_DEFINE

#define PBL_AUDIO_DAI_STATE_DEFINE (   sym)
Value:
static struct pbl_audio_dai_state sym##_dai_state

Define the class state of a DAI.

Parameters
symSymbol of the instance.

◆ PBL_AUDIO_FMT_CODEC_MASTER

#define PBL_AUDIO_FMT_CODEC_MASTER   PBL_BIT(1)

The codec drives the bit and frame clocks.

◆ PBL_AUDIO_FMT_I2S

#define PBL_AUDIO_FMT_I2S   PBL_BIT(0)

I2S framing.

◆ PBL_AUDIO_MAX_PERIODS

#define PBL_AUDIO_MAX_PERIODS   32U

Most periods in a ring.

◆ PBL_AUDIO_RATE_11025

#define PBL_AUDIO_RATE_11025   PBL_BIT(1)

11.025 kHz.

◆ PBL_AUDIO_RATE_12000

#define PBL_AUDIO_RATE_12000   PBL_BIT(2)

12 kHz.

◆ PBL_AUDIO_RATE_16000

#define PBL_AUDIO_RATE_16000   PBL_BIT(3)

16 kHz.

◆ PBL_AUDIO_RATE_22050

#define PBL_AUDIO_RATE_22050   PBL_BIT(4)

22.05 kHz.

◆ PBL_AUDIO_RATE_24000

#define PBL_AUDIO_RATE_24000   PBL_BIT(5)

24 kHz.

◆ PBL_AUDIO_RATE_32000

#define PBL_AUDIO_RATE_32000   PBL_BIT(6)

32 kHz.

◆ PBL_AUDIO_RATE_44100

#define PBL_AUDIO_RATE_44100   PBL_BIT(7)

44.1 kHz.

◆ PBL_AUDIO_RATE_48000

#define PBL_AUDIO_RATE_48000   PBL_BIT(8)

48 kHz.

◆ PBL_AUDIO_RATE_8000

#define PBL_AUDIO_RATE_8000   PBL_BIT(0)

8 kHz.

Typedef Documentation

◆ pbl_audio_period_cb

typedef void(* pbl_audio_period_cb) (const struct pbl_audio *audio, void *period, const struct pbl_audio_pos *pos, void *arg)

Period callback, called from the driver's interrupt.

Parameters
audioDevice.
periodPeriod the client now owns.
posStream position.
argCallback argument.

Enumeration Type Documentation

◆ pbl_audio_dir

Stream direction.

Enumerator
PBL_AUDIO_PLAYBACK 

From memory to the hardware.

PBL_AUDIO_CAPTURE 

From the hardware to memory.

Function Documentation

◆ pbl_audio_buf_alloc()

void * pbl_audio_buf_alloc ( size_t  size)

Allocate a ring: aligned and padded to PBL_AUDIO_BUF_ALIGN, reachable by DMA.

Parameters
sizeBytes, from pbl_audio_ring_bytes().
Returns
Zeroed ring, or NULL if out of memory.

◆ pbl_audio_buf_free()

void pbl_audio_buf_free ( void *  buf)

Free a ring from pbl_audio_buf_alloc().

Parameters
bufRing, or NULL.

◆ pbl_audio_dai_card()

static const struct pbl_audio * pbl_audio_dai_card ( const struct pbl_audio_dai *  dai)
inlinestatic

Get the card of a DAI, for drivers.

Parameters
daiDAI.
Returns
Card, set once it is initialized.

References pbl_audio::dai, and pbl_audio_dai::state.

◆ pbl_audio_dir()

static uint8_t pbl_audio_dir ( const struct pbl_audio *  audio)
inlinestatic

Get the direction of a card.

Parameters
audioCard.
Returns
One of pbl_audio_dir.

References pbl_audio::dai, and pbl_audio_dai::dir.

◆ pbl_audio_gain_to_step()

static int32_t pbl_audio_gain_to_step ( int32_t  gain_mdb,
int32_t  min_mdb,
int32_t  step_mdb 
)
inlinestatic

Convert a gain in mdB to the register steps of a hardware gain stage.

Parameters
gain_mdbGain in mdB.
min_mdbGain of step 0, in mdB.
step_mdbGain per step, in mdB.
Returns
Step, rounded down.

◆ pbl_audio_get_caps()

int pbl_audio_get_caps ( const struct pbl_audio *  audio,
struct pbl_audio_caps *  caps 
)

Get what a device can do, including its codec.

Parameters
audioDevice.
[out]capsCapabilities.
Returns
0 or a negative errno.

◆ pbl_audio_get_position()

int pbl_audio_get_position ( const struct pbl_audio *  audio,
struct pbl_audio_pos *  pos 
)

Get the current stream position.

Exact to the frame for drivers that report their progress within a period, otherwise the position at the last period boundary.

Parameters
audioDevice.
[out]posPosition; xrun is not reported.
Returns
0, or -EAGAIN if not streaming.

◆ pbl_audio_is_running()

bool pbl_audio_is_running ( const struct pbl_audio *  audio)

Check whether a device is streaming.

Parameters
audioDevice.
Returns
True while streaming.

◆ pbl_audio_period()

void * pbl_audio_period ( const struct pbl_audio *  audio,
uint8_t  idx 
)

Get a period of the running stream, for drivers.

Parameters
audioDevice.
idxPeriod index, below the number of periods.
Returns
Period.

◆ pbl_audio_period_elapsed()

void pbl_audio_period_elapsed ( const struct pbl_audio *  audio)

Report the end of the period the hardware was working on.

Called by drivers from their interrupt handler, once per period, in ring order.

Parameters
audioDevice.

◆ pbl_audio_period_ready()

int pbl_audio_period_ready ( const struct pbl_audio *  audio,
void *  period 
)

Give a period back to the hardware: refilled for playback, read for capture.

May be called from the period callback or any other context.

Parameters
audioDevice.
periodPeriod from the callback.
Returns
0, -EINVAL if the client does not own period, or -EAGAIN if not streaming.

◆ pbl_audio_rate_bit()

uint32_t pbl_audio_rate_bit ( uint32_t  rate)

Get the bit of a sample rate.

Parameters
rateSample rate, in Hz.
Returns
One of the PBL_AUDIO_RATE_* bits, 0 for a rate without one.

◆ pbl_audio_report_xrun()

void pbl_audio_report_xrun ( const struct pbl_audio *  audio)

Report a hardware overrun or underrun, flagged in the next callback.

Called by drivers, from any context.

Parameters
audioDevice.

◆ pbl_audio_ring_bytes()

size_t pbl_audio_ring_bytes ( const struct pbl_audio_stream *  stream)

Get the bytes of a ring.

Parameters
streamStream.
Returns
Size of the ring, rounded up to PBL_AUDIO_BUF_ALIGN.

◆ pbl_audio_set_gain()

int pbl_audio_set_gain ( const struct pbl_audio *  audio,
int32_t  gain_mdb 
)

Set the gain.

Parameters
audioDevice.
gain_mdbGain in mdB, clamped to the range of the device and rounded down to its step.
Returns
0 or a negative errno.

◆ pbl_audio_set_mute()

int pbl_audio_set_mute ( const struct pbl_audio *  audio,
bool  mute 
)

Mute or unmute.

Parameters
audioDevice.
muteTrue to mute.
Returns
0 or a negative errno.

◆ pbl_audio_start()

int pbl_audio_start ( const struct pbl_audio *  audio,
const struct pbl_audio_stream *  stream 
)

Start streaming.

Parameters
audioDevice.
streamStream; copied, but the ring must stay valid until pbl_audio_stop() returns.
Returns
0, -EBUSY if streaming, -EINVAL for a stream the device cannot run, or another negative errno.

◆ pbl_audio_stop()

int pbl_audio_stop ( const struct pbl_audio *  audio)

Stop streaming.

No callback runs once it returns. A no-op if not streaming.

Parameters
audioDevice.
Returns
0 or a negative errno.

Variable Documentation

◆ MIC

const struct pbl_audio* const MIC
extern

The board's microphone, a capture device.

◆ SPEAKER

const struct pbl_audio* const SPEAKER
extern

The board's speaker, a playback device.