random findings by wt8008
RSS icon Home icon
  • MythTV with SPDIF Digital Sound Output and Passthrough

    Posted on March 24th, 2011 wt8008 No comments

    Most modern motherboards will include a S/PDIF header, but with a soldering iron, one is easily added. This allows for digital audio output from the onboard sound. The next part shows how to configure and test a setup using ALSA tools and mplayer for Linux. Finally, a custom asoundrc configuration is used for MythTV to allow for analog and digital output for stereo streams and digital passthrough for DTS and Dolby Digital encoded audio.

    S/PDIF Connector

    My MythTV system uses the ECS K8T890-A motherboard, it does not contain a S/PDIF header. Upon closer inspection, near the PCI-e slot, you will find that ECS used a Realtek ALC655 for audio output. The datasheet for the ALC655 is publicly available. From the specifications, the ALC655 supports digital output, but ECS did not include headers for it. With a soldering iron, this is a non-issue. The reference design gives a simple circuit consisting of a few capacitors and resistors to allow the ALC655’s output to be interfaced with a S/PDIF compatible receiver. S/PDIF connectors used are typically RCA or BNC. The decision will be based upon what cables and connectors you have available. Cables with characteristics impedance of 75 Ω should be used for best performance. This value is typically used for video coax. Once you have a RCA or BNC pannel/borad mount connector, you need to secure it to the PC chasis. If you have old, unused PCI cards, you can remove their brackets for use. As an example, with a unused Ethernet card, the hole for the Ethernet jack can allow you to glue in a RCA socket.

    From the `Pin Assignments’ section in the datasheet, pin 48 is named SPDIFO. In the `Pin Description’ section it describes the function as “S/PDIF output” which is what is needed. The dot indicates Pin 1 (see the photo below modified from Japanese Wikipedia.) Pin 48 is on the edge of the device and is not a pin in the center, making it easier to perform soldering. (Warranty likely does not allow for user modifications. ;-)) Solder a thin wire such as 30 AWG to the pin. Make sure the wire is long enough to reach the PC case. Tug on the wire to verify the solder joint; the wire should not break off. Next the wire needs to be secured, as the solder joint cannot handle stress. Glue the wire about 1 inch away from the ALC655 to the motherboard edge; hot glue will work for this application.


    Next, secure with hot glue the RCA socket to the Ethernet back plate. Solder on the circuit from the design guide directly on the back of the RCA socket pins. Finally, solder the glued wire to the input of the circuit. Remember the solder a ground connection, the ground connection is the metal of the back plate itself. Now, connect a 75 Ω cable between the computer and the device.

    Linux Setup

    Now to properly configure the digital output and test it using mplayer.

    In alsamixer(1) verify that IEC958 output is not muted. Use the hotkey `M’ to unmute it. The value should read “MM” when muted, and “00” when enabled. Now lets review what devices are available on the system with:

    aplay -l

    the output is

    [email protected]:~$ aplay -l
    **** List of PLAYBACK Hardware Devices ****
    card 0: V8237 [VIA 8237], device 0: VIA 8237 [VIA 8237]
      Subdevices: 4/4
      Subdevice #0: subdevice #0
      Subdevice #1: subdevice #1
      Subdevice #2: subdevice #2
      Subdevice #3: subdevice #3
    card 0: V8237 [VIA 8237], device 1: VIA 8237 [VIA 8237]
      Subdevices: 1/1
      Subdevice #0: subdevice #0

    With only one sound card, they should be all listed as card 0. Also, note that I have 4 subdevices. Some users will see an analog and digital device here. (I do not so the configuration file in the next section differs.) Next, view the ALSA names for the sound devices on your system with

    aplay -L

    the output is

    [email protected]:~$ aplay -L
        Discard all samples (playback) or generate zero samples (capture)
        Playback/recording through the PulseAudio sound server
        VIA 8237, VIA 8237
        Front speakers
        VIA 8237, VIA 8237
        4.0 Surround output to Front and Rear speakers
        VIA 8237, VIA 8237
        4.1 Surround output to Front, Rear and Subwoofer speakers
        VIA 8237, VIA 8237
        5.0 Surround output to Front, Center and Rear speakers
        VIA 8237, VIA 8237
        5.1 Surround output to Front, Center, Rear and Subwoofer speakers
        VIA 8237, VIA 8237
        IEC958 (S/PDIF) Digital Audio Output

    You should see a iec958 or spdif device here.

    Use mplayer(1) to output via S/PDIF by manually selecting it on the command line with:

    mplayer -vo none -ao alsa:device=iec958 filename

    where -vo is no video out device, -ao alsa:device=iec958 is the audio out device by using the alsa audio driver, and device name is from the ones given in aplay -L. The sound should only output from the digital port. You can test files with encoded DTS audio or Dolby Digital audio by using

    mplayer -vo none -ao alsa:device=iec958 -ac hwdts,hwac3 filename

    This allows for directly passing the encoded audio signal to your receiver, the software will do no decoding. You should be able to get bit perfect surround sound.

    MythTV Audio Settings

    To always use digital output, go to
    Utilities/Setup -> General -> Audio page
    and use the following settings:

     Audio output device:         ALSA:iec958
     Digital Audio Capabilities
         Dolby Digital            check
         DTS                      check

    Digital Output

    Note the name of the device `iec958′ matches the output of aplay -L and mplayer.

    On the next page,

     Use internal volume controls      uncheck

    Internal Mixer Setting

    If this is not unchecked, then the system will decode and re-encode the DTS/Dolby Digital audio as PCM.

    The system is then setup to always output a digital signal.

    MythTV Setup for Dual, Analog and Digital, Outputs

    If the audio source was not DTS or Dolby Digital encoded, then you can output both to the digital and analog outputs with a custom .asoundrc configuration by creating a virtual device that maps the left and right audio channels. If the audio source was DTS or Dolby Digital encoded, only the digital output is available. (MythTV only can output one audio stream.)

    A custom .asoundrc is needed.

    # ~/.asoundrc or /etc/asound.conf
    # ALSA configuration file
    ##### USAGE #####
    # Save this file as "~/.asoundrc" (for user-specific sound configuration) or
    # "/etc/asound.conf" (for system-wide sound configuration) and specify ALSA
    # device names ad described in the next section.
    ##### IMPORTANT #####
    # To make this ALSA configuration file work with your sound card, you will need
    # to define the appropriate card and device information for the "analog-hw" and
    # "digital-hw" devices below.  You can find the card and device information
    # using "aplay -l".
    # Default output device
    pcm.!default {
      type plug
      slave {
        pcm dualout
    ##### Configuration File #####
    # Override the default output used by ALSA.  If you do not override the
    # default, your default device is identical to the (unmixed) "analog" device
    # shown below.  If you prefer mixed and/or digital output, uncomment the
    # appropriate four lines below (only one slave.pcm line).
    # Alias for device names
    # Actual device name usable for applications
    pcm.dualout {
        type plug
        slave.pcm "dual"
        hint {
            show on
            description "Output to analog and digital ports"
    ctl.dualout {
        type hw
        card 0
    # The following devices are not useful by themselves.  They require specific
    # rates, channels, and formats.  Therefore, you probably do not want to use
    # them directly.  Instead use of of the devices defined above.
    # Alias for analog output on the card
    # Do not use this directly--it requires specific rate, channels, and format
    pcm.analog-hw {
      type hw
      card 0     # MODIFY to match your setup
     # The default value for device is 0, so no need to specify
    #  - Uncomment one of the below or create a new "device N" line as appropriate
    #    for your sound card
    #  device 1
    #  device 4
    # Control device (mixer, etc.) for the card
    ctl.analog-hw {
      type hw
      card 0
    # Alias for digital (S/PDIF) output on the card
    # Do not use this directly--it requires specific rate, channels, and format
    pcm.digital-hw {
      type hw
      card 0
      device 0    # MODIFY to match your setup
      subdevice 3 # MODIFY to match your setup
                  #  Remove this if not needed (many won't need this line)
    #  - Comment out "device 1" above and uncomment one of the below or create a
    #    new "device N" line as appropriate for your sound card
    #  device 2
    #  device 4
    # Control device (mixer, etc.) for the card
    ctl.digital-hw {
      type hw
      card 0
    # multi -- a virtual device with four channels. This maps two stero streams
    # from the analog and digital outputs as one 4-channel stream. (Complex format)
    pcm.multi {
      type multi
      slaves.a.pcm "analog-hw"
      slaves.a.channels 2
      slaves.b.pcm "digital-hw"
      slaves.b.channels 2
      bindings.0.slave a
      bindings.0.channel 0
      bindings.1.slave a
      bindings.1.channel 1
      bindings.2.slave b
      bindings.2.channel 0
      bindings.3.slave b
      bindings.3.channel 1
    ctl.multi {
      type hw
      card 0
    # dual -- creates a 4 channel pcm stream based on the multi device. Maps this
    # devices 4 streams to the multi device. (Interleaved format, supported by most
    # applications)
    pcm.dual {
        type route
        slave.pcm "multi"
        slave.channels 4
        ttable.0.0 1
        ttable.1.1 1
        ttable.0.2 1
        ttable.1.3 1
    ctl.dual {
        type hw
        card 0

    This defines a name for the analog and digital output device. Next, it creates a virtual 4 channel device and maps the analog and digital audio channels to it. Finally, an interleaved format device is created for MythTV’s usage.

    The pcm.analog-hw block defines the exact card and device of your analog output. Typically this is card 0 and device 0. Verify the card and device indices with mplayer(1):

    mplayer -ao alsa:device=hw0.0 filename

    The expected output is sound from your speakers connected to the analog outputs. Similarly, for the pcm.digital-hw block, find the exact card and device (and possibly subdevice) with

    mplayer -ao alsa:device=hw0.0.3 filename

    My IEC958 device is located on card: 0, device 0, subdevice 3. Use the process of elimination to determine the exact index of the device. Modify the corresponding section of the configuration file with the card, device, and subdevice index.

    The other pcm.multi, pcm.dualout, and pcm.dual do not need to be modified as they only preform channel mapping. In MythTV, configure the default output device to be ALSA:dualout. (`Scan for audio devices’ feature only detects “type plug”.)

    Analog Digital Output

    Stereo analog and stereo sound should output for 2 channel audio sources. For audio encoded in DTS or Dolby Digital, your receive should receive the raw data and preform the decoding.