This is the documentation for the latest (main) development branch of Zephyr. If you are looking for the documentation of previous releases, use the drop-down menu on the left and select the desired version.

Audio Codec

Overview

The Audio Codec API provides access to digital audio codecs.

Configuration Options

Related configuration options:

API Reference

group audio_codec_interface

Abstraction for audio codecs.

Since

1.13

Version

0.1.0

Typedefs

typedef void (*audio_codec_error_callback_t)(const struct device *dev, uint32_t errors)

Callback for error interrupt.

Param dev:

Pointer to the codec device

Param errors:

Device errors (bitmask of audio_codec_error_type values)

Enums

enum audio_pcm_rate_t

PCM audio sample rates.

Values:

enumerator AUDIO_PCM_RATE_8K = 8000

8 kHz sample rate

enumerator AUDIO_PCM_RATE_16K = 16000

16 kHz sample rate

enumerator AUDIO_PCM_RATE_24K = 24000

24 kHz sample rate

enumerator AUDIO_PCM_RATE_32K = 32000

32 kHz sample rate

enumerator AUDIO_PCM_RATE_44P1K = 44100

44.1 kHz sample rate

enumerator AUDIO_PCM_RATE_48K = 48000

48 kHz sample rate

enumerator AUDIO_PCM_RATE_96K = 96000

96 kHz sample rate

enumerator AUDIO_PCM_RATE_192K = 192000

192 kHz sample rate

enum audio_pcm_width_t

PCM audio sample bit widths.

Values:

enumerator AUDIO_PCM_WIDTH_16_BITS = 16

16-bit sample width

enumerator AUDIO_PCM_WIDTH_20_BITS = 20

20-bit sample width

enumerator AUDIO_PCM_WIDTH_24_BITS = 24

24-bit sample width

enumerator AUDIO_PCM_WIDTH_32_BITS = 32

32-bit sample width

enum audio_dai_type_t

Digital Audio Interface (DAI) type.

Values:

enumerator AUDIO_DAI_TYPE_I2S

I2S Interface.

enumerator AUDIO_DAI_TYPE_INVALID

Other interfaces can be added here.

enum audio_property_t

Codec properties that can be set by audio_codec_set_property().

Values:

enumerator AUDIO_PROPERTY_OUTPUT_VOLUME

Output volume.

enumerator AUDIO_PROPERTY_OUTPUT_MUTE

Output mute/unmute.

enum audio_channel_t

Audio channel identifiers to use in audio_codec_set_property().

Values:

enumerator AUDIO_CHANNEL_FRONT_LEFT

Front left channel.

enumerator AUDIO_CHANNEL_FRONT_RIGHT

Front right channel.

enumerator AUDIO_CHANNEL_LFE

Low frequency effect channel.

enumerator AUDIO_CHANNEL_FRONT_CENTER

Front center channel.

enumerator AUDIO_CHANNEL_REAR_LEFT

Rear left channel.

enumerator AUDIO_CHANNEL_REAR_RIGHT

Rear right channel.

enumerator AUDIO_CHANNEL_REAR_CENTER

Rear center channel.

enumerator AUDIO_CHANNEL_SIDE_LEFT

Side left channel.

enumerator AUDIO_CHANNEL_SIDE_RIGHT

Side right channel.

enumerator AUDIO_CHANNEL_ALL

All channels.

enum audio_codec_error_type

Codec error type.

Values:

enumerator AUDIO_CODEC_ERROR_OVERCURRENT = BIT(0)

Output over-current.

enumerator AUDIO_CODEC_ERROR_OVERTEMPERATURE = BIT(1)

Codec over-temperature.

enumerator AUDIO_CODEC_ERROR_UNDERVOLTAGE = BIT(2)

Power low voltage.

enumerator AUDIO_CODEC_ERROR_OVERVOLTAGE = BIT(3)

Power high voltage.

enumerator AUDIO_CODEC_ERROR_DC = BIT(4)

Output direct-current.

Functions

static inline int audio_codec_configure(const struct device *dev, struct audio_codec_cfg *cfg)

Configure the audio codec.

Configure the audio codec device according to the configuration parameters provided as input

Parameters:
  • dev – Pointer to the device structure for codec driver instance.

  • cfg – Pointer to the structure containing the codec configuration.

Returns:

0 on success, negative error code on failure

static inline void audio_codec_start_output(const struct device *dev)

Set codec to start output audio playback.

Setup the audio codec device to start the audio playback

Parameters:
  • dev – Pointer to the device structure for codec driver instance.

static inline void audio_codec_stop_output(const struct device *dev)

Set codec to stop output audio playback.

Setup the audio codec device to stop the audio playback

Parameters:
  • dev – Pointer to the device structure for codec driver instance.

static inline int audio_codec_set_property(const struct device *dev, audio_property_t property, audio_channel_t channel, audio_property_value_t val)

Set a codec property defined by audio_property_t.

Set a property such as volume level, clock configuration etc.

Parameters:
  • dev – Pointer to the device structure for codec driver instance.

  • property – The codec property to set

  • channel – The audio channel for which the property has to be set

  • val – pointer to a property value of type audio_codec_property_value_t

Returns:

0 on success, negative error code on failure

static inline int audio_codec_apply_properties(const struct device *dev)

Atomically apply any cached properties.

Following one or more invocations of audio_codec_set_property, that may have been cached by the driver, audio_codec_apply_properties can be invoked to apply all the properties as atomic as possible

Parameters:
  • dev – Pointer to the device structure for codec driver instance.

Returns:

0 on success, negative error code on failure

static inline int audio_codec_clear_errors(const struct device *dev)

Clear any codec errors.

Clear all codec errors. If an error interrupt exists, it will be de-asserted.

Parameters:
  • dev – Pointer to the device structure for codec driver instance.

Returns:

0 on success, negative error code on failure

static inline int audio_codec_register_error_callback(const struct device *dev, audio_codec_error_callback_t cb)

Register a callback function for codec error.

The callback will be called from a thread, so I2C or SPI operations are safe. However, the thread’s stack is limited and defined by the driver. It is currently up to the caller to ensure that the callback does not overflow the stack.

Parameters:
  • dev – Pointer to the audio codec device

  • cb – The function that should be called when an error is detected fires

Returns:

0 if successful, negative errno code if failure.

union audio_dai_cfg_t
#include <codec.h>

Digital Audio Interface Configuration.

Configuration is dependent on DAI type

Public Members

struct i2s_config i2s

I2S configuration.

struct audio_codec_cfg
#include <codec.h>

Codec configuration parameters.

Public Members

uint32_t mclk_freq

MCLK input frequency in Hz.

audio_dai_type_t dai_type

Digital interface type.

audio_dai_cfg_t dai_cfg

DAI configuration info.

union audio_property_value_t
#include <codec.h>

Codec property values.

Public Members

int vol

Volume level in 0.5dB resolution.

bool mute

Mute if true, unmute if false.