aboutsummaryrefslogtreecommitdiff
path: root/backends/mixer
diff options
context:
space:
mode:
authorColin Snover2017-09-05 00:57:14 -0500
committerColin Snover2017-09-12 11:30:01 -0500
commit4a75fbab1b508f4240615c62811e2c8828534177 (patch)
tree1f8d878f4ab2a9a60f1958bdb0e33b981010ecf7 /backends/mixer
parenteb4e9fe1d48bee1c9f641b019a29642780604b30 (diff)
downloadscummvm-rg350-4a75fbab1b508f4240615c62811e2c8828534177.tar.gz
scummvm-rg350-4a75fbab1b508f4240615c62811e2c8828534177.tar.bz2
scummvm-rg350-4a75fbab1b508f4240615c62811e2c8828534177.zip
SDL: Reduce audio playback latency
The previous default buffer size of 4096 samples for 44kHz mixer would add up to 93ms of audio latency, which is fine for early adventure games, but this is significantly more latency than is acceptable for games with full motion video. For these games, the latency needs to be kept within roughly +15ms and -45ms of video frame presentation to avoid lip sync problems. With this change, the default audio buffer size is calculated to be 1024 samples at 44kHz (which happens to match what DOSBox uses). There is a possibility that the reduced latency may cause issues that did not previously exist with things like the MT-32 emulator, where a larger buffer size allowed for a larger window where high-complexity synthesis that could not be generated in realtime could be balanced out by low-complexity synthesis that could be generated faster than realtime. In this case, rather than increasing the system mixer buffer size again, please move the MT-32 emulator into its own thread and give it its own larger ring buffer into which it can generate more sample data in advance, independently from the rest of the audio system. For other systems where this buffer size reduction might cause a problem with audio drop-outs, a new audio_buffer_size configuration option has been added to allow users to tweak the audio buffer size to match their machine's ability to generate audio samples. Fixes Trac#10033. Also improves playback of samples in SCI that were programmed to restart across several consecutive frames, relying on lower audio latency in the original engine for this to not sound bad, like the hopping sound at the start of chapter 1 of KQ7, and the sound of turning on the power in the digger train in the Lighthouse volcano.
Diffstat (limited to 'backends/mixer')
-rw-r--r--backends/mixer/sdl/sdl-mixer.cpp46
1 files changed, 38 insertions, 8 deletions
diff --git a/backends/mixer/sdl/sdl-mixer.cpp b/backends/mixer/sdl/sdl-mixer.cpp
index 5a31feb4dc..11a45ebcb2 100644
--- a/backends/mixer/sdl/sdl-mixer.cpp
+++ b/backends/mixer/sdl/sdl-mixer.cpp
@@ -125,6 +125,24 @@ void SdlMixerManager::init() {
startAudio();
}
+static uint32 roundDownPowerOfTwo(uint32 samples) {
+ // Public domain code from Sean Eron Anderson
+ // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
+ uint32 rounded = samples;
+ --rounded;
+ rounded |= rounded >> 1;
+ rounded |= rounded >> 2;
+ rounded |= rounded >> 4;
+ rounded |= rounded >> 8;
+ rounded |= rounded >> 16;
+ ++rounded;
+
+ if (rounded != samples)
+ rounded >>= 1;
+
+ return rounded;
+}
+
SDL_AudioSpec SdlMixerManager::getAudioSpec(uint32 outputRate) {
SDL_AudioSpec desired;
@@ -139,19 +157,31 @@ SDL_AudioSpec SdlMixerManager::getAudioSpec(uint32 outputRate) {
if (freq <= 0)
freq = outputRate;
- // Determine the sample buffer size. We want it to store enough data for
- // at least 1/16th of a second (though at most 8192 samples). Note
- // that it must be a power of two. So e.g. at 22050 Hz, we request a
- // sample buffer size of 2048.
- uint32 samples = 8192;
- while (samples * 16 > freq * 2)
- samples >>= 1;
+ // One SDL "sample" is a complete audio frame (i.e. all channels = 1 sample)
+ uint32 samples = 0;
+
+ // Different games and host systems have different performance
+ // characteristics which are not easily measured, so allow advanced users to
+ // tweak their audio buffer size if they are experience excess latency or
+ // drop-outs by setting this value in their ScummVM config file directly
+ if (ConfMan.hasKey("audio_buffer_size", appDomain))
+ samples = ConfMan.getInt("audio_buffer_size", appDomain);
+
+ // 256 is an arbitrary minimum; 32768 is the largest power-of-two value
+ // representable with uint16
+ if (samples < 256 || samples > 32768)
+ // By default, hold no more than 45ms worth of samples to avoid
+ // perceptable audio lag (ATSC IS-191). For reference, DOSBox (as of Sep
+ // 2017) uses a buffer size of 1024 samples by default for a 16-bit
+ // stereo 44kHz mixer, which happens to be the next lowest power of two
+ // below 45ms.
+ samples = freq / (1000.0 / 45);
memset(&desired, 0, sizeof(desired));
desired.freq = freq;
desired.format = AUDIO_S16SYS;
desired.channels = 2;
- desired.samples = (uint16)samples;
+ desired.samples = roundDownPowerOfTwo(samples);
desired.callback = sdlCallback;
desired.userdata = this;