Believe it or not, your PC speaker is part of the Linux console and thus a character device. Therefore, ioctl() requests exist to manipulate it. For the internal speaker the following 2 requests exist:
Generates a beep for a specified time using the kernel timer.
Example: ioctl (fd, KDMKTONE,(long) argument).
Generates an endless beep or stops a currently sounding beep.
Example: ioctl(fd,KIOCSOUND,(int) tone).
The argument consists of the tone value in the low word and
the duration in the high word. The tone value is not the
frequency. The PC mainboard timer 8254 is clocked at 1.19 MHz and so
it's 1190000/frequency. The duration is measured in timer ticks. Both
ioctl calls return immediately so you can this way produce beeps
without blocking the program.
KDMKTONE should be used for warning signals because you don't have to worry about stopping the tone.
KIOCSOUND can be used to play melodies as demonstrated in the example program splay (please send more .sng files to me). To stop the beep you have to use the tone value 0.