(SOLVED) How to lower application volume through cron!

Questions about applications and software
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
ericramos1990

(SOLVED) How to lower application volume through cron!

Post by ericramos1990 »

Hi guys, if you were to go to your sound settings, you will see a tab called "Applications"

Here you can control the volume of individual applications.

Does anyone know how to do the same thing, but control the application volumes through a terminal command?

Thanks in advance!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 7 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
donalduck
Level 4
Level 4
Posts: 243
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: How to control application volume through a command?

Post by donalduck »

Hi,

I suppose your sound device is managed by pulseaudio and you are the process owner:

Code: Select all

ps -ef | grep [p]ulse
if it is, then you can use CLI program "pacmd", have a look at man page pulse-cli-syntax for complete syntax.

basic usage below, first identify one available sink(s) via "pacmd list-sink-inputs", then for ex. set the volume at 50% for the sink num 0.

Code: Select all

pacmd set-sink-volume 0 32768
PS1: volume must be between 0 and 65536

PS2: if you don't use pulse, you may have a look & install "aumix" and "alsamixer" programs.
ericramos1990

Re: How to control application volume through a command?

Post by ericramos1990 »

Hey donaldduck thanks a lot for the response

This is my output for pacmd set-sink-volume 0 32768

Code: Select all

Welcome to PulseAudio! Use "help" for usage information.
>>> 2 sink input(s) available.
    index: 67
	driver: <protocol-native.c>
	flags: 
	state: RUNNING
	sink: 1 <alsa_output.pci-0000_00_14.2.analog-stereo>
	volume: 0: 100% 1: 100%
	        0: 0.00 dB 1: 0.00 dB
	        balance 0.00
	muted: no
	current latency: 486.69 ms
	requested latency: 20.00 ms
	sample spec: s16le 2ch 44100Hz
	channel map: front-left,front-right
	             Stereo
	resample method: (null)
	module: 9
	client: 63 <ALSA plug-in [plugin-container]>
	properties:
		media.name = "ALSA Playback"
		application.name = "ALSA plug-in [plugin-container]"
		native-protocol.peer = "UNIX socket client"
		native-protocol.version = "28"
		application.process.id = "5162"
		application.process.user = "eric"
		application.process.host = "ericslinuxmint16"
		application.process.binary = "plugin-container"
		application.language = "en_US.UTF-8"
		window.x11.display = ":0"
		application.process.machine_id = "ded3b767539af142703199c6528836a4"
		application.process.session_id = "c1"
		module-stream-restore.id = "sink-input-by-application-name:ALSA plug-in [plugin-container]"
    index: 74
	driver: <protocol-native.c>
	flags: VARIABLE_RATE START_CORKED 
	state: RUNNING
	sink: 1 <alsa_output.pci-0000_00_14.2.analog-stereo>
	volume: 0: 100% 1: 100%
	        0: 0.00 dB 1: 0.00 dB
	        balance 0.00
	muted: no
	current latency: 386.69 ms
	requested latency: 36.00 ms
	sample spec: float32le 2ch 44100Hz
	channel map: front-left,front-right
	             Stereo
	resample method: speex-float-1
	module: 9
	client: 68 <VLC media player>
	properties:
		media.name = "audio stream"
		application.name = "VLC media player"
		native-protocol.peer = "UNIX socket client"
		native-protocol.version = "28"
		application.id = "org.VideoLAN.VLC"
		application.version = "2.0.8"
		application.icon_name = "vlc"
		application.language = "en_US.UTF-8"
		application.process.id = "5130"
		application.process.machine_id = "ded3b767539af142703199c6528836a4"
		application.process.session_id = "ded3b767539af142703199c6528836a4-1384876322.253512-38616424"
		application.process.user = "eric"
		application.process.host = "ericslinuxmint16"
		application.process.binary = "vlc"
		window.x11.display = ":0"
		module-stream-restore.id = "sink-input-by-application-id:org.VideoLAN.VLC"
I have vlc open and firefox open, what do I choose from the output to set the sink volume?

PS I use the code

Code: Select all

amixer sset 'Master' 71% &> /dev/null &
to set the master volume if that somehow helps
donalduck
Level 4
Level 4
Posts: 243
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: How to control application volume through a command?

Post by donalduck »

Im am no sound expert but I think the number of your target sink is 1. the number aside sink:
(the manual say you can also use symbolic notation)

Code: Select all

pacmd list-sink-inputs
so the command to set sound volume at 50% will be like:

Code: Select all

pacmd set-sink-volume 1 32768
ericramos1990

Re: How to control application volume through a command?

Post by ericramos1990 »

Hey donalduck!
pacmd set-sink-volume 1 32768
worked!

However, I would like to adjust specific programs, not the master volume. Any other suggestions? O.o
donalduck
Level 4
Level 4
Posts: 243
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: How to control application volume through a command?

Post by donalduck »

ok I read the manual for you :roll:
and I found that the argument to change individual volumes was "set-sink-input-volume"

so first identify the index number of the relevant sink (aside index: ) you want to modify :

Code: Select all

pacmd list-sink-inputs
let's say it's 18, then you can mute this sink volume :

Code: Select all

pacmd set-sink-input-volume 18 0
magical :)
ericramos1990

Re: How to control application volume through a command?

Post by ericramos1990 »

Ahhh that's perfect! :D :D

Thanks dude!

However, the number changes everytime even if it is the same program. I'm going to want to have it be consistent.

I'll figure something out, however, it will be in a couple of days since I have a lot of schoolwork to attend to!

I'll let you know if I find anything soon, and if you find something please let me know! :D
donalduck
Level 4
Level 4
Posts: 243
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: How to control application volume through a command?

Post by donalduck »

I'm pretty sure the cinamon "sound" graphical application is just a presentation layer, and that every relevant info in here is also extracted by pacmd command.
so if there is an invariant in the graphical panel, there should be also the same invariant in pacmd extraction and then you could build a command or script around pacmd.

for exemple this one-liner packs the sink index number and matching application name in the same line...

Code: Select all

pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p' -e 's/^\t*application.name = "\(.*\)"$/\1/p' | sed 'N;s/\n/ /'
thus will be very easy to change volume for all sinks with same application.name for ex.

Now I'm curious about the reason why you want to control multiple sounds with command line(s) ?
ericramos1990

Re: How to control application volume through a command?

Post by ericramos1990 »

Hey donalduck!

My apologies for taking so long, my last weeks of school are getting hectic!

To answer your question, there are a variety of things I would like to do!

I have a cron job to play .wav sounds of the time every 00 and 30 minutes! (I got rid of all clocks/alarms in my room to save space/electricity!)

When this plays, sometimes I can't hear the notification because I usually have music blasting, or a loud youtube video playing, etc.

I would like to simply have my time notifications play, while everything else "hides away" with like 25-50% volume, so I can hear it consistently!

Not a huge deal, but it would be really cool to implement!
ericramos1990

Re: How to control application volume through a command?

Post by ericramos1990 »

donalduck I figured it out!

Here is what I used:

Code: Select all

pacmd set-sink-input-volume $(pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p' -e 's/^\t*= "\(.*\)"$/\1/p' | sed 'N;s/\n/ /') 32768


/home/eric/scripts/crontime

pacmd set-sink-input-volume $(pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p' -e 's/^\t*= "\(.*\)"$/\1/p' | sed 'N;s/\n/ /') 65536
Nice and simple! Thanks a lot!

I mentioned a little while ago that there is a variety of things I wanted to do, there is one more thing that would like to learn to do, I hope you can help if possible =)

I am still having trouble targeting one specific program, I want to "hide away" vlc to 1/4 volume when I open up my firefoxyoutube script, but I still can't figure out how to do that.

When I run

Code: Select all

pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p' -e 's/^\t*application.name = "\(.*\)"$/\1/p' | sed 'N;s/\n/ /'
I get

Code: Select all

2986 ALSA plug-in [plugin-container]
2987 VLC media player
But how can I get it to choose the ID of VLC? O.o

Please lead me in the right direction if you can =)
donalduck
Level 4
Level 4
Posts: 243
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: (1/2 SOLVED)Command to control individual application vo

Post by donalduck »

Hello,
quick and dirty ^ ^ you could add another sed command at the end of previously quoted

Code: Select all

 | sed -n 's/ VLC media player//p'

by the way to extract all numbers of running (or suspended) sinks is easier with

Code: Select all

pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p'
didn't know pacmd could handle multiple sink numbers within the same command line, if you need multiple commands, think about using xargs.
just combine previous command with xargs to have a list of individual commands:

Code: Select all

pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p' | xargs -I :: pacmd set-sink-input-volume :: 32768
ericramos1990

Re: (1/2 SOLVED)Command to control individual application vo

Post by ericramos1990 »

Hi Donalduck!

I just want to say thank you for all of your help!

It was really easy to tweak VLC, but I had a lot of trouble tweaking Firefox sound.

I suppose it is because Firefox is not listed as its own dedicated application, and pacmd lists Firefox as "ALSA plug-in [plugin-container]"

I think the problem was that it had brackets, and I wasn't able to learn how to bypass them, but I found an alternative:

Code: Select all

pacmd set-sink-input-volume $(pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p' -e 's/^\t*application.name = "\(.*\)"$/\1/p' | sed 'N;s/\n/ /'  | sed -n 's/ ALSA plug-in//p' | sed 's/[^0-9]*//g') 65536
The last sed part just extracts the numbers, which was all that I needed for the application ID! I'm sure I could have done a better job but it works!

and for VLC I used what you gave me =D

pacmd set-sink-input-volume $(pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p' -e 's/^\t*application.name = "\(.*\)"$/\1/p' | sed 'N;s/\n/ /' | sed -n 's/ VLC media player//p') 65536

Of course the volumes aren't going to be 65536 that would make this entire thing obsolete lol

You heavily influenced me to learn the sed command at least to an ameteurish level that I can stand on my own two feet. It looked so hard at first but I am able to grasp at least a little now! Thank you so much for helping a random stranger, people like you are the reason why I love Linux and the Linux community so much!

I was also able to make a master volume script, and a script to decrease the brightness of my monitor even further than it's settings allows me using xrandr.

Let me know if you would like to look at those for whatever reason

(I am a health nut and looking a bright lights before bedtime affects the circadian rhythm, ruining your sleep! So I dim my screen at night time so I can sleep better)
ericramos1990

Re: (SOLVED)Command to control individual application volume

Post by ericramos1990 »

This is me right now:
http://www.youtube.com/watch?v=WWaLxFIVX1s


The main reason why I wanted to do this is for a cronjob/tab function for a clock that announces the time every 30 minutes!

I want my applications to "fade away" a bit while the time is being said.

Unfortunately, the script works flawlessly on its own, but it doesn't work as a crontab!

Here is the script:

Code: Select all

#Lowers the volume of VLC and Firefox to half level
pacmd set-sink-input-volume $(pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p' -e 's/^\t*application.name = "\(.*\)"$/\1/p' | sed 'N;s/\n/ /'  | sed -n 's/ VLC media player//p') 32768

pacmd set-sink-input-volume $(pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p' -e 's/^\t*application.name = "\(.*\)"$/\1/p' | sed 'N;s/\n/ /'  | sed -n 's/ ALSA plug-in//p' | sed 's/[^0-9]*//g') 32768

aplay /command/that/says/the/time

#Puts sound volumes back to normal 
pacmd set-sink-input-volume $(pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p' -e 's/^\t*application.name = "\(.*\)"$/\1/p' | sed 'N;s/\n/ /'  | sed -n 's/ VLC media player//p') 65536

pacmd set-sink-input-volume $(pacmd list-sink-inputs | sed -n -e 's/^ *index: \(.*\)$/\1/p' -e 's/^\t*application.name = "\(.*\)"$/\1/p' | sed 'N;s/\n/ /'  | sed -n 's/ ALSA plug-in//p' | sed 's/[^0-9]*//g') 65536

Everything works, except for the application volumes lowering.


Anyone have any suggestions?

It's definitely a cron problem
ericramos1990

Re: (SOLVED)Command to control individual application volume

Post by ericramos1990 »

I'm thinking maybe cron needs to know what sound card to use?

Such as for GUI apps, you need to tell it DISPLAY=:0

Does anyone know the equivilent of DISPLAY=:0 but for sound cards?
donalduck
Level 4
Level 4
Posts: 243
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: How to lower application volume for cronjobs?

Post by donalduck »

thanks, you are welcome.

so the force is with you, almost ...

next step is to debug crontab job looking at logs (maybie in /var/log), or changing command so you can know command exit code and error message(s) ...

one way to do that is by creating a short script and have cron run that script, example below

Code: Select all

#!/bin/sh
if my-command  2>&1 1>> /tmp/my-command-log; then
  do-something;
else
  do-other-thing;
fi
consider also that cron generally does not inherit user full environnement variables...

another tip with crontab is to use MAILTO="ad@mail" at the very top of the crontab with your personal email (localhost mail will be better)
you will be mailed with cron jobs output.

Code: Select all

man 5 crontab
ericramos1990

Re: How to lower application volume for cronjobs?

Post by ericramos1990 »

Hey dd!

I tried many cron fixes regarding setting the shell and environment, but I don't think it's that anymore.

I discovered that under cron if I output "pacmd list-sink-inputs" it thinks there are 0 inputs

Code: Select all

Welcome to PulseAudio! Use "help" for usage information. >>> 0 sink input(s) available. >>>
That is the problem!

Would you know how to possibly get cron to understand that I DO have "pacmd list-sink-inputs" ?

Is that still considered a cron environment problem?

Thanks I hope you can help, if not I understand =)
donalduck
Level 4
Level 4
Posts: 243
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: How to lower application volume for cronjobs?

Post by donalduck »

strange, I just tried running your pacmd list-sink-inputs command in my user cron table, and it works ^^

if music is played under distinct user account(s) than cron job, then I guess there will be no input ... hope this help you.
ericramos1990

Re: How to lower application volume for cronjobs?

Post by ericramos1990 »

Well at least it works for one of us :lol:

That's strange, I am the only user for my pc

I am running my programs regularly

Do I need to run anything as root or something?

I have tried altering the following code in different ways, but no luck:

Code: Select all

* * * * * pacmd list-sink-inputs > ~/desktop/test.txt
No matter what I try, I still get:

Code: Select all

    Welcome to PulseAudio! Use "help" for usage information. >>> 0 sink input(s) available. >>>
How are you setting up your cron that is superior to mine? :cry:
donalduck
Level 4
Level 4
Posts: 243
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: How to lower application volume for cronjobs?

Post by donalduck »

I don't think so. to be very precise I set myself in home dir, edited a raw text file with vi, and used

Code: Select all

crontab my-text-file
do not use sudo, and make sure a music is playing while the pacmd runs of course :wink:
ericramos1990

Re: How to lower application volume for cronjobs?

Post by ericramos1990 »

DD, I found the solution!

I need to head to class, but I'll tell you my results later!

Thanks for helping me from the beginning :D
Locked

Return to “Software & Applications”