2010-06-12 02:54:51 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2014-02-18 02:34:24 +01:00
|
|
|
*
|
2010-06-12 02:54:51 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-02-18 02:34:24 +01:00
|
|
|
*
|
2010-06-12 02:54:51 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-05-31 00:25:40 +02:00
|
|
|
#include "common/scummsys.h"
|
|
|
|
|
2010-08-12 06:30:26 +00:00
|
|
|
#if defined(SDL_BACKEND)
|
2010-06-12 02:54:51 +00:00
|
|
|
|
|
|
|
#include "backends/mixer/sdl/sdl-mixer.h"
|
2011-04-24 11:34:27 +03:00
|
|
|
#include "common/debug.h"
|
2010-06-24 04:11:54 +00:00
|
|
|
#include "common/system.h"
|
2010-06-12 02:54:51 +00:00
|
|
|
#include "common/config-manager.h"
|
2011-04-24 11:34:27 +03:00
|
|
|
#include "common/textconsole.h"
|
2010-06-12 02:54:51 +00:00
|
|
|
|
2015-03-08 13:45:03 +01:00
|
|
|
#if defined(GP2X)
|
2010-07-01 06:07:24 +00:00
|
|
|
#define SAMPLES_PER_SEC 11025
|
2018-09-20 16:33:08 +02:00
|
|
|
#elif defined(PLAYSTATION3) || defined(PSP2) || defined(NINTENDO_SWITCH)
|
2015-03-08 13:45:03 +01:00
|
|
|
#define SAMPLES_PER_SEC 48000
|
2010-07-01 06:07:24 +00:00
|
|
|
#else
|
2014-06-22 18:22:50 +02:00
|
|
|
#define SAMPLES_PER_SEC 44100
|
2010-07-01 06:07:24 +00:00
|
|
|
#endif
|
2010-06-12 02:54:51 +00:00
|
|
|
|
2010-06-24 04:11:54 +00:00
|
|
|
SdlMixerManager::~SdlMixerManager() {
|
|
|
|
_mixer->setReady(false);
|
2010-06-20 20:19:53 +00:00
|
|
|
|
|
|
|
SDL_CloseAudio();
|
|
|
|
|
2020-08-20 01:25:19 +02:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
2010-06-20 20:19:53 +00:00
|
|
|
}
|
|
|
|
|
2010-06-24 04:11:54 +00:00
|
|
|
void SdlMixerManager::init() {
|
|
|
|
// Start SDL Audio subsystem
|
2010-06-23 19:34:07 +00:00
|
|
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) {
|
|
|
|
error("Could not initialize SDL: %s", SDL_GetError());
|
|
|
|
}
|
|
|
|
|
2015-01-25 21:23:26 +01:00
|
|
|
#if SDL_VERSION_ATLEAST(2, 0, 0)
|
|
|
|
const char *sdlDriverName = SDL_GetCurrentAudioDriver();
|
|
|
|
#else
|
2014-02-12 11:36:56 +00:00
|
|
|
const int maxNameLen = 20;
|
|
|
|
char sdlDriverName[maxNameLen];
|
|
|
|
sdlDriverName[0] = '\0';
|
|
|
|
SDL_AudioDriverName(sdlDriverName, maxNameLen);
|
2015-01-25 01:34:57 +01:00
|
|
|
#endif
|
2015-01-25 21:23:26 +01:00
|
|
|
debug(1, "Using SDL Audio Driver \"%s\"", sdlDriverName);
|
2014-02-14 15:34:22 +00:00
|
|
|
|
2010-06-24 04:11:54 +00:00
|
|
|
// Get the desired audio specs
|
2010-11-28 14:57:04 +00:00
|
|
|
SDL_AudioSpec desired = getAudioSpec(SAMPLES_PER_SEC);
|
2010-06-24 04:11:54 +00:00
|
|
|
|
2011-06-14 00:15:50 +01:00
|
|
|
// Needed as SDL_OpenAudio as of SDL-1.2.14 mutates fields in
|
|
|
|
// "desired" if used directly.
|
|
|
|
SDL_AudioSpec fmt = desired;
|
|
|
|
|
2010-06-24 04:11:54 +00:00
|
|
|
// Start SDL audio with the desired specs
|
2011-06-14 00:15:50 +01:00
|
|
|
if (SDL_OpenAudio(&fmt, &_obtained) != 0) {
|
2010-06-24 04:11:54 +00:00
|
|
|
warning("Could not open audio device: %s", SDL_GetError());
|
|
|
|
|
2015-03-08 13:13:50 +01:00
|
|
|
// The mixer is not marked as ready
|
2019-11-30 13:38:06 +00:00
|
|
|
_mixer = new Audio::MixerImpl(desired.freq);
|
2015-03-08 13:13:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The obtained sample format is not supported by the mixer, call
|
|
|
|
// SDL_OpenAudio again with NULL as the second argument to force
|
|
|
|
// SDL to do resampling to the desired audio spec.
|
|
|
|
if (_obtained.format != desired.format) {
|
|
|
|
debug(1, "SDL mixer sound format: %d differs from desired: %d", _obtained.format, desired.format);
|
|
|
|
SDL_CloseAudio();
|
2010-06-24 04:11:54 +00:00
|
|
|
|
2015-03-08 13:13:50 +01:00
|
|
|
if (SDL_OpenAudio(&fmt, NULL) != 0) {
|
|
|
|
warning("Could not open audio device: %s", SDL_GetError());
|
2011-06-14 00:15:50 +01:00
|
|
|
|
2015-03-08 13:13:50 +01:00
|
|
|
// The mixer is not marked as ready
|
2019-11-30 13:38:06 +00:00
|
|
|
_mixer = new Audio::MixerImpl(desired.freq);
|
2015-03-08 13:13:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_obtained = desired;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(1, "Output sample rate: %d Hz", _obtained.freq);
|
|
|
|
if (_obtained.freq != desired.freq)
|
|
|
|
warning("SDL mixer output sample rate: %d differs from desired: %d", _obtained.freq, desired.freq);
|
|
|
|
|
|
|
|
debug(1, "Output buffer size: %d samples", _obtained.samples);
|
|
|
|
if (_obtained.samples != desired.samples)
|
|
|
|
warning("SDL mixer output buffer size: %d differs from desired: %d", _obtained.samples, desired.samples);
|
2011-06-14 00:15:50 +01:00
|
|
|
|
2011-06-17 19:47:32 +02:00
|
|
|
#ifndef __SYMBIAN32__
|
2015-03-08 13:13:50 +01:00
|
|
|
// The SymbianSdlMixerManager does stereo->mono downmixing,
|
|
|
|
// but otherwise we require stereo output.
|
|
|
|
if (_obtained.channels != 2)
|
|
|
|
error("SDL mixer output requires stereo output device");
|
2011-06-17 19:47:32 +02:00
|
|
|
#endif
|
2011-06-14 00:15:50 +01:00
|
|
|
|
2019-11-30 13:38:06 +00:00
|
|
|
_mixer = new Audio::MixerImpl(_obtained.freq);
|
2015-03-08 13:13:50 +01:00
|
|
|
assert(_mixer);
|
|
|
|
_mixer->setReady(true);
|
2010-06-24 04:11:54 +00:00
|
|
|
|
2015-03-08 13:13:50 +01:00
|
|
|
startAudio();
|
2010-06-24 04:11:54 +00:00
|
|
|
}
|
|
|
|
|
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.
2017-09-05 00:57:14 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-11-28 14:57:04 +00:00
|
|
|
SDL_AudioSpec SdlMixerManager::getAudioSpec(uint32 outputRate) {
|
2010-06-12 02:54:51 +00:00
|
|
|
SDL_AudioSpec desired;
|
|
|
|
|
2020-06-18 18:11:28 +02:00
|
|
|
// There was once a GUI option for this, which was removed. Configurability
|
|
|
|
// is retained for advanced users only who wish to use the commandline
|
|
|
|
// option (--output-rate) or modify their ScummVM config file directly.
|
2017-09-05 22:30:01 -05:00
|
|
|
uint32 freq = 0;
|
2020-06-18 18:11:28 +02:00
|
|
|
if (ConfMan.hasKey("output_rate"))
|
|
|
|
freq = ConfMan.getInt("output_rate");
|
2017-09-05 22:30:01 -05:00
|
|
|
if (freq <= 0)
|
|
|
|
freq = outputRate;
|
2010-06-12 02:54:51 +00:00
|
|
|
|
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.
2017-09-05 00:57:14 -05:00
|
|
|
// 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
|
2020-06-18 18:11:28 +02:00
|
|
|
if (ConfMan.hasKey("audio_buffer_size", Common::ConfigManager::kApplicationDomain))
|
|
|
|
samples = ConfMan.getInt("audio_buffer_size", Common::ConfigManager::kApplicationDomain);
|
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.
2017-09-05 00:57:14 -05:00
|
|
|
|
|
|
|
// 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);
|
2010-06-12 02:54:51 +00:00
|
|
|
|
|
|
|
memset(&desired, 0, sizeof(desired));
|
2017-09-05 22:30:01 -05:00
|
|
|
desired.freq = freq;
|
2010-06-12 02:54:51 +00:00
|
|
|
desired.format = AUDIO_S16SYS;
|
|
|
|
desired.channels = 2;
|
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.
2017-09-05 00:57:14 -05:00
|
|
|
desired.samples = roundDownPowerOfTwo(samples);
|
2010-06-24 04:11:54 +00:00
|
|
|
desired.callback = sdlCallback;
|
2010-06-12 02:54:51 +00:00
|
|
|
desired.userdata = this;
|
|
|
|
|
2010-06-24 04:11:54 +00:00
|
|
|
return desired;
|
2010-06-12 02:54:51 +00:00
|
|
|
}
|
|
|
|
|
2010-06-24 04:11:54 +00:00
|
|
|
void SdlMixerManager::startAudio() {
|
|
|
|
// Start the sound system
|
|
|
|
SDL_PauseAudio(0);
|
2010-06-12 02:54:51 +00:00
|
|
|
}
|
|
|
|
|
2010-06-24 04:11:54 +00:00
|
|
|
void SdlMixerManager::callbackHandler(byte *samples, int len) {
|
|
|
|
assert(_mixer);
|
|
|
|
_mixer->mixCallback(samples, len);
|
2010-06-12 02:54:51 +00:00
|
|
|
}
|
|
|
|
|
2010-06-24 04:11:54 +00:00
|
|
|
void SdlMixerManager::sdlCallback(void *this_, byte *samples, int len) {
|
|
|
|
SdlMixerManager *manager = (SdlMixerManager *)this_;
|
|
|
|
assert(manager);
|
2010-06-12 02:54:51 +00:00
|
|
|
|
2010-06-24 04:11:54 +00:00
|
|
|
manager->callbackHandler(samples, len);
|
2010-06-12 02:54:51 +00:00
|
|
|
}
|
|
|
|
|
2010-06-29 01:08:36 +00:00
|
|
|
void SdlMixerManager::suspendAudio() {
|
|
|
|
SDL_CloseAudio();
|
|
|
|
_audioSuspended = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SdlMixerManager::resumeAudio() {
|
|
|
|
if (!_audioSuspended)
|
|
|
|
return -2;
|
2013-01-24 20:15:13 +01:00
|
|
|
if (SDL_OpenAudio(&_obtained, NULL) < 0) {
|
2010-06-29 01:08:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
SDL_PauseAudio(0);
|
|
|
|
_audioSuspended = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-12 02:54:51 +00:00
|
|
|
#endif
|