Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static Uint8 *audio_pos; // global pointer to the audio buffer to be played
- static Uint32 audio_len; // remaining length of the sample we have to play
- void my_audio_callback(void *userdata, Uint8 *stream, int len) {
- if (audio_len == 0)
- return;
- len = (len > audio_len ? audio_len : len);
- SDL_memcpy (stream, audio_pos, len); // simply copy from one buffer into the other
- SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);// mix from one buffer into another
- audio_pos += len;
- audio_len -= len;
- }
- void playAudio(std::string path)
- {
- // local variables
- static Uint32 wav_length; // length of our sample
- static Uint8 *wav_buffer; // buffer containing our audio file
- static SDL_AudioSpec wav_spec; // the specs of our piece of music
- /* Load the WAV */
- // the specs, length and buffer of our wav are filled
- if (SDL_LoadWAV(path.c_str(), &wav_spec, &wav_buffer, &wav_length) == NULL)
- {
- QuitOnSDLError();
- }
- // set the callback function
- wav_spec.callback = my_audio_callback;
- wav_spec.userdata = NULL;
- wav_spec.samples = 1024;
- // set our global static variables
- audio_pos = wav_buffer; // copy sound buffer
- audio_len = wav_length; // copy file length
- /* Open the audio device */
- if (SDL_OpenAudio(&wav_spec, NULL) < 0) {
- fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
- exit(-1);
- }
- /* Start playing */
- SDL_PauseAudio(0); //pauze op 0 zetten
- // wait until we're done playing
- while (audio_len > 0) {
- SDL_Delay(100);
- }
- // shut everything down
- SDL_CloseAudio();
- SDL_FreeWAV(wav_buffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement