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);
}