Thread: Outputting many beeps through the pc speaker

  1. #1
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22

    Question Outputting many beeps through the pc speaker

    Hi. I'm starting to learn C from the beginning again after a long time. As i wrote the program for the second exercise of the first chapter of Kernighan & Ritchie's book and learned to make my pc speaker beep, i began to wonder how to output many beeps through the pc speaker using the printf function only. By that i mean a single printf execution (no loops or things like that.)

    I was hoping that a code as simple as this would work:

    Code:
    #include <stdio.h>
    
    main()
    {
        printf("\a\a");
    }
    but unfortunately, it didn't. I wonder why there is some sort of "asymmetry" among escape sequencies. I'll make my point clearer. I can use the code above, replacing \a\a by \n\n to output two newline characters one inmediately after the other. However, this is not possible when it comes to \a. I don' hear two beeps one after the other.

    I hope you can guide me towards the resolution of this problem i have.

    Thank you very much,

    Roberto.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by rllovera
    I'll make my point clearer. I can use the code above, replacing \a\a by \n\n to output two newline characters one inmediately after the other. However, this is not possible when it comes to \a. I don' hear two beeps one after the other.
    Do you hear one beep? The difference might be your perception.

    Do you hear no beeps? It may be the environment from which it is run.
    \a (alert) Produces an audible or visible alert without changing the active position.
    In a command shell, I hear two beeps with the code you posted.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I wouldn't worry too much about it. Its just an implementation detail for the OS. The following code only beeps once for me:
    Code:
    #include <stdio.h>
    
    main () {
    	int i;
    	for (i = 0; i < 1000; ++i)
    		printf ("\a");
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by Dave_Sinkula
    Do you hear one beep? The difference might be your perception.

    Do you hear no beeps? It may be the environment from which it is run.

    In a command shell, I hear two beeps with the code you posted.
    I hear only one beep. By the way, I'm using Gentoo Linux and support for pcspkr has been compiled into the kernel. Why does my environment only beep once? How can that behaviour be changed?

  5. #5
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by QuestionC
    I wouldn't worry too much about it. Its just an implementation detail for the OS. The following code only beeps once for me:
    Code:
    #include <stdio.h>
    
    main () {
    	int i;
    	for (i = 0; i < 1000; ++i)
    		printf ("\a");
    }
    I've tried a thing like that and it hasn't worked for me either. However, i want to know why i'm not getting what i should be getting and learn the steps to make the program do what i want.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Maybe try sleeping between beeps. I'm guessing your system recieves a long list of beep events on the queue at once, beeps once, and discards the rest.

    Code:
    for(i = 0; i < 10; ++i)
    {
        usleep(100000);
        putchar('\a');
    }
    Substitute Sleep() on Windows.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by jafet
    Maybe try sleeping between beeps. I'm guessing your system recieves a long list of beep events on the queue at once, beeps once, and discards the rest.

    Code:
    for(i = 0; i < 10; ++i)
    {
        usleep(100000);
        putchar('\a');
    }
    Substitute Sleep() on Windows.
    This does not work for me. What should i do to configure my machine so it does not discard beeps? How can i find out if it is discarding them?

    Thanks in advance.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Use Beep() ?

  9. #9
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by citizen
    Use Beep() ?
    I want to make it work using the \a sequence. I may sound stubborn but i think there'll be a lesson to learn after i solve this matter.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    \a does different things on different systems. On a Windows 98 computer, two '\a's in quick succession sounds the speaker once, because the standard Windows sound takes a while to play. This can be fixed on systems such as this with a delay, as jafet already pointed out.

    i began to wonder how to output many beeps through the pc speaker using the printf function only.
    This is not possible, or it is not possible on all systems. (See Dave_Sinkula's post.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by dwks
    \a does different things on different systems. On a Windows 98 computer, two '\a's in quick succession sounds the speaker once, because the standard Windows sound takes a while to play. This can be fixed on systems such as this with a delay, as jafet already pointed out.


    This is not possible, or it is not possible on all systems. (See Dave_Sinkula's post.)
    But delaying the second beep was not a solution for me.

    Are you suggesting there's no way to hear the two beeps on my machine? I'm sure there must be something that can be configured in order to hear them...

    Thanks.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rllovera
    Are you suggesting there's no way to hear the two beeps on my machine? I'm sure there must be something that can be configured in order to hear them...
    You may be sure, but you're wrong. Speaker typically ignore inputs to them that are too close together to a previous one, because they are designed with the human ear in mind and the human ear cannot discern signals that are too close together. Very few operating systems have any means of introducing delays between two close output events (eg two \a's being output by a program), because such tricks compromise performance and because there is no general way of introducing delays so they will work with all speakers or sound devices, so it is up to the applications to ensure any desired delays occur.

    Even if it could be configured, what would be the purpose? Your application would work on your machine, but would not work on anyone elses machine that has not been configured the same as yours. The simple solution, already suggested in this thread, of including a delay will work regardless of operating system or configuration settings, as long as the delay is not excessively short.

  13. #13
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22
    Quote Originally Posted by grumpy
    You may be sure, but you're wrong. Speaker typically ignore inputs to them that are too close together to a previous one, because they are designed with the human ear in mind and the human ear cannot discern signals that are too close together. Very few operating systems have any means of introducing delays between two close output events (eg two \a's being output by a program), because such tricks compromise performance and because there is no general way of introducing delays so they will work with all speakers or sound devices, so it is up to the applications to ensure any desired delays occur.

    Even if it could be configured, what would be the purpose? Your application would work on your machine, but would not work on anyone elses machine that has not been configured the same as yours. The simple solution, already suggested in this thread, of including a delay will work regardless of operating system or configuration settings, as long as the delay is not excessively short.
    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. 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.

    Thanks for your time and patience.

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    i am not sure of why your delay is that working. If would be better of to post your code. An alternative for delay is just a for loop which is done manually. this is actually recommended for delay Cos it kills the processing time

    Code:
    int i;
    
    for(i=0;i<100000;i++);
    
    printf("\a");
    ssharish2005

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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