A sound program

Source: Internet
Author: User
Tags usleep
  1. # Include <fcntl. h>
  2. # Include <stdio. h>
  3. # Include <stdlib. h>
  4. # Include <string. h>
  5. # Include <unistd. h>
  6. # Include <sys/IOCTL. h>
  7. # Include <sys/types. h>
  8. # Include <Linux/KD. h>
  9. /* Set the default value */
  10. # Define default_freq 10000/* set an appropriate frequency */
  11. # Define default_length 1000/* 200 microseconds. The audible length is measured in microseconds */
  12. # Define default_reps 5/* No repeated voices by default */
  13. # Define default_delay 500/* in microseconds */
  14. /* Define a structure to store the required data */
  15. Typedef struct
  16. {
  17. Int freq;/* the expected output frequency, in Hz */
  18. Int length;/* audible length, in microseconds */
  19. Int reps;/* repeated times */
  20. Int delay;/* Two audible intervals, in microseconds */
  21. } Beep_parms_t;
  22. /* Print the help information and exit */
  23. Void usage_bail (const char * executable_name)
  24. {
  25. Printf ("Usage:/n/T % s [-F frequency] [-l length] [-r reps] [-D delay]/n", executable_name );
  26. Exit (1 );
  27. }
  28. /* Analyze the running parameters, which have the following meanings: * "-F <frequency value in Hz>" * "-l <audible duration in milliseconds>" * "-r <repeated times> "*" -d <Interval Duration in milliseconds> "*/
  29. Void parse_command_line (char ** argv, beep_parms_t * result)
  30. {
  31. Char * arg0 = * (argv ++ );
  32. While (* argv)
  33. {
  34. If (! Strcmp (* argv, "-F "))
  35. {/* Frequency */
  36. Int freq = atoi (* (++ argv ));
  37. If (freq <= 0) | (freq> 10000 ))
  38. {
  39. Fprintf (stderr, "Bad parameter: frequency must be from 1 .. 10000/N ");
  40. Exit (1 );
  41. }
  42. Else {
  43. Result-> freq = freq; argv ++;
  44. }
  45. }
  46. Else if (! Strcmp (* argv, "-l "))
  47. {/* Duration */
  48. Int length = atoi (* (++ argv ));
  49. If (length <0)
  50. {
  51. Fprintf (stderr, "Bad parameter: length must be> = 0/N ");
  52. Exit (1 );
  53. }
  54. Else {
  55. Result-> length = length; argv ++;
  56. }
  57. }
  58. Else if (! Strcmp (* argv, "-R "))
  59. {/* Repeated times */
  60. Int reps = atoi (* (++ argv ));
  61. If (REPS <0)
  62. {
  63. Fprintf (stderr, "Bad parameter: reps must be> = 0/N ");
  64. Exit (1 );
  65. } Else
  66. {
  67. Result-> reps = reps; argv ++;
  68. }
  69. }
  70. Else if (! Strcmp (* argv, "-d "))
  71. {/* Latency */
  72. Int delay = atoi (* (++ argv ));
  73. If (delay <0)
  74. {
  75. Fprintf (stderr, "Bad parameter: delay must be> = 0/N ");
  76. Exit (1 );
  77. } Else {
  78. Result-> delay = delay; argv ++;
  79. }
  80. } Else {
  81. Fprintf (stderr, "Bad parameter: % s/n", * argv );
  82. Usage_bail (arg0 );
  83. }
  84. }
  85. }
  86. Int main (INT argc, char ** argv)
  87. {
  88. Int console_fd; int I;/* loop counter * // * set the voice parameter to the default value */
  89. Beep_parms_t parms = {default_freq, default_length, default_reps, default_delay };
  90. /* Analyze parameters. If possible, update the audible parameters */
  91. Parse_command_line (argv, & parms );
  92. /* Open the console and terminate the program if it fails */
  93. If (console_fd = open ("/dev/DSP", o_wronly) =-1)
  94. {
  95. Fprintf (stderr, "failed to open console./N ");
  96. Perror ("open ");
  97. Exit (1 );
  98. }
  99. /* Start to let the speaker speak */
  100. For (I = 0; I <parms. reps; I ++)
  101. {/* Do not know where the number 1190000 comes from */
  102. Int magical_fairy_number = 1190000/parms. freq;
  103. IOCTL (console_fd, kiocsound, magical_fairy_number );
  104. /* Start voice */
  105. Usleep (1000 * parms. Length);/* Wait ...*/
  106. IOCTL (console_fd, kiocsound, 0 );
  107. /* Stop the speaker */
  108. Usleep (1000 * parms. Delay);/* Wait ...*/
  109. }/* Replay */
  110. Return exit_success;
  111. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.