Thread: Outputting many beeps through the pc speaker

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rllovera
    Ok, i've tried delays (as i've said) twice, with values 100000 and 1000000, and it didn't work. One thing makes me sure of the possibility of outputting more than one beep.
    I didn't suggest it is impossible to output more than one discernible sound. I said that it is unlikely that the operating system will include features to add delays between two consecutive bits of output to the sound card, so they can be percieved by a human as two distinct sounds if your program does nothing to make sure they occur separately.

    A relatively normal way (it depends on what version of unix you use) to introduce a fixed delay is to use a call of
    Code:
    delay(number_of_seconds * drv_usectohz(1000000));
    because delay() works in clock ticks.

    One other thing to check is that your are flushing stdout between and after both your desired beep events. For example;
    Code:
         fprintf(stdout, "\a");
         fflush(stdout);
         delay(drv_usectohz(1000000));
         fprintf(stdout, "\a");
         fflush(stdout);
    Quote Originally Posted by rllovera
    In Gentoo Linux (the OS i use), when you install a software title and the installation program wnats to warn you about something, you get three beeps, each of them accompanied by a dot displayed on the terminal. So that's why i think it can be done.

    Besides, if only one beep were outputted by my program, only a single call for the attention of the program user would be available, which is highly undesireable. While i can't make an argument to refute yours, i'm still not inclind to believe i can't make what i want.
    I never said what you want to do is impossible. You were focusing on tweaking the operating system to do what you want, whereas you need to tweek your program to exhibit the intended behaviour. The above should provide a start.

  2. #17
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by grumpy
    I didn't suggest it is impossible to output more than one discernible sound. I said that it is unlikely that the operating system will include features to add delays between two consecutive bits of output to the sound card, so they can be percieved by a human as two distinct sounds if your program does nothing to make sure they occur separately.

    A relatively normal way (it depends on what version of unix you use) to introduce a fixed delay is to use a call of
    Code:
    delay(number_of_seconds * drv_usectohz(1000000));
    because delay() works in clock ticks.

    One other thing to check is that your are flushing stdout between and after both your desired beep events. For example;
    Code:
         fprintf(stdout, "\a");
         fflush(stdout);
         delay(drv_usectohz(1000000));
         fprintf(stdout, "\a");
         fflush(stdout);
    I never said what you want to do is impossible. You were focusing on tweaking the operating system to do what you want, whereas you need to tweek your program to exhibit the intended behaviour. The above should provide a start.
    Ok. I see there might not be an easy way to accomplish my goal of outputting more than one beep using printf only. I will try the suggestion you gave but i still don't understand why "\a" does not work on my machine as other escape sequences do. C is a strange language, i suppose. I guess i might find an answer later.

    Thanks to all of you who answered my questions.

  3. #18
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by vart
    Why to kill processors time if you want just to wait? What will be with your comp if every program will "wait" the same way?
    Thats a good point Vart. But i have clearly said that, it is not a efficient way of doing it. The only reason i said him to use for loop is because his delay wasn't working on this machine. Which donst really make any sense. But any way, for look is not a good idea as u said

    ssharish2005

  4. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rllovera
    Ok. I see there might not be an easy way to accomplish my goal of outputting more than one beep using printf only. I will try the suggestion you gave but i still don't understand why "\a" does not work on my machine as other escape sequences do.
    Because \a is one of the escape sequences that corresponds to output that is not visible on the screen and because stdout is buffered. The way you would judge whether something visible appears on the screen is by looking for it. You would probably also do something such as read from stdin.

    Buffering means that output does not occur immediately, but occurs either when the buffer is full (so a big chunk is output at once) or when the stream (or its buffer) is explicitly flushed (which my code example did). Flushing also occurs as part of any operation to read from stdin (eg a call to scanf()). So, even if you wait ten seconds between two calls printf("\a"), the actual sounds do not occur ten seconds apart.

    With visible output (eg multiple \n's) you are probably only looking for the net visible effect, whereas with \a's the effect you are looking for is a timing relationship. If you output a \n twice, the net effect is scrolling down two lines. The net effect is the same, whether the second \n is output immediately after the first, or ten minutes later. When outputting multiple \a's, the effect is a sound, the sound heard depends on how far apart in time the distinct \a's physically trigger the speaker. Buffering changes the timing relationship (eg the first \a is kept in buffer until the second one is output, and then both are output to the speaker within a microsecond or so --- and the second is therefore ignored by the speaker). Which is why your program needs to introduce lags, and flush output periodically, if you care about hearing multiple beeps.
    Quote Originally Posted by rllovera
    C is a strange language, i suppose. I guess i might find an answer later.
    C is strange in many ways, but this is not one of them. A lot of programming languages and library function use buffered I/O to improve the performance of programs that do a lot of I/O.
    Last edited by grumpy; 11-21-2006 at 12:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  2. PC Issue: Repeated Long Beeps
    By alphaoide in forum Tech Board
    Replies: 8
    Last Post: 07-19-2005, 08:46 AM
  3. Using PC Speaker and NOT the sound card in a console...
    By Trauts in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-09-2003, 08:52 PM
  4. PC Speaker...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2002, 08:19 AM
  5. PC Speaker Beeps
    By Brian in forum C Programming
    Replies: 7
    Last Post: 01-16-2002, 03:25 PM