Beep on Linux in C
Asked Answered
R

5

16

I want to generate a beep sound with a specific frequency and length (for different sound signals) using the system beeper (and only the speakers if beeper is not available / accessible). I know it is possible to do this by using ioctl, but that requires root access, which I don't want.

I know I could just use the "beep" command, but that would be a dependency, which, if possible, shouldn't be used (no external dependencies at all, just the basic linux libraries and C).

What I currently have is the following code (but this requires superuser privileges to run):

#include <stdlib.h>
#include <fcntl.h>
#include <linux/kd.h>

int main(int argc, char *argv[])
{
 int fd = open("/dev/console", O_RDONLY);
 if (fd == -1 || argc != 3) return -1;
 return ioctl(fd, KDMKTONE, (atoi(argv[2])<<16)+(1193180/atoi(argv[1])));
}

If there is no other way to do this, then I will use beep, but I would really like to avoid dependencies and integrate the beep directly into my script, but I'm sure somebody here will know a solution / workaround.

I don't really want external libraries as the program should be as lightweight as possible.

Renovate answered 9/4, 2012 at 12:3 Comment(5)
If running under a desktop, you could use ALSA. And you could also use the play command ... And some desktops have notifications...Corell
@BasileStarynkevitch Wouldn't that be over-complicating things? I just want a simple beep with a specific frequency and length. As I already told Pablo, I want to keep it as lightweight as possible, but I suppose using beep or suid to gain root access is the way to go, then?Renovate
See if this helps you. @paxdiablo claims it can run from a user account too.Basra
@PavanManjunath I have seen this article before, but it just gives me "Could not open /dev/console for writing. open: Permission denied".Renovate
toot is a cross-platform C file that try to call several sound generators to produce the beepTypo
R
3

I think the only way to do this is to either use suid to give my own program root access, or to use beep, which already has suid. I suppose I will just add one more dependency, then, as beep is not too big anyway.

Thank you for all the answers, I'm sure other libraries are great for more complex signals, but I need a very simple one!

I think this question can be marked as solved / closed, then.

If anybody finds a way to create a beep using the console without superuser-privileges, I'm still interested in this solution :)

Thank you all again.

Renovate answered 9/4, 2012 at 12:34 Comment(0)
P
7

Please look at the standard linux beep source code. http://www.johnath.com/beep/beep.c

It uses KIOCSOUND ioctl to "beep", but you don't need superuser privileges to make it play. I have configured it to be readable and executable by users on the "beep" group.

So my standard user with UID 1000 is in the group with GID 501 (i called it "beep"). Next to this I had to chmod 4750 /usr/bin/beep and now I'm able to play beeps (in the range 20-20000Hz) without asking for superuser privileges.

Phonemic answered 11/6, 2012 at 13:9 Comment(0)
F
4

The most basic beep is still '\a' , if your terminal supports it:

fprintf(stdout, "\aBeep!\n" );
Forejudge answered 9/4, 2012 at 15:2 Comment(2)
Yeah, but that's just one frequency.Renovate
I said it was basic ... Sorry about that.(the reaction might still be usable for people who visit this page afterwards.)Forejudge
R
3

I think the only way to do this is to either use suid to give my own program root access, or to use beep, which already has suid. I suppose I will just add one more dependency, then, as beep is not too big anyway.

Thank you for all the answers, I'm sure other libraries are great for more complex signals, but I need a very simple one!

I think this question can be marked as solved / closed, then.

If anybody finds a way to create a beep using the console without superuser-privileges, I'm still interested in this solution :)

Thank you all again.

Renovate answered 9/4, 2012 at 12:34 Comment(0)
N
1

open("/dev/tty", O_RDONLY); instead /dev/console will work with sound OK for terminal consoles without requiring superuser privileges. But virtual consoles in X session will fail with sound then, even for superuser.

Nocturn answered 21/12, 2019 at 18:20 Comment(1)
There is a beepcommand, like in github.com/NaWer/beep/tree/master/MusicBasir
S
0

Try using an audio library such as OpenAL.

Significs answered 9/4, 2012 at 12:5 Comment(3)
I want to keep the program as lightweight as possibleRenovate
Then just output a bell character \a and leave the sound making to the terminal emulator. Of course, you can't change the pitch and tone of that sound...Corell
@BasileStarynkevitch Yeah, of course that's possible, too, but I want specific frequencies to differ the signals a bit.Renovate

© 2022 - 2024 — McMap. All rights reserved.