Looking for Command Line Volume Controls

I have an I2S audio device working with my Rock PI 4 right now. But I cannot control the volume in any way I’m used to (I’m porting a project from an RPi 4 system).

I’ve tried alsamixer and no device settings seem to affect the output volume.

I’ve tried amixer from the command line (amixer sset PCM 20%)

I’ve tried pulse audio (pactl set-sink-volume 0 20%)

No matter what I do, my speaker is at full volume.

What’s the correct way to change the volume? I am running headless and don’t have a GUI. I need a way to do this from the command line. Suggestions?

Thanks!

The codec and I2S is disconnected maybe try create an alsa softvol you will have to play that pcm once before it shows in alsamixer.

pcm.array_gain {
 type softvol
   slave {
   pcm "array"
   }
 control {
   name "Mic Gain"
   count 2
   card 0
   }
 min_dB -40.0
 max_dB 10.0
 resolution 80
}

Thanks for the idea. I’ve tried things like this and while they devices show up, they don’t do anything. That is, I can do

amixer sget PCM
amixer sset PCM 50%

and see the results change in the text returned, but it doesn’t affect anything on the audio volume.

What I did get to work was this:
pactl set-sink-input-volume 7 10%

That is, it’s using PulseAudio (which is fine with me… I don’t care HOW I play sounds so long as I can and can control the volume). I’m playing notification sounds, only.

All was great! But then I rebooted and now the pactl stuff doesn’t work. No idea why (but happy for ideas!)

pactl list sink-inputs

shows nothing, now, where-as it showed one device (numbered 7, oddly) before.

So I’m back to square one; why on earth this would go away after rebooting I have no idea!

The weird thing about softvols is you have to play them first or they don’t exist.

So you need to change the above to work with the i2s index and pcm.
Just aplay -Darray gain or whatever you call the softvol ctrl+c
Then set you softvol via alsamixer or amixer
Then aplay again.

Dunno if you did it that way as having to play first to initialise the softvol has thrown me before.

Thanks and yep, I tried that, but it’s not happy. I have a new device but it’s not controlling anything. It’s like the “Master” device I had, originally.

pcm.soft_vol {
type softvol
slave {
pcm “default”
}
control {
name “SoftMaster”
card 1
}
}

pi@master:~$ aplay -Dsoft_vol beep.wav
Playing WAVE ‘beep.wav’ : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
aplay: set_params:1270: Broken configuration for this PCM: no configurations available

pi@master:~$ amixer controls
numid=1,iface=MIXER,name=‘PCM’
numid=2,iface=MIXER,name=‘SoftMaster’

pi@master:~$ aplay -Ddefault /opt/code/pi_orchestration/lib/tones/beep.wav
Works just perfectly (but no volume control)!

Crazy that I had things working via pulseaudio and now that’s broken, too. I’ve tried removing the /etc/asound.conf file (also .asoundrc as I tried that, too) but that changes nothing, as well.

I even tried the asound.conf file from my RPi but no go on that, too. Really odd…

Thanks!

Is your default pcm the same as “card 1” when you do a aplay -l ?

Its also missing the volume settings

 min_dB -40.0
 max_dB 10.0
 resolution 80

https://alsa.opensrc.org/How_to_use_softvol_to_control_the_master_volume

I would actually define your PCMs and use -D rather than default.

Thanks for keeping me honest. I appreciate it!

So here’s what I have at this point:

pi@master:~$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: sndrpihifiberry [snd_rpi_hifiberry_dac], device 0: HifiBerry DAC HiFi pcm5102a-hifi-0 []
Subdevices: 0/1
Subdevice #0: subdevice #0
card 1: rockchipes8316c [rockchip,es8316-codec], device 0: ff880000.i2s-ES8316 HiFi ES8316 HiFi-0 []
Subdevices: 0/1
Subdevice #0: subdevice #0
card 2: HDMICODEC [HDMI-CODEC], device 0: ff8a0000.i2s-i2s-hifi i2s-hifi-0 []
Subdevices: 0/1
Subdevice #0: subdevice #0

no aplay -D# works (like aplay -D0 …)

aplay -Dpulse works (plays through the pulseaudio driver).

pi@master:~$ aplay -L
default
Playback/recording through the PulseAudio sound server
null
Discard all samples (playback) or generate zero samples (capture)
pulse
PulseAudio Sound Server
soft_vol
sysdefault:CARD=sndrpihifiberry
snd_rpi_hifiberry_dac,
Default Audio Device

I’ve been looking over the Alsa docs to try and configure the soft_vol to work with the pulse audio driver and can’t make that work. Nothing I do will play a sound except if I play through -Dpulse.

I’m sure I’m confused and likely merging multiple things that should not be, but this is how the system booted up. I’m OK using anything that will give me I2S audio output and volume control! But I think things are screwed up with multiple drivers and I just haven’t been able to unravel it, yet.

pi@master:~$ pactl load-module module-alsa-sink control=PCM
Failure: Module initialization failed

So it seems I can’t get pulse audio to control volume either. Crazy…

Have you installed pulseaudio-alsa

https://wiki.archlinux.org/index.php/PulseAudio#Expose_PulseAudio_sources,_sinks_and_mixers_to_ALSA

and have a asound.conf something like this

# Use PulseAudio by default
pcm.!default {
  type pulse
  fallback "sysdefault"
  hint {
    show on
    description "Default ALSA Output (currently PulseAudio Sound Server)"
  }
}

ctl.!default {
  type pulse
  fallback "sysdefault"
}

# vim:set ft=alsaconf:

Then reboot or restart pulse audio and pacmd set the default sink/source

Thanks for sticking with me on all of this. I’ve learned a bit more about how this is all supposed to work and have a solution that’s working for me, now. I never could get the amixer stuff working and I think I know (partially) why.

Seems the pulseaudio server has issues when started for a user at system startup (in rc.local for example) and then the user wants to use it from a login shell.

That is, in rc.local I have this:

sudo -i -u pi -- sh -c "aplay -D Pulse /var/tones/beep.wav"

Works, great! Plays a beep and the pulseaudio server is running under user “pi.”

But if I then log in (ssh) to my system and do

aplay -D Pulse /var/tones/beep.wav

Nothing happens. Likewise, if I try to use the pa* controls (pactl, pacmd, etc) they all fail. I have to do

sudo -i -u pi -- sh -c "aplay -D Pulse /var/tones/beep.wav"
or
sudo -i -u pi -- sh -c "pactl set-sink-volume 2 10%"

When I do that, all is fine. Works as expected. So that’s simply what I’m going to do. I have all of this scripted, anyway, so it’s easy for me to change my “beep.sh” script to use sudo. I still don’t know why this is an issue (if I “sudu -u pi” then user pi should be able to use it, I would think. But for another day… I have other things I need to get working, then I can come back and optimize.)

Hope this helps someone else!

pulseaudio should not be started systemwide or via root really.

The services are installed by default

systemctl --user start pulseaudio
systemctl --user enable pulseaudio

Not sure why you are using rc.local as you are prob alone in that matter.

I’m not starting any services, outside of what’s being done by the standard install of the sound system, and doing nothing as root.

I want to play a sound when my system completes booting and I to an “aplay” in rc.local (which I have done for years). I do it with sudo -u pi so it’s not running as root, but as user pi (and it is if I look at the process). But the fact that I did a sudo means that I must then ALWAYS use sudo -u pi to play sounds or adjust the pulseaudio system.

If I do NOT play the sound in rc.local, then I can use the pi login from the command line to do so. But I need to play the startup sound :grinning:

1 Like