0
我有以下的代碼片段。我需要獲取音頻樣本並相應地播放它們。SDL音頻回撥不起作用?
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;
我已經評論這段代碼,因爲每當我打開它給我的錯誤couldnt打開音頻。作爲頂級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);
什麼可能是不回撥的問題?
@我想明白回調假設要做的每個樣本是什麼?這意味着它會得到一個模擬數據流?並在這裏做一個抽樣? – user2711681
@ user2711681 SDL會在需要更多音頻數據時調用您的回調,因爲在回放期間它將消耗數據(通過將數據發送到揚聲器)。您的應用程序將在回調中運行以生成更多數據,以保持播放聲音。 – unwind
.wav已經是數字格式了,我正確嗎?那麼回調會做什麼是獲得每個數據位或字節播放? – user2711681