Main Content

Play High-Quality Audio from Raspberry Pi Using I2S-Based DAC

Inter-IC声音protoc总线(i2)是一种沟通ol 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

  1. Mount a DAC HAT on your Raspberry Pi.

  2. Connect a pair of speakers or headphones to the analog audio output of the DAC HAT.

  3. Configure your Raspberry Pi hardware as described inHardware Setup.

    Note

    Ensure that you install the Raspbian image provided by MathWorks®.

Enable I2S Drivers onRaspberry Pi

  1. In the MATLAB®Command Window, type thelistAudioDevicescommand. If this command lists your audio device, skip the steps in this section and directly perform the steps inPlay Audio in Simulink.

  2. Open an SSH command-line session as described inOpen Command-Line Session with Raspberry Pi Hardware.

  3. In the terminal, typesudo nano /boot/config.txtto edit theconfig.txtfile.

    1. To enable the I2S interface in the Raspberry Pi device tree, uncomment the linedtparam=i2s=onin the hardware interface section of the file.

    2. To configure the HiFiBerry DAC, adddtoverlay=hifiberry-dacplusin the lirc-rpi module section of the file.

    3. To disable the onboard ALSA audio devices for Raspberry Pi, comment out the linedtparam=audio=onin the enable audio section.

  4. Disconnect Raspberry Pi from your computer and connect it again.

    Warning

    Ensure that the DAC HAT is still mounted on Raspberry Pi.

Play Audio in Simulink

  1. Configure your Simulink®model to run on Raspberry Pi as described inModel Configuration Simulink Support Package for Raspberry Pi Hardware.

  2. Add anALSA Audio Playbackblock to the model.

  3. Open theALSA Audio Playbackblock in your model. Specify the device number of the DAC HAT in theDevice nameparameter.

    If you do not know the device number of DAC, use thelistAudioDevicesfunction as described inList Available ALSA Audio Output Devices. The resulting structured array displays the device number of your DAC.

  4. On theHardwaretab of the toolstrip, in theModesection, selectRun on board, and then selectBuild, Deploy & Start. This action builds, downloads, and runs the model on the Raspberry Pi hardware. The audio starts playing.

See Also