Thread: Sound in C++?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Location
    MA
    Posts
    7

    Arrow Sound in C++?

    I've programmed in QBASIC for years but now I am learning C++ since it is quite superior.

    How does one make music with the PC speaker? In BASIC it was pretty simple, is there a similar set of commands for C++?

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Under DOS there were the sound (freq) and nosound () functions.

    Don't know how portable they are though...
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  3. #3
    Registered User
    Join Date
    Jul 2005
    Location
    MA
    Posts
    7
    I'm using the Borland compiler under Editplus. Would those work, and if so how exactly do I use them?

    I'm a C++ newb so sorry for the easy questions.

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Simply call sound () with the frequency you need and call nosound to stop the sound.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  5. #5
    Registered User
    Join Date
    Jul 2005
    Location
    MA
    Posts
    7
    I think I need some packages for this. I found sound.h but when I include it I get a bunch of errors asking for config.h and a bunch of errors in lzexpand.h

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    First - There is no sound in ANSI/ISO standard (i.e. portable) C++. And while we're at it, there are no graphics, mouse, or color either.

    So, all this stuff is system & compiler specific.

    In a modern MS-Windows system there is no easy way to make sounds from the PC's internal speaker. If you don't have a sound card, beep() will work. If you do have a sound card, beep() will play the Windows Default sound from the external speakers.

    On an older system (i.e. Win98) you can write to the registers that control frequency and duration of sound from the speaker. Newer operating systems "protect" the hardware from direct access by your user-mode programs. (You have to write a driver.) You'll get an error if you try to run your Q-BASIC speaker-sound program in a "DOS window" on Win2000 or WinXP.

    The easiest way to get sound is to use playsound() to play a wave file thru your soundcard and external speakers.

    [EDIT] ---------------------------------------------

    And, I have more bad news. You can't mix & match .h files from different compilers. See this FAQ.
    Last edited by DougDbug; 07-08-2005 at 06:27 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This code will produce a noise, probably a Windows sound. Unless the sound's off.

    Code:
    cout << "\a";
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sound lags in multi-thread version
    By VirtualAce in forum Game Programming
    Replies: 23
    Last Post: 08-27-2008, 11:54 AM
  2. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 01:58 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Help me modify my Sound Server
    By zdude in forum C Programming
    Replies: 1
    Last Post: 05-14-2003, 05:15 PM
  5. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:34 PM