I am trying to write a program that plays an audio file but on the event that ‘r’ is pressed the audio will skip back to the start and play again.
Here is what i have so far but it doesn’t change the audio at all it just keep playing through.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include “SDL/SDL_config.h”
#include “SDL/SDL.h”
#include "SDL/SDL_audio.h"
struct {
SDL_AudioSpec spec; /* SDL Audio Spec Structure /
Uint8 sound; / Pointer to wave data /
Uint32 soundlen; / Length of wave data /
int soundpos; / Current play position /
} wave;
static int done = 0;
void poked(int sig)
{
done = 1;
}
/ Call this instead of exit(), so we can clean up SDL: atexit() is evil. /
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
/
- This is a call-back function that will be automatically called by SDL
-
@param
*/
void SDLCALL fillerup(void unused, Uint8 stream, int len)
{
Uint8 waveptr;
int waveleft;
/ Set up the pointers /
waveptr = wave.sound + wave.soundpos;
waveleft = wave.soundlen - wave.soundpos;
/ Go! /
while ( waveleft <= len )
{
/ Process samples /
Uint8 process_buf[waveleft];
SDL_memcpy(process_buf, waveptr, waveleft);
/ do processing here, e.g. /
/ processing the audio samples in process_buf[] /
// play the processed audio samples
SDL_memcpy(stream, process_buf, waveleft);
stream += waveleft;
len -= waveleft;
// ready to repeat play the audio
waveptr = wave.sound;
waveleft = wave.soundlen;
wave.soundpos = 0;
}
/ Process samples /
Uint8 process_buf[len];
SDL_memcpy(process_buf, waveptr, len);
/ do processing here, e.g. /
/ processing the audio samples in process_buf[] */
// play the processed samples
SDL_memcpy(stream, process_buf, len);
wave.soundpos += len;
}
static const int SCR_WIDTH = 240;
static const int SCR_HEIGHT = 180;
void playAudio(char argv[], int argc)
{
char name[32];
SDL_Surface screen;
/ Load the SDL library /
if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 )
{
fprintf(stderr, “Couldn’t initialize SDL: %s\n”,SDL_GetError());
return;
}
if ( argc <=1 )
{
fprintf(stderr, “Usage %s \n”, argv[0]);
return;
}
/ Make a screen for visual information & for accepting keyboard events*/
#ifndef DARWIN
screen = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT, 0, 0);
#else
screen = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT, 24, 0);
#endif
if (!screen)
{
fprintf(stderr, “SDL: could not set video mode - exiting\n”);
quit(1);
}
/* Load the wave file into memory /
if ( SDL_LoadWAV(argv,&wave.spec, &wave.sound, &wave.soundlen) == NULL )
{
fprintf(stderr, “Couldn’t load %s: %s\n”, argv[1], SDL_GetError());
quit(1);
}
// set up the callback function
wave.spec.callback = fillerup;
#if HAVE_SIGNAL_H
/ Set the signals /
#ifdef SIGHUP
signal(SIGHUP, poked);
#endif
signal(SIGINT, poked);
#ifdef SIGQUIT
signal(SIGQUIT, poked);
#endif
signal(SIGTERM, poked);
#endif / HAVE_SIGNAL_H /
/ Initialize fillerup() variables /
if (SDL_OpenAudio(&wave.spec, NULL) < 0 )
{
fprintf(stderr, “Couldn’t open audio: %s\n”, SDL_GetError());
SDL_FreeWAV(wave.sound);
quit(2);
}
// start playing
SDL_PauseAudio(0);
/ Let the audio run */
printf(“Using audio driver: %s\n”, SDL_AudioDriverName(name, 32));
}
int main(int argc, char *argv[])
{
playAudio(argv[1], argc);
Uint8 *keys;
SDL_Event event;
while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
{
// Poll input queue, run keyboard loop
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT )
done = 1;
}
keys = SDL_GetKeyState(NULL);
if (keys[SDLK_q])
{
done = 1;
}
else if(keys[SDLK_r])
{
playAudio(argv[1], argc);
}
SDL_Delay(100);
}
/* Clean up on signal */
SDL_CloseAudio();
SDL_FreeWAV(wave.sound);
SDL_Quit();
return(0);
}
Thanks for the help. SDL is really doing my head in.