4

I am trying to get audio information like volume for a C program in OpenBSD. Via shell the command would be

mixerctl outputs.master

But how can I get that in C? So far I have only found something similar in the audio(4) manpage, but I couldn't get that to work (I am not good at C):

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/audioio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    audio_info_t *info;
    int fd;

    fd = fopen("/dev/audioctl", "r");
    if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
        fprintf(stderr, "%s\n", strerror(errno));
    ...
}

gives me Inappropriate ioctl for device. What am I doing wrong? Is this the right way to get the volume?

Solution:

My fault appears to be a mixture of opening the file incorrectly and handing over the info variable. Both rooted in me getting confused with pointers... Here is how I got it to work:

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/audioio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    audio_info_t info;
    FILE *fd;

    fd = fopen("/dev/audioctl", "r");
    if (ioctl(fileno(fd), AUDIO_GETINFO, &info) < 0)
        fprintf(stderr, "%s\n", strerror(errno));

    printf("%d", info.play.gain);
    fclose(fd);
}
tls
  • 41
  • 5
  • GCC has a `-Wall` switch to warn if your code tries to do these kinds of things (other compilers probably do as well). I always use it when compiling code to avoid silly errors. Compiling your original code with it gives a warning like this: `audio.c:12:8: warning: incompatible pointer to integer conversion assigning to 'int' from 'FILE *' (aka 'struct __sFILE *')` – jacwah Jun 12 '15 at 16:27
  • 1
    `mixerctl` and `audioctl` are originally written in c, and only about 400 lines each - if you have openbsd base sources (which i'd recommend), you can find them in `/usr/src/usr.bin/`, [or online](http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/audioctl/audioctl.c?rev=1.28) – pete Jun 19 '15 at 17:55
  • yes, thanks. Since changes in `audio(4)` broke this solution, seeing how `mixerctl` did it helped a lot with fixing/rewriting. – tls Nov 25 '15 at 21:01

0 Answers0