LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: smallpeg

弱弱地问:怎么样编个程序让机箱发出声音呢?

[复制链接]
 楼主| 发表于 2005-9-21 15:52:54 | 显示全部楼层
$ cat /proc/bus/input/devices
I: Bus=0011 Vendor=0001 Product=0001 Version=ab41
N: Name="AT Translated Set 2 keyboard"
P: Phys=isa0060/serio0/input0
H: Handlers=kbd mouse0 event0
B: EV=120017
B: KEY=40000 4 2000078 3802078 f840d001 b2ffffdf ffefffff ffffffff fffffffe
B: REL=140
B: MSC=10
B: LED=7

I: Bus=0011 Vendor=0002 Product=0005 Version=0051
N: Name="ImPS/2 Logitech Wheel Mouse"
P: Phys=isa0060/serio1/input0
H: Handlers=mouse1 event1
B: EV=7
B: KEY=70000 0 0 0 0 0 0 0 0
B: REL=103
我的不行,那我该怎么办呢?
回复 支持 反对

使用道具 举报

发表于 2005-9-21 16:31:49 | 显示全部楼层
你可能没有安装discover软件包吧。
回复 支持 反对

使用道具 举报

发表于 2007-5-26 09:49:13 | 显示全部楼层
我的相关模块已经加载了,但是没声音啊,请问什么怎么回事呢
回复 支持 反对

使用道具 举报

发表于 2007-5-28 22:24:34 | 显示全部楼层
我是printf("\x007");
主板上有个speek插槽,插入蜂鸣器就能beep了

在redhat9和winxp下都有声,在嵌入式xp下无声,这是让人郁闷的地方
回复 支持 反对

使用道具 举报

发表于 2007-5-30 22:52:33 | 显示全部楼层
楼主看看是不是延时不够呀!
回复 支持 反对

使用道具 举报

发表于 2007-10-16 18:02:29 | 显示全部楼层
试过是可以的,不过首先运行的环境(shell)中没有禁用蜂鸣器。
回复 支持 反对

使用道具 举报

发表于 2007-10-18 13:14:40 | 显示全部楼层
  1. /*  beep - just what it sounds like, makes the console beep - but with
  2. * precision control.  See the man page for details.
  3. *
  4. * Try beep -h for command line args
  5. *
  6. * This code is copyright (C) Johnathan Nightingale, 2000.
  7. *
  8. * This code may distributed only under the terms of the GNU Public License
  9. * which can be found at http://www.gnu.org/copyleft or in the file COPYING
  10. * supplied with this code.
  11. *
  12. * This code is not distributed with warranties of any kind, including implied
  13. * warranties of merchantability or fitness for a particular use or ability to
  14. * breed pandas in captivity, it just can't be done.
  15. *
  16. * Bug me, I like it:  http://johnath.com/  or johnath@johnath.com
  17. */

  18. #include <fcntl.h>
  19. #include <getopt.h>
  20. #include <signal.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/types.h>
  27. #include <linux/kd.h>

  28. /* I don't know where this number comes from, I admit that freely.  A
  29.    wonderful human named Raine M. Ekman used it in a program that played
  30.    a tune at the console, and apparently, it's how the kernel likes its
  31.    sound requests to be phrased.  If you see Raine, thank him for me.  
  32.    
  33.    June 28, email from Peter Tirsek (peter at tirsek dot com):
  34.    
  35.    This number represents the fixed frequency of the original PC XT's
  36.    timer chip (the 8254 AFAIR), which is approximately 1.193 MHz. This
  37.    number is divided with the desired frequency to obtain a counter value,
  38.    that is subsequently fed into the timer chip, tied to the PC speaker.
  39.    The chip decreases this counter at every tick (1.193 MHz) and when it
  40.    reaches zero, it toggles the state of the speaker (on/off, or in/out),
  41.    resets the counter to the original value, and starts over. The end
  42.    result of this is a tone at approximately the desired frequency. :)
  43. */
  44. #ifndef CLOCK_TICK_RATE
  45. #define CLOCK_TICK_RATE 1193180
  46. #endif

  47. #define VERSION_STRING "beep-1.2.2"
  48. char *copyright =
  49. "Copyright (C) Johnathan Nightingale, 2002.  "
  50. "Use and Distribution subject to GPL.  "
  51. "For information: http://www.gnu.org/copyleft/.";

  52. /* Meaningful Defaults */
  53. #define DEFAULT_FREQ       440.0 /* Middle A */
  54. #define DEFAULT_LENGTH     200   /* milliseconds */
  55. #define DEFAULT_REPS       1
  56. #define DEFAULT_DELAY      100   /* milliseconds */
  57. #define DEFAULT_END_DELAY  NO_END_DELAY
  58. #define DEFAULT_STDIN_BEEP NO_STDIN_BEEP

  59. /* Other Constants */
  60. #define NO_END_DELAY    0
  61. #define YES_END_DELAY   1

  62. #define NO_STDIN_BEEP   0
  63. #define LINE_STDIN_BEEP 1
  64. #define CHAR_STDIN_BEEP 2

  65. typedef struct beep_parms_t {
  66.   float freq;     /* tone frequency (Hz)      */
  67.   int length;     /* tone length    (ms)      */
  68.   int reps;       /* # of repetitions         */
  69.   int delay;      /* delay between reps  (ms) */
  70.   int end_delay;  /* do we delay after last rep? */
  71.   int stdin_beep; /* are we using stdin triggers?  We have three options:
  72.                      - just beep and terminate (default)
  73.                      - beep after a line of input
  74.                      - beep after a character of input
  75.                      In the latter two cases, pass the text back out again,
  76.                      so that beep can be tucked appropriately into a text-
  77.                      processing pipe.
  78.                   */
  79.   struct beep_parms_t *next;  /* in case -n/--new is used. */
  80. } beep_parms_t;

  81. /* Momma taught me never to use globals, but we need something the signal
  82.    handlers can get at.*/
  83. int console_fd = -1;

  84. /* If we get interrupted, it would be nice to not leave the speaker beeping in
  85.    perpetuity. */
  86. void handle_signal(int signum) {
  87.   switch(signum) {
  88.   case SIGINT:
  89.     if(console_fd >= 0) {
  90.       /* Kill the sound, quit gracefully */
  91.       ioctl(console_fd, KIOCSOUND, 0);
  92.       close(console_fd);
  93.       exit(signum);
  94.     } else {
  95.       /* Just quit gracefully */
  96.       exit(signum);
  97.     }
  98.   }
  99. }

  100. /* print usage and exit */
  101. void usage_bail(const char *executable_name) {
  102.   printf("Usage:\n%s [-f freq] [-l length] [-r reps] [-d delay] "
  103.          "[-D delay] [-s] [-c]\n",
  104.          executable_name);
  105.   printf("%s [Options...] [-n] [--new] [Options...] ... \n", executable_name);
  106.   printf("%s [-h] [--help]\n", executable_name);
  107.   printf("%s [-v] [-V] [--version]\n", executable_name);
  108.   exit(1);
  109. }


  110. /* Parse the command line.  argv should be untampered, as passed to main.
  111. * Beep parameters returned in result, subsequent parameters in argv will over-
  112. * ride previous ones.
  113. *
  114. * Currently valid parameters:
  115. *  "-f <frequency in Hz>"
  116. *  "-l <tone length in ms>"
  117. *  "-r <repetitions>"
  118. *  "-d <delay in ms>"
  119. *  "-D <delay in ms>" (similar to -d, but delay after last repetition as well)
  120. *  "-s" (beep after each line of input from stdin, echo line to stdout)
  121. *  "-c" (beep after each char of input from stdin, echo char to stdout)
  122. *  "-h/--help"
  123. *  "-v/-V/--version"
  124. *  "-n/--new"
  125. *
  126. * March 29, 2002 - Daniel Eisenbud points out that c should be int, not char,
  127. * for correctness on platforms with unsigned chars.
  128. */
  129. void parse_command_line(int argc, char **argv, beep_parms_t *result) {
  130.   int c;

  131.   struct option opt_list[4] = {{"help", 0, NULL, 'h'},
  132.                                {"version", 0, NULL, 'V'},
  133.                                {"new", 0, NULL, 'n'},
  134.                                {0,0,0,0}};
  135.   while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVn", opt_list, NULL))
  136.         != EOF) {
  137.     int argval = -1;    /* handle parsed numbers for various arguments */
  138.     float argfreq = -1;
  139.     switch(c) {      
  140.     case 'f':  /* freq */
  141.       if(!sscanf(optarg, "%f", &argfreq) || (argfreq >= 20000 /* ack! */) ||
  142.          (argfreq <= 0))
  143.         usage_bail(argv[0]);
  144.       else
  145.         result->freq = argfreq;   
  146.       break;
  147.     case 'l' : /* length */
  148.       if(!sscanf(optarg, "%d", &argval) || (argval < 0))
  149.         usage_bail(argv[0]);
  150.       else
  151.         result->length = argval;
  152.       break;
  153.     case 'r' : /* repetitions */
  154.       if(!sscanf(optarg, "%d", &argval) || (argval < 0))
  155.         usage_bail(argv[0]);
  156.       else
  157.         result->reps = argval;
  158.       break;
  159.     case 'd' : /* delay between reps - WITHOUT delay after last beep*/
  160.       if(!sscanf(optarg, "%d", &argval) || (argval < 0))
  161.         usage_bail(argv[0]);
  162.       else {
  163.         result->delay = argval;
  164.         result->end_delay = NO_END_DELAY;
  165.       }
  166.       break;
  167.     case 'D' : /* delay between reps - WITH delay after last beep */
  168.       if(!sscanf(optarg, "%d", &argval) || (argval < 0))
  169.         usage_bail(argv[0]);
  170.       else {
  171.         result->delay = argval;
  172.         result->end_delay = YES_END_DELAY;
  173.       }
  174.       break;
  175.     case 's' :
  176.       result->stdin_beep = LINE_STDIN_BEEP;
  177.       break;
  178.     case 'c' :
  179.       result->stdin_beep = CHAR_STDIN_BEEP;
  180.       break;
  181.     case 'v' :
  182.     case 'V' : /* also --version */
  183.       printf("%s\n",VERSION_STRING);
  184.       exit(0);
  185.       break;
  186.     case 'n' : /* also --new - create another beep */
  187.       result->next = (beep_parms_t *)malloc(sizeof(beep_parms_t));
  188.       result->next->freq       = DEFAULT_FREQ;
  189.       result->next->length     = DEFAULT_LENGTH;
  190.       result->next->reps       = DEFAULT_REPS;
  191.       result->next->delay      = DEFAULT_DELAY;
  192.       result->next->end_delay  = DEFAULT_END_DELAY;
  193.       result->next->stdin_beep = DEFAULT_STDIN_BEEP;
  194.       result->next->next       = NULL;
  195.       result = result->next; /* yes, I meant to do that. */
  196.       break;
  197.     case 'h' : /* notice that this is also --help */
  198.     default :
  199.       usage_bail(argv[0]);
  200.     }
  201.   }
  202. }  

  203. void play_beep(beep_parms_t parms) {
  204.   int i; /* loop counter */

  205.   /* try to snag the console */
  206.   if((console_fd = open("/dev/console", O_WRONLY)) == -1) {
  207.     fprintf(stderr, "Could not open /dev/console for writing.\n");
  208.     printf("\a");  /* Output the only beep we can, in an effort to fall back on usefulness */
  209.     perror("open");
  210.     exit(1);
  211.   }
  212.   
  213.   /* Beep */
  214.   for (i = 0; i < parms.reps; i++) {                    /* start beep */
  215.     if(ioctl(console_fd, KIOCSOUND, (int)(CLOCK_TICK_RATE/parms.freq)) < 0) {
  216.       printf("\a");  /* Output the only beep we can, in an effort to fall back on usefulness */
  217.       perror("ioctl");
  218.     }
  219.     /* Look ma, I'm not ansi C compatible! */
  220.     usleep(1000*parms.length);                          /* wait...    */
  221.     ioctl(console_fd, KIOCSOUND, 0);                    /* stop beep  */
  222.     if(parms.end_delay || (i+1 < parms.reps))
  223.        usleep(1000*parms.delay);                        /* wait...    */
  224.   }                                                     /* repeat.    */

  225.   close(console_fd);
  226. }



  227. int main(int argc, char **argv) {
  228.   char sin[4096], *ptr;
  229.   
  230.   beep_parms_t *parms = (beep_parms_t *)malloc(sizeof(beep_parms_t));
  231.   parms->freq       = DEFAULT_FREQ;
  232.   parms->length     = DEFAULT_LENGTH;
  233.   parms->reps       = DEFAULT_REPS;
  234.   parms->delay      = DEFAULT_DELAY;
  235.   parms->end_delay  = DEFAULT_END_DELAY;
  236.   parms->stdin_beep = DEFAULT_STDIN_BEEP;
  237.   parms->next       = NULL;

  238.   signal(SIGINT, handle_signal);
  239.   parse_command_line(argc, argv, parms);

  240.   /* this outermost while loop handles the possibility that -n/--new has been
  241.      used, i.e. that we have multiple beeps specified. Each iteration will
  242.      play, then free() one parms instance. */
  243.   while(parms) {
  244.     beep_parms_t *next = parms->next;

  245.     if(parms->stdin_beep) {
  246.       /* in this case, beep is probably part of a pipe, in which case POSIX
  247.          says stdin and out should be fuly buffered.  This however means very
  248.          laggy performance with beep just twiddling it's thumbs until a buffer
  249.          fills. Thus, kill the buffering.  In some situations, this too won't
  250.          be enough, namely if we're in the middle of a long pipe, and the
  251.          processes feeding us stdin are buffered, we'll have to wait for them,
  252.          not much to  be done about that. */
  253.       setvbuf(stdin, NULL, _IONBF, 0);
  254.       setvbuf(stdout, NULL, _IONBF, 0);
  255.       while(fgets(sin, 4096, stdin)) {
  256.         if(parms->stdin_beep==CHAR_STDIN_BEEP) {
  257.           for(ptr=sin;*ptr;ptr++) {
  258.             putchar(*ptr);
  259.             fflush(stdout);
  260.             play_beep(*parms);
  261.           }
  262.         } else {
  263.           fputs(sin, stdout);
  264.           play_beep(*parms);
  265.         }
  266.       }
  267.     } else {
  268.       play_beep(*parms);
  269.     }

  270.     /* Junk each parms struct after playing it */
  271.     free(parms);
  272.     parms = next;
  273.   }

  274.   return EXIT_SUCCESS;
  275. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2009-8-2 17:30:00 | 显示全部楼层
何必这么麻烦


  1. #include   <stdlib.h>   
  2.   #include   <fcntl.h>   
  3.   #include   <linux/kd.h>   
  4.    
  5.   int   main(int   argc,   char   *argv[])   
  6.   {   
  7.           int   fd   =   open("/dev/tty10",   O_RDONLY);   
  8.           if   (fd   ==   -1   ||   argc   !=   3)   
  9.                   return   -1;   
  10.           return   ioctl(fd,   KDMKTONE,   (atoi(argv[2])<<16)+(1193180/atoi(argv[1])));   
  11.   }   

复制代码


  gcc   -o   beep   beep.c   

  ./beep   1000   1000
  两个参数分别控制声音频率和时间。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表