shell script for volume control

Questions about codecs, DVD playback, web plugins...
Forum rules
Before you post please read how to get help
throatgorge
Level 1
Level 1
Posts: 45
Joined: Fri Mar 28, 2014 1:01 pm

shell script for volume control

Postby throatgorge » Fri Jun 16, 2017 4:57 pm

System: Linux Mint 18.1 xfce
I had a soundcard that was supposed to last twelve years but lasted 20. Anyhow I replaced it, and the volume control fader has no effect. I got a 50 watt per channel receiver, insomnia and two toddlers, so volume control is VERY important around here and I often tweek volume control from all three perimeters, the knob on my receiver, the fader, and the media fader.
Now I have to open my "sound settings" tab over to "output devices," and scroll down to my sound card to adjust the volume. Being able to do this quickly is sometimes the difference between relaxing to some media before getting sleep and spending another hour or so putting babies back to sleep.
If I leave the volume control applet open, I can access this with no trouble, but I often reboot my computer because I dual boot into windows XP to use my legacy audio software which I've recorded music with for around 20 years.
I've been experimenting with writing shell script to get a one click operation; I use redshift at night to darken my monitor and disrupt the blue light that keeps my brain from forming melatonin. I got sick of opening the terminal, typing in "redshift" and then entering the command, so I wrote all of that down in a text file, saved it with the extension .sh, and made it executable. Presto! OK, so my question is, what commands and what syntax do I have to use in order to create a shell that can execute this operation from a single command shell on my desktop? I am still not familiar enough with terminal commands although before I used windows in the early 90's I was operating my computer using MSDOS commands, and later did essentially the same things by writing bash files in MSDOS with windows notepad.

rene
Level 6
Level 6
Posts: 1338
Joined: Sun Mar 27, 2016 6:58 pm

Re: shell script for volume control

Postby rene » Sat Jun 17, 2017 5:06 am

The base of the Linux sound software stack is formed by ALSA, the Advanced Linux Sound Architecture, with these days normally a midlevel layer on top in the form of Pulseaudio; you can arrange for volume adjustment at both levels; should preferably do so at the Pulseaudio one.

Pulseaudio makes available the pactl tool, which you can use in the form of, say,

Code: Select all

pactl set-sink-volume 0 0
pactl set-sink-volume 0 50%
pactl set-sink-volume 0 +10%
pactl set-sink-volume 0 -10%

In Mint 17.3 pactl has a bug that necessitates writing that last example as pactl -- set-sink-volume 0 -10% instead; would not know if the Mint 18 version still has the issue; see https://bugs.freedesktop.org/show_bug.cgi?id=77108. The first 0 in those commands specifies the sink, the sound card, and is automatically correct if you have only one card installed. Otherwise decide which sink to use from the output of pactl list sinks. Use man pactl for further information.

At the ALSA level you have available the amixer command line mixer. For example,

Code: Select all

amixer set Master 0
amixer set Master 50%
amixer set Master 10%+
amixer set Master 10%-

In this, not specifying a card number with -c <number> means card 0; if not, use aplay -l to identify the number. "Master" is the name of a generally available control; you can list available controls with amixer scontrols and learn more from man amixer.

You'd as said preferably use the Pulseaudio level, even if only since it's installed anyway; becomes by that simple fact the preferred adjustment level for the user.

throatgorge
Level 1
Level 1
Posts: 45
Joined: Fri Mar 28, 2014 1:01 pm

Re: shell script for volume control

Postby throatgorge » Sat Jun 17, 2017 7:47 pm

thanks, I tried running this commands in the terminal, and there was no effect on the volume.

rene
Level 6
Level 6
Posts: 1338
Joined: Sun Mar 27, 2016 6:58 pm

Re: shell script for volume control

Postby rene » Sat Jun 17, 2017 8:37 pm

The only likely reason for that is that you are not specifying the correct sink for pactl or card/device/control for amixer. Post the mentioned outputs of pactl list sinks and aplay -l. Note also that if your outputs are digitally connected indeed no volume control is available from ALSA; that Master control controls analogue, S/PDIF is at the ALSA level only mutable, not volume-controllable.


Return to “Sound, Multimedia, & Codecs”