SDL2.0 plays a sound file in a loop and adds a comment.
/* load and loop a WAV file *//* loopwaves.c is much more robust in handling WAVE files--this are only for simple waves*/#include " Sdl_config.h "#include <stdio.h> #include <stdlib.h> #if have_signal_h#include <signal.h> #endif # Include "SDL.h" #include "sdl_audio.h"//sdl play buffer definition Struct{sdl_audiospec spec; Uint8 *sound; /* Pointing to the sound data pointer */uint32 Soundlen; /* Length of wave data */int soundpos; /* Current Play position */} wave;/* call this function to exit the SDL subsystem */static voidquit (int rc) {sdl_quit (); exit (RC);} void Sdlcallfillerup (void *unused, Uint8 * stream, int len) {Uint8 *waveptr;int waveleft;/* set current sound pointer sound pointer + current playback position */waveptr = Wave.sound + wave.soundpos;//remaining sound data sound data length-current playback Position waveleft = wave.soundlen-wave.soundpos;/* If remaining data < required length */while (Waveleft <= Len) {//Copy remaining data to audio buffer sdl_memcpy (stream, waveptr, waveleft);//audio buffer stream pointer moves back stream + = Waveleft;//len=len-waveleftlen-= waveleft;//Copy play remaining data//waveptr point to sound data start waveptr = wave.sound;//length equals len Waveleft = Wave.soundlen;//position equals Lenwave.soundpos = 0;} Re-copy memory to audio device buffer//That is, loop play sdl_memcpy (stream, waveptr, Len); Current playback position +=lenwave.soundpos + = Len; static int done = 0;//exit signal voidpoked (int sig) {done = 1;} Intmain (int argc, char *argv[]) {int I;char filename[4096];/* Enable standard application Logging */sdl_logsetpriority ( Sdl_log_category_application, Sdl_log_priority_info);/* Initialize the SDL Audio library */if (Sdl_init (Sdl_init_audio) < 0) {SDL_ LogError (sdl_log_category_application, "couldn ' t initialize SDL:%s\n", Sdl_geterror ()); return (1);} Copy the file name to the filename buffer if (argc >= 1) {sdl_strlcpy (filename, argv[1], sizeof (filename));} else {sdl_strlcpy (filename, "D:\\vs2010_pro\\ffmpeg_test\\output\\audio.aac", sizeof (filename));} /* Load the sound file into memory */if (sdl_loadwav (filename, &wave.spec, &wave.sound, &wave.soundlen) = = NULL) {Sdl_logerror ( Sdl_log_category_application, "couldn ' t load%s:%s\n", Argv[1], sdl_geterror ()); Quit (1);} The audio callback fills the data in the callback function with the size of the audio buffer requirement wave.spec.callback = Fillerup; #if have_signal_h/* Set the sigNals */#ifdef sighupsignal (SIGHUP, poked); #endifsignal (SIGINT, poked); #ifdef sigquitsignal (Sigquit, poked); # Endifsignal (SIGTERM, poked); #endif/* Have_signal_h *//* Show The list of available drivers */sdl_log ("Available audio dri Vers: ");//print all audio drivers on the computer for (i = 0; I < sdl_getnumaudiodrivers (); ++i) {Sdl_log ("%i:%s", I, Sdl_getaudiodriver (i));} /* Initialize the audio device and turn it on because our output WAV has header information all we do not need to set the parameter */if (Sdl_openaudio (&wave.spec, NULL) < 0) {Sdl_logerror (sdl_log_ Category_application, "couldn ' t Open audio:%s\n", Sdl_geterror ()); Sdl_freewav (Wave.sound); Quit (2);} Print the current audio device Sdl_log ("Using Audio driver:%s\n", Sdl_getcurrentaudiodriver ());/* Start playing the sound this time, SDL takes the data in an asynchronous thread-by-loop callback, */sdl_ Pauseaudio (0);//If you are currently playing then the main thread Delay 1s//or have an exit signal then exit while (!done && (sdl_getaudiostatus () = = Sdl_audio_playing )) Sdl_delay (1000);/* Turn off audio */sdl_closeaudio ();//Release allocated sound data memory sdl_freewav (Wave.sound); Exit the program and Sdlsdl_quit (); return (0);}
SDL2.0 Example Code Analysis---Loop to play WAV Loopwav