Setting A Button On Your Remote To Toggle The S/PDIF Audio Source

From MythTV Official Wiki
Jump to: navigation, search

If you are using digital audio with MythTV, you may have a need to toggle between PCM and analog audio output to your S/PDIF port. This switch can be accomplished manually using “alsamixer”, “amixer” or your favorite graphical mixer but that's not very convenient. If you are using a remote control via the lirc package then these directions will help you set a button on the remote to toggle this setting.

We'll assume that your MythTV frontend userid is “mythtv” which would make your home directory “/home/mythtv”. If you have used a different userid, then substitute its home directory in all of what follows.

Find the SPDIF/IEC958 command

First, find out what the command is to change the output from analog (RCA) to SPDIF (IEC958). Plug in the SPDIF cable into your amplifier and test it with your DVD player or other device. Then remove the protective black plug from the SPDIF output on your computer. Then try a GUI front end like the Gnome Volume control to switch the IEC958 output on and off. You should be able to see the laser light (red) turn on and off. DO NOT LOOK INTO THE LASER. You can put your finger in front of the socket and you'll see the light go on and off as you switch the output on and off.

Turn the IEC958 output off using the GUI. Then do:

amixer >/tmp/off.txt

Turn the output on using the GUI, then:

amixer >/tmp/on.txt

Then find what changed:

diff -U 10 /tmp/on.txt /tmp/off.txt

You should be able to see what changed. Here is example output:

--- /tmp/on.txt  2006-12-28 11:21:20.000000000 +0900
+++ /tmp/off.txt  2006-12-28 11:21:28.000000000 +0900
@@ -81,11 +81,11 @@
   Playback channels: Mono
   Mono: Playback [off]
 Simple mixer control 'IEC958 Output',0
   Capabilities: pswitch pswitch-joined
   Playback channels: Mono
-  Mono: Playback [on]
+  Mono: Playback [off]
 Simple mixer control 'PC Speaker',0
   Capabilities: pvolume pvolume-joined
   Playback channels: Mono
   Limits: Playback 0 - 3
   Mono: Playback 2 [67%]

You can see that the switch for 'IEC958 Output',0 'Playback' changed from 'on' to 'off'. Ignore the "Mono" Playback channels in the "IEC958 Output" stanza, this does not actually switch it to mono!

So the command to turn the IEC958 output on would be

/usr/bin/amixer set 'IEC958 Output',0 'Playback' 'on'

Note that this command is different to the command used in the toggleAudio script below, because 2 different people are contributing to this article!

But you could substitute your own amixer command above and change the "AUDIO=" line to match what you have to do to find what state the current IEC958 output is set to. In this case it would be

#   Get current setting of PCM/analog switch
AUDIO=`amixer get 'IEC958 Output',0 'Playback' | grep Mono: | cut -d[ -f 2`

if [ "${AUDIO}" == "on]" ]; then

Create the script

Create a script in your home directory named /home/mythtv/bin/toggleAudio:

Script.png /home/mythtv/bin/toggleAudio

#!/bin/sh

#  Get current setting of PCM/analog switch
AUDIO=`amixer get 'IEC958 Playback Source' | grep 'Item0:' | cut -d\' -f 2`

if [ "${AUDIO}" == "PCM" ]; then
  #  Route analog signal to S/PDIF
  amixer set 'IEC958 Playback Source',0 'Analog'
  # Could also be: sudo /sbin/alsactl -f /home/mythtv/asound.analog2spdif restore 0
else
  #  Route PCM signal to S/PDIF
  amixer set 'IEC958 Playback Source',0 'PCM'
  #  Could also be: sudo /sbin/alsactl -f /home/mythtv/asound.pcm2spdif restore 0
fi

This script uses “amixer” commands to switch the IEC958 source. The exact options used on this command depend on what audio device you computer contains and the driver that it uses. Entering the “amixer” without any options will list all of the controls that are exposed for your audio device. You should be able to use the output of this command to figure out how to modify the “amixer” commands in this script to work with your sound card.

The “Could also be:” lines above suggest an alternate approach. This would be to manually configure your card for each type of output and then use “alsactl” to save the entire mixer configuration into a file for each type. In the script you call “alsactl” to restore the approariate mixer configuration for each type. With this approach we're restoring the entire set of mixer settings rather than just changing one setting.

The following procedure could be used to create the alsactl files used in the script.

  1. Manually configure the mixer settings so that analog output, i.e. the output from the TV, goes to the S/PDIF port
  2. Execute “sudo /sbin/alsactl -f /home/mythtv/asound.analog2spdif store 0”
  3. Manually configure the mixer settings so that PCM output, i.e. the output from DVDs or MythMusic, goes to the S/PDIF port
  4. Execute “sudo /sbin/alsactl -f /home/mythtv/asound.pcm2spdif store 0”

Make sure irexec is running

The irexec progarm is a lirc client that can execute system commands when buttons are pressed on the remote. It needs to be started either at the system level during boot or by your MythTV user. If you start it at the system level during boot then you need to specify a configuration file for it using its “-f” If you start it as a user it uses your ~/.lircrc file as its configuration file.

The command “ps -e | grep irexec” will show you if irexec is running or not. If it isn't, the following directions will help you set things up so that it starts when your MythTV user logs in.

Assuming that you are using KDE desktop, you can start irexec when your MythTV userid logs in by putting the following command into a script file in your ~/.kde/Autostart folder. If you used Jarod Wilson's Howto as a guide to installing MythTV then you already have a script there called myth-load.sh and you could just add this command to that script:

irexec &

The “&” at the end of the line causes this command to run in background so that the script can continue executing the rest of the commands that it contains.

Add the definition for the button to the .lircrc file

The names of the buttons on your remote are listed in the /etc/lircd.conf file. We'll assume you have a button called “RED” in that file that refers to the button you want to use to execute the script that toggles the audio setting. Add the following stanza to your /home/mythtv/.lircrc file:

Script.png /home/mythtv/.lircrc

...

# toggle ALSA output between Analog and PCM audio output to the S/PDIF port
begin
prog = irexec
button = red
repeat = 3
config = /home/mythtv/bin/toggleAudio
end

...