Volume control, revisited [solved]


I’m having problems controlling the volume and I don’t use raspspotify. I usualy send sounds from python but I just cannot get the volume to adjust.

from subprocess import call
call(["amixer", "-D", "pulse", "sset", "Master", "0%"])

doesn’t work nor does
call(["pactl", "--", "set-sink-volume", "0", "5%"])

Any help is appreciated.
rpi ZeroW with buster.

cat /etc/asound.conf
options snd_rpi_googlevoicehat_soundcard index=0

pcm.softvol {
type softvol
slave.pcm dmix
name Master
card 0

type softvol
slave.pcm dsnoop
control {
name Micro
card 0
min_dB -10.0
max_dB 50.0
resolution 256}

pcm.!default {
type asym
playback.pcm "plug:softvol"
capture.pcm "plug:micboost"

ctl.!default {
type hw
card 0

When you just type this amixer -D… command in the terminal does it changes the sound volume?


Just checked and on a terminal the following works to must sound:

amixer -D pulse sset Master 0%

Could you confirm that it works too on your side? If yes then maybe look at the permission you have when running this in Python

Thank you.

Yes it does work. Checked it with alsamixer. However, cannot change the volume within Python running it with user’s (pi) credentials.

This’ my test code:

: ~cat stream.py
#!/usr/bin/env python3

import os
import subprocess as sub

sub.call(["amixer", "-D", "pulse", "sset", "Master", "0%"])

player = "mplayer"
stream = "http://icy-4.radioparadise.com/aac-32"
volume_option ="-volume"
volume = 50

command = f"{player} {stream} {volume}"

Happy Holidays.

just tested your code and this is working here.

:thinking: yes it works but at what volume level? This’ the issue, I cannot adjust the volume.

Here’s a simple test code which starts with volume at 100% then goes down to 0%. In my case, there’s no change. Don’t know if it’s a code, hemisphere or hardware issue.

test .wav file can be find here: http://test.ebolisa.net/img/g.wav

#!/usr/bin/env python3

import simpleaudio as sa
from time import sleep
import subprocess as sub
import warnings # to supress wav msg

filename = 'g.wav'

def play():
  wave_obj = sa.WaveObject.from_wave_file(filename)
  play_obj = wave_obj.play()
  play_obj.wait_done()  # Wait until sound has finished playing

# set volume to 100%
sub.call(["amixer", "-D", "pulse", "sset", "Master", "100%"])

# setvolume to 50%
sub.call(["amixer", "-D", "pulse", "sset", "Master", "50%"])

# set volume to 0%
sub.call(["amixer", "-D", "pulse", "sset", "Master", "0%"])

Ok, Looks like by switching player from mplayer to mpv, had a better luck.
Now I can control the volume from python using the following test code:

#!/usr/bin/env python3
import subprocess as sub

filename = 'g.wav'

def play(vol, file):
  sub.call(["mpv", "--volume", vol, file])

# set volume to 100%
VOLUME = "100.0"
play(VOLUME, filename)

# setvolume to 50%
VOLUME = "50.0"
play(VOLUME, filename)

# setvolume to mute
VOLUME = "0.0"
play(VOLUME, filename)

However, mpv response is much slower than mplayer.
Any thoughts?


I personnaly just use aplay as it is already included in raspbian

Closing the topic