Score:1

Where is the volume-file (sound)?

si flag

I have a script that prints the volume of my laptop, which is really convoluted and slow:

/bin/sh -c \"amixer get Master "
                                       "| tail -n1 "
                                       "| grep -Po '\\[\\K[^%]*' "
                                       "| head -n1\""

In Unix, so I assume Linux as well, everything is a file. So where is the file holding the volume, so I can just cat that? Like

/sys/class/backlight/acpi_video0/brightness
guiverc avatar
cn flag
Please provide an OS/release detail; SE *Unix & Linux* covers Linux (*Unix too*) so why are you asking here? Do note: how *apps* see the root file-system varies; so product & release details matters with Ubuntu due to *confinement* & other differences on some products.
cn flag
Why the need for a file? A function is all you need and a lot more stable.
sadkfdsfjklsdfjklds avatar
si flag
@Rinzwind is it though? You can read the file in any Linux distribution (I am including future versions of Ubuntu in this), as long as future kernels don't handle it differently. The script I use or you provided will break whenever amixer or awk breaks or is not installed. Also, performance. I am querying this 10 times per second on old hardware.
cn flag
@sadkfdsfjklsdfjklds see my edit. Looks like the volume is retrieved from the device using ioctl. How's your C?
sadkfdsfjklsdfjklds avatar
si flag
@Rinzwind Thank you! I use C for pretty much everything, so should be able to figure it out.
Score:1
cn flag

There is already a tool for that and it is available on all Ubuntu. Your command can be used like this:

amixer sget Master | awk -F"[][]" '/Left:/ { print $2 }'

for a system that has stereo to probe the LEFT speaker. Other options are "Right" and "Mono" and it will show the volume as a percentage (100%). Just put that result in a variable.


To fix guiverc's comment:

amixer get Master | egrep -o '[0-9]{1,3}%'

will show all percentages for all lines (if 1 percentage it is Mono, otherwise Left, Right in that order)


To address the "file" method:

From what I see in the source code /dev/dsp is probed using ioctl.

#include <sys/ioctl.h>

 int ioctl(int fd, unsigned long request, ...);

The ioctl() system call manipulates the underlying device parameters of special files. In particular, many operating characteristics of character special files (e.g., terminals) may be controlled with ioctl() requests. The argument fd must be an open file descriptor.

The second argument is a device-dependent request code. The third argument is an untyped pointer to memory. It's traditionally char *argp (from the days before void * was valid C), and will be so named for this discussion.

An ioctl() request has encoded in it whether the argument is an in parameter or out parameter, and the size of the argument argp in bytes. Macros and defines used in specifying an ioctl() request are located in the file <sys/ioctl.h>. See NOTES.

But my C knowledge is very limited ;)

cn flag
@guiverc is this edit better? :)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.