App Development - Sound

Apps play and record audio with the audio stream (<tactility/drivers/audio_stream.h>), a device of type AUDIO_STREAM_TYPE that every firmware with audio has. It uses one audio device (AUDIO_CODEC_TYPE) for input and one for output, and converts the sample rate of your stream to the one of the device.

The sound module (<sound/sound.h>) picks those devices and keeps the audio settings: see Sound for what the user sets.

Naming

  • Audio is the kernel level: the device types and drivers that move samples, like AUDIO_CODEC_TYPE, AUDIO_STREAM_TYPE, audio_codec_*(), audio_stream_*() and Drivers/audio-stream-module.
  • Sound is what is built on top of it: the sound module (sound_*()), the tactility.sound service and the Sound settings app. They pick the devices, keep the settings and handle the media keys.

Using the sound module

The sound module is part of the SDK, but apps don't use it by default. Add it to main/CMakeLists.txt:

tactility_component_register(SRCS ${SOURCE_FILES} REQUIRES sound-module)

Devices

#include <sound/sound.h>

static bool print_device(struct Device* device, void* context) {
    printf("%s\n", device->name);
    return true; // continue
}

// The connected devices that can play audio
sound_for_each_device(AUDIO_CODEC_DIR_OUTPUT, NULL, print_device);

// The device that the audio stream uses now
struct Device* active = sound_get_active_device(AUDIO_CODEC_DIR_OUTPUT);
  • sound_get_default_device() and sound_set_default_device() read and set the device that is used for a direction while it's connected, by name. An empty name picks a device automatically. Setting it saves it, and the audio stream switches to it right away. An open stream of that direction is closed then.
  • sound_get_switch_to_new_devices() and sound_set_switch_to_new_devices() read and set whether a device that is connected later becomes the default device.
  • sound_get_device_settings() returns the saved volume and mute of a device. The audio service saves them when they change, so apps that change the volume use audio_stream_set_volume().

The first call reads the settings file. Call the functions from your app's own task, not from the LVGL thread.

Volume and media keys

  • sound_get_output_volume(), sound_set_output_volume(), sound_is_output_muted() and sound_set_output_muted() read and change the volume and mute of the output. A change is saved for the active device.
  • A media key or headset button requests a play/pause toggle. A media app checks for it with sound_consume_play_pause_request() on its own task, and calls sound_clear_play_pause_request() when it opens, so that a press from before doesn't toggle it.

The tactility_audio_*() functions of <tactility/audio_service.h> are deprecated. The header is now part of the sound module, so code that includes it keeps compiling with REQUIRES sound-module, with a deprecation warning, and apps that were built with them keep working.

Audio stream functions

audio_stream_get_codec() returns the device that is used for a direction, and audio_stream_set_preferred_codec() sets the device that is used while it's started. The sound module calls the latter: apps use sound_set_default_device(), which also saves the choice.