Code Comments
Programming Forum and web based access to our favorite programming groups.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?
Post Follow-up to this message"joshua crawford" <jgcrawford@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/
Post Follow-up to this messageGrant Taylor wrote: > "joshua crawford" <jgcrawford@gmail.com> writes: > > > 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?
Post Follow-up to this message> 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
Post Follow-up to this message"joshua crawford" <jgcrawford@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.
--
Grant Taylor
Embedded Linux Consultant
http://www.picante.com/
Post Follow-up to this messageGrant Taylor wrote: > "joshua crawford" <jgcrawford@gmail.com> writes: > > 666), > > 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.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.