Play High-Quality Audio from Raspberry Pi Using I2S-Based DAC
Inter-IC Sound Bus (I2S) is a communication protocol for transferring digital audio signal. Using an I2S-based digital-to-analog convertor (DAC), you can play high-quality audio from Raspberry Pi®.
Connect DAC HAT and Install Raspbian Image on Raspberry Pi
Mount a DAC HAT on your Raspberry Pi.
Connect a pair of speakers or headphones to the analog audio output of the DAC HAT.
Configure your Raspberry Pi hardware as described in Hardware Setup.
Ensure that you install the Raspbian image provided by MathWorks®.
Enable I2S Drivers on Raspberry Pi
Open an SSH command-line session as described in Open Command-Line Session with Raspberry Pi Hardware.
In the terminal, type
sudo nano /boot/config.txtto edit the
To enable the I2S interface in the Raspberry Pi device tree, uncomment the line
dtparam=i2s=onin the hardware interface section of the file.
To configure the HiFiBerry DAC, add
dtoverlay=hifiberry-dacplusin the lirc-rpi module section of the file.
To disable the onboard ALSA audio devices for Raspberry Pi, comment out the line
dtparam=audio=onin the enable audio section.
Disconnect Raspberry Pi from your computer and connect it again.
Ensure that the DAC HAT is still mounted on Raspberry Pi.
Play Audio in Simulink
Configure your Simulink® model to run on Raspberry Pi as described in Model Configuration Simulink Support Package for Raspberry Pi Hardware.
Add an ALSA Audio Playback block to the model.
Open the ALSA Audio Playback block in your model. Specify the device number of the DAC HAT in the Device name parameter.
If you do not know the device number of DAC, use the
listAudioDevicesfunction as described in List Available ALSA Audio Output Devices. The resulting structured array displays the device number of your DAC.
On the Hardware tab of the toolstrip, in the Mode section, select
Run on board, and then select Build, Deploy & Start. This action builds, downloads, and runs the model on the Raspberry Pi hardware. The audio starts playing.