Friday, October 14, 2016

Volume Control script for Raspberry Pi

Here is a small script to control audio volume. Feel free to modify the script as per need. Make sure pulseaudio is is up and running.

Filename: volchg.sh

Usage: 
volchg.sh [+/-] [x]

volchg.sh --increase volume by 5%
volchg.sh + --increase volume by 5%
volchg.sh + 10 --increase volume by 10%
volchg.sh - --decrease volume by 5%
volchg.sh - 10 --decrease volume by 10%

Script

#!/bin/sh
#############################
### Volume Up/Down Script ###
#############################

# Specify increase or decrease
MODE=$1

# Percentage Change
VAL=$2

#Set defaut change to 5%
if [ -z "$2" ]; then
  VAL=5
fi

#Get MAX Value
MAX_VOL=`amixer cget numid=3 | grep "max=" |  cut -d, -f5 | cut -d= -f 2`

#Get current val
CURRENT_VOL=`amixer cget numid=3 | grep ":" | cut -d, -f2`

#UPDATE VOL

if [ "$MODE" = "+" ]; then
  MUL=1
else
  MUL=-1
fi

NEW_VOL=$(( ($CURRENT_VOL + ($MUL * (( $VAL * $MAX_VOL) / 100 ))) ))
PER_VOL=$(( ($NEW_VOL * 100) / $MAX_VOL ))

amixer cset numid=3 $NEW_VOL > /dev/null
echo "Volume Level: $PER_VOL%"

Have fun !!!

Tuesday, September 13, 2016

Raspberry Pi - Bluetooth Choppy Audio

Raspberry Pi 3, Model B / RASPBIAN OS

BCM43438 is a single chip provide 2.4GHz 802.11n wireless LAN and Bluetooth radio support.

With a lot of google, hit and trial methods solution found to the choppy audio problem. Try turning off WiFi on Raspberry Pi while streaming bluetooth audio.

This worked for me.

Thanks.

Raspberry Pi - A2DP Profile - Bluetooth Audio Streaming via Cellphone

Raspberry Pi 3, Model B / RASPBIAN OS

Recently purchased Raspberry Pi, and I am pretty happy with it. Credit card sized SOC and it's pretty amazing. Love for music is pretty high so first thing I took up is to stream music from from my cellphone connected to Raspberry Pi via bluetooth. And Raspberry Pi will throw audio out to speakers connected to 3.5 mm audio jack.

I won't go deep into configurations settings as you will find plenty of articles to configure settings for A2DP profiles and make Raspberry Pi using audio input stream from cellphone. Though I will highlight few things on which I faced difficulty

  1. First OS I installed was Ubuntu Mate
    • A2DP profile settings worked via Desktop mode. Kick off bluetooth connectivity wizard and you are all set.
    • However when I booted to console only mode, tried bunch of things but couldn't get things to work
  2. Eventually I switched to RASPBIAN and it worked via UI as well as console mode.
Few commands which came in handy
  1. Bluetooth utility
    bluetoothctl
    #show - display basic information
    #scan on - turns scanning on
    #pair xx:xx:xx:xx:xx:xx - start pairing with cellphone
    #trust xx:xx:xx:xx:xx:xx - add it to trust store
    #connect xx:xx:xx:xx:xx:xx - connects to cellphone
    #ctrl + d - exits bluetooth prompt
  2. pulseaudio
    pulseaduio -D - starts pulse audio instance
    pulseaduio -k - kills any pulseaudio running instance
  3. Audio Bus
    pactl list sources short - lists all audio sources
    pactl list sinks short - lists output
  4. Audio Output
    amixer cset numid=3 0 -- Automatic selection
    amixer cset numid=3 1 -- sets output to HDMI
    amixer cset numid=3 2 -- sets output to 3.5mm analogue port
  5. Raspberry Pi Config
    sudo raspi-config - master utility to change various settings for Raspberry Pi


Thanks.