Advertisement
Guest User

Untitled

a guest
May 30th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. cat sdl_audio.cpp
  2. #include <SDL2/SDL.h>
  3.  
  4. #define MUS_PATH "test.wav"
  5.  
  6. // prototype for our audio callback
  7. // see the implementation for more information
  8. void my_audio_callback(void *userdata, Uint8 *stream, int len);
  9.  
  10. // variable declarations
  11. static Uint8 *audio_pos; // global pointer to the audio buffer to be played
  12. static Uint32 audio_len; // remaining length of the sample we have to play
  13.  
  14.  
  15. /*
  16. ** PLAYING A SOUND IS MUCH MORE COMPLICATED THAN IT SHOULD BE
  17. */
  18. int main(int argc, char* argv[]){
  19.  
  20.         // Initialize SDL.
  21.         if (SDL_Init(SDL_INIT_AUDIO) < 0)
  22.                         return 1;
  23.  
  24.         // local variables
  25.         static Uint32 wav_length; // length of our sample
  26.         static Uint8 *wav_buffer; // buffer containing our audio file
  27.         static SDL_AudioSpec wav_spec; // the specs of our piece of music
  28.  
  29.  
  30.         /* Load the WAV */
  31.         // the specs, length and buffer of our wav are filled
  32.         if( SDL_LoadWAV(MUS_PATH, &wav_spec, &wav_buffer, &wav_length) == NULL ){
  33.           return 1;
  34.         }
  35.         // set the callback function
  36.         wav_spec.callback = my_audio_callback;
  37.         wav_spec.userdata = NULL;
  38.         // set our global static variables
  39.         audio_pos = wav_buffer; // copy sound buffer
  40.         audio_len = wav_length; // copy file length
  41.  
  42.         /* Open the audio device */
  43.         if ( SDL_OpenAudio(&wav_spec, NULL) < 0 ){
  44.           fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
  45.           exit(-1);
  46.         }
  47.  
  48.         /* Start playing */
  49.         SDL_PauseAudio(0);
  50.  
  51.         // wait until we're don't playing
  52.         while ( audio_len > 0 ) {
  53.                 SDL_Delay(100);
  54.         }
  55.  
  56.         // shut everything down
  57.         SDL_CloseAudio();
  58.         SDL_FreeWAV(wav_buffer);
  59.  
  60. }
  61.  
  62. // audio callback function
  63. // here you have to copy the data of your audio buffer into the
  64. // requesting audio buffer (stream)
  65. // you should only copy as much as the requested length (len)
  66. void my_audio_callback(void *userdata, Uint8 *stream, int len) {
  67.  
  68.         if (audio_len ==0)
  69.                 return;
  70.  
  71.         len = ( len > audio_len ? audio_len : len );
  72.         //SDL_memcpy (stream, audio_pos, len);                                  // simply copy from one buffer into the other
  73.         SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);// mix from one buffer into another
  74.  
  75.         audio_pos += len;
  76.         audio_len -= len;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement