Beep! (How to make)

Is there some simple cross-platform way to play a simple "beep' when the user types something wrong? I don't want to set up an entire audio library and a set of .mp3 or .wav files. Just trigger a system "beep". Or the appropriate system notification sounds for the platform.

2 Likes

For Windows.

Depending on your use case printing '\x07' might be good enough.

7 Likes

Right. I'm hoping that someone wrote a crate with platform support for various platforms - Windows, Linux, Mac. I've been able to find good cross-platform Rust crates for almost everything, all the way up to full 3D graphics. So I have almost no platform-conditional configuration in my code. This makes testing simpler.

GUI program. No text console.

There's the crate "beep", which plays fixed tones cross-platform.

Ideally, I'd like cross-platform access to the system sounds of the platform. Linux, Windows, and Mac all have systems for making common sounds easily. Somebody must have made a cross-platform wrapper for those by now. Maybe as part of something else larger.

I don't even know if my system (FreeBSD plus awesome wm) has a "system sound". :thinking: Under Linux/BSD, does a "default" sound exist, or is it created by the corresponding terminal emulation and not standardized? It might be different when you have a KDE or GNOME application, I guess, but I don't really know,.

It's part of the console (ioctl) at the kernel level. It's also present at the terminal level (which is how they can suppor thiings like visual beeps). DM-integrated terminal programs typically tie it into the DM, naturally.

On teletypes it would literally ring a bell.

Unfortunately not every Linux or macOS system is attached to teletype these days.

And on Linux they changed the way beeps are done recently.

1 Like

I assumed you would not find a cross platform crate that does what you want.

My post was not meant to be a solution but, hopefully, a helpful hint to someone willing to build a solution.

The next billion-dollar startup will probably sell physical bells attached via USB to the computer, to those of us missing the 70s.

3 Likes

But it's not an ioctl call for tty's, right? I searched the manpage and didn't find anything. Is it just interpreting 0x07 characters, but doing so at the kernel level when writing it to the console?

Kernel interpretes 0x07, but there are more ioctls to drive PC speaker. They are described in the appropriate manual.

Windows has it's own FILE_DEVICE_BEEP (not documented very well: it's present in headers and you can find it in list of devices, and it works, but I coulnd't find actual documentation about how you are supposed to use it).

The whole thing is convoluted enough that if you want to do beep from GUI app you can not use 0x07, you need to dig into bazillion ways to make a sound in various environments (there are three or four ways in Windows, dozen in Linux and I don't know how many on macOS).

3 Likes

The key to this is "cross platform". I can only rarely get a test on a Mac, and I run Windows tests under Wine. So I'm looking for something I can use without spending a lot of time chasing down test systems for something little like this.

Each tty can do what it wants.

But where is the kernel involved?

Maybe it calls an ioctl, maybe it plays an audio file via Pulse or ALSA or PipeWire, maybe it dispatches a command to the display manager (which in turn plays an audio file, or not), maybe it flashes the screen instead of making sound, maybe it does nothing at all...

But I didn't find an ioctl call which performs the sound (or rings the bell). That was my question. Anyway, not a problem, I was just curious how it works.

Oh! See KDMKTONE and KIOCSOUND e.g. here. (Discourse is kindly telling me @VorfeedCanal already linked that page.)

"Beep" shows up in my alsa channels, so that would be another way.

Looks like setterm sets defaults indirectly through some "ANSI"-like codes described in section 4 here, thought that doesn't make sound on its own.

My current terminal doesn't respect either of those and probably just dispatches some display manager level event.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.