Configuring Audio Output in Buildroot - cu-ecen-aeld/buildroot-assignments-base GitHub Wiki

This guide outlines the complete pipeline for configuring a Raspberry Pi to handle audio processing using either USB or I2S hardware interfaces. Whether one is building a complex audio processing engine or a simple playback device, these steps will help ensure proper hardware and software linkage for your chosen audio output.

1. USB Audio Configuration

Note: This section was developed based on this project's implementation - Embedded Linux Audio Playback Processor. We hope it serves as a valuable foundation for your own embedded audio processing projects using Buildroot and ALSA!

References

  • Project overview page - link
  • Project home page - link

The Audio Library Stack

To build a functional audio processor, we need three layers of software. We configure Buildroot to automatically compile these into our OS:

  • libsndfile (The Parser): Audio files (like .wav) contain headers and interleaved channel data. libsndfile handles the file I/O, allowing our application to read raw audio frames into memory.
  • SoundTouch (The DSP Engine): This is an open-source C++ library for processing audio. It takes the raw frames provided by libsndfile and applies real-time Digital Signal Processing (like altering tempo or pitch) without artifacts.
  • ALSA (The Hardware Interface): The Advanced Linux Sound Architecture. This is the Linux kernel framework that talks to a hardware interface, like a USB audio dongle or speaker. Our application writes the final, processed frames directly to the ALSA PCM(Pulse Code Modulation) driver.

Configuring Buildroot for Audio

To include these libraries in your custom OS, navigate to your Buildroot directory and run:

make menuconfig

Enable the following target packages:

  • ALSA Utilities (Necessary for hardware testing): Target packages ---> Audio and video applications ---> [*] alsa-utils (Ensure aplay/arecord is selected inside its sub-menu)
  • libsndfile: Target packages ---> Libraries ---> Audio/Sound ---> [*] libsndfile
  • SoundTouch: Target packages ---> Libraries ---> Audio/Sound ---> [*] soundtouch

Note: For remote development, it is recommended to also enable dropbear (under Networking applications) for an SSH server, and configure eth0 for DHCP in the System Configuration menu.

Kernel Configuration for USB Audio

This step is done to ensure that the device drivers are enabled in the Linux Kernel. Before building your image, you must enable USB audio at the kernel level:

make linux-menuconfig

Navigate to the following path and press the Spacebar until it shows an asterisk [*]: Device Drivers ---> Sound card support ---> Advanced Linux Sound Architecture ---> USB sound devices ---> [*] USB Audio/MIDI driver

Save, exit, and compile your system using make.

Hardware Verification & ALSA Commands

Once you boot your Raspberry Pi, you need to verify the ALSA hardware pipeline.

Step 1: Verify the Hardware is Detected

SSH into your Raspberry Pi and run:

aplay -l

This lists all playback hardware devices. You should see your USB sound card listed here (e.g., card 3: Device [USB Audio Device]).

Step 2: Test the ALSA Driver with a Sine Wave

Before running the custom C++ application (or any audio based code application that you will be writing), verify that ALSA can successfully drive the speakers. Please use the speaker-test utility:

speaker-test -D plughw:3,0 -c 1 -t sine -f 440

(Note: Replace 3,0 with the card and device number output by aplay -l). If the hardware and kernel are configured correctly, this command will bypass your custom code and play a continuous 440 Hz 'A' note directly through the connected speakers.

Connecting the Pipeline in Code

With the OS correctly configured, your application can now integrate these tools:

  1. Use sf_readf_short() from libsndfile to pull data from a file. (See example implementation here).
  2. Pass that data into the SoundTouch object to apply tempo or pitch shifts. (See example implementation here).
  3. Push the processed data directly to the speaker using snd_pcm_writei() from the ALSA library.

2. I2S Sound Buildroot Configuration

While Section 1 covers USB-based audio interfaces, this section deals with configuring Buildroot for I2S (Inter-IC Sound) audio playback. This involves enabling specific device tree overlays and kernel modules to communicate directly with hardware DACs via the board's GPIO pins.

Configuring and Building

If using a Raspberry Pi 3B+ please modify your config.txt to ensure these changes are included. Other hardware platforms are likely to use similar settings. dtparam=i2s=on dtoverlay=hifiberry-dac dtoverlay=i2s-mmap

To customize config.txt, see instructions in the Raspberry Pi Hardware Hints Page

  1. In the buildroot folder, run "make menuconfig" and enable all the ALSA (Advanced Linux Sound Architecture) drivers required for audio. "make menuconfig" --> "Target Packages" --> "Audio and video applications" --> "alsa_utils"

image

Recommendation is to enable all the supported tools inside alsa-utils. But if required, you can only enable required tools.

  1. Run save-config.sh and ensure the below packages are added in defconfig file as shown below:

    BR2_PACKAGE_ALSA_UTILS=y BR2_PACKAGE_ALSA_UTILS_ALSACONF=y BR2_PACKAGE_ALSA_UTILS_ACONNECT=y BR2_PACKAGE_ALSA_UTILS_ALSALOOP=y BR2_PACKAGE_ALSA_UTILS_ALSAUCM=y BR2_PACKAGE_ALSA_UTILS_ALSATPLG=y BR2_PACKAGE_ALSA_UTILS_AMIDI=y BR2_PACKAGE_ALSA_UTILS_AMIXER=y BR2_PACKAGE_ALSA_UTILS_APLAY=y BR2_PACKAGE_ALSA_UTILS_APLAYMIDI=y BR2_PACKAGE_ALSA_UTILS_ARECORDMIDI=y BR2_PACKAGE_ALSA_UTILS_ASEQDUMP=y BR2_PACKAGE_ALSA_UTILS_ASEQNET=y BR2_PACKAGE_ALSA_UTILS_BAT=y BR2_PACKAGE_ALSA_UTILS_IECSET=y BR2_PACKAGE_ALSA_UTILS_SPEAKER_TEST=y

  2. Below modules are required to run audio on buildroot image:

     snd_soc_pcm5102a
     snd_soc_rpi_simple_soundcard
     snd_soc_bcm2835_i2s
    

To add them, modprobe manually once the buildroot image is flashed or automate it by adding these changes as part of a start stop script as shown below:

image

and run this startup script using the rootfs overlay strategy discussed and implemented in Assignment 7 (or by including in a package).

  1. Optional: Add a package with sound and i2s related files and configuration. This will vary by project. The package at https://github.com/cu-ecen-aeld/final-project-sachininja/tree/i2s-buildroot/base_external/package/i2s_package installs files related to the project described at https://github.com/sachininja/final-project-SachinMathad/wiki/Project-Overview

  2. Clean build the buildroot image and flash it on rpi

Validation

  1. Verify that the audio soundcard is loaded in rpi by running aplay -L command. It should output as shown below:

image

This ensures that the audio related packages and overlays are present and working.

  1. Follow the i2s pin configuration to connect the i2s pins to HW codec as shown here. or connect the speaker to audio jack and run below command:

    aplay <audio_file.wav>

  2. Enjoy the music on your devboard! :)

References: