Speaker (audio output) driver interface.
More...
|
| typedef const struct AudioDevice | AudioDevice |
| | Audio output device, defined by the driver implementation.
|
| |
| typedef void(* | AudioTransCB) (uint32_t *free_size) |
| | Request for more audio data.
|
| |
| typedef void(* | AudioPlaybackCB) (const int16_t *samples, size_t count, uint32_t sample_time, void *context) |
| | Observer of the mono PCM committed to DMA, including underrun silence.
|
| |
Speaker (audio output) driver interface.
The driver plays 16-bit mono PCM queued with audio_write(). Once started, it asks for more data through the AudioTransCB passed to audio_start() whenever queue space frees up. The per-implementation AudioDevice definitions live in the speaker driver groups.
static void prv_refill(uint32_t *free_size) {
size_t len =
MIN(*free_size,
sizeof(s_pcm));
generate_pcm(s_pcm, len);
}
...
audio_stop(AUDIO);
uint32_t audio_write(AudioDevice *audio_device, void *writeBuf, uint32_t size)
Queue PCM data for playback.
void audio_set_volume(AudioDevice *audio_device, int volume)
Set the playback volume.
void audio_start(AudioDevice *audio_device, AudioTransCB cb)
Start playback.
void audio_init(AudioDevice *audio_device)
Initialize the audio device.
#define MIN(a, b)
Get the smaller of two values.
Definition math.h:36
◆ AUDIO_PLAYBACK_SAMPLE_RATE
| #define AUDIO_PLAYBACK_SAMPLE_RATE (16000) |
PCM sample rate, in Hz, and rate of the clock seen by AudioPlaybackCB.
◆ AudioDevice
Audio output device, defined by the driver implementation.
◆ AudioPlaybackCB
| typedef void(* AudioPlaybackCB) (const int16_t *samples, size_t count, uint32_t sample_time, void *context) |
Observer of the mono PCM committed to DMA, including underrun silence.
Runs in the DMA ISR: copy the samples immediately, never block or keep the pointer.
- Parameters
-
| samples | Samples committed to DMA. |
| count | Number of samples. |
| sample_time | Presentation time of the first sample, on a wrapping AUDIO_PLAYBACK_SAMPLE_RATE clock derived from uptime and re-anchored whenever it drifts more than 8 ms from it. |
| context | User context given to audio_set_playback_callback(). |
◆ AudioTransCB
| typedef void(* AudioTransCB) (uint32_t *free_size) |
Request for more audio data.
Invoked on the system task; the consumer may call audio_write() synchronously from it.
- Parameters
-
| [in,out] | free_size | Free queue space in bytes. |
◆ audio_init()
Initialize the audio device.
- Parameters
-
| audio_device | Audio device. |
◆ audio_set_playback_callback()
Register or unregister the playback observer.
Provided by drivers that select SPEAKER_PLAYBACK_OBSERVER.
- Parameters
-
| audio_device | Audio device. |
| cb | Observer, or NULL to unregister synchronously. |
| context | User context passed to cb. |
- Returns
- False if the device format (sample rate, channels) cannot be observed.
◆ audio_set_volume()
| void audio_set_volume |
( |
AudioDevice * |
audio_device, |
|
|
int |
volume |
|
) |
| |
|
extern |
Set the playback volume.
- Parameters
-
| audio_device | Audio device. |
| volume | Volume from 0 to 100. |
◆ audio_start()
Start playback.
- Parameters
-
| audio_device | Audio device. |
| cb | Called whenever the device wants more data. |
◆ audio_stop()
Stop playback.
- Parameters
-
| audio_device | Audio device. |
◆ audio_write()
| uint32_t audio_write |
( |
AudioDevice * |
audio_device, |
|
|
void * |
writeBuf, |
|
|
uint32_t |
size |
|
) |
| |
|
extern |
Queue PCM data for playback.
Data that does not fit in the queue is dropped.
- Parameters
-
| audio_device | Audio device. |
| writeBuf | 16-bit PCM samples. |
| size | Size of writeBuf in bytes; 0 only queries the free space. |
- Returns
- Free queue space in bytes after the write.