Multimedia
  Home arrow Multimedia arrow Page 3 - Learning Sound for Game Programming using SDL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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? 
Google.com  
MULTIMEDIA

Learning Sound for Game Programming using SDL
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Learning Sound for Game Programming using SDL - Playing the Audio
    ( Page 3 of 4 )



    Playing the audio not only means filling the buffer with required data but also loading the audio file to be played. The functions required are the callback function and the file playing functions. The file playing functions include SDL_LoadWav, SDL_BuildAudioCVT, SDL_ConvertAudio and SDL_FreeWAV.

    SDL_LoadWav loads a wav file and returns the given SDL_AudioSpec with the corresponding data filled. The first parameter is the name of the wav file. The second is SDL_AudioSpec. If successful the third parameter would contain a malloc'd buffer that contains the audio data and the last parameter would have the length of the malloc'd audio buffer. In code it would be:

    SDL_AudioSpec wave;
    Uint8 *data;
    Uint32 dlen;
    char *file;
    SDL_LoadWAV(file, &wave, &data, &dlen);


    The above code would load the file represented by file into the data and set its specifications into wave and the length of the buffer into dlen.

    To actually use the data it must be converted, for which SDL_AudioCVT structure is used. This structure must be initialized. The function to initialize the structure is SDL_BuildAudioCVT. The parameters are a pointer to the SDL_AudioCVT structure, a format of the source in UInt16, channels in the source in UInt8, the rate of the sample in int, a format of the destination in UInt16, channels in the destination in UInt8, the rate of the sample of destination in int where the source and destination are the formats of conversion. In code:

    SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16,2, 22050);

    where cvt is the SDL_AudioCVT structure, wave.format, wave.channels, wave.freq are the format, channels and frequency of source format and AUDIO_S16,2, 22050 are the format, channels and frequency of destination format. Discussing SDL_AudioCVT is beyond the scope of this article. I will be discussing it in the near future.

    The SDL_ConvertAudio function converts one format of audio to another. It takes only one parameter, the previously initialized SDL_AudioCVT. It converts the data pointed to by the buffer of the SDL_AudioCVT member. To understand it fully let's have a look at some detailed code. The comments are self explanatory:

    SDL_AudioSpec *desired, *obtained;
    SDL_AudioSpec wav_spec;
    SDL_AudioCVT wav_cvt;
    Uint32 wav_len;
    Uint8 *wav_buf;
    int ret;

    /* Allocated audio specs */
    desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
    obtained=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));

    /* Set desired format */
    desired->freq=22050;
    desired->format=AUDIO_S16LSB;
    desired->samples=8192;
    desired->callback=my_audio_callback;
    desired->userdata=NULL;

    /* Open the audio device */
    if ( SDL_OpenAudio(desired, obtained) < 0 ){
      fprintf(stderr, "Couldn't open audio: %sn", SDL_GetError());
      exit(-1);
    }
    free(desired);

    /* Load the test.wav */
    if( SDL_LoadWAV("test.wav", &wav_spec, &wav_buf,
    &wav_len) == NULL ){

      fprintf(stderr, "Could not open test.wav: %sn",
    SDL_GetError());

      SDL_CloseAudio();
      free(obtained);
      exit(-1);
    }

    /* Build AudioCVT */
    ret = SDL_BuildAudioCVT(&wav_cvt,
    wav_spec.format, wav_spec.channels, wav_spec.freq,
    obtained->format, obtained->channels, obtained->freq);

    /* Check that the convert was built */
    if(ret==-1){
      fprintf(stderr, "Couldn't build converter!n");
      SDL_CloseAudio();
      free(obtained);
      SDL_FreeWAV(wav_buf);
    }

    /* Setup for conversion */
    wav_cvt.buf=(Uint8 *)malloc(wav_len*wav_cvt.len_mult);
    wav_cvt.len=wav_len;
    memcpy(wav_cvt.buf, wav_buf, wav_len);

    /* We can delete to original WAV data now It is coming up
    next*/

    SDL_FreeWAV(wav_buf);

    /* And now we're ready to convert */
    SDL_ConvertAudio(&wav_cvt);

    /* do whatever */
    .

    Once building and conversion is done, the file loaded into the user data has to be released, as it is no longer required. The conversion provides it to the application as a part of the buffer of the SDL_AudioCVT buffer member. To release the memory occupied by user data, SDL_FreeWAV has to be used.

    In code:

    SDL_FreeWAV(wav_buf);

    That covers the functions. The next section will show how to use then to play the sound.



     
     
    >>> More Multimedia Articles          >>> More By A.P.Rajshekhar
     

       

    MULTIMEDIA ARTICLES

    - Basic Lighting in OpenGL and SDL Game Progra...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek