/* tune - Plays a fairly distinctive tune using the system speaker

   By James Stanley

   No license - Use as you wish */

#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/kd.h>

#define DELAY 5
#define LONGDELAY 30
#define NOTETIME 100
#define LONGNOTETIME 200

void msleep(int ms) {
  usleep(ms*1000);
}

void beep(int fd, int freq, int ms) {
  ioctl(fd, KIOCSOUND, 1193180/freq);
  msleep(ms);
  ioctl(fd, KIOCSOUND, 0);
}

int main() {
  int fd;

  fd = open("/dev/console", O_WRONLY);
 
  beep(fd, 300, NOTETIME);
  msleep(DELAY);
  beep(fd, 300, NOTETIME);
  msleep(DELAY);
  beep(fd, 300, NOTETIME);
  msleep(DELAY);
  beep(fd, 400, LONGNOTETIME);
  msleep(LONGDELAY);
  beep(fd, 400, NOTETIME);
  msleep(DELAY);
  beep(fd, 400, NOTETIME);
  msleep(DELAY);
  beep(fd, 400, NOTETIME);
  msleep(DELAY);
  beep(fd, 200, LONGNOTETIME);
  
  close(fd);

  return 0;
}
