Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Linux speaker beep
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?


Report this thread to moderator Post Follow-up to this message
Old Post
joshua crawford
09-18-04 01:56 PM


Re: Linux speaker beep
"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/

Report this thread to moderator Post Follow-up to this message
Old Post
Grant Taylor
09-18-04 08:58 PM


Re: Linux speaker beep
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?


Report this thread to moderator Post Follow-up to this message
Old Post
joshua crawford
09-19-04 08:56 AM


Re: Linux speaker beep
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Brian Raiter
09-21-04 02:02 AM


Re: Linux speaker beep
"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/

Report this thread to moderator Post Follow-up to this message
Old Post
Grant Taylor
09-21-04 02:02 AM


Re: Linux speaker beep
Grant 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.


Report this thread to moderator Post Follow-up to this message
Old Post
joshua crawford
09-21-04 09:00 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:14 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.