From 76f277eeb471285aa6893480599512952e12d385 Mon Sep 17 00:00:00 2001
From: Steven Newbury <steve@snewbury.org.uk>
Date: Thu, 16 Oct 2014 23:06:27 +0100
Subject: [PATCH] Set pulseaudio channel map to WAVE-EX

Pulseaudio expects clients to specify their channel-map if the
default (ALSA) map does not route the audio to the expected speakers.

Many Google results suggest dealing with this by re-routing the audio
channels with the appropriate ALSA plugin, but this will then simply
break any clients which expect the default ALSA mapping.

Virtually all media files and codecs, certainly flac, dca, a52, and of
course anything based on Microsoft's WAVEFORMAT_EXTENSIBLE specification,
assume the layout in the table here:
http://en.wikipedia.org/wiki/Surround_sound#Standard_speaker_channels

Fortunately, pulseaudio directly addresses this with a built-in channel
map for WAVE-EX which can be set automatically in the stream sample-spec.
---
 NEWS                                     | 2 ++
 src/output/plugins/PulseOutputPlugin.cxx | 6 +++++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index f8ec01980..c1cf72471 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,6 @@
 ver 0.20 (not yet released)
+* output
+  - pulse: set channel map to WAVE-EX
 
 ver 0.19.2 (not yet released)
 * decoder
diff --git a/src/output/plugins/PulseOutputPlugin.cxx b/src/output/plugins/PulseOutputPlugin.cxx
index 120bad090..5dc733383 100644
--- a/src/output/plugins/PulseOutputPlugin.cxx
+++ b/src/output/plugins/PulseOutputPlugin.cxx
@@ -523,7 +523,11 @@ pulse_output_setup_stream(PulseOutput *po, const pa_sample_spec *ss,
 	assert(po != nullptr);
 	assert(po->context != nullptr);
 
-	po->stream = pa_stream_new(po->context, po->name, ss, nullptr);
+	/* WAVE-EX is been adopted as the speaker map for most media files */
+	pa_channel_map chan_map;
+	pa_channel_map_init_auto(&chan_map, ss->channels,
+				 PA_CHANNEL_MAP_WAVEEX);
+	po->stream = pa_stream_new(po->context, po->name, ss, &chan_map);
 	if (po->stream == nullptr) {
 		SetError(error, po->context, "pa_stream_new() has failed");
 		return false;