Audio stream class.
More...
|
| | 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.
|
| |
|
| #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.
|
| |
|
| 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.
|
| |
|
| 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.
|
| |
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:
- Playback: the period has been played and is handed out zero-filled; the client refills it. A period the client has not given back when the hardware reaches it again plays silence and sets
xrun in the next callback. The ring is played from its initial contents, so fill it, with silence if nothing else, before starting.
- Capture: the period holds new samples; the client releases it once it has read them. A period still held when the hardware needs it again is overwritten, and
xrun is set.
static void prv_period_done(
const struct pbl_audio *audio,
void *period,
fill(period);
}
.channels = 1,
.buf = ring,
.period_frames = 512,
.periods = 2,
.period_done = prv_period_done,
};
...
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.
◆ 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
| 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
| 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
A codec: a separate chip converting between the link and the analog side.
◆ pbl_audio_amp
An amplifier between the output and the speaker.
◆ 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
A digital audio interface of the SoC, streaming one direction.
◆ 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. |
◆ PBL_AUDIO_AMP_INIT
| #define PBL_AUDIO_AMP_INIT |
( |
|
sym, |
|
|
|
_name, |
|
|
|
_init, |
|
|
|
_parent, |
|
|
|
_ops |
|
) |
| |
Value: { \
.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
-
| sym | Symbol of the amplifier. |
| _name | Name. |
| _init | Init function, or NULL. |
| _parent | Parent device, or NULL. |
| _ops | Driver 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; \
.fmt = (_fmt) | (_clocks), \
}; \
.dai = (_dai), \
.
state = &sym##_audio_state, \
}; \
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
What a device is wired to.
Definition audio.h:320
#define PBL_DEVICE_STATE_DEFINE(sym)
Define the runtime state of device instance sym.
Definition device.h:132
Define a card.
- Parameters
-
◆ PBL_AUDIO_CODEC_INIT
| #define PBL_AUDIO_CODEC_INIT |
( |
|
sym, |
|
|
|
_name, |
|
|
|
_init, |
|
|
|
_parent, |
|
|
|
_ops |
|
) |
| |
Value:
Initializer of a struct pbl_audio_codec.
- Parameters
-
| sym | Symbol of the codec. |
| _name | Name. |
| _init | Init function, or NULL. |
| _parent | Parent device, or NULL. |
| _ops | Driver 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
-
◆ 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
-
| sym | Symbol 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) |
◆ PBL_AUDIO_MAX_PERIODS
| #define PBL_AUDIO_MAX_PERIODS 32U |
◆ PBL_AUDIO_RATE_11025
| #define PBL_AUDIO_RATE_11025 PBL_BIT(1) |
◆ PBL_AUDIO_RATE_12000
| #define PBL_AUDIO_RATE_12000 PBL_BIT(2) |
◆ PBL_AUDIO_RATE_16000
| #define PBL_AUDIO_RATE_16000 PBL_BIT(3) |
◆ PBL_AUDIO_RATE_22050
| #define PBL_AUDIO_RATE_22050 PBL_BIT(4) |
◆ PBL_AUDIO_RATE_24000
| #define PBL_AUDIO_RATE_24000 PBL_BIT(5) |
◆ PBL_AUDIO_RATE_32000
| #define PBL_AUDIO_RATE_32000 PBL_BIT(6) |
◆ PBL_AUDIO_RATE_44100
| #define PBL_AUDIO_RATE_44100 PBL_BIT(7) |
◆ PBL_AUDIO_RATE_48000
| #define PBL_AUDIO_RATE_48000 PBL_BIT(8) |
◆ PBL_AUDIO_RATE_8000
| #define PBL_AUDIO_RATE_8000 PBL_BIT(0) |
◆ 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
-
| audio | Device. |
| period | Period the client now owns. |
| pos | Stream position. |
| arg | Callback argument. |
◆ pbl_audio_dir
Stream direction.
| Enumerator |
|---|
| PBL_AUDIO_PLAYBACK | From memory to the hardware.
|
| PBL_AUDIO_CAPTURE | From the hardware to memory.
|
◆ 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
-
- Returns
- Zeroed ring, or NULL if out of memory.
◆ pbl_audio_buf_free()
| void pbl_audio_buf_free |
( |
void * |
buf | ) |
|
◆ pbl_audio_dai_card()
◆ pbl_audio_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_mdb | Gain in mdB. |
| min_mdb | Gain of step 0, in mdB. |
| step_mdb | Gain per step, in mdB. |
- Returns
- Step, rounded down.
◆ pbl_audio_get_caps()
Get what a device can do, including its codec.
- Parameters
-
| audio | Device. |
| [out] | caps | Capabilities. |
- Returns
- 0 or a negative errno.
◆ pbl_audio_get_position()
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
-
| audio | Device. |
| [out] | pos | Position; 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
-
- 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
-
| audio | Device. |
| idx | Period 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
-
◆ 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
-
| audio | Device. |
| period | Period 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
-
- 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
-
◆ pbl_audio_ring_bytes()
Get the bytes of a ring.
- Parameters
-
- 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
-
| audio | Device. |
| gain_mdb | Gain 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
-
| audio | Device. |
| mute | True to mute. |
- Returns
- 0 or a negative errno.
◆ pbl_audio_start()
Start streaming.
- Parameters
-
| audio | Device. |
| stream | Stream; 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
-
- Returns
- 0 or a negative errno.
◆ MIC
The board's microphone, a capture device.
◆ SPEAKER
The board's speaker, a playback device.