Pandora Audio Switch
v0.5
Versionhistory:v0.5
v0.1 - initial release
v0.2 - added the templatename pattern to make adding custom templates easier
v0.3 - adding templates to the script is no longer necessary. it will look for the template file according to -o templatename
v0.4 - added zaxxon compatibility
v0.5 - added working software volume control for the hardware device
- Switch your system's default audio output with ease and almost zero config work
- Have as many bluetooth or hardware based audio devices as you like
- Have a simple menubar toggle to click-switch audio output
- Entirely "onboard" ALSA based (no further dependencies)
I've been using bluetooth audio on the pandora for a while now. But bluetooth audio isn't that simple with ALSA. For some reason there's no really helpful documentation. At least i didn't find any. So i kept struggeling. I finally managed to get where i wanted to go and i wanted to make things a little easier for others.
The pandora already has everything you need to get bluetooth audio going. Proper configuration just isn't simple, so ppl may try, but give up after some frustrating experience. PAS is here to help.
This is all done with ALSA only. PulseAudio may be great and all, but it's resource hungry and we don't even need it.
What PAS does is: it holds predefined ALSA config files with device definitions (one per device) and copies them to the correct spot on demand. This enables you to easily set up the devices you have as default ALSA devices and switch between them on the fly with just a click of a button or a simple bash script call (whatever you prefer). I may even write a gui at some point. But usage could not be simpler as it already is.
The basic Design
PAS provides the following:
- pas.sh
- This is the actual script. It copies the config file corresponding to the audio output mode of your choice.
- .asoundrc_hw
- This is the predefined config to enable audio through the built in speakers.
- .asoundrc_bt
- This is the predefined config template to enable bluetooth audio devices.
This is the strong side of this design: You would simply start with the provided .asoundrc_bt, add your device specific MAC address and save it as a new template, .asoundrc_carstereo for example. Got a shiny new headset? Go ahead and add .asoundrc_headset to your collection of templates.
All you need to do now is to make pas.sh aware of the templates you want to use. Ill show you how to do that a couple of lines later.
All it takes to switch from carstereo to headset is a simple call:
Code:
pas.sh -o headset
Prerequisites
In order for this to work out properly we need to take care of a couple of things:
- Edit /etc/bluetooth/audio.conf
Make sure the [General] section holds the following:
Code:[General] Enable=Source,Sink,Headset,Socket,Control
If you like you can also enable autoconnection:
Code:AutoConnect=true
- Edit /usr/share/alsa/alsa.conf
For the sake of compatibility with libpnd change
Code:~/.asoundrc
Code:/home/${USER}/.asoundrc
- If you worked on ALSA's config files (~/.asoundrc and/or /etc/asound.conf) before, you may want to back them up now and keep them somewhere save. Because we will now delete those files:
Code:rm -v ~/.asoundrc rm -v /etc/asound.conf
- Find your device's MAC address:
Enable bluetooth on the pandora and activate your bluetooth device (e.g. your headset) so it can be found.
On the pandora a window should pop up, the bluetooth device manager:
If it doesn't pop up, open it manually with:
Code:blueman-manager
Code:hcitool scan
Code:hcitool con
Do not pair with them yet!
Simply download and extract the provided pas.zip. It holds all the necessary files to start with. To keep things simple place the files in
Code:
~/pas/
Luckily you will not need to edit .asoundrc_hw. So just forget about it.
Edit .asoundrc_bt and add a device specific MAC address you wrote down before:
Code:
# This is the bluetooth enabled ALSA config file.
# It is meant to set the main audio output to bluetooth.
# Please set device's MAC Address correctly or try autodetection.
#
# Try Alsa's BT autodetection
#@hooks [
# {
# func load
# files [
# "/usr/share/alsa/bluetooth.conf"
# ]
# errors false
# }
#]
pcm.!default {
type plug
slave.pcm "btsoftvol"
}
pcm.btheadset {
type bluetooth
device "XX:XX:XX:XX:XX:XX"
rate "44100"
profile "auto"
}
pcm.bluetooth {
type plug
slave {
pcm btheadset
}
}
ctl.bluetooth {
type plug
slave {
pcm btheadset
}
}
pcm.btsoftvol {
type softvol
slave.pcm "bluetooth"
control.name "Master"
control.card 0
}
ctl.btsoftvol {
type softvol
slave.pcm "bluetooth"
control.name "Master"
control.card 0
}
"bt_headset" will be this template's name. That's important, because we will use that name later in pas.sh.
If you want to use other bluetooth devices just edit .asoundrc_bt again, add another MAC address and save the file with a different name, .asoundrc_bt_carstereo for example.
Do this for as many device specific (because they hold the device specific MAC address) files as you like.
Editing pas.sh is no longer necessary. Here's the code anyway:
Code:
#!/bin/bash
##################
# #
# PAS v0.4 #
# #
##################
#
# This is a quick and dirty but still quite awesome script to switch ALSA
# output on the fly. It copies valid config files to enable output
# devices on demand.
# As of version 0.4 two templates are provided:
#
# 1. BT output -> .asoundrc_bt
# 2. HW output -> .asoundrc_hw
#
# You only need to add a valid device MAC address to .asoundrc_bt
# and save it as .asoundrc_outputname
# Make then use of it with: pas.sh -o outputname
# For every outputname you need a .asoundrc_outputname file!
#
# Please save your own customized .asoundrc files in PAS_PATH.
# Because of libpnd it is best to avoid ~ and use absolute paths here:
PAS_PATH=/home/${USER}/pas/
# Before any sound is being played ALSA parses the config files, so
# there's NO need to restart your system after you used this script.
#
# Please set PAS_PATH correctly!
if [[ $# = 0 ]]; then
echo "No options parsed. Try "$0" --help"
fi
while [[ $# > 0 ]]
do
key="$1"
case $key in
-o|--output)
OUTPUT="$2"
shift
if [ -f ${PAS_PATH}.asoundrc_${OUTPUT} ];then
echo "Switching audio output to: "${OUTPUT}
echo "using config files from: "${PAS_PATH}
rm -v /home/${USER}/.asoundrc
cp -v ${PAS_PATH}.asoundrc_${OUTPUT} /home/${USER}/.asoundrc
else
echo "Unsupported output mode parsed. Exiting."
echo "For every -o outputname you need a .asoundrc_outputname file!"
fi
;;
-h|--help)
echo "##################"
echo "# #"
echo "# PAS v0.4 #"
echo "# #"
echo "##################"
echo
echo "Pandora Audio Switch v0.4"
echo "------------------------"
echo "Usage:"
echo "-h|--help Display this message."
echo "-o|--output Set audio output device."
echo
echo "Example: ./pas.sh -o bt_headset"
echo " ./pas.sh -o boombox"
echo
;;
*)
echo "Unknown option parsed. Exiting."
;;
esac
shift
done
Code:
pas.sh -o hw
pas.sh -o bt_carstereo
pas.sh -o bt_boombox
Pair your devices
Open the bluetooth device manager again.
Right-click on your device. Click on "Setup":
Connect using the A2DP protocol.
If you're asked to enter the PIN code, try: 0000
If that doesn't work go study your device's user manual.
Hopefully your device gets connected. Click the star button to "trust" that device.
Use pas.sh
Let's assume you just connected your headset. Set it as ALSA's default output device with:
Code:
pas.sh -o bt_headset
Enjoy bluetooth audio
All of a sudden your headset's battery is empty.
Code:
pas.sh -o hw
Hints
Add the following to ~/.bashrc to make pas.sh available from everywhere:
Code:
alias pas='~/pas/pas.sh'
mplayer defaults to the oss audio driver on superzaxxon. It will ignore our alsa settings and you will not get bluetooth audio output.
You can change that by editing /usr/etc/mplayer/mplayer.conf:
Code:
ao=alsa
PAS does set pcm.!default devices. Basically this means that every program that plays audio through ALSA will use that default device. We do it that way, because with bluetooth you don't want to tell every program separately to use the bluetooth device. You prolly just want all the audio through bluetooth. Well, i do
Unfortunately this setup does not work with all programs (for different reasons). But it does work with most.
Attachments
Last edited: