[Previous]
[Contents]
[Next]

sound()

turn on the PC's speaker, at a given frequency

Synopsis:

#include <i86.h>
void sound( unsigned frequency );

Description:

The sound() function turns on the PC's speaker at the specified frequency. The frequency is in Hertz (cycles per second). The speaker can be turned off by calling the nosound() function after an appropriate amount of time.

When you use the sound() function, your program must be linked for privity level 1, and the process must be run by the superuser. For more information on privity, see

Examples:

#include <i86.h>

/*
    The numbers in this table are the timer divisors
    necessary to produce the pitch indicated in the
    lowest octave that is supported by the sound()
    function.

    To raise the pitch by N octaves, simply divide the
    number in the table by 2 to the power of N since a
    pitch that is an octave above another has double
    the frequency of the original pitch.

    The frequency obtained by these numbers is given by
    1193180 / X where X is the number obtained in the
    table.
*/

unsigned short Notes[] = {
    19327 ,        /* B             */
    18242 ,        /* C             */
    17218 ,        /* C #  ( D b )  */
    16252 ,        /* D             */
    15340 ,        /* D #  ( E b )  */
    14479 ,        /* E             */
    13666 ,        /* F             */
    12899 ,        /* F #  ( G b )  */
    12175 ,        /* G             */
    11492 ,        /* G #  ( A b )  */
    10847 ,        /* A             */
    10238 ,        /* A #  ( B b )  */
    9664 ,         /* B             */
    9121 ,         /* C             */
    0
};

#define FACTOR    1193180
#define OCTAVE    4

void main()        /* play the scale */
  {
    int i;
    for( i = 0; Notes[i]; ++i ) {
      sound( FACTOR / (Notes[i] / (1 << OCTAVE)) );
      delay( 200 );
      nosound();
    }
  }

Classification:

Intel

Safety:
Interrupt handler Yes
Signal handler Yes
Thread Yes

Caveats:

The sound() function works only if either the program is owned by root and is run using setuid(), or if the invoking user is root.

See also:

delay(), nosound()


[Previous]
[Contents]
[Next]