[mythtv-users] ALSA and SPDIF Out For nForce2 Motherboards

Michael T. Dean mtdean at thirdcontact.com
Sun Apr 11 04:36:38 EDT 2004


Sorry for the length of the post, but I'm trying to clear up all 
confusion on ALSA's .asoundrc and which ALSA device to use for S/PDIF 
output from Myth.  The small posts I've made didn't seem to provide 
enough information, so I'm going all out on this one.  That entails a 
lot of "how it works" and a very long--but very flexible--.asoundrc.  
(Oh, and by the way, thanks to those who asked these questions, I've 
finally cleared up _all_ my confusion about how to create a proper 
.asoundrc.  :)

Derek Watson wrote:

>I struggled to work out what to put here also, but in the end discovered
>that using -ac alsa9:nforce works nicely in mplayer, and ALSA:nforce
>works in Myth. This is using the same mapping as you use below, and the
>.asoundrc file that was posted to the list a while back.
>  
>
I'm assuming you mean the .asoundrc posted by chua_kanteck ( 
http://www.gossamer-threads.com/perl/mailarc/gforum.cgi?username=chua_kianteck@yahoo.co.uk;guest=2852963&t=search_engine 
).  To be inclusive, I'm posting another copy so we can pick it apart:

pcm.nforce-hw {
type hw
card 0
}

pcm.!default {
type plug
slave.pcm "nforce"
}

pcm.nforce {
type dmix
ipc_key 1234
slave {
pcm "hw:0,2"
period_time 0
period_size 1024
buffer_size 4096
rate 44100
}
}

ctl.nforce-hw {
type hw
card 0
}

>However, now I have a problem in that the sample rate that my tv card is
>using to capture audio (SAA7134) is 32kHz which the digital output
>interface can't support (get a message saying requested 3200Hz, got
>0Hz). I get no sound at all when using 48kHz - maybe the SAA7134 doesn't
>support this audio sample rate?
>
>Hope this helps you, and if anyone has any ideas on how to overcome the
>32/48 kHz problem mentioned I'd love to hear from you :-)
>  
>
Now, it may seem like I'm going on and on about this ( 
http://www.gossamer-threads.com/perl/mailarc/gforum.cgi?post=119104;search_string=spdif%20nforce%20.asoundrc;guest=2852963&t=search_engine#119104 
andhttp://www.gossamer-threads.com/perl/mailarc/gforum.cgi?post=118593;search_string=stuttering%20asoundrc%20nforce;guest=2852963&t=search_engine#118593 
), but that's only because it seems my warnings are going unheeded--*the 
dmix plugin is evil!!!* (Or, more precisely, dmix is evil when used 
incorrectly.  Apologies to any ALSA developers for the drama. ;)

You'll notice that the .asoundrc above defines a virtual device (or 
"alias") called "nforce-hw" and another called "nforce".  The 
"nforce-hw" device is connected directly to "hw:0,0" and the "nforce" 
device uses the dmix plugin to perform direct software mixing of audio 
streams (i.e. takes two inputs and provides one output), but it  _only_ 
supports audio at a fixed frequency (by default 48000Hz) format (S16), 
channels (2), and period_time (125000) (see
http://www.alsa-project.org/alsa-doc/alsa-lib/pcm_plugins.html#pcm_plugins_dmix 

).  However, by defining a slave device, one can modify these defaults.  
In the .asoundrc given, the modified frequency is 44100Hz--good for 
CD's.  Unfortunately, bad for DVD's, bad for MythTV 32KHz channels, bad 
for MythTV 48KHz channels, bad for some QuickTime and some AVI files, 
etc.  That's why your sound doesn't work with "ALSA:nforce"--the audio 
format is wrong.

Note that the .asoundrc also defines a virtual device called "default" 
(using pcm.!default to override the "default" definition of "default").  
This device uses the "plug" (automatic conversion) plugin to 
automatically convert the sample rate, channels, and format as 
appropriate.  chua_kanteck has, therefore, told ALSA that if no device 
is specified (or if a device named "default" is specified), it should 
use the plug plugin to convert the audio in for output on the S/PDIF device.

Assuming S/PDIF is on device 2 as above, you should be able to send a 
signal directly to the hardware by specifying "ALSA:hw:0,2" to Myth and 
"alsa9:hw:0,2" to MPlayer.  Unfortunately, though, the signals are not 
necessarily the right format, so we'll need to do converstion.  Note, 
however, that on some nForce2 boards S/PDIF is device 1 (and possibly 
some other device).  If this is the case with your board (run "aplay -l" 
to find out), replace all occurences of "device 2" with "device 1" and 
(for completeness sake) all occurences of "hw:0,2" in the comments with 
"hw:0,1".

Therefore, I recommend the following .asoundrc.  Feel free to change the 
alias names (note that spdif--among other names--is reserved, though).  
I used analog, mixed-analog, digital, and mixed-digital because they 
aren't reserved, they don't have special characters, and they're 
descriptive.  If using multiple sound cards, you might want to add 
something to specify that this is nForce analog, etc.

To use the .asoundrc below with Myth, specify "ALSA:analog" for analog 
output, "ALSA:digital" for S/PDIF output, "ALSA:mixed-analog" for 
software-mixed analog output, or "ALSA:mixed-digital" for software-mixed 
S/PDIF output.  The mixed output will allow you to hear sounds generated 
by other programs using the mixed output device on your Myth box while 
watching TV, DVD's, etc.  The non-mixed output will block other sounds 
so you won't be interrupted by beeps, clicks, etc. while watching a 
recording--but, unfortunately, after the recording finishes (and the 
ALSA device is released), the other sounds--having been queued up--will 
play back one after the other (it's not a pleasant sound, and is often 
much louder than the recording).  Note, also, that the rate conversion 
to 48000Hz used by the mixed output may affect the audio quality 
(especially for low-bitrate audio).

Note that I've included a control device (ctl.name) for each virtual 
device.  The standard way to find a control device is to prepend "ctl." 
to the audio device name, so we define one just in case an application 
tries to find the control device.  Most of the time, this is unnecessary 
since your program won't access the control device--especially on the 
devices you won't use directly--but when using certain programs (like 
JACK, for example) a control device is required, so it makes sense to 
define one and doesn't hurt to define one even for the devices you won't 
use directly.

Finally, I included a commented out section that allows you to change 
the default output device used by ALSA.  Uncomment the definition of 
"pcm.!default" at the top of the file, but be sure to only uncomment one 
"slave.pcm" line (i.e. uncomment four lines).

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

HTH.

Mike

The (I hope it's the definitive) .asoundrc for nForce2

# 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).
#pcm.!default {
#  type plug
## 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"
#}

# Alias for analog output on the nForce2 (hw:0,0)
# - 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 nForce2
pcm.analog {
  type plug
  slave.pcm "analog-hw"
}

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

# Alias for (rate-converted) mixed analog output on the
# nForce2 (hw:0,0)
#  - This will accept audio input--regardless of rate--and
# convert to the rate required for the dmix plugin
# (in this case 48000Hz)
pcm.mixed-analog {
  type plug
  slave.pcm "dmix-analog"
}

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

# Alias for (rate-converted) digital (S/PDIF) output on the
# nForce2 (hw:0,2)
#  - 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 nForce2 card
ctl.digital {
  type hw
  card 0
}

# Alias for mixed (rate-converted) digital (S/PDIF) output on the
# nForce2 (hw:0,2)
#  - 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 nForce2 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 nForce2 (hw:0,0)
# 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
}

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

# Alias for digital (S/PDIF) output on the nForce2 (hw:0,2)
# Do not use this directly--it requires specific rate,
# channels, and format
pcm.digital-hw {
  type hw
  card 0
  device 2
}

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

# Direct software mixing plugin for analog output on
# the nForce2 (hw:0,0)
# 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 nForce2 card
ctl.dmix-analog {
  type hw
  card 0
}

# Direct software mixing plugin for digital (S/PDIF) output
# on the nForce2 (hw:0,2)
# 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 nForce2 card
ctl.dmix-digital {
  type hw
  card 0
}




More information about the mythtv-users mailing list