Home > Archive > Unix Programming > September 2004 > Linux speaker beep
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Linux speaker beep
|
|
| joshua crawford 2004-09-18, 8:56 am |
| 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 2004-09-18, 3:58 pm |
| "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/
| |
| joshua crawford 2004-09-19, 3:56 am |
| Grant 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?
| |
| Brian Raiter 2004-09-20, 9:02 pm |
| > 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 2004-09-20, 9:02 pm |
| "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/
| |
| joshua crawford 2004-09-21, 4:00 am |
| Grant Taylor wrote:
> "joshua crawford" <jgcrawford@gmail.com> writes:
>
>
666),[color=darkred]
>
> 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.
|
|
|
|
|