Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. static Uint8 *audio_pos; // global pointer to the audio buffer to be played
  2. static Uint32 audio_len; // remaining length of the sample we have to play
  3.  
  4. void my_audio_callback(void *userdata, Uint8 *stream, int len) {
  5.  
  6. if (audio_len == 0)
  7. return;
  8.  
  9. len = (len > audio_len ? audio_len : len);
  10.  
  11. SDL_memcpy (stream, audio_pos, len); // simply copy from one buffer into the other
  12.  
  13. SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);// mix from one buffer into another
  14.  
  15. audio_pos += len;
  16. audio_len -= len;
  17. }
  18.  
  19. void playAudio(std::string path)
  20. {
  21.  
  22.  
  23. // local variables
  24. static Uint32 wav_length; // length of our sample
  25. static Uint8 *wav_buffer; // buffer containing our audio file
  26. static SDL_AudioSpec wav_spec; // the specs of our piece of music
  27.  
  28.  
  29. /* Load the WAV */
  30. // the specs, length and buffer of our wav are filled
  31. if (SDL_LoadWAV(path.c_str(), &wav_spec, &wav_buffer, &wav_length) == NULL)
  32. {
  33. QuitOnSDLError();
  34. }
  35. // set the callback function
  36. wav_spec.callback = my_audio_callback;
  37. wav_spec.userdata = NULL;
  38. wav_spec.samples = 1024;
  39. // set our global static variables
  40. audio_pos = wav_buffer; // copy sound buffer
  41. audio_len = wav_length; // copy file length
  42.  
  43. /* Open the audio device */
  44. if (SDL_OpenAudio(&wav_spec, NULL) < 0) {
  45. fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
  46. exit(-1);
  47. }
  48.  
  49. /* Start playing */
  50. SDL_PauseAudio(0); //pauze op 0 zetten
  51.  
  52. // wait until we're done playing
  53. while (audio_len > 0) {
  54. SDL_Delay(100);
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61. // shut everything down
  62. SDL_CloseAudio();
  63. SDL_FreeWAV(wav_buffer);
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement