当我在Emscripten编译的C ++环境中调用SDL_PauseAudio(1)
时,在声音停止发出扬声器之前会有大约一秒左右的延迟。
如何避免这种延迟,并立即暂停?
C ++代码如下:
#include <iostream>
#include <cmath>
#include <emscripten.h>
#include <SDL_audio.h>
using std::cerr;
using std::endl;
using std::string;
int count = 0;
int paused = true;
void getMoreAudio(void *, Uint8 *stream, int len) {
for (auto i=0; i<len; i++) {
stream[i] = static_cast<Uint8>(sin((count + i) * M_PI / 400.00) * 128);
}
count = count + len;
}
extern "C" EMSCRIPTEN_KEEPALIVE void togglePause() {
paused = !paused;
SDL_PauseAudio(paused ? 1 : 0);
}
int main() {
SDL_AudioSpec desired;
SDL_AudioSpec obtained;
desired.freq = 48000;
desired.format = AUDIO_U8;
desired.channels = 1;
desired.samples = 4096;
desired.padding = 0;
desired.size = 8192;
desired.callback = &getMoreAudio;
desired.userdata = nullptr;
auto res = SDL_OpenAudio(&desired, &obtained);
SDL_PauseAudio(paused ? 1 : 0);
emscripten_set_main_loop([](){}, 0, false);
return 0;
}
我在http://plnkr.co/edit/O4XbXYBXHg4ZwUWB5puY?p=preview设置了一个Plunker。通过按下按钮启动音频似乎是即时的,但暂停不会。
我观察到Linux + Mac上Chrome的延迟。