我有以下代码片段。我需要在哪里获取音频样本并相应地播放它们。
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;
这是我的回调函数。
void fillerup(void *unused, Uint8 *stream, int len)
{
Uint8 *waveptr;
int waveleft=0;
printf("in fillerup");
waveptr = wave.sound + wave.soundpos;
waveleft = wave.soundlen - wave.soundpos;
while ( waveleft <= len ) {
/* Process samples */
Uint8 *process_buf = (Uint8 *)malloc(waveleft * sizeof(Uint8));
if(process_buf == 0) {
// do something here
}
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;
free(process_buf);
}
}
在我的主要我有这个代码。
if ( SDL_LoadWAV("file1.wav",&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
fprintf(stderr, "Couldn't load %s: %s\n", "file1.wav", SDL_GetError());
//quit(1);
}
// set up the callback function
wave.spec.callback = fillerup;
我已经评论了这段代码,因为每当我打开它时,都会出现无法打开音频的错误。最上面的 LOADWav 没有给我错误并检查 wav 文件是否存在。
/*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);
不做回调的问题可能是什么?