XBMC and pulseaudio master volume (Ubuntu 11.10)

If you use XBMC as standalone in Ubuntu(choose it in the login screen), volume control is limited to XBMC as a source in pulseaudio. If you login to Gnome/Unity/whatever, and forget to set sound level to 100% before you logout, XBMC sound volume will suffer from this.

Solution:
You can control sound volume in terminal with

pactl set-sink-volume # n%

.
# is a number of a so called sink, which is the same as an output device(analog, HDMI, SPDIF, etc). n% is percentage of volume. This will set sink 0 to 100% volume:

pactl set-sink-volume 0 100%

You need to find out which number your default output device is:

pactl list sinks

Should output something like this:

Sink #0
	State: RUNNING
	Name: alsa_output.pci-0000_00_1b.0.analog-stereo
	Description: Intern lyd Analog Stereo
	Driver: module-alsa-card.c
	Sample Specification: s16le 2ch 44100Hz
	Channel Map: front-left,front-right
	Owner Module: 4
	Mute: no
	Volume: 0:  90% 1:  90%
	        0: -2,75 dB 1: -2,75 dB
	        balance 0,00
	Base Volume: 100%
	             0,00 dB
...

Play some music and test(music should mute 1 second):

pactl set-sink-volume 0 0%; sleep 1; pactl set-sink-volume 0 90%

To get XBMC set volume to 100% before startup we create a new command called xbmc-standalone-max-volume. Do this as root:

cp /usr/bin/xbmc-standalone /usr/bin/xbmc-standalone-max-volume
nano /usr/bin/xbmc-standalone-max-volume

In xbmc-standalone-max-volume find pulse start section and add three lines after it:

PULSE_START="$(which start-pulseaudio-x11)"
if [ -n "$PULSE_START" ]; then
  $PULSE_START
else
  PULSE_SESSION="$(which pulse-session)"
  if [ -n "$PULSE_SESSION" ]; then
    XBMC="$PULSE_SESSION $XBMC"
  fi
fi
# set volume to 100%
PACTL="$(which pactl)"
$PACTL set-sink-volume 0 100%

Now edit XBMC session:

nano /usr/share/xsession/XBMC.desktop

Change Exec and TryExec:

[Desktop Entry]
Name=XBMC
Comment=This session will start XBMC Media Center
Exec=xbmc-standalone-max-volume
TryExec=xbmc-standalone-max-volume
Type=Application

You may also do the same with xbmc -> xbmc-max-volume, but when inside gnome you could simply use your keyboard and Alt-Tab, set volume up, and Alt-Tab back.