Estimated reading time: 2 minutes

This documentation was last modified: Sunday, October 14th, 2018 at 8:27 am

Picroft - outputting audio

If you are developing on Picroft, and want to do audio-related Skills development, you will find this information on outputting audio useful.

How to output audio via HDMI

  • You will need your Picroft set up on your Raspberry Pi 3
  • You will need an HDMI cable to plug into TV or speakers
  1. Connect a keyboard and/or monitor to Picroft, or alternatively, ssh into Picrot. By default the user is pi and the password is mycroft.
  2. Run nano auto_run.sh (or use your preferred text editor)
  3. Change "1" to "2". e.g. sudo amixer cset numid 3 "1" to sudo amixer cset numid 3 "2"
  4. Save the change.
  5. Reboot Picroft

Audio should now play through the device you have the HDMI cable connected to (ie TV or speakers).

How to output audio through USB

  • You will need your Picroft set up on your Raspberry Pi 3
  • You will need an HDMI cable to plug into TV or speakers

Typically, USB audio should be connected to hwplug:1,0 but to verify, run this command:

aplay -L

Next, find the hwplug output for the device you want to use. Take this, and update the mycroft.conf file accordingly. This file is located at ~/mycroft.conf on Picroft.

"play_wav_cmdline": "aplay -Dhw:0,0 %1"

now becomes

"play_wav_cmdline": "aplay -Dplughw:1,0 %1"

You can now run ./auto_run.sh to start the program back up and test and ensure the output comes through the USB speakers.

How to output audio through Bluetooth

  • You will need your Picroft set up on your Raspberry Pi 3
  • You will need a Bluetooth audio device to connect to

First, we need to enable Bluetooth.

  1. Edit the /etc/mycroft/mycroft.conf file
  2. Add "port": "/dev/ttyAMA0" to the enclosure settings. You may need to use "port": "/dev/ttyAMA1" or "port": "/dev/ttyAMA2" - the port can vary depending on the Picroft and Raspbian build.

Next, we set up the Bluetooth connection.

Using bluetoothctl -a, follow these steps:

  • Agent on
  • Scan on
  • Note the Bluetooth speaker address
  • Pair the address
  • Trust the address
  • Connect to the address

To enable auto connect, follow these steps:

In the /usr/local/sbin directory, create a file called startBTAudio.sh.

The file should have the following contents:

 #!/bin/sh
echo "connect BTSPEAKERADDRESS" | bluetoothctl -a
echo "exit" | bluetoothctl -a

In the /etc/systemd/system directory, create two files:

The first file should be called BTSpeaker.service and have the following contents:

[Unit]
Description=Bluetooth Speaker Auto Connect

[Service]
Type=simple
ExecStart=/usr/local/sbin/startBTAudio.sh

The second file should be called BTSpeaker.timer and have the following contents:

[Unit]
Description=Run BTSpeaker Service After 30s 

[Timer]
OnBootSec=30s
Unit=BTSpeaker.Service  

[Install]
WantedBy=multi-user.target

Next, run systemctl enable BTSpeaker.timer

Next, we connect the audio.

  1. First, install pulseaudio-module-bluetooth by running apt-get install pulseaudio-module-bluetooth
  2. Run pacmd list-sinksand note the Bluetooth sink name
  3. Edit /etc/pulse/deault.pa and add set-default-sink SINKNAME and add load-module module-native-protocol-unix auth-anonymous=1 socket=/tmp/pulse-socket
  4. Edit /home/mycroft/.config/pulse/client.conf and add default-server = unix:/tmp/pulse-socket
  5. Edit /etc/mycroft/mycroft.conf and set default_wav_cmdline to "aplay -Dpulse %1" and set default_mp3_cmdline to "mpg123 -o pulse %1"

Reboot the Picroft and test that audio is outputting to the Bluetooth device.


Help us serve you better by rating this documentation