RPi3 Audio Output

I’ve previously used a Sonos via the MiOS binding to handle announcements from OH, but as I’ve recently added an amp it means that the Sonos is now not connected to it’s own speakers anymore. This means no more announcements, so I’m trying to get the built in Audio capabilities to work.

Does anyone have a simple guide on getting the audio output side working on a RPi3? I’ve got audio playing via the audio jack from the command line via mpg321

sudo apt-get -y install mpg321
mpg321 /etc/openhab2/sounds/doorbell.mp3

As far as I can tell, the config in OH2 (PaperUI) is all correct, I’ve specified the default sink to be “System Speaker (with mp3 support)” and the Voice to use Mary-TTS in English, but nothing I do results in any successful ‘play’ or ‘say’ output from within either rules or the karaf console.

I’m just getting errors about not being able to do stuff!

2017-01-01 14:54:48.691 [WARN ] [me.io.javasound.internal.AudioPlayer] - No line found: No line matching interface SourceDataLine supporting format PCM_SIGNED 48000.0 Hz, 16 bit, mono, 2 bytes/frame, 24000.0 frames/second, little-endian is supported.
2017-01-01 14:54:48.692 [INFO ] [me.io.javasound.internal.AudioPlayer] - Available lines are:
2017-01-01 14:54:48.727 [ERROR] [.internal.EnhancedJavaSoundAudioSink] - An exception occurred while playing audio : 'Cannot create AudioDevice'

I’ve also tried setting the masterVolume within my test script, but that just gives me a different set of errors in the log

2017-01-01 14:42:12.040 [ERROR] [se.smarthome.core.audio.AudioManager] - An exception occured while getting the volume of sink enhancedjavasound : 'Cannot determine master volume level'
2017-01-01 14:42:12.044 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Test system': An error occured during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

Any guides/pointers would be appreciated!

1 Like

Hi,

I’d suggest there are two issues to resolve to get playback of audio files through the RPi 3.5mm audio jack. It looks like you’re half way there, but for completeness and to help others, here’s information on both…

Configure the RPi and test via the command line
Configure the RPi to use the local audio system, and 3.5mm jack (and not the HDMI output):
Use the command-line configuration tool to update (good to do), then configure audio:

    $ sudo raspi-config
    7 Advanced Options, A0 Update - update the tool
    7 Advanced Options, A4 Audio, 1 Force 3.5mm
    Reboot as requested.

Test the audio output. The default Raspbian image comes with lots of sample audio files ideal for this, and several command-line tools to play them.

    $ /usr/bin/aplay /usr/share/scratch/Media/Sounds/Vocals/Singer2.wav

Note - the command line gives the FULL path to the aplay binary, not just ‘aplay’, and the file is a WAV audio recording rather than a MP3. You’ve clearly installed MPG123 to play MP3 files:

    $ sudo apt install mpg123
    $ which mpg123
    /usr/bin/mpg123
    $ /usr/bin/mpg123 /usr/share/scratch/Media/Sounds/Animal/Cat.mp3

Write a rule to play the audio file.
Now, OpenHAB includes the function playSound() which is documented as supporting WAV or MP3 files located in the OpenHAB runtime/sounds directory (e.g. I created the directory manually and copied in a test file)
So, this should work in a rule:

    PlaySound("Singer2.wav")

For me, this has only ever thrown multimedia.actions.Audio errors, so I call the command line audio player including the full file name with the path to it:

executeCommandLine("/usr/bin/aplay /usr/share/scratch/Media/Sounds/Vocals/Singer2.wav")

Taking your example and your paths, my guess is may have a screwed-up ALSA config (hope fixed by using raspi-config) and simply need to add the full path to the mpg123 binary to get it to work (as the OpenHAB shell doesn’t have PATH set in the same way as your bash shell):

executeCommandLine("/usr/bin/mpg123 /etc/openhab2/sounds/doorbell.mp3")

Speech Synth
Sadly, I have never managed to get the in-built Say() command to work, so have a hack bash script which invokes flite directly -ugly, but just about works!

    var String Message
    Message = "This is a test!"
    executeCommandLine("/opt/openhab/bin/fest_speak.sh " + Message)

And the local script fest_speak.sh (needs flite and alsa installed via ‘sudo apt-get install flite alsa-utils’) :

#!/bin/bash 
# Run speech synth on a local device
# Set playback volume to 100% (amixer controls)
/usr/bin/amixer cset numid=1 100%
/usr/bin/flite -voice awb -t "$*"

Once again, after installing flite, you should be able to test speech on the command line by entering the same commands as in the above script:

$ sudo apt-get -y install flite alsa-utils
$ /usr/bin/amixer cset numid=1 100%
$ /usr/bin/flite -voice awb -t "This is a test!"

Ideally, it would be great to fix PlaySound() and Say() to work as advertised on the RPi, and remove my somewhat hacky workarounds - any help would be greatly appreciated!

Good Luck!

3 Likes

Thanks for that, I hadn’t considered going around the problem by using the exec capability, I guess I was hoping that the built in playSound() and say() functions would work with a little bit more work.

Other threads suggest that the “Cannot create AudioDevice” issue is in the user permissions, but I haven’t had the time to try it yet.

sudo adduser openhab audio

I know other people seem to have these functions working (e.g. setMasterVolume in OH2 / rpi3), so I’ll see what the permissions thing yields.

So initially running the permissions change made no difference, even after a restart of OH2.

BUT, rebooting the box this morning then allowed the playSound() & say() functions now work as expected, both via karaf and in rules.

I was about to start a new thread, but then I saw this one…

I’m trying similarly to output audio from my Pi3 box, but I’m trying to use a USB sound card. I had this working before I encountered some unrelated issues and started over with a fresh install.

Incidentally, I have Shairport Sync running successfully on the same RPI3, but that software has its own output configuration allowing me to set the output to the USB card; I just have not been successful making this work on a system level, and by extension work for OpenHab.

I’ve found about 20 different examples and tutorials for setting the default audio output but they are all different and many reference config files I don’t have installed. I’m looking for instructions that specifically refer to an updated Raspberry Jessie, installed as openHabian. I’m running openHabian 1.0 with openHAB 2.0.0~20161224034628 (Build #672) on a Raspberry Pi 3 rev 1.2.

I’d very much appreciate some help or to be pointed to an up to date tutorial.