Thread: How do I get sound?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    7

    How do I get sound?

    In Turbo C++, how do I get sound to come out of my speakers? I know how to program it, but I can't hear anything.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What kind of sound? PC speaker or sound card?

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    7
    Whichever sound(1000) will work for. I believe it's the PC Speaker, so that one I guess.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The sound() function should work. You must then use nosound() to turn off the sound. In order for it to be useful you will need to set up some kind of timer.

    An eaisy way would be to only hook the PIT timer interrupt. When the interrupt is generated and control is handed to your ISR, stop the sound. This will only work for 1 sound and not a chunk or block of sounds. If you reprogram the buffer during the timer interrupt then your sound will stop and the quality will decrease (but PC speaker quality isn't good anyways). Using this method (and more) it is possible to generate digital sounds through the PC speaker, but the timing is not nearly as easy as it is with Sound Blaster programming. The main problem is that the computer does not generate an interrupt to stop the sound (to my knowledge). If it does or did, then you would need to hook the interrupt that it generates. Upon entering the ISR, then you could fill the buffer and begin playing. Again, I'm used to programming sound cards so this may not be totally accurate.

    You could also forget the ISR approach and set up a timer based on the frequency of the PIT timer similar to framerate counters. The problem here is that this is not very accurate. For accurate timing you must hook the PIT timer interrupt, interrupt 08h (PIT?), or interrupt 70h.

    I'm also not sure if the PC speaker executes in the background or not. Sometimes it seems to, and others it does not. It will be very hard to find any docs on PC speaker programming as it is very old school. Castle Wolfenstein (not the 3d one), Mean Streets, and some others did use the PC speaker for digital sound, but those were programmed when the 8086, 80286, and 80386 were top of the line systems. Castle Wolfenstein was programmed for the IBM PCjr way back in 1982. I have not been able to find ANY (I never use capitals, but I stress ANY) docs on programming digital sound through the PC speaker.

    Your best bet is to check out www.creative.com and go to their developer section. You do not have to log in or be a member to download their old DOS documents. Download all of their docs relating to DOS Sound Blaster programming. If you do not understand interrupt service routines, check out Randall Hyde's art of assembly language programming doc (book) on the net or go to www.programmersheaven.com and look for source concerning sound card programming. Also www.gamedev.net may have some info as well as The Programmer's Lair (they have Ethan Brodsky's code for the sound card) - forget the URL. You can link to it from programmersheaven and maybe from this board's links section.

    Sorry so long, but there is a lot involved if you want to make more than bleeps and blips from the PC speaker. That might be good for pong on the Atari 2600, but not for the PC.

    If you are dead set on PC speaker, you will need a formula to convert from analog to digital and back again.

  5. #5
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859
    or be cheap and use the printf("\a")
    Yoshi

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I did some research and did find some PC speaker programming docs, much to my surprise. There is a doc on www.gamedev.net about the PC speaker and it explains how to send 8-bit sound through the PC speaker. It is in their articles and resources section under sound programming (non-DirectX).

  7. #7
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Just curious, but it is possible to generate sounds using the PC speaker in Windows? It'd be nice if the speaker was listed as a multimedia device (yes, I know there are third-party drivers for it... :P)

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You could also (if you have no life like me!) get down into DEBUG or something, and code an assembler function yourself. ASCII #7 is a beep in console programming, I'm not sure about Windows. That's what I always do - I guess I like being self-sufficient! (because i have no life).

  9. #9
    Registered User JTtheCPPgod's Avatar
    Join Date
    Dec 2001
    Posts
    44
    Originally posted by Bubba

    Sorry so long, but there is a lot involved if you want to make more than bleeps and blips from the PC speaker. That might be good for pong on the Atari 2600, but not for the PC.
    So what if all I want is blips and bleeps from the PC? I'm not understanding how to achieve even that.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I forgot to actually SHOW the assembler code for playing a beep in my above reply.

    MOV DL,07
    MOV AH,02
    INT 21
    INT 20

    Hope this helps.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    sound (freqinhertz);
    delay(sometimelimit);
    nosound();

    If delay is not avail. just use a loop and count to a certain number. For timing either use the PIT or difftime().

  12. #12
    Registered User JTtheCPPgod's Avatar
    Join Date
    Dec 2001
    Posts
    44
    Are there any libraries i need to include for those to work? Cos they're not.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    They are in dos.h

    If you do not have a DOS compiler, you will not be able to use these.

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