tHog

Linux audio tips and tricks

Preface

This page is not about specific sound cards and drivers. These are more general tips for dealing with audio in Linux. You may find some hardware-specific information in my machines page, as well as the audio hardware page.

Encoding multichannel audio into AC3 for Dolby Digital amplifiers

I assume you already have the hardware and software for AC3 passthrough. For example, with MPlayer you can use afm=hwac3 to dump the compressed audio directly to your amplifier via S/PDIF (optical or coaxial). The next stage is to encode other formats on the fly, so the amplifier can play them as proper multichannel audio.

MPlayer also has provisions for this, using af=lavcac3enc. I have used it succesfully for a long time to play 6-channel Vorbis or AAC, but for some reason my E-Mu 0404 USB did not like it.

The canonical solution is the a52 plugin in ALSA. In Gentoo, you need to install alsa-plugins with USE=ffmpeg. Then you can use something like this in .asoundrc:

pcm.dolbyslave {
   type hw
   card 1
   device 1
}

pcm.dolbyenc {
    type a52
    bitrate 640
    slavepcm "dolbyslave"
}

Now you can use this for further multichannel tricks. For example, use the examples below to expand 2 channels into 5.1, and then use this for digital transport into the amp. (Replace the "surround51" slave by "dolbyenc".)

(I don't actually recommend this. It is probably better to pass raw 2ch audio into the amp, and let it do the expansion with better algorithms. Also, AC3 is a lossy compression scheme. But for something like Ortoperspekta, this is the only option, besides the full 6 channels of analog cables.)

Modified Dolby Surround: Expanding 2-channel audio into 5.1

There are many surround sound systems by Dolby, but here I am referring to the original analogue encoding, which is simply called Dolby Surround. It is a way of storing/transmitting multichannel movie soundtracks in just two channels, which correspond to front left and right. In decoding, the rear channel is formed as L-R and the centre as L+R; there is only one rear channel, though it may be played from multiple speakers.

This kind of decoding works surprisingly well for 2-channel music recordings, whether or not they are produced with a Dolby Surround encoding. It can be approximated by very simple additions to a 2-channel amplifier, with the requisite number of speakers. A variant with two rear speakers in different polarities is described here. I happened to write that one as well, but I have found the idea in several audio enthusiast sources, and it is hardly original to me.

While we are dealing with computers, the same setup can be easily realized in software, using a relatively simple ALSA configuration. If you have a sound card and an amplifier with 5.1 channels, you can use this entry in .asoundrc:

pcm.pms {
# modified basic dolby surround, aka poor man's surround

# idea adapted from http://ubuntuforums.org/showthread.php?t=783222

# by teknohog

    type route
    slave.pcm surround51
    slave.channels 6

# sqrt(1/2) * left -> left
# sqrt(1/2) * right -> right
    ttable.0.0 0.70710678118654757
    ttable.1.1 0.70710678118654757

# 0.5*(left - right) -> left rear
    ttable.0.2 0.5
    ttable.1.2 -0.5

# 0.5*(right - left) -> right rear
    ttable.1.3 0.5
    ttable.0.3 -0.5

# 0.5*(left + right) -> center
    ttable.0.4 0.5
    ttable.1.4 0.5

# 0.5*(left + right) -> subwoofer
    ttable.0.5 0.5
    ttable.1.5 0.5
}

This can be used by any ALSA application, by choosing the output device 'pms'. For example:

$ mplayer -ao alsa:device=pms d2.ogg

Notes

Ortoperspekta

Ortoperspekta is an early form of surround sound, invented by Tapio M. Köykkä. It is originally intended for reproducing music from a 2-channel (ordinary 'stereo') recording, but could be interesting for other uses as well. It attempts to approximate a realistic soundscape in the entire listening space, rather than create a precise L-R positional audio at one sweetspot.

It is a subset of the basic Dolby Surround routing, using only the centre and surround channels. Worth a try especially if you only have three speakers, and only the centre one must be a good one.

The placement of the rear/side speakers has not been obvious to me. From the usual OP descriptions it seems that they should be on the sides, rather than in the back. As far as I know, the sides are also a good place for the surround speakers in Dolby systems, but due to practical reasons they are often behind the listeners. Thus, it may be a good idea to use the front L/R speakers as the side channels in OP. As software configuration allows easy experimentation, here are the .asoundrc entries for the two different OP placements:

pcm.opback {
# ortoperspekta for a 5.1 sound card + amplifier
# using rear speakers as the side channels
# by teknohog

    type route
    slave.pcm surround51
    slave.channels 6

# 0.5*(left - right) -> left rear
    ttable.0.2 0.5
    ttable.1.2 -0.5

# 0.5*(right - left) -> right rear
    ttable.1.3 0.5
    ttable.0.3 -0.5

# 0.5*(left + right) -> center
    ttable.0.4 0.5
    ttable.1.4 0.5
}

pcm.opfront {
# ortoperspekta for a 5.1 sound card + amplifier
# using front speakers as the side channels
# by teknohog

    type route
    slave.pcm surround51
    slave.channels 6

# 0.5*(left - right) -> left front
    ttable.0.0 0.5
    ttable.1.0 -0.5

# 0.5*(right - left) -> right front
    ttable.1.1 0.5
    ttable.0.1 -0.5

# 0.5*(left + right) -> center
    ttable.0.4 0.5
    ttable.1.4 0.5
}

Notes

2-channel version

The .asoundrc entry is written for a 5.1 sound card, so that it can be easily used with a 5.1 speaker setup. However, there are actually only two different signals, the sum and difference. These could be played back by a 2-channel card and amplifier, but the rear speakers should be connected in a special way:
pcm.op {
# ortoperspekta for a 2-channel sound card + amplifier
# by teknohog

    type route
    slave.pcm front
    slave.channels 2

# centre channel: 0.5*(left + right) -> left
    ttable.0.0 0.5
    ttable.1.0 0.5

# side channel: 0.5*(right - left) -> right
    ttable.1.1 0.5
    ttable.0.1 -0.5
}

                       ,---.
             ,---------|-  |
             |         |   |
             |  ,------|+  |
             | |       `---'
             | |    front center
             | |
             | |  ,--------------.              
             | `--|+            +|--------------.
             |    |  L        R  |              |
             `----|-            -|---------.    |
                  |  amplifier   |         |    |
                  `--------------'         |    |
                                           |    |
                                           |    |
    ,--------------------------------------'    |
    |    ,---.                         ,---.    |
    |    |   |                         |   |    |
    `----|+ -|-------------------------|- +|----'
         |   |                         |   |
         `---'                         `---'
       left side                     right side

Terminology nitpicks


Risto A. Paju