«

»

Jul 26

How To: Re-Map Speaker Channels in Pulseaudio

Your Problem:

Alright, so you ponied-up and purchased a nice 5.1 soundsystem– you’re the man now dog. You hook that bad-boy up to your new HDMI-capable receiver and you’re ready to take your beast new soundsystem for it’s maiden voyage through the frequencies of your favorite song from the Mary Poppins soundtrack ohh what’s the name of that track– Ohhh yea “Spoonful of Sugar” in crisp 6-channel sound. You finally sit down and hit “play” only to find that the sound is all messed up! Sounds that should be coming out of the center channel are instead coming out of your left rear speaker and your right rear speaker is for some strange reason acting as your subwoofer. This is clearly one of those god damn you Linux moments.I mean don’t get me wrong, I love Linux but it’s the kind of love that’s filled with moments of utter hatred– so really like pretty-much all forms of love I guess.

So, how do we fix this. The answer is simple use PulseAudio.

Background on PulseAudio:

PulseAudio as I understand it (and this is an understanding based on tinkering and usage… not research)  basically rides on top of the lower-level sound program called Alsa. Alsa used to handle all your audio needs by itself before PulseAudio came out… the reason PulseAudio was developed however is because Alsa has very few options and can’t handle much configuration for advanced behavior such as turning down the volume for individual applications or having several computers all using the speakers of one computer over a network connection in a client/server model of audio playback. Because of the evolving needs of the userbase and the limited ability of Alsa to accomodate those needs, PulseAudio was born.

People LOVE to hate on PulseAudio because back in the day it used to be filled with tons of bugs and knowledge/familiarity with it was low as it was the new kid on the linux-audio block; however since those days PulseAudio has matured ten fold and is pretty-much the galactic standard for audio in modern linux distributions… including of course Ubuntu. Ubuntu comes by default using PulseAudio so we don’t need to install any fancy schmancy new programs here– just modify the configuration we already have.

The Fix:

In order to fix this problem we’re going to need to make use of PulseAudio’s “module-remap-sink” functionality.

Module-remap-sink (or “remap-sink” for the rest of this post) will basically  allow us to remap individual channels to send them to their proper speaker. The first step here is to build a channel-map to guide you in the process and help you keep your head on straight.

A great command to help you setup your channel-map is:

$speaker-test  -c6 -l1 -twav

Speaker-test will generate a voice which tells you what each speaker presently maps to, a very helpful command that is included in all of the Ubuntu derivatives.

SOUND                                           SPEAKER

Left Front          ————-> Left Front
Center Front     ————-> Left Rear
Right Front       ————-> Right Front
Left Rear            ————-> Center Front
Subwoofer         ————->  Right Rear
Right Rear         ————-> Subwoofer

From the channel-map we created above, we can see that two of the 6 channels are just fine– so we need to re-map the other four. In order to do this we’re going to need to modify two files, the “/etc/pulse/default.pa” file and the “/etc/pulse/system.pa” file. Let’s open both of those files up with your favorite text editor.

In both files we’re going to comment-out the following code as indicated:

Change

### Automatically load driver modules depending on the hardware available
.ifexists module-udev-detect.so
load-module module-udev-detect
.else
### Alternatively use the static hardware detection module (for systems that
### lack udev support)
load-module module-detect
.endif

to

### Automatically load driver modules depending on the hardware available
.ifexists module-udev-detect.so
#load-module module-udev-detect
.else
### Alternatively use the static hardware detection module (for systems that
### lack udev support)
#load-module module-detect
.endif

At this point we’re going to save the changes in the “system.pa” file and close it. Now the rest of the changes are to occur in the “default.pa” file which has already been slightly modified as mentioned above.

NOTE:
Adding the following lines to the “/etc/pulse/default.pa” will make these re-mappings global for the entire system. If you only wanted to make these channel-remappings for your user you could add them to the “/home/*your_username*/.pulse/default.pa” file instead.

Now let’s use the remap-sink to modify the four channels that are not mapped properly. Modify the following three lines to fit your situation and add them to the “default.pa” file.

# Definition of standard sink
load-module module-alsa-sink sink_name=standard device=hw:0 channels=6 channel_map=front-left,front-center,front-right,rear-left,rear-right,subwoofer

# Remapping Function
load-module module-remap-sink sink_name=remapped_sound master=standard channels=6 master_channel_map=front-left,front-center,front-right,rear-left,rear-right,subwoofer channel_map=front-left,rear-left,front-right,front-center,rear-right,subwoofer

# Setting Default to your new re-mapped sink
# (should match the "sink_name" in line 2)
set-default-sink remapped_sound

The first line is a definition of the standard/existing sink. The second line references that definition in both the “master” and “master_channel_map” variables. The second line above is the one that actually handles the remapping. Read the blurb from PulseAudio.org below which defines the “master_channel_map” variable in the second line for a better understanding.

(from PulseAudio.org):

“master_channel_map –

The channels in the master sink, where the channels listed in channel_map will be relayed to. channel_map and master_channel_map must have equal number of channels listed, because the channels will be mapped based on their position in the list, i.e. the first channel in channel_map will be relayed to the first channel in master_channel_map and so on.”

master_channel_map (input)            channel_map (output)

Left Front         ————–> Left Front
Center Front       ————–> Left Rear
Right Front        ————–> Right Front
Left Rear          ————–> Center Front
Subwoofer          ————–> Right Rear
Right Rear         ————–> Subwoofer

So the channels (in the order they are listed) in “master_channel_map” will map onto the channels (in the order they are listed) of “channel_map”. Sort of like the channel-map we drew out above except with different titles on the columns… make sense? I hope so.

Once you’ve added those two lines to the “/etc/pulse/default.pa” file you can save the changes and restart PulseAudio with the commands below:

$sudo /etc/init.d/pulseaudio restart
$pulseaudio --kill
$pulseaudio --start

Perhaps the method above is a bit overkill but I like to be positive.

From here you should be able to load “gnome-volume-control” on the terminal and see that under the “Hardware” tab you have a new device that you are able to select that relates to the new remapped sink you just created. Select that device (if it isn’t selected already) and let’s take those new speakers for another test drive. I recommend re-running the speaker-test command to double-check everything.

$speaker-test  -c6 -l1 -twav

Hope this tutorial worked out for you, if it didn’t or if something could be improved feel free to throw it in the comments!

16 comments

1 ping

Skip to comment form

  1. Petey Carcass

    Could you be a bit more specific on what exactly to modify in the last part? I’ve been at this so long today that the urge to kill a random human-being has become a bit too strong for my own liking. I’ve tried replacing the sink-names with the one I’ve got from $pacmd which should be hw=0 since it’s the first and only sound-device connected. HELP!

  2. Eric

    Hey Petey, I hear you there on killing people linux-related or not :) … If I’m understanding you correctly you’re concerned with matching the names of your pre-existing sinks… firstly, you can name the sinks anything you damn-well please, they are names just used for human reference, to keep things simple I used “standard” which was the unadulterated parent-class for the output device, which by itself was mapped incorrectly and “remapped_sound” for the name of the child-class of the output device; this child-class did the work of the remapping and would then output audio directly to the ALSA hw=0 device at the lowest level before the speakers.

    You should not need to use the names of your existing channels because we have taken the time to comment out the “#load-module module-udev-detect” and “#load-module module-detect” portions which auto-detect, create and name the sinks that you see presently (this is done during pulseaudio’s startup). If this step is implemented properly the sinks you have presently (or any sinks really) will not exist at all during the restart of pulseaudio. Once we’ve turned-off the auto-sink detection and naming we need to manually create and map the new sink that we create surrounding hw=0. That step is the second portion of the configuration.

    For my own reference, what does your “Sound –> Speaker” table look like for your improperly mapped channels, with that info I can help you create proper “master_channel_map” and “channel_map” lines.

  3. Petey

    Hi! Thanks for the help! I’m completely new at Linux, started setting up an HTPC yesterday and it was my first time ever using Linux. Managed to install all the programs I needed, setting up a connection to my NAS, got sound running and then -smack- the rest of the day spent googling how to remap the sinks and pretty much getting nowhere!
    I’ve tried just following the example above just to see if it would have any impact at all but I don’t get any sound at all then. I’m not sure but it seems like commenting out the load-module-lines leads to the soundcard not beeing initialized, then again – I hardly know what I’m talking about here! :)

    My table would look like this:

    Sound Speaker
    Front left Front left
    Center Rear right
    Front right Front right
    Rear right Sub
    Rear left Center
    Sub Rear left

    Might be worth noting that I’m running Ubuntu 11.10, seems like there’s been alot of changes in that version from earlier ones if I understand things correctly.

  4. Petey

    I don’t know if it’s relevant but when I run pacmd my intial channelmap looks like this:

    front-left,front-right,rear-left,rear-right,front-center,lf.

    I tried changing the master_channel_map accordingly and then adjusting the remapped channel_map to:

    front-left,rear-right,front-right,subwoofer,front-center,rear-left

    but whenever I do any changes I don’t get any ouput through the speakers.

  5. Eric

    It’s worth noting that all my boxes are some Ubuntu-derivative of version 11.04… however as far as I’m aware Ubuntu has not diverged from PulseAudio and that config should be the same.

    Based on the data you gave me your changes to the file should look like this:
    ###################
    # Definition of standard sink
    load-module module-alsa-sink sink_name=standard device=hw:0 channels=6 channel_map=front-left,front-center,front-right,rear-left,rear-right,subwoofer

    # Remapping Function
    load-module module-remap-sink sink_name=remapped_sound master=standard channels=6 master_channel_map=front-left,front-center,front-right,rear-left,rear-right,subwoofer channel_map=front-left,rear-right,front-right,front-center,subwoofer,rear-left

    # Setting Default to your new re-mapped sink
    # (should match the “sink_name” in line 2)
    set-default-sink remapped_sound
    ###################

    Make sure you disable or change the Default statement at the bottom of the default.pa file, a default statement should already be present by default and if not removed may override your settings because it’s the last thing read-in towards the bottom of the default.pa file.

    Once you apply these changes and restart pulseaudio what options are you presented with in “gnome-volume-control” under the “Hardware” tab in the “choose a device to configure” area? Are you able to choose the proper device under the “Output” tab? Do you get any sound now? — If so has the channel-map changed?

  6. Petey

    Sorry but I gave up and went back to Windows 7, it might not work well but at least it works.
    I’m thankful for your help though and I think others will be too, I’ve read lots of post about people having the same problem that I did but your site was the only place with a way to fix it.

    1. Eric

      I’m sorry to hear that man. If you ever think to consider Linux again, the biggest piece of advice I can give you is to check that the hardware is friendly with Linux prior. The easiest way to do that is to buy a very popular make/model of computer or components (if you’re building it). For instance, I won’t buy a printer until I at least google the model number along with the keyword “Linux” to make sure lots of folks aren’t complaining that it’s broken.
      Anyways happy to help— I’m sure you’ll come back to the dark side someday :)

      1. Petey

        I’m already considering it, just picked up a CPU-cooler which was the only part missing to build another computer out of parts I’ve had lying around so it gives me an opportunity to experiment without not being able to watch movies meanwhile. Got to raise my bloodpressure som way, learning Linux seems like the way to go! ;)

        Funny how I missed the Reply-button up until now… :)

        1. Tomawyder

          You may also consider running windows version of pulse (under cygwin for example) and replay linux audio through windows machine’s speakers. For me it’s a great advantage of pulse. No additional cables required for audio.

  7. Sagar Behere

    Hey,

    Commenting out the “#load-module module-udev-detect” and “#load-module module-detect” portions causes my usb webcam’s mic to not be detected.

    Is there a way to add the usb mic OR is there a way to keep the module-detect and still use remap-sink? In that case, what should master= ?

    Thanks in advance,
    Sagar

    1. Eric

      Yeah as this tutorial was built around an HTPC, I never ran into that circumstance however that would surely happen if we disabled the autodetect functionality… this leaves us with two options… 1). re-enable autodetect and work around it OR 2). leave autodetect off and setup a new map for the input channel.

      You can use my other article to identify the usb microphone device from there just look up the syntax for input devices in pulseaudio and you should be golden I’ll take a look into it when I get home… I’ll need to do this For my HTPC soon as I re-equip it for videoconferencing.

  8. David

    Greetings!

    My problem basically is that my LFE and center channels are swapped. If i go over this howto, things get even worse. It doesn’t seem to matter how i edit the channel maps, the result is always the same: there is no entry in the sound setting/hardware tab, and if i run a speaker-test from terminal, all the six channels are played on the front-left and front-right speakers, just the intensity is different. I’ve tried using the exact same code that is in the article, just to see if it makes a difference, but the results are always the same.

    What am i doing wrong??

    Sorry if i if i wrote in a confusing style, I’ve been working on this for day now and i’m getting frustrated with it. If something is not clear, tell me and i try to amke it more clear. please, someone help me.

    1. Bob

      Hi David,
      Quick check 1 & 2:
      1) Did you use “gnome-volume-control”, [hardware] tab to select the correct Sound Device and it’s 5.1 channels. Like what was mentioned above:
      “From here you should be able to load “gnome-volume-control” on the terminal and see that under the “Hardware” tab you have a new device that you are able to select that relates to the new remapped sink you just created. Select that device (if it isn’t selected already) and let’s take those new speakers for another test drive. I recommend re-running the speaker-test command to double-check everything.”
      2) Have you ever physically wired your speakers “wrongly” to your PC. I used to do that very much for testing and tuning, and often enough forgotten about it.
      If this have not reach you too late, I hope it helps.
      I came from the “dark-side” WinXp and now camp at Ubuntu derivative LinuxMint11.
      I used to be pretty frustrated because I kept forgetting the difference in my OS environment. Hope I could elevate this from as many people as possible.

  9. Lester

    Hey, thanks for the info.

    I’m having a weird one though:

    Front Left -> Front Left AND Back Left
    Front Right -> Front Right
    Back Right -> Front Right

    A remapping wouldn’t fix this, as if I remap Front Right to Front Back I would lose Front Right altogether, wouldn’t I? Any ideas on what could be wrong?

  10. Lester

    Please disregard my last comment. I’m on a new receiver and I hadn’t even enabled multichannel, lol.

  11. Bob

    Hi Eric,
    Nice guide and thanks – even-though I googled here for something slightly different.
    I remember and recognised the speaker-test command immediately and gave your variation a try.
    I am using LinuxMint11 64bit on my desktop – Gigabyte MoBo onboard 5.1sound.
    Do you happen to know if there is a GUI to temporary re-map rear to front OR centre/sub to Front channels. So that I could test/tune my 5.1 speakers in 2channels software like Audacity, etc… w/o the need to physically switch/swap the connections from behind my PC.
    BTW, thanks for sharing and just my 2cent back for those who come after me looking for answers. I know my existing 5.1 is working b/c my DVD cleaning disc came with a 5.1 audio test and a slight variation of your command work on my system.
    $ speaker-test -c6 -l1 -twav
    Only hear voice on Front Left and Front Right and beep from Subwoofer
    $ speaker-test -Dplug:surround51 -c6 -l1 -twav
    By using -Dplug:surround51 option, all 6 channels sound correct. Beep is still from Subwoofer though. Sorry, not sure why -Dplug… option, got that a couple of months back when setting my 5.1 by googling.
    Note for those having problem:
    i) instead of -twav, I also tried -tpink and -tsine. These was my time saver as one of my speaker “rear-left” is much softer than the rest, also inaudible when I can hear the rest clearly. Increase the sound volume as necessary.
    ii) In Sound->Preference->Hardware, select the right device (if you have more than 1 sound card) and the right “Profile”. It took me a long time to figure out I have to change my original profile from “Stereo” to “Analog Surround 5.1″ before it worked.
    iii) I was from WinXp and hope could help people cross-over and smell the fresh air.
    Regards,
    Bob
    PS: Yes, I created bash script for those command for future use and reference.

  1. Identifying different audio devices » ConfigNewton.com

    [...] « How To: Remapping Speaker Channels in Pulseaudio [...]

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>