> 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/
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?
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
> 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.
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.