Configuring Digital Sound

From MythTV Official Wiki
Revision as of 04:38, 5 May 2010 by Klk (Talk | contribs)

Jump to: navigation, search

Basics

Audio.pngMythTV relies on the operating system sound system to provide audio. You should make sure first and foremost that you have a working sound system outside of MythTV before thinking there is something wrong with the software.

On Linux there are generally three major sound systems that are supported by MythTV:

  • ALSA: Advanced Linux Sound Architecture
  • aRts: Analog Realtime Synthesizer
  • OSS: Open Sound System

Check the appropriate support site or Linux documentation in addition to any hints here in getting your sound card working under Linux. The MythTV wiki Sound card page also contains useful information on this topic. If you have additional information, please add it to the External Links page.

The default in modern Linux systems that use the 2.6 kernel is now ALSA. For MythTV installations it is advisable to turn off any sound servers that sit on top of ALSA such as Pulse Audio or the KDE or Gnome sound servers.

Note: The title of this page may be "Configuring Digital Sound", but one can just as easily follow the steps outlined to get simple analog stereo working as well. It is just a matter of using the .asoundrc below and using the mixed-analog device.

Bit Perfect Audio

Digital sound is the preferred method to get audio from a MythTV installation for two reasons

1. We want to have the audio decoding done outside of the computer for greater sound quality.

2. We want to play DVD with full surround sound using DTS or Dolby Digital.

You need to be aware that ALSA by default will upsample all of your audio to 48 kHz. This keeps it compatible with TV broadcasts as well as Dolby Digital and DTS surround on DVDs. ALSA typically takes the sound from your content, routes it through a module called "plug" where this resampling takes place, and then routes it to a module called "dmix", a software mixer, before it outputs it to your SPDIF/iec958 connector.

For Die hard Audiophiles, this resampling is a real source of irritation as CD audio is made using PCM at 44.1Khz. The upsampling in ALSA is unfortunately very low quality, and if you have a half decent amplifier and speakers, you will notice that CD playback has a congested compressed sound to it. This is very easy to overcome, by sending all your audio directly to your soundcards SPDIF or IEC958 Connector. The device name for this connector is typically "cards.pcm.iec958". So just configure this device as the audio device in MythTV.

On Mythfrontend Utilities/Setup->Setup-General go to the Audio page and set your audio device as

 Audio output device        = ALSA:cards.pcm.iec958
 Passthrough outputdevice   = Alsa:iec958:{AES0 0x02}
 Enable AC3 to SPDIF passthrough   check
 Enable DTS to SPDIF passthrough   check
 Use internal volume controls      uncheck

With most modern Linux distros there is not a need to mess around with the ALSA configuration files

Check your device names with

  aplay -L
  iec958 'cards.pcm.iec958'
  spdif 'cards.pcm.iec958'

Don't forget to unmute your iec958 output using alsamixer. Now when you play a CD your amp should show PCM output at 44.1 kHz, and TV broadcasts as PCM at 48 kHz, and your DVDs will also have full blown surround.

Don't forget to rip your CDs using Lossless FLAC as the encoder (this is the default for the "Perfect" setting in the MythTV) will preserve the 44.1 kHz audio files from your CDs.

An option was added in version 0.22 (thread): if you're confident that ALSA isn't going to resample (twice) and have appropriate hardware, you can add a setting 'DisableResampler' with value 1 to the database. Otherwise the modern ALSA does software mixing by default and resamples to 48k before sending data to the card.

For the vast majority it's far better that we resample - SRC anywhere else in the chain (ALSA, card) would be done with a SNR much less than a *worst case* 97 dB..

echo "insert into settings values ('DisableResampler', 0, '<hostname of frontend>')" | mysql -u mythtv -p mythconverg

PulseAudio

Several Linux distros (Ubuntu, Fedora) now use PulseAudio per default.

PulseAudio is a sound server, whose advantages are

  • volume settings (mixer) specific and stored per application
  • management of all sound applications in one place using GUI tools
  • abstraction of hardware, including autodetection of soundcards via HAL, moving from one output to another without restarting the application and using multiple sound cards together to output more channels
  • and last but not least network transparency, i.e. sound output on different computer is possible.

Disadvantages are:

  • Not supported by MythTV
  • High latency, sound samples can take anywhere from half a second to 2 seconds longer to reach the sound card. Whilst this is faster than other sound servers it is unacceptably slow compared to direct interaction with alsa and can cause significant problems with video playback.

See also PulseAudio's website on how to setup your computer and applications for PulseAudio.

Quick Check of Sound System

You can use ALSA's speaker-test utility to perform output tests to determine if your speaker and sound configurations are correct and functioning properly. A somewhat crude invocation to see if the system's default output configuration to a set of 5.1 surround sound speakers is working would be:

speaker-test -twav -c6 -Dplug:surround51 

Some receivers/sound devices may not properly play LPCM audio until this test is executed. Essentially it sets some state somewhere that allows LPCM to be passed to the receiver. At least this was the case on my receiver, an STR-DA3300ES Sony. Bitstreams worked fine, but LPCM would not work until I ran these speaker tests. There is probably more going on here but it won't hurt to run this test to initialize some state to make sure everything works.

 speaker-test -c2 --device cards.pcm.iec958 --rate 44100
 speaker-test -c2 --device cards.pcm.iec958 --rate 48000

ALSA Utilities

Package.png Don't forget that there are some utilities that are included in the alsa-utils package. Use your favorite package manager to install the package. This is especially true for alsaconf recommended in the installation guide.

  • alsa_init
  • alsaconf configures your sound device (run as root)
  • alsa_snddevice
  • alsamixer
    • text based mixer. Fills screen with all available audio inputs.
    • push the slider up to increase volume.
    • The MM at the top of column. Means MUTE Hit M key to unmute
    • The word CAPTUR above a column means it's being used as an input. Use space to select. Note that selecting some inputs automatically deselects others.
  • aplay
    • ALSA command-line sound player. Also has options (-L and -l) for listing ALSA devices which can be helpful
  • arecord
    • ALSA command-line sound player. Also has options (-L and -l) for listing ALSA devices which can be helpful
  • test_ioctl
    • Run as root. Can unmute sound and increase volume. Use -h for full usage information

Setting up ALSA's .asoundrc, Properly

Using the "default" ALSA configuration (or the .asoundrc specified for your particular sound card at the http://alsa-project.org/ site) gives only a very basic configuration and relies on the user to specify all the sound stream handling required for a system's sound card. Instead of specifying this configuration information every time the sound card is used (or configuring it in every application), it makes more sense to create an ALSA configuration file.

To use the ALSA configuration file below, save the text (including comments) as /etc/asound.conf (for system-wide usage) or as ~/.asoundrc (for user-specific configuration). Edit and use the file as described in the comments at the top. (Note that the only edit you should need to do is specifying card number and device number for the "analog-hw" and/or "digital-hw" virtual devices.)

To use one of the named devices in Myth, specify the appropriate device for the Audio output device setting (in the General settings section of the frontend settings) using the format ALSA:devicename (case sensitive). You will need to type in the value, it's not in the list. Next, set the Mixer Device field to ALSA:default. The only valid device names in the below ALSA configuration file are default, analog, mixed-analog, digital, and mixed-digital.

The filtering performed by these definitions may prevent AC-3/DTS passthrough from working. You may specify a different device for the Passthrough output device.

To test the output devices, execute the command:

aplay -D devicename /path/to/audio/file.au

replacing devicename with one of default, analog, mixed-analog, digital, or mixed-digital. Because aplay doesn't support many formats, an au file is probably the easiest type to test with. If you need one, download ftp://ftp.kernel.org/pub/linux/kernel/SillySounds/english.au and you can hear Linus pronounce Linux (from 1994).

Script.png ~/.asoundrc

# ~/.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.


##### DEVICE NAMES #####
# This configuration file defines four devices for use by the user.  Those
# devices are "analog", "mixed-analog", "digital", and "mixed-digital".  The
# user may also re-define "default" to be identical to one of the above-named
# devices (i.e. to send all sound output to the digital output unless otherwise
# specified).  Use the device names as described below:
#  - "analog" outputs to the analog output directly and (at least on software
#  sound cards) blocks other audio output.  After playback completes, "queued"
#  sounds are output in sequence.
#  - "mixed-analog" mixes audio output from multiple programs into the analog
#  output (so you can hear beeps, alerts, and other noises while playing back
#  an audio stream).
#  - "digital" outputs to the digital output directly.  Since most (all?)
#  digital outputs expect 48kHz PCM audio, this may not work for some playback
#  (i.e. CD's--which are 44.1kHz PCM audio--or 32kHz audio streams from TV
#  recordings, etc.).
#  - "mixed-digital"

# All other devices created within this file are used only by the configuration
# file itself and should /not/ be used directly.  In other words, do not use
# the devices "analog-hw", "dmix-analog", "digital-hw", or "dmix-digital".


##### 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".


##### 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).
#
# Note, also, that as of ALSA 1.0.9, "software" sound cards have been modified
# such that their default "default" device is identical to the "mixed-analog"
# device.  Whether using an ALSA version before or after 1.0.9, it does no harm
# and has no affect on performance to redefine the device (even if the
# redefinition does not change anything).  Also, by using this ALSA
# configuration file, you once again have access to unmixed analog output using
# the "analog" device.
pcm.!default {
  type plug
## Uncomment the following to use (unmixed) "analog" by default
#  slave.pcm "analog-hw"
## Uncomment the following to use "mixed-analog" by default
  slave.pcm "dmix-analog"
## Uncomment the following to use (unmixed) "digital" by default
#  slave.pcm "digital-hw"
## Uncomment the following to use "mixed-digital" by default
#  slave.pcm "dmix-digital"
}

# Control device (mixer, etc.) for the card
ctl.!default {
  type hw
  card 0
}

# Alias for (converted) analog output on the card
# - This is identical to the device named "default"--which always exists and
# refers to hw:0,0 (unless overridden)
# - Therefore, we can specify "hw:0,0", "default", or "analog" to access analog
# output on the card
# - Note that as of ALSA 1.0.9, "software" sound card definitions redefine
# "default" to do mixing, meaning this device is different from "default" and
# allows playback while blocking other sound sources (until playback
# completes).
pcm.analog {
  type plug
  slave.pcm "analog-hw"
  hint {
    show on
    description "Analog Output - Use analog outputs, converting samples, format, and rate as necessary."
  }
}

# Control device (mixer, etc.) for the card
ctl.analog {
  type hw
  card 0
}

# Alias for (converted) mixed analog output on the card
# - This will accept audio input--regardless of rate--and convert to the rate
# required for the dmix plugin (in this case 48000Hz)
# - Note that as of ALSA 1.0.9, "software" sound card definitions redefine
# "default" to do mixing, meaning this device is identical to "default" for
# "software" sound cards.
pcm.mixed-analog {
  type plug
  slave.pcm "dmix-analog"
  hint {
    show on
    description "Mixed Analog Output - Use analog outputs, converting samples, format, and rate as necessary. Allows mixing with system sounds."
  }
}

# Control device (mixer, etc.) for the card
ctl.mixed-analog {
  type hw
  card 0
}

# Alias for (converted) digital (S/PDIF) output on the card
# - This will accept audio input--regardless of rate--and convert to the rate
# required for the S/PDIF hardware (in this case 48000Hz)
pcm.digital {
  type plug
  slave.pcm "digital-hw"
  hint {
    show on
    description "Digital Output - Use digital outputs, converting samples, format, and rate as necessary."
  }
}

# Control device (mixer, etc.) for the card
ctl.digital {
  type hw
  card 0
}

# Alias for mixed (converted) digital (S/PDIF) output on the card
#  - This will accept audio input--regardless of rate--and convert to the rate
#  required for the S/PDIF hardware (in this case 48000Hz)
pcm.mixed-digital {
  type plug
  slave.pcm "dmix-digital"
  hint {
    show on
    description "Mixed Digital Output - Use digital outputs, converting samples, format, and rate as necessary. Allows mixing with system sounds."
  }
}

# Control device (mixer, etc.) for the card
ctl.mixed-digital {
  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
  # 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 1
#  - 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
}

# Direct software mixing plugin for analog output on the card
# Do not use this directly--it requires specific rate, channels, and format
pcm.dmix-analog {
  type dmix
  ipc_key 1234
  slave {
    pcm "analog-hw"
    period_time 0
    period_size 1024
    buffer_size 4096
    rate 48000
  }
}

# Control device (mixer, etc.) for the card
ctl.dmix-analog {
  type hw
  card 0
}

# Direct software mixing plugin for digital (S/PDIF) output on the card
# Do not use this directly--it requires specific rate, channels, and format
pcm.dmix-digital {
  type dmix
  ipc_key 1235
  slave {
    pcm "digital-hw"
    period_time 0
    period_size 1024
    buffer_size 4096
    rate 48000
  }
}

# Control device (mixer, etc.) for the card
ctl.dmix-digital {
  type hw
  card 0
}

For a detailed description of how this ALSA configuration file works, see this post from the mailing list.

It is possible to configure ALSA to output PCM audio simultaneously on both the analog and the digital outputs. This means that any 2-channel audio MythTV decodes will be sent to both outputs. It does not mean that you will get AC-3 or DTS through the digital output and analog audio through the analog output. To configure ALSA this way, add the following at the end of the above /etc/asound.conf or .asoundrc file:

pcm.!default {
  type plug
  slave {
    pcm multi
    rate 48000
  }
  ttable.0.0 1.0
  ttable.1.1 1.0
  ttable.0.2 1.0
  ttable.1.3 1.0
}

pcm.stereo {
  type plug
  slave {
    pcm multi
    rate 48000
  }
  ttable.0.0 1.0
  ttable.1.1 1.0
  ttable.0.2 1.0
  ttable.1.3 1.0
}

ctl.stereo {
  type hw
  card 0
}

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
}

Then set the Audio output device setting (in the General settings section of the frontend settings) to ALSA:multi (case sensitive). If you want to use AC3 passthrough, you should also set the passthrough device to ALSA:digital or ALSA:mixed-digital. If you leave it set to Default, you will hear horrible noise on your analog output. Note that this means that when you are watching a source that uses AC3 passthrough, you will hear nothing on your analog output.

NOTES: Works great from me set like this. (FC 6, Myth 0.20 installed from RPMS). Everything goes to the right place. In my case the analog sound to the TV is connected to my HTPC's "Front" output jacks, so I must set the Mixer Device (in the General settings section of the frontend settings) to default, as above, and type "Front" into Mixer Controls so that Myth's volume control adjusts the proper channels. Even though I am on FC 6, I am using both parts of the above /etc/asound.conf.

If you connect the digital output (SPDIF) of your computer to your stereo, and the analog output to your television, this set-up allows you to listen to the audio of a program through both your stereo system and your television's speakers simultaneously, or to choose between the two simply by muting one.

More information about this is available from the knoppmyth wiki: http://knoppmythwiki.org/index.php?page=DigitalAudioHowTo

Questions and Answers

I get no sound from MythTV when watching TV but MythMusic and command-line test all work fine. What is the problem?

- Check to see if you have the "AC3 Passthrough to S/PDIF" option checked in Setup. This option is should only be enabled if your tuner hardware provides AC3 audio streams. If are not receiving AC3 audio streams from your tuner hardware and check this box, you will not get any sound. (see thread)

- Check your definition of the mixer device. If you're using ALSA:default for audio output, you should change the mixer device from "/dev/mixer" to "ALSA:default". (from the Gentoo Linux Wiki)

How do I set up Myth plugins to use digital sound with mplayer?

Add the following to your mplayer lines in the setting dialog, substituting the device alias you wish to use.

-ao alsa:device=digital -afm hwac3

This will tell mplayer to send output audio to the digital device. It will pass ac3 or dts directly to hardware digital out. This allows you to watch dvds and videos with digital sound correctly, while still being able to watch those with mp3 audio without having to edit the command line.

An alternate Mplayer parameter list that can be used is:

-ac hwac3,hwdts,

Note the leading comma, this allow mplayer to fall back on audio codecs, from AC3, to DTS (if the file is DTS the mplayer lines at the beginning of this section will not work) if it is neither then the leading comma means it will fall back to default and play regularly (as in an mp3 avi file).

In mythtv, the final mplayer line would look something like this:

mplayer -ac hwac3,hwdts, -quiet %s

How do I do this with a PVR-350?

The PVR-350 encodes the TV signal it receives from its built-in tuner or from its various audio and video inputs into MPEG-2 format and streams that output to MythTV which writes that stream into files. When MythTV plays back those MPEG-2 files or those generated by other PVR-x50 and similar cards, it simply streams them them back to the PVR-350 and the PVR-350 decodes the audio and video and sends them out of its cables.

To get the audio produced by the PVR-350 many people take its audio outputs and use a cable to pipe them back into their sound card's Line In jack. If you're using the Line Out jack on your sound card to send analog audio to your amplifier, then all you need to do is unmute the Line In control in your audio mixer and set the volume. However, redirecting this output to the S/PDIF output in digital format requires a few more steps.


Important.png Note: The names of the sound card controls used in the following command examples will vary depending on the audio device installed in your computer and the driver module it uses. The command “amixer” entered without any options will output all of the controls exposed for your audio device and “alsamixer” or your favorite graphical mixer program can also be used to manipulate these controls to make these settings.

Set Line In as a capture source and mute it so that it doesn't go out the Line Out jack:

$ amixer set 'Line',0 0%,0% mute cap

Set Capture as a capture source:

$ amixer set 'Capture',0 0%,0% mute cap

Tell the sound card to turn on S/PDIF output:

$ amixer set 'IEC958',0 unmute

Tell the sound card to route analog input to the S/PDIF port:

$ amixer set 'IEC958 Playback Source',0 'Analog In'

This last setting needs to be toggled back and forth when you switch from watching TV, TV recordings and other MPG sources via the PVR-350 card to watching DVDs, videos and listening to music. All of the non-PVR audio is PCM audio, not analog. Switching back to PCM audio can be done with:

$ amixer set 'IEC958 Playback Source',0 'PCM'

If you like, you can set up a button on your remote to toggle this setting.

If your sound card does not allow you to set the "IEC958 Playback Source", or you want to avoid having to toggle this setting back and forth, then you need to find some other way to route the analog audio from the Line In jack to PCM audio. One technique would be to use the “arecord” and “aplay” commands to route the capture sources on the sound card back in as PCM audio:

$ arecord -D hw:0,0 -f dat | aplay -D mixed-digital

Important.png Note: Fedora Core 5 users should leave off the “-D mixed-digital” portion of this command

If you use this approach then you will probably want to execute this command at startup so that it is always running and available while the MythTV frontend is up. One way to do that would be to add it to a script in ~/.kde/Autostart (assuming that you're using the KDE desktop.) Ad a script containing this line or add it to a script that's already there:

$ arecord -D hw:0,0 -f dat | aplay -D mixed-digital &

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.


Important.png Note: Again, Fedora Core 5 users should leave out the “-D mixed-digital” portion of this command

Note that this approach may not produce the best possible sound quality. Ths sound is going out of the PVR-350, back into the soundcard, being sampled by “arecord” and “aplay” and then sent out the S/PDIF to be converted back to analog by your apmlifier. If you have an optical interconnect between your MythTV frontend and your amplifier it may help reduce ground-loop hum though. On the other hand, this technique may introduce a time lag that causes your audio to be out of sync with the video when you watch TV.

Backing up your mixer settings

Once you have your audio configured to your liking you may way to capture a snapshot of the mixer configuration as a backup, just in case. The “alsactl” command can be used for this purpose. Running the following command as root will update the default profile. Some Linux distributions use this profile to save the current ALSA mixer settings during shutdown so that they can be restored during the next boot. This is often accomplished using settings in the modules configuration file (/etc/modprobe.conf in recent Redhat/Fedora distributions.)

$ /sbin/alsactl store 0

This command will save a copy of the current mixer settings in your home directory. (Substitute your MythTV user if you used something other than “mythtv”.)

$ /sbin/alsactl -f /home/mythtv/asound.backup store 0

This command will restore the settings you saved in your home directory. (Substitute your MythTV user if you used something other than “mythtv”.)

$ /sbin/alsactl -f /home/mythtv/asound.backup restore 0

How do I do this in KnoppMyth?

KnoppMyth forum post

How do I do this in Fedora Core?

Configuring Digital Sound with AC3 and SPDIF

How do I get my mac mini SPDIF out working?

In Ubuntu 8.04, append this to /etc/modprobe.d/alsa-base:

 options snd-hda-intel model=macmini

Also read in mac mini ubuntu guide. [1]

I also found that sometimes the SPDIF goes into a funky state and a reboot is the only thing that gets it out of this state. So if you think everything is right, but the sound just isn't working, try rebooting again.

How do I get 5.1 AC3 audio to work again?

If your Dolby receiver once properly received 5.1 audio, but now just puts out loud static, an iec setting might have been changed. This fix should apply for pass-through AC3 and AC3 upconverting (stereo to 5.1 surround). The command line tool iecset will tell you the current settings:

Mode: consumer
Data: audio
Rate: 48000 Hz
Copyright: protected
Emphasis: none
Category: PCM coder
Original: original
Clock: 1000 ppm

If you are feeding the receiver raw audio (PCM), then 'Data: audio' is correct. However, if you are feeding it an encoded stream (Dolby AC3 or DTS), 'Data: non-audio' is correct. To change it, type 'iecset audio on' or 'iecset audio off.'

However, usually you will want to go back and forth between sources. To enforce 'audio off' for AC3 stream, you can open your device with an AES parameter. To get this parameters, do 'iecset -x' after setting it up how you'd like. In this case, we're getting AC3 working again, so after turning audio off, iecset -x gives:

AES0=0x02,AES1=0x82,AES2=0x00,AES3=0x02

Add this to your device string under Setup->General->Audio System->Digital Output Device. For example, I have multiple digital cards and I want to use my INTEL card, so combining the output of 'aplay -L' and 'iecset -x' I use this as my Digital Output Device:

ALSA:iec958:CARD=Intel,DEV=0,AES0=0x02,AES1=0x82,AES2=0x00,AES3=0x02

My Audio Output Device is the same, up to the first AES setting (ALSA:iec958:CARD=Intel,DEV=0). I assume that if I had problems with direct PCM audio not working, I would want to use this as my Digital Audio Device string (AES0 is the only thing that changes):

ALSA:iec958:CARD=Intel,DEV=0,AES0=0x00,AES1=0x82,AES2=0x00,AES3=0x02