Volume control node-red-contrib-sox-utils

Is there a possibility to control the volume of the audio played by the sox-play node of node-red-contrib-sox-utils with a msg?
I know I can change the gain in dB in the Output settings but I want to control the volume with a slider in the dashboard. Therefore I want to control this with a msg.

Hello @Janik ,
I’m the author of node-red-contrib-sox-utils. Unfortunately this is not possible as sox doesn’t support gain changes at runtime. This is also not the use case of gain.
What you are looking for is to change the actual output volume of your system.
The easiest way to do this would be to use an exec node and the amixer command line Tool.

On a raspberry pi for example the default soundcard is card 0 and the output is called Headphones. So to change output volume with an exec node you would use the following command to set the default output to 50%:

amixer -c 0 sset Headphone 50%

Here is a simple example flow for this:

[{"id":"efc34c235ac8c50f","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"ac97d4b459aea873","type":"inject","z":"efc34c235ac8c50f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"50","payloadType":"str","x":190,"y":140,"wires":[["ab9a888f29d5340c"]]},{"id":"b2d7ab7907288340","type":"inject","z":"efc34c235ac8c50f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":190,"y":200,"wires":[["ab9a888f29d5340c"]]},{"id":"a378ddaeb2cb964c","type":"inject","z":"efc34c235ac8c50f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"75","payloadType":"str","x":190,"y":260,"wires":[["ab9a888f29d5340c"]]},{"id":"41bc2ece79c1d4d6","type":"exec","z":"efc34c235ac8c50f","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":530,"y":200,"wires":[["857140897d230d1f"],[],[]]},{"id":"ab9a888f29d5340c","type":"template","z":"efc34c235ac8c50f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"amixer -c 0 sset Headphone {{payload}}%","output":"str","x":380,"y":200,"wires":[["41bc2ece79c1d4d6"]]},{"id":"857140897d230d1f","type":"debug","z":"efc34c235ac8c50f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":200,"wires":[]}]

This should give you the general idea. Just connect a dashboard slider instead of the injects and you have a volume control.
If you have a different soundcard as an output you will have to adapt both the card number and the name of the property to change accordingly.

I hope this helps and best regards Johannes

Edit:
You should probably use the mapped Volume for a more natural adjustment.
Use:

amixer -c 0 sset Headphone -M 50%

with the additional -M flag. You can add it in the template node in the example above.

1 Like

Hallo @JGKK ,
thank you for the suggestion. But I found out with alsamixer that my soundcard doesn't have any volume controls:
"This sound decvice does not have any controls."
I am using a Focusrite Scarlett Solo 3rd Gen. Is there another possibility to control the volume?

That is probably because your interface has its own hardware volume gain adjustment on the interface or the supplied driver simply doesn't support it. You can do some Alsa trickery though. You will need to add a virtual softvol device that has your device as a slave to your alsa configuration file.
The process is described here:

You than use this virtual device for sox and the exec volume control.

Johannes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.