can someone help with sounds and...

This is a discussion on can someone help with sounds and... within the Game Programming forums, part of the General Programming Boards category; Hello, I am creating a small game and I would like to include sound files in my game. I am ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Post can someone help with sounds and...

    Hello, I am creating a small game and I would like to include sound files in my game. I am pretty new to C++ and I am not sure how to do this.

    I am also looking for a good C++ game coding tutorial, something on the beginning level. Could someone point me in the right direction? Thanks

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Sounds eh.

    Do you want them as resources or what? If you want to know how to use sounds in DirectX I have code that will do it but it won't help you.

    I am pretty new to C++ and I am not sure how to do this.

    I am also looking for a good C++ game coding tutorial, something on the beginning level.
    Based on these two statements you are a long way from putting sound in anything. Learn C/C++ really well and then DirectX (for sounds) or OpenGL/Direct3D (for graphics).

    I cannot explain how to do sounds in DirectX here. I recommend the book DirectX9 Audio Exposed. Excellent book.

    My complete sound source is available for d/l here in this forum - do a board search.

  3. #3
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    I agree with Budda, but if you really want to add some sound to your game, learn some Win32 and use the PlaySound function. The sound wont be good but it will probibly work for what you are doing. But still learn DirectX becouse you will need it later down the road, even to do something as simple as making the computer play two sounds at the same time.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Well if you could in real-time convert all your sounds into one wave file you could use PlaySound to play multiple sounds.

    Multiple sounds are accomplished, usually, by simply adding all the samples together at any point in time and dividing by the number of samples. As frequencies lay over the top of one another and are added in - each distinct sound will be heard and audible to the human ear.

    If you are talking and someone talks at the same time or a noise happens in the background - your ear does not have multiple digital channels now does it? No. The sound waves hit your eardrum at the same time. Actually the sound waves mix and mesh with one another and it all comes roaring into your ear. Now the way your ear sends the signal to your brain and the way an analog to digital converter works are very different. But same concept.

    Multiple channels can be accomplished by doing this:

    This code will play 10 sounds at the same time in a 255 byte sound buffer. This is about 20 milliseconds of sound at 44,100 hz.

    Code:
    WORD PrimaryBuffer[255];
    
    struct SoundBuffer
    {
      WORD *pBuffer;
      DWORD dwLength;
    }
    
    void AdditiveMixSounds(SoundBuffer *pSoundBuffers,WORD iNumBuffers,WORD iBufferPos)
    {
      WORD FinalSampleValue=0;
      BYTE CurrentBufferNum=0;
      BYTE PrimaryBufferPos=0;
      
      do
      {
         do
         {
            if (iBufferPos<pSoundBuffers[CurrentBufferNum].dwLength)
            {
              FinalSampleValue+=pSoundBuffers[CurrentBufferNum].pBuffer[iBufferPos];
              //Underflow - signal clip low
              if (FinalSampleValue<0) FinalSampleValue=0;
    
              //Overflow - signal clip high
              if (FinalSampleValue>=65535) FinalSampleValue=65535;
            }
            CurrentBufferNum++;
         } while (CurrentBufferNum<iNumBuffers);
    
          //Additive mixing - add up samples, divide by number of samples
          FinalSampleValue/=iNumBuffers;
          CurrentBufferNum=0;
          iBufferPos++;
      
          //Write out final sample to buffer - this buffer will be 'played'
          PrimaryBuffer[PrimaryBufferPos]=FinalSampleValue;
          PrimaryBufferPos++;
      } while (PrimaryBufferPos<255);
    }
    Now if you sent PrimaryBuffer to the DMA to be streamed to the sound card - you would hear all the sounds at the same time in one stream of data.

    This is how a sound board mixer works as well. You have all these instruments and microphones all plugged into one board. It takes the signal from each input and mixes it with the others through some mixing algorithm - probably not additive.

    Additive mixing works, but it is not the best. The more sounds you add the quieter the resulting sound is. Also note that I'm clamping the high and low values. This is the same as producing distortion (essentially the waveform has ventured beyond the boundaries of what our system can handle) in sound. This code will simply clip the values which is probably the best thing to do. Wrapping them is very ugly.
    Last edited by VirtualAce; 03-08-2005 at 08:37 AM.

  5. #5
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195

    Thumbs up

    Cool i will have to implement that concept into future games i make untill i learn DirectX, thanks Budda

  6. #6
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    For the now, you could use FMOD. Here is a simple tutorial for it. Yes, you should do the things that Bubba explained when you are that far into programming, but for now this is a good cross-platform sound API.

    - SirCrono6

    P.S. See , lots of people use it
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  7. #7
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    I spend lot of time making classes to use with FMOD, so that it's as easy as:

    Code:
    CSound s1;
    s1.init("song.mp3", SAMPLE, LOOP_ON);
    s1.play();
    If you'rte interested, I could whip one up for you.
    This war, like the next war, is a war to end war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sounds in game
    By Gordon in forum Windows Programming
    Replies: 7
    Last Post: 10-07-2008, 11:14 AM
  2. Looking for tiles and sounds
    By ~Kyo~ in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 11-13-2004, 05:32 PM
  3. Replies: 7
    Last Post: 06-20-2003, 01:05 PM
  4. Sounds
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-17-2003, 07:27 AM
  5. Sounds, or no sounds?
    By face_master in forum C++ Programming
    Replies: 3
    Last Post: 09-03-2001, 06:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21