Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Linux speaker beep

280 views
Skip to first unread message

joshua crawford

unread,
Sep 18, 2004, 4:27:36 AM9/18/04
to
G'day,
I'm writing a ringtone manager for nokia ringtones and I'd like to be
able to play the ringtone on the PC speaker. I've had some success in
DOS with turbo C and its sound(), delay() and nosound(). Is there
anything similar for Linux? I know I can make a simple beep with '\a',
but that's not what I need. If not, can someone please show me how to
do this with the /dev/audio device?

Grant Taylor

unread,
Sep 18, 2004, 1:03:23 PM9/18/04
to
"joshua crawford" <jgcra...@gmail.com> writes:

> I'm writing a ringtone manager for nokia ringtones and I'd like to
> be able to play the ringtone on the PC speaker. I've had some
> success in DOS with turbo C and its sound(), delay() and
> nosound(). Is there anything similar for Linux?

If you just want to make beeps in various pitches and durations, there
are ioctls to do this with the PC hardware beeper. "KDMKTONE" will
beep for a specified length of time at a particular frequency. The
driver code is in drivers/char/vt.c.

It's not obvious what happens if there is no vga console but there is
still a beeper.

Example:

#include <linux/kd.h>

static void pc_beep(int freq) {
int fd;

/* Try both std and devfs name */
fd = open("/dev/tty1", O_RDWR|O_NONBLOCK);
if (fd < 0) {
fd = open("/dev/vc/1", O_RDWR|O_NONBLOCK);
}
if (fd >= 0) {
ioctl(fd, KDMKTONE, ((75/*ms?*/ << 16) | freq));
}
}

--
Grant Taylor
Embedded Linux Consultant
http://www.picante.com/

joshua crawford

unread,
Sep 19, 2004, 1:42:54 AM9/19/04
to
Grant Taylor wrote:
> "joshua crawford" <jgcra...@gmail.com> writes:
>
> > I'm writing a ringtone manager for nokia ringtones and I'd like to
> > be able to play the ringtone on the PC speaker. I've had some
> > success in DOS with turbo C and its sound(), delay() and
> > nosound(). Is there anything similar for Linux?
>
> If you just want to make beeps in various pitches and durations,
there
> are ioctls to do this with the PC hardware beeper. "KDMKTONE" will
> beep for a specified length of time at a particular frequency. The
> driver code is in drivers/char/vt.c.
>
> It's not obvious what happens if there is no vga console but there is
> still a beeper.
>
> Example:

Thanks. Based on your example (and others like it I've since found
online), I've added the following code:
void beep(int freq, int duration) {
int fd;
fd = open("/dev/tty1", O_WRONLY|O_NONBLOCK);
if (fd >= 0) {
ioctl(fd, KDMKTONE, (duration << 16) | (freq?(1193180 /
freq):0));
usleep(duration * 1000);
ioctl(fd, KDMKTONE, 0);
close(fd);
}
}

It works, if I run it as root. As a normal user there is no sound. I've
added the user to group tty (/dev/tty1 is root tty 666), but it still
doesn't work. Is there a way I can make this work as a normal user?

Brian Raiter

unread,
Sep 20, 2004, 7:52:41 PM9/20/04
to
> It works, if I run it as root. As a normal user there is no
> sound. I've added the user to group tty (/dev/tty1 is root tty 666),
> but it still doesn't work. Is there a way I can make this work as a
> normal user?

If you're running in a linux console (as opposed to e.g. an xterm),
the console understands the following control code sequences:

ESC [ 10 ; <n> ] = set bell frequency to <n> Hz.
ESC [ 11 ; <n> ] = set bell duration to <n> msec.

So in theory you could just use the following macro to play a given
tone:

#define note(hz, ms) (printf("\033[10;%d]\033[11;%d]\a", (hz), (ms)))

b

Grant Taylor

unread,
Sep 20, 2004, 8:12:53 PM9/20/04
to
"joshua crawford" <jgcra...@gmail.com> writes:

> fd = open("/dev/tty1", O_WRONLY|O_NONBLOCK);

> ioctl(fd, KDMKTONE, (duration << 16) | (freq?(1193180 /

> It works, if I run it as root. As a normal user there is no


> sound. I've added the user to group tty (/dev/tty1 is root tty 666),
> but it still doesn't work. Is there a way I can make this work as a
> normal user?

Hmm, what fails, the open or the ioctl? If it's the open, then it's
the tty permissions (possibly including /dev/console as opposed to
/dev/tty1) that need adjusting. If it's the ioctl, then the thing
might just have a root-only clause in the driver ioctl method. You
would have to whack that in the driver source.

joshua crawford

unread,
Sep 21, 2004, 12:25:20 AM9/21/04
to
Grant Taylor wrote:
> "joshua crawford" <jgcra...@gmail.com> writes:
>
> > fd = open("/dev/tty1", O_WRONLY|O_NONBLOCK);
> > ioctl(fd, KDMKTONE, (duration << 16) | (freq?(1193180 /
>
> > It works, if I run it as root. As a normal user there is no
> > sound. I've added the user to group tty (/dev/tty1 is root tty
666),
> > but it still doesn't work. Is there a way I can make this work as a
> > normal user?
>
> Hmm, what fails, the open or the ioctl?

The ioctl(). The error (from perror()) is "Operation not permitted".

> If it's the open, then it's
> the tty permissions (possibly including /dev/console as opposed to
> /dev/tty1) that need adjusting.

I tried using (and played with the permissions on) /dev/console,
/dev/tty and /dev/tty1.

> If it's the ioctl, then the thing
> might just have a root-only clause in the driver ioctl method. You
> would have to whack that in the driver source.

If that's the only solution, I think I'll just run it with sudo.

0 new messages