Difference between revisions of "Configuring Digital Sound"

From MythTV Official Wiki
Jump to: navigation, search
(Added link to FC document)
m (Minor distro independace edit)
Line 12: Line 12:
 
== ALSA Utilities ==
 
== ALSA Utilities ==
  
Don't forget that there are some utilities that are included in the alsa-utils package.  Use your favorite RPM manager to install the package.  This is especially true for alsaconf recommended in the installation guide.
+
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
 
*alsa_init

Revision as of 10:25, 8 February 2007

Basics

MythTV 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.

ALSA Utilities

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
  • 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 for listing ALSA devices which can be helpful
  • arecord
    • ALSA command-line sound player. Also has options 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.

[Note: This is not required on Fedora Core 5 and 6 systems. The default ALSA configuration contains all of the capabilities delivered by the configuration file shown below and using this configuration file will break the ALSA configuration on FC5 systems. FC5 users can use ALSA:default as the Audio output device and set mixer to default in the General settings section of the frontend settings. ]

[Note2: If you are using a version of ALSA newer than 1.0.12 -- http://alsa.opensrc.org/.asoundrc states that for most uses an .asoundrc is not necessary. Use "aplay -l" to see a list of available device names and use "ALSA:<device name>" for the Audio output device and "default" for the mixer in the General settings section of the frontend settings. ]

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 field using the device name, but without the "ALSA:" prefix. Generally, you can use default for mixer regardless of which output device you've chosen.

At least one user reported being able to use either ASLA:digital-hw or ALSA:dmix-digital for watching TV/recordings; however, MythMusic would not work logging the error Rate doesn't match (requested 44100Hz, got 48000Hz). Changing the output device to ALSA:default fixed the problem.

The filtering performed by these definitions will prevent AC-3/DTS passthrough from working. If using passthrough, you may specify "ALSA:digital-hw" to Myth, but some things (i.e. CD playback) will no longer work.

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. aplay doesn't support many formats, so an au file is probably the easiest one 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).

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

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

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

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

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

If you would like simultaneous digital and analog output, 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.

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 "default". (from the Gentoo Linux Wiki)

But I want to have all my sound go to the S/PDIF connector, how do I do that?

It varies by hardware, but you should be able to set up your .asoundrc to route all PCM data (2-channel) to your S/PDIF connector. See above or here's a simple example:

pcm.!default { 
type hw 
card 0 
device 1 
} 

The card and device numbers are directly related to your hardware. My system has the following devices shown using aplay -l

card 0: YMF744 [Yamaha DS-XG PCI (YMF744)], device 1: YMFPCI - IEC958 
[YMFPCI - IEC958] 
Subdevices: 1/1 
Subdevice #0: subdevice #0 

In this scenario, you would set your MythTV audio device to /dev/adsp. You can also try typing in the audio device box `ALSA:default` or `ALSA:spdif` as appropriate for your hardware to try to utilize MythTV's internal ALSA support. In order to use this though, you need to have compiled MythTV with ALSA support.

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.

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

As the mythtv user run xine. Open the Setup screen (alt+s or accessible with a mouse). Select the audio tab. Under speaker arrangement select Pass Through.

If this does not work open xine's Setup, on the gui tab change Configuration experience level to Master of the known universe and select Apply. Then select the audio tab and examine the device.alsa_passthrough_device setting.

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 any other MPG format file for that matter, 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.

[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

[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.

[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

http://www.mythtv.org/wiki/index.php/Configuring_Digital_Sound_with_AC3_and_SPDIF,