Thread: Beep

  1. #1
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Question Beep

    How can I make different basic tones (which function?). The windows beep() function is only one sound. So is there any windows function? (or other header file?)
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: Beep

    Originally posted by Stan100
    The windows beep() function is only one sound.
    No, not if you're using a real (NT,2k,XP) version of Windows.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    PIT

    You might look for information on programming the 8254 "PIT" (Programmable Interface Timer). The PC speaker is connected to the PIT.

    I've done a tiny bit of research, but haven't written any code or really studied the stuff I printed-out yet.

    Here's what I think I know... The PIT is driven by an approximately 19kHz clock. There are two timers. You load one of the timers with a count that sets the frequency (19kHz / count = freq ???). The other timer gets loaded with a count that sets the duration.

    [edit]-----------------------------------------------------------
    It's a PIC = Programmable Interrupt Controller and the base clock frequency is 1,193,180 Hz.
    I couldn't find sound() in MSVC++, but if your compiler has it, great! Neither approach is portable, so use the simplest solution!
    [edit]-----------------------------------------------------------

    If you're running NT, Win 2k, or XP, you'll also have to look-up how to get-around the hardware "protection". (Look for information on writing drivers.)

    Windows 98 shouldn't have any problems. I'm writing this on a Win 98 machine without a sound card or external speakers. It runs test programs written in Quick-BASIC. I got tired of the thing beeping when the test passed, beeping when the test failed, and beeping when I was supposed to do something. So I wrote three subroutines for "pass sound", fail sound" and "alert sound". The sound routines get fouled-up (extra long duration) when I'm running multiple applications (fooling around on the internet - like now!)
    Last edited by DougDbug; 01-06-2003 at 09:38 PM.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    There is a function called sound, defined in the header file dos.h. It turns on the speaker at a specified frequency. The function nosound turns off the speaker.

    void sound(unsigned frequency);
    void nosound(void);

    The value of the frequency is in hertz(Hz).

    You can use the function called delay, also defined in dos.h, to make the tone last for a specified number of milliseconds.

    void delay(unsigned milliseconds);
    Code:
    /*Program to play a 100Hz tone for 5 seconds*/
    #include<dos.h>
    int main()
    {
       sound(100); delay(5000); nosound(); return(0);
    }
    However, I think the sound function works only for DOS. I do not know about a similar function for Windows.

    Compiler:Turbo C++ v3.01.
    Last edited by sundeeptuteja; 01-07-2003 at 06:09 AM.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    my dos.h does not have sound(), delay(), or nosound()

    I'm using MVC++ 6.0

    Is there another header or one I can download?
    Last edited by Trauts; 01-08-2003 at 09:39 AM.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    I believe the functions sound(), nosound() and delay() are available only with Borland compilers. You can get the free Borland compiler here : http://www.borland.com
    Last edited by sundeeptuteja; 01-08-2003 at 09:47 AM.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Would it be possible to use those headers with MVC++ 6.0? If so, can you please upload the dos.h file and any other required ones for the sound() and nosound()? Thanks!
    Last edited by Trauts; 01-08-2003 at 09:51 AM.

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Unfortunately the header files/libraries are usually provided as part of the compiler. I doubt that the header file created for Borland C++ will work with MSVC++(I used to think like you at one time myself, I found out the hard way).
    Last edited by sundeeptuteja; 01-08-2003 at 09:56 AM.

  9. #9
    Registered User SubLogic's Avatar
    Join Date
    Jan 2003
    Posts
    33
    You could try using sndPlaySound() but it only works in Windows because it's a windows api function..
    0100001

  10. #10
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    No, not if you're using a real (NT,2k,XP) version of Windows
    So what? Win98 was a bootleg model? No, I don't believe so.

    I know how to play a sound, I want basic tones. C-D-E-F-G-A-B-C
    I making a piano program, and I know there must be a way to do it w/out downloading each tone (which I'm NOT)




    Song of the day:
    If I could write some magic code, that everyone could read.
    I'd write some code, of RPG's, where everyone is free.
    And when I write my magic code, it would not be wrote in C.
    Not in sharp, nor in BASIC, some code for all of us.
    "My code is magic, you see right here, 'cuz it was written in C++!"
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Sound Card?

    I'm confused. Are you using a sound card? Because I've only used beep() to get a sound from the internal PC speaker. Otherwise the WinAPI plays the Windows Default Sound (I think ?)

    If you are using a sound card... well, what you're trying to do has to be the easiest thing that a sound card does! OK... I've never looked into how to do it, but I'm sure that there is a way to send a pitch (or frequency) and ADSR (Attack-Delay-Sustain-Release) command to a sound card.

    You might try posting a question on the Game Board. Gamers make lots of sounds and they don't want to use CPU/bus cycles to read and decode wave files! Or, search for sample MIDI programs on the web... A piano program sounds like a MIDI application to me... I think they call these things MIDI even if you don't use the actual MIDI hardware interface.

    There are probably generic sound-blaster compatable libraries available too.

    You may have to make your own table to convert note-letters to frequency.

    If you're trying to make sounds from the internal speaker (why?) then you have to write directly to the hardware (PIT / Speaker), or use a different compiler. The internal speaker is very limited... It's connected to a one-bit digital output (on-off). So, it normally outputs square waves and has no volume control. You can vary the pulse width to reduce the volume, but this changes the harmonic structure and is apparently complicated too (CPU intensive and requiring interrupts). Or, you can get really fancy and use pulse-width modulation to simulate sine waves (or complex wave forms). I can't think of any good reason for generating complex sounds through a speaker that's never going to sound as good as the cheapest sound-card-speaker combination.

  12. #12
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Beep(Frequency, Duration) // WinNt, Win2K, Win XP only!

    No, not if you're using a real (NT,2k,XP) version of Windows.
    Yes!!! Sang-drax is RIGHT!!! (He/She usually is)

    Beep(Frequency, Duration)

    The paramaters are ignored with Win95, 98 and ME

    This is from Microsoft:
    Beep

    The Beep function generates simple tones on the speaker. The function is synchronous; it does not return control to its caller until the sound finishes.

    BOOL Beep(
    DWORD dwFreq,
    DWORD dwDuration
    );

    Parameters
    dwFreq
    [in] Frequency of the sound, in hertz. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).
    Windows 95/98/Me: The Beep function ignores this parameter.

    dwDuration
    [in] Duration of the sound, in milliseconds.
    Windows 95/98/Me: The Beep function ignores this parameter.

    Return Values
    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero. To get extended error information, call GetLastError.

    Remarks
    Terminal Services: The beep is redirected to the client.

    Windows 95/98/Me: On computers with a sound card, the function plays the default sound event. On computers without a sound card, the function plays the standard system beep.

    Example Code
    For an example, see Registering a Control Handler Function.
    Requirements
    Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, Windows 95.
    Server: Included in Windows .NET Server 2003, Windows 2000 Server, Windows NT Server.
    Header: Declared in Winbase.h; include Windows.h.
    Library: Use Kernel32.lib.

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I tried using winbase.h's
    Beep(100, 200);

    like this:

    Code:
    #include<winbase.h>
    #include<windows.h>
    int main()
    {
       Beep(100, 200);
       return(0);
    }
    and that gave 94 errrors, so I tried this:

    Code:
    #include<windows.h>
    #include<winuser.h>
    int main()
    {
    	for (int x = 100; x<500; x++)
    	   Beep(x, 50);
       return(0);
    }
    Which worked fine. I'm working on something I'll post up when I finish
    Last edited by Trauts; 01-09-2003 at 05:47 PM.

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Hmm... I got home and tried working on my header file on MVC++ 6.0 but with Windows XP, and it doesn't play anything. I then remembered this:

    On computers with a sound card, the function plays the default sound event. On computers without a sound card, the function plays the standard system beep.

    Is that why it isn't working, or is it not compatible with XP?

  15. #15
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    quote:
    --------------------------------------------------------------------------------

    No, not if you're using a real (NT,2k,XP) version of Windows.

    --------------------------------------------------------------------------------



    Yes!!! Sang-drax is RIGHT!!! (He/She usually is)

    Yes, I know it doesn't play on Win98,95,etc. And yes he/she (plz clear this up for us snag!) is usually right but to say that the models besides NT,2k,and Xp aren't real is going a tad bit far.
    (Yes, I'm using Win98)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linux beep function
    By Calef13 in forum C++ Programming
    Replies: 7
    Last Post: 07-14-2007, 01:39 AM
  2. I need to play a pre recorded vox file using C
    By m.sudhakar in forum C Programming
    Replies: 4
    Last Post: 11-17-2006, 06:59 PM
  3. alert beep sound
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 02-14-2006, 02:24 PM
  4. Beep();
    By SirCrono6 in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2004, 04:47 PM
  5. Something Like beep()
    By Trauts in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2003, 02:51 PM