Multimedia
  Home arrow Multimedia arrow Page 4 - Learning Sound for Game Programming us...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
OLM
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MULTIMEDIA

Learning Sound for Game Programming using SDL
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-03-27

    Table of Contents:
  • Learning Sound for Game Programming using SDL
  • Opening the audio
  • Playing the Audio
  • Playing the sound in the real world

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Virtual Tradeshows by Ziff Davis Enterprise – A Unique Opportunity to Interact with IT Experts, Access Information, and Gain Insight on Today’s Trends in Technology Learn more

    Learning Sound for Game Programming using SDL - Playing the sound in the real world
    (Page 4 of 4 )

    Up to now I have shown you the code snippets. Now it's time for a full fledged application. So here goes.

    First the includes:

    #include "SDL.h"
    #include "SDL_audio.h"

    Then comes the main part and opening the audio:

    int main()
    {
     
    extern void mixaudio(void *unused, Uint8 *stream, int
    len);
     
    SDL_AudioSpec fmt;

      /* Set 16-bit stereo audio at 22Khz */
     
    fmt.freq = 22050;
     
    fmt.format = AUDIO_S16;
     
    fmt.channels = 2;
     
    fmt.samples = 512; /* A good value for games */
     
    fmt.callback = mixaudio;
     
    fmt.userdata = NULL;

      /* Open the audio device and start playing sound! */
     
    if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
       
    fprintf(stderr, "Unable to open audio: %sn",
    SDL_GetError());
       
    exit(1);
     
    }

      //can call other functions like mixing and playing
    functions
     
    :
     
    PlaySound("start.wav");
     
    :
     
    SDL_CloseAudio();//closes the audio and setting the fmt
    to null
    }

    The next part is playing the wav file. For this we need a structure that keeps track of the current sound data, position and length. The following is the structure:

    #define NUM_SOUNDS 2
    struct sample {
     
    Uint8 *data;
     
    Uint32 dpos;
     
    Uint32 dlen;
    } sounds[NUM_SOUNDS];

    The next part is playing the file. The function goes likes this:

    void PlaySound(char *file)
    {
     
    int index;
     
    SDL_AudioSpec wave;
     
    Uint8 *data;
     
    Uint32 dlen;
     
    SDL_AudioCVT cvt;

      /* Look for an empty (or finished) sound slot */
     
    for ( index=0; index<NUM_SOUNDS; ++index ) {
       
    if ( sounds[index].dpos == sounds[index].dlen ) {
         
    break;
       
    }
     
    }
     
    if ( index == NUM_SOUNDS )
       
    return;

     
    /* Load the sound file and convert it to 16-bit stereo
    at 22kHz */
     
    if ( SDL_LoadWAV(file, &wave, &data, &dlen) == NULL ) {
       
    fprintf(stderr, "Couldn't load %s: %sn", file,
    SDL_GetError());
       
    return;
     
    }
     
    SDL_BuildAudioCVT(&cvt, wave.format, wave.channels,
    wave.freq,
    AUDIO_S16, 2, 22050);
     
    cvt.buf = malloc(dlen*cvt.len_mult);
     
    memcpy(cvt.buf, data, dlen);
     
    cvt.len = dlen;
     
    SDL_ConvertAudio(&cvt);
     
    SDL_FreeWAV(data);

      /* Put the sound data in the slot (it starts playing
    immediately) */
     
    if ( sounds[index].data ) {
       
    free(sounds[index].data);
     
    }
     
    SDL_LockAudio();
     
    sounds[index].data = cvt.buf;
     
    sounds[index].dlen = cvt.len_cvt;
     
    sounds[index].dpos = 0;
     
    SDL_UnlockAudio();
    }

    Finally we have to define the callback function for the SDL_AudioSpec which is:

    void mixaudio(void *unused, Uint8 *stream, int len)
    {
     
    int i;
      Uint32 amount;
     
    for ( i=0; i<NUM_SOUNDS; ++i ) {
       
    amount = (sounds[i].dlen-sounds[i].dpos);
       
    if ( amount > len ) {
         
    amount = len;
       
    }
       
    SDL_MixAudio(stream, &sounds[i].data[sounds[i].dpos],
    amount, SDL_MIX_MAXVOLUME);
       
    sounds[i].dpos += amount;
     
    }
    }

    That brings us to the end of this article. I have left several aspects unexplained. The reason is that just explaining them as stand alone functions wouldn't do any good. They have to be understood in the context of rendering and scenes. SDL_MixAudio for mixing and the APIs for timer, threading, networking and CD-ROM access are among such functions. In the next article in this ongoing series I will be moving towards rendering using OpenGL with SDL as the base framework. In the rendering and animations, the real utility of the above mentioned APIs will be revealed. Till next time...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Sound is one of the most important components of any game. In this article I have...
     

       

    MULTIMEDIA ARTICLES

    - Working with Colors in OpenGL for Game Progr...
    - Animation in OpenGL for Game Programming usi...
    - Game Programming with SDL: Getting Started w...
    - Using OpenGL with SDL for Game Programming
    - Learning Sound for Game Programming using SDL
    - Game Programming using SDL: Raw Graphics and...
    - Game Programming using SDL: Getting Started
    - Network Radio With Icecast
    - Learning To SMILe

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway