Multimedia
  Home arrow Multimedia arrow Page 2 - 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 - Opening the audio
    ( Page 2 of 4 )



    To open anything, be it a file or a socket, certain data has to be passed to the environment such as file name, mode and so forth. Opening the audio is no different. The data required to be passed include frequency, format and more. In order to provide these to the environment, SDL_OpenAudio() is used. This method takes two parameters; both are references of type SDL_AudioSpec which is a structure. The members of this structure are:

    The freq is an integer representing the frequency of the sound to be played. It is measured in samples per second. The common values are 11025, 22050 and 44100. The higher the value, the higher the frequency and the better the quality.

    The format of the audio is represented by format. The data type is UInt16. The format means the size and type of samples being sent. The common value is AUDIO_S16. The other acceptable values include AUDIO_U16  and AUDIO_U8. The U stands for unsigned bits, the S stands for signed bits and the number represents the bits in the samples. Hence the value AUDIO_S16 represents a sample having 16 bits which are unsigned.

    Channels is a value that refers to the number of separate channels to be used. A value of 1 indicates that mono (single channel) and a value of 2 indicates that stereo channel must be used.

    Samples refers to the size of the audio buffer in samples.

    Callback takes the pointer to the function that would be used to fill the audio buffer. The function takes user data, stream and length of the user data as parameters.

    Apart from these, the other members include an unsigned 8 bit integer representing silence, UInt32 representing the size of the buffer and a void pointer to the user data.

    In code it would look like this:

      SDL_AudioSpec wanted;
      void fill_audio(void *udata, Uint8 *stream, int len);

      /* Set the audio format */
      wanted.freq = 22050;
      wanted.format = AUDIO_S16;
      wanted.channels = 2; /* 1 = mono, 2 = stereo */
      wanted.samples = 1024; /* Good low-latency value for
    callback */

      wanted.callback = fill_audio;
      wanted.userdata = NULL;

    where the frequency is 22050, the format to be used is in 16 bit unsigned integer, the channel is stereo, the size of the audio buffer in the sample is 1024 and the function is fill_audio. There is user data to be passed. The next step is playing the audio.



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek