/*****************************************************************************\
*   This program is free software; you can redistribute it and/or modify      *
*   it under the terms of the GNU General Public License as published by      *
*   the Free Software Foundation; either version 2 of the License, or         *
*   (at your option) any later version.                                       *
*                                                                             *
*   This program is distributed in the hope that it will be useful,           *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*   GNU General Public License for more details.                              *
*                                                                             *
*   You should have received a copy of the GNU General Public License         *
*   along with this program; if not, write to the Free Software Foundation,   *
*   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.            *
\*****************************************************************************/

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Spielen mit dem internen PC-Lautsprecher unter Linux
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * Das Programm erzeugt auf dem internen Lautsprecher einen
 * Ton der Frequenz freq [Hz] und der Dauer duration [ms]
 *
 * Aufruf:   beep [freq duration]
 *
 * Beim Aufruf ohne Parameter wird ein 880-Hz-Ton 100 ms lang erzeugt.
 *
 * Wegen des Zugriffs auf /dev/console (wahlweise /dev/tty0)
 * und der ioctl-Aufrufe kann das Programm nur von root 
 * aufgerufen werden (ggf. mit 'chmod 4711 beep' SUD-root setzen)
 * Für eine Produktiv-Version sollte noch eine Fehlerbehandlung
 * hinzugefügt werden.
 *
 * JPL 10.11.04
 */

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

int main (int argc, char *argv[])
  {
  int fd;
  long duration, freq, res;
  fd = open("/dev/console",O_RDONLY);
  if (argc > 2)
    {
    freq = atoi(argv[1]);
    duration = atoi(argv[2]);
    }
  else
    {
    freq = 880;
    duration = 100;
    }
  if (freq > 0)
    {
    res = (duration << 16) + (1193180/freq);
    ioctl(fd,KDMKTONE,res);
    }
  usleep(duration*1000);
  close(fd);
  return(0);
  }

/*

Bei Verwendung von Noten gilt für die Frequenzen:

      262   C - "middle C" 
      277   C# 
      294   D 
      311   D# 
      330   E
      349   F 
      370   F# 
      392   G 
      415   G# 
      440   A
      466   A# 
      494   H

Um andere Oktaven zu bilden, einfach immer die höchste Noten-Freqenz 
mit 1.0595, multiplizieren und runden. Zum Beispiel ergibt sich
aus den H oben: 494 * 1.0595 = 523.393, gerundet 523 Hz für das "C",
das eine Oktave höher liegt als das "middle C".
*/

