Initial commit

This commit is contained in:
2026-02-02 04:50:13 +01:00
commit 5b11698731
22592 changed files with 7677434 additions and 0 deletions

View File

@@ -0,0 +1,253 @@
/* 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 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "backends/mixer/atari/atari-mixer.h"
#include <math.h>
#include <mint/falcon.h>
#include <mint/osbind.h>
#include <mint/ostruct.h>
#include <usound.h> // https://github.com/mikrosk/usound
#include "backends/platform/atari/atari-debug.h"
#include "common/config-manager.h"
#define DEFAULT_OUTPUT_RATE 24585
#define DEFAULT_OUTPUT_CHANNELS 2
#define DEFAULT_SAMPLES 2048 // 83ms
void AtariAudioShutdown() {
Jdisint(MFP_TIMERA);
AtariSoundSetupDeinitXbios();
}
static volatile bool muted;
static volatile bool endOfPlayback;
static void __attribute__((interrupt)) timerA(void)
{
if (endOfPlayback && !muted) {
*((volatile unsigned char *)0xFFFF8901L) &= 0xFC; // disable playback/repeat (and triggers another interrupt)
muted = true;
}
endOfPlayback = true;
*((volatile byte *)0xFFFFFA0FL) &= ~(1<<5); // clear in service bit
}
AtariMixerManager::AtariMixerManager() : MixerManager() {
atari_debug("AtariMixerManager()");
suspendAudio();
ConfMan.registerDefault("output_rate", DEFAULT_OUTPUT_RATE);
_outputRate = ConfMan.getInt("output_rate");
if (_outputRate <= 0)
_outputRate = DEFAULT_OUTPUT_RATE;
ConfMan.registerDefault("output_channels", DEFAULT_OUTPUT_CHANNELS);
_outputChannels = ConfMan.getInt("output_channels");
if (_outputChannels <= 0 || _outputChannels > 2)
_outputChannels = DEFAULT_OUTPUT_CHANNELS;
ConfMan.registerDefault("audio_buffer_size", DEFAULT_SAMPLES);
_samples = ConfMan.getInt("audio_buffer_size");
if (_samples <= 0)
_samples = DEFAULT_SAMPLES;
g_system->getEventManager()->getEventDispatcher()->registerObserver(this, 10, false);
}
AtariMixerManager::~AtariMixerManager() {
atari_debug("~AtariMixerManager()");
g_system->getEventManager()->getEventDispatcher()->unregisterObserver(this);
AtariAudioShutdown();
Mfree(_atariSampleBuffer);
_atariSampleBuffer = _atariPhysicalSampleBuffer = _atariLogicalSampleBuffer = nullptr;
delete[] _samplesBuf;
_samplesBuf = nullptr;
}
void AtariMixerManager::init() {
AudioSpec desired, obtained;
desired.frequency = _outputRate;
desired.channels = _outputChannels;
desired.format = AudioFormatSigned16MSB;
desired.samples = _samples;
if (!AtariSoundSetupInitXbios(&desired, &obtained)) {
error("Sound system is not available");
}
if (obtained.format != AudioFormatSigned8 && obtained.format != AudioFormatSigned16MSB) {
error("Sound system currently supports only 8/16-bit signed big endian samples");
}
// don't use the recommended number of samples
obtained.size = obtained.size * desired.samples / obtained.samples;
obtained.samples = desired.samples;
_outputRate = obtained.frequency;
_outputChannels = obtained.channels;
_samples = obtained.samples;
_downsample = (obtained.format == AudioFormatSigned8);
ConfMan.setInt("output_rate", _outputRate);
ConfMan.setInt("output_channels", _outputChannels);
ConfMan.setInt("audio_buffer_size", _samples);
atari_debug("setting %d Hz mixing frequency (%d-bit, %s)",
_outputRate, obtained.format == AudioFormatSigned8 ? 8 : 16, _outputChannels == 1 ? "mono" : "stereo");
atari_debug("sample buffer size: %d", _samples);
ConfMan.flushToDisk();
_atariSampleBuffer = (byte*)Mxalloc(obtained.size * 2, MX_STRAM);
if (!_atariSampleBuffer)
error("Failed to allocate memory in ST RAM");
_atariPhysicalSampleBuffer = _atariSampleBuffer;
_atariLogicalSampleBuffer = _atariSampleBuffer + obtained.size;
Setinterrupt(SI_TIMERA, SI_PLAY);
Xbtimer(XB_TIMERA, 1<<3, 1, timerA); // event count mode, count to '1'
Jenabint(MFP_TIMERA);
_samplesBuf = new uint8[_samples * _outputChannels * 2]; // always 16-bit
_mixer = new Audio::MixerImpl(_outputRate, _outputChannels == 2, _samples);
_mixer->setReady(true);
resumeAudio();
}
void AtariMixerManager::suspendAudio() {
atari_debug("suspendAudio");
Buffoper(0x00);
muted = true;
_audioSuspended = true;
}
int AtariMixerManager::resumeAudio() {
atari_debug("resumeAudio");
_audioSuspended = false;
update();
return 0;
}
bool AtariMixerManager::notifyEvent(const Common::Event &event) {
switch (event.type) {
case Common::EVENT_QUIT:
case Common::EVENT_RETURN_TO_LAUNCHER:
if (!muted) {
Buffoper(0x00);
muted = true;
atari_debug("silencing the mixer");
}
return false;
default:
break;
}
return false;
}
void AtariMixerManager::update() {
if (_audioSuspended) {
return;
}
assert(_mixer);
int processed = -1;
if (muted || endOfPlayback) {
endOfPlayback = false;
processed = _mixer->mixCallback(_samplesBuf, _samples * _outputChannels * 2);
}
if (processed > 0) {
byte* tmp = _atariPhysicalSampleBuffer;
_atariPhysicalSampleBuffer = _atariLogicalSampleBuffer;
_atariLogicalSampleBuffer = tmp;
if (_downsample) {
// use the trick with move.b (a7)+,dx which skips two bytes at once
// basically supplying move.w (src)+,dx; asr.w #8,dx; move.b dx,(dst)+
__asm__ volatile(
" move.l %%a7,%%d0\n"
" move.l %0,%%a7\n"
" moveq #0x0f,%%d1\n"
" and.l %2,%%d1\n"
" neg.l %%d1\n"
" lsr.l #4,%2\n"
" jmp (2f,%%pc,%%d1.l*2)\n"
"1: move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
" move.b (%%a7)+,(%1)+\n"
"2: dbra %2,1b\n"
" move.l %%d0,%%a7\n"
: // outputs
: "g"(_samplesBuf), "a"(_atariPhysicalSampleBuffer), "d"(processed * _outputChannels * 2/2) // inputs
: "d0", "d1", "cc" AND_MEMORY
);
memset(_atariPhysicalSampleBuffer + processed * _outputChannels * 2/2, 0, (_samples - processed) * _outputChannels * 2/2);
Setbuffer(SR_PLAY, _atariPhysicalSampleBuffer, _atariPhysicalSampleBuffer + _samples * _outputChannels * 2/2);
} else {
memcpy(_atariPhysicalSampleBuffer, _samplesBuf, processed * _outputChannels * 2);
memset(_atariPhysicalSampleBuffer + processed * _outputChannels * 2, 0, (_samples - processed) * _outputChannels * 2);
Setbuffer(SR_PLAY, _atariPhysicalSampleBuffer, _atariPhysicalSampleBuffer + _samples * _outputChannels * 2);
}
if (muted) {
Buffoper(SB_PLA_ENA | SB_PLA_RPT);
endOfPlayback = true;
muted = false;
}
} else if (processed == 0 && !muted) {
Buffoper(0x00);
muted = true;
}
if (processed > 0 && processed != _samples) {
atari_warning("processed: %d, _samples: %d", processed, _samples);
}
}

View File

@@ -0,0 +1,57 @@
/* 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 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_MIXER_ATARI_H
#define BACKENDS_MIXER_ATARI_H
#include "backends/mixer/mixer.h"
#include "common/events.h"
/**
* Atari XBIOS based audio mixer.
*/
class AtariMixerManager : public MixerManager, Common::EventObserver {
public:
AtariMixerManager();
virtual ~AtariMixerManager();
virtual void init() override;
void update();
void suspendAudio() override;
int resumeAudio() override;
bool notifyEvent(const Common::Event &event) override;
private:
int _outputRate = 0;
int _outputChannels = 0;
int _samples = 0;
uint8 *_samplesBuf = nullptr;
byte *_atariSampleBuffer = nullptr;
byte *_atariPhysicalSampleBuffer = nullptr;
byte *_atariLogicalSampleBuffer = nullptr;
bool _downsample = false;
};
#endif

View File

@@ -0,0 +1,100 @@
/* 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 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Allow use of stuff in <nds.h>
#define FORBIDDEN_SYMBOL_EXCEPTION_printf
#define FORBIDDEN_SYMBOL_EXCEPTION_unistd_h
#include "common/scummsys.h"
#if defined(__DS__)
#include "backends/mixer/maxmod/maxmod-mixer.h"
#include "common/system.h"
#include <nds.h>
#include <maxmod9.h>
MaxModMixerManager::MaxModMixerManager(int freq, int bufSize)
:
_freq(freq),
_bufSize(bufSize) {
}
MaxModMixerManager::~MaxModMixerManager() {
// HACK: This is called during the OSystem destructor, but Audio::MixerImpl calls
// mutex functions from OSystem during its destructor, which causes a crash.
// _mixer->setReady(false);
_mixer = 0;
mmStreamClose();
}
mm_word on_stream_request( mm_word length, mm_addr dest, mm_stream_formats format ) {
Audio::MixerImpl *mixer = (Audio::MixerImpl *)g_system->getMixer();
assert(mixer);
mixer->mixCallback((byte *)dest, length * 4);
return length;
}
void MaxModMixerManager::init() {
_mixer = new Audio::MixerImpl(_freq, _bufSize / 4);
assert(_mixer);
mm_ds_system sys;
sys.mod_count = 0;
sys.samp_count = 0;
sys.mem_bank = 0;
mmInit( &sys );
_stream.sampling_rate = _freq;
_stream.buffer_length = _bufSize / 4;
_stream.callback = on_stream_request;
_stream.format = MM_STREAM_16BIT_STEREO;
_stream.thread_stack_size = 0;
// Don't create a thread
_stream.minus_thread_prio = 1;
mmStreamOpen( &_stream );
_mixer->setReady(true);
}
void MaxModMixerManager::suspendAudio() {
mmStreamClose();
_audioSuspended = true;
}
int MaxModMixerManager::resumeAudio() {
if (!_audioSuspended)
return -2;
mmStreamOpen( &_stream );
_audioSuspended = false;
return 0;
}
void MaxModMixerManager::updateAudio() {
if (!_audioSuspended)
mmStreamUpdate();
}
#endif

View File

@@ -0,0 +1,64 @@
/* 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 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_MIXER_MAXMOD_H
#define BACKENDS_MIXER_MAXMOD_H
#include "backends/mixer/mixer.h"
#include <mm_types.h>
/**
* MaxMod mixer manager. It wraps the actual implementation
* of the Audio:Mixer used by the engine, and sets up
* MaxMod and the callback for the audio mixer implementation.
*/
class MaxModMixerManager : public MixerManager {
public:
MaxModMixerManager(int freq, int bufSize);
virtual ~MaxModMixerManager();
/**
* Initialize and setups the mixer
*/
virtual void init();
/**
* Pauses the audio system
*/
virtual void suspendAudio();
/**
* Resumes the audio system
*/
virtual int resumeAudio();
/**
* Updates the audio system
*/
void updateAudio();
protected:
mm_stream _stream;
int _freq, _bufSize;
};
#endif

71
backends/mixer/mixer.h Normal file
View File

@@ -0,0 +1,71 @@
/* 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 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_MIXER_ABSTRACT_H
#define BACKENDS_MIXER_ABSTRACT_H
#include "audio/mixer_intern.h"
/**
* Abstract class for mixer manager. Subclasses
* implement the real functionality.
*/
class MixerManager {
public:
MixerManager() : _mixer(0), _audioSuspended(false) {}
virtual ~MixerManager() { delete _mixer; }
/**
* Initialize and setups the mixer
*/
virtual void init() = 0;
/**
* Get the audio mixer implementation
*/
Audio::Mixer *getMixer() { return (Audio::Mixer *)_mixer; }
// Used by LinuxMoto Port
/**
* Pauses the audio system
*/
virtual void suspendAudio() = 0;
/**
* Resumes the audio system
*/
virtual int resumeAudio() = 0;
/**
* Returns true if this is a null device and won't output any audio.
*/
virtual bool isNullDevice() const { return false; }
protected:
/** The mixer implementation */
Audio::MixerImpl *_mixer;
/** State of the audio system */
bool _audioSuspended;
};
#endif

View File

@@ -0,0 +1,69 @@
/* 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 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "backends/mixer/null/null-mixer.h"
#include "common/savefile.h"
NullMixerManager::NullMixerManager() : MixerManager() {
_outputRate = 22050;
_callsCounter = 0;
_samples = 8192;
while (_samples * 16 > _outputRate * 2)
_samples >>= 1;
_samplesBuf = new uint8[_samples * 4];
}
NullMixerManager::~NullMixerManager() {
delete[] _samplesBuf;
}
void NullMixerManager::init() {
_mixer = new Audio::MixerImpl(_outputRate, true, _samples);
assert(_mixer);
_mixer->setReady(true);
}
void NullMixerManager::suspendAudio() {
_audioSuspended = true;
}
int NullMixerManager::resumeAudio() {
if (!_audioSuspended) {
return -2;
}
_audioSuspended = false;
return 0;
}
bool NullMixerManager::isNullDevice() const {
return true;
}
void NullMixerManager::update(uint8 callbackPeriod) {
if (_audioSuspended) {
return;
}
_callsCounter++;
if ((_callsCounter % callbackPeriod) == 0) {
assert(_mixer);
_mixer->mixCallback(_samplesBuf, _samples);
}
}

View File

@@ -0,0 +1,56 @@
/* 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 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_MIXER_NULL_H
#define BACKENDS_MIXER_NULL_H
#include "backends/mixer/mixer.h"
/** Audio mixer which in fact does not output audio.
*
* It is used by events recorder since the recorder is intentionally
* turning sound off to avoid stuttering.
*
* It returns correct output and shoots callbacks, so all OSystem
* users could work without modifications.
*/
class NullMixerManager : public MixerManager {
public:
NullMixerManager();
virtual ~NullMixerManager();
void init() override;
void update(uint8 callbackPeriod = 10);
void suspendAudio() override;
int resumeAudio() override;
bool isNullDevice() const override;
private:
uint32 _outputRate;
uint32 _callsCounter;
uint32 _samples;
uint8 *_samplesBuf;
};
#endif

View File

@@ -0,0 +1,292 @@
/* 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 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/scummsys.h"
#if defined(SDL_BACKEND)
#include "backends/mixer/sdl/sdl-mixer.h"
#include "common/debug.h"
#include "common/system.h"
#include "common/config-manager.h"
#include "common/textconsole.h"
#if defined(PLAYSTATION3) || defined(PSP2) || defined(NINTENDO_SWITCH)
#define SAMPLES_PER_SEC 48000
#elif defined(__MINT__)
#define SAMPLES_PER_SEC 49170
#else
#define SAMPLES_PER_SEC 44100
#endif
SdlMixerManager::SdlMixerManager() : _isSubsystemInitialized(false), _isAudioOpen(false) {
}
SdlMixerManager::~SdlMixerManager() {
if (_mixer)
_mixer->setReady(false);
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_CloseAudioDevice(SDL_GetAudioStreamDevice(_stream));
SDL_DestroyAudioStream(_stream);
#else
if (_isAudioOpen)
SDL_CloseAudio();
#endif
if (_isSubsystemInitialized)
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
void SdlMixerManager::init() {
bool isDisabled = ConfMan.hasKey("disable_sdl_audio") && ConfMan.getBool("disable_sdl_audio");
if (isDisabled) {
warning("SDL audio subsystem was forcibly disabled by disable_sdl_audio option");
return;
}
// Get the desired audio specs
SDL_AudioSpec desired = getAudioSpec(SAMPLES_PER_SEC);
// Start SDL Audio subsystem
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_InitSubSystem(SDL_INIT_AUDIO)) {
#else
if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) {
#endif
warning("Could not initialize SDL audio subsystem: %s", SDL_GetError());
return;
}
_isSubsystemInitialized = true;
#if SDL_VERSION_ATLEAST(2, 0, 0)
const char *sdlDriverName = SDL_GetCurrentAudioDriver();
#else
const int maxNameLen = 20;
char sdlDriverName[maxNameLen];
sdlDriverName[0] = '\0';
SDL_AudioDriverName(sdlDriverName, maxNameLen);
#endif
debug(1, "Using SDL Audio Driver \"%s\"", sdlDriverName);
#if SDL_VERSION_ATLEAST(3, 0, 0)
_isAudioOpen = true;
_obtained = desired;
#else
// Needed as SDL_OpenAudio as of SDL-1.2.14 mutates fields in
// "desired" if used directly.
SDL_AudioSpec fmt = desired;
// Start SDL audio with the desired specs
if (SDL_OpenAudio(&fmt, &_obtained) != 0) {
warning("Could not open audio device: %s", SDL_GetError());
return;
}
_isAudioOpen = true;
// 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();
if (SDL_OpenAudio(&fmt, nullptr) != 0) {
warning("Could not open audio device: %s", SDL_GetError());
return;
}
_obtained = desired;
}
#endif
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);
#if !SDL_VERSION_ATLEAST(3, 0, 0)
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);
debug(1, "Output channels: %d", _obtained.channels);
if (_obtained.channels != 1 && _obtained.channels != 2)
error("SDL mixer output requires mono or stereo output device");
#endif
int desiredSamples = 0;
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_GetAudioDeviceFormat(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &_obtained, &desiredSamples);
#else
desiredSamples = desired.samples;
#endif
_mixer = new Audio::MixerImpl(_obtained.freq, _obtained.channels >= 2, desiredSamples);
assert(_mixer);
_mixer->setReady(true);
startAudio();
}
#if !SDL_VERSION_ATLEAST(3, 0, 0)
static uint32 roundDownPowerOfTwo(uint32 samples) {
// Public domain code from Sean Eron Anderson
// https://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;
}
#endif
SDL_AudioSpec SdlMixerManager::getAudioSpec(uint32 outputRate) {
SDL_AudioSpec desired;
// 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.
uint32 freq = 0;
if (ConfMan.hasKey("output_rate"))
freq = ConfMan.getInt("output_rate");
if (freq <= 0)
freq = outputRate;
uint32 channels = 2;
if (ConfMan.hasKey("output_channels"))
channels = ConfMan.getInt("output_channels");
if (channels > 2 || channels <= 0)
channels = 2;
// 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", Common::ConfigManager::kApplicationDomain))
samples = ConfMan.getInt("audio_buffer_size", Common::ConfigManager::kApplicationDomain);
// 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.channels = channels;
#if SDL_VERSION_ATLEAST(3, 0, 0)
desired.format = SDL_AUDIO_S16;
#else
desired.format = AUDIO_S16SYS;
desired.samples = roundDownPowerOfTwo(samples);
desired.callback = sdlCallback;
desired.userdata = this;
#endif
return desired;
}
void SdlMixerManager::startAudio() {
// Start the sound system
#if SDL_VERSION_ATLEAST(3, 0, 0)
_stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &_obtained, sdlCallback, this);
if (_stream)
SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(_stream));
#else
SDL_PauseAudio(0);
#endif
}
void SdlMixerManager::callbackHandler(byte *samples, int len) {
assert(_mixer);
_mixer->mixCallback(samples, len);
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
void SdlMixerManager::sdlCallback(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount) {
if (additional_amount > 0) {
Uint8 *data = SDL_stack_alloc(Uint8, additional_amount);
if (data) {
SdlMixerManager *manager = (SdlMixerManager *)userdata;
assert(manager);
manager->callbackHandler(data, additional_amount);
SDL_PutAudioStreamData(stream, data, additional_amount);
SDL_stack_free(data);
}
}
}
#else
void SdlMixerManager::sdlCallback(void *this_, byte *samples, int len) {
SdlMixerManager *manager = (SdlMixerManager *)this_;
assert(manager);
manager->callbackHandler(samples, len);
}
#endif
void SdlMixerManager::suspendAudio() {
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_CloseAudioDevice(SDL_GetAudioStreamDevice(_stream));
SDL_DestroyAudioStream(_stream);
#else
SDL_CloseAudio();
#endif
_audioSuspended = true;
}
int SdlMixerManager::resumeAudio() {
if (!_audioSuspended)
return -2;
#if SDL_VERSION_ATLEAST(3, 0, 0)
_stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &_obtained, sdlCallback, this);
if(!_stream)
return -1;
if (SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(_stream)))
return -1;
#else
if (SDL_OpenAudio(&_obtained, nullptr) < 0) {
return -1;
}
SDL_PauseAudio(0);
#endif
_audioSuspended = false;
return 0;
}
#endif

View File

@@ -0,0 +1,96 @@
/* 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 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BACKENDS_MIXER_SDL_H
#define BACKENDS_MIXER_SDL_H
#include "backends/platform/sdl/sdl-sys.h"
#include "backends/mixer/mixer.h"
/**
* SDL mixer manager. It wraps the actual implementation
* of the Audio:Mixer used by the engine, and setups
* the SDL audio subsystem and the callback for the
* audio mixer implementation.
*/
class SdlMixerManager : public MixerManager {
public:
SdlMixerManager();
virtual ~SdlMixerManager();
/**
* Initialize and setups the mixer
*/
virtual void init();
// Used by Event recorder
/**
* Pauses the audio system
*/
virtual void suspendAudio();
/**
* Resumes the audio system
*/
virtual int resumeAudio();
protected:
/**
* The obtained audio specification after opening the
* audio system.
*/
SDL_AudioSpec _obtained;
/**
* Returns the desired audio specification
*/
virtual SDL_AudioSpec getAudioSpec(uint32 rate);
/**
* Starts SDL audio
*/
virtual void startAudio();
/**
* Handles the audio callback
*/
virtual void callbackHandler(byte *samples, int len);
/**
* The mixer callback entry point. Static functions can't be overridden
* by subclasses, so it invokes the non-static function callbackHandler()
*/
#if SDL_VERSION_ATLEAST(3, 0, 0)
static void sdlCallback(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount);
#else
static void sdlCallback(void *this_, byte *samples, int len);
#endif
bool _isSubsystemInitialized;
bool _isAudioOpen;
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_AudioStream *_stream = nullptr;
#endif
};
#endif