2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-21 12:25:28 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// AudioManager.cpp
|
|
|
|
//
|
2020-06-26 16:11:24 +00:00
|
|
|
// Low-level audio functions (using SDL2).
|
2020-06-21 12:25:28 +00:00
|
|
|
//
|
|
|
|
|
2012-10-13 18:29:53 +00:00
|
|
|
#include "AudioManager.h"
|
|
|
|
|
2013-01-04 23:31:51 +00:00
|
|
|
#include "Log.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "Settings.h"
|
|
|
|
#include "Sound.h"
|
2012-10-13 18:29:53 +00:00
|
|
|
|
2020-06-26 16:11:24 +00:00
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
2022-01-04 20:21:26 +00:00
|
|
|
AudioManager::AudioManager() noexcept
|
2013-05-14 19:31:39 +00:00
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
// Init on construction.
|
2020-06-21 12:25:28 +00:00
|
|
|
init();
|
2013-05-21 08:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioManager::~AudioManager()
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
// Deinit on destruction.
|
2020-06-21 12:25:28 +00:00
|
|
|
deinit();
|
2013-05-21 08:40:01 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 21:43:06 +00:00
|
|
|
AudioManager& AudioManager::getInstance()
|
2013-05-21 08:40:01 +00:00
|
|
|
{
|
2021-11-15 21:43:06 +00:00
|
|
|
static AudioManager instance;
|
|
|
|
return instance;
|
2013-05-21 08:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioManager::init()
|
|
|
|
{
|
2020-12-20 23:23:22 +00:00
|
|
|
LOG(LogInfo) << "Setting up AudioManager...";
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) {
|
|
|
|
LOG(LogError) << "Error initializing SDL audio!\n" << SDL_GetError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:56:21 +00:00
|
|
|
LOG(LogInfo) << "Audio driver: " << SDL_GetCurrentAudioDriver();
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2020-12-20 23:23:22 +00:00
|
|
|
SDL_AudioSpec sRequestedAudioFormat;
|
|
|
|
|
|
|
|
SDL_memset(&sRequestedAudioFormat, 0, sizeof(sRequestedAudioFormat));
|
|
|
|
SDL_memset(&sAudioFormat, 0, sizeof(sAudioFormat));
|
|
|
|
|
2020-12-23 14:32:10 +00:00
|
|
|
// Set up format and callback. SDL will negotiate these settings with the audio driver, so
|
|
|
|
// if for instance the driver/hardware does not support 32-bit floating point output, 16-bit
|
|
|
|
// integer may be selected instead. ES-DE will handle this automatically as there are no
|
|
|
|
// hardcoded audio settings elsewhere in the code.
|
2020-12-20 23:23:22 +00:00
|
|
|
sRequestedAudioFormat.freq = 44100;
|
2020-12-22 22:27:23 +00:00
|
|
|
sRequestedAudioFormat.format = AUDIO_F32;
|
2020-12-20 23:23:22 +00:00
|
|
|
sRequestedAudioFormat.channels = 2;
|
2020-12-22 22:27:23 +00:00
|
|
|
sRequestedAudioFormat.samples = 1024;
|
2020-12-20 23:23:22 +00:00
|
|
|
sRequestedAudioFormat.callback = mixAudio;
|
|
|
|
sRequestedAudioFormat.userdata = nullptr;
|
|
|
|
|
2021-11-17 16:48:49 +00:00
|
|
|
for (int i = 0; i < SDL_GetNumAudioDevices(0); ++i) {
|
2020-12-23 16:56:21 +00:00
|
|
|
LOG(LogInfo) << "Detected playback device: " << SDL_GetAudioDeviceName(i, 0);
|
2020-12-20 23:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sAudioDevice = SDL_OpenAudioDevice(0, 0, &sRequestedAudioFormat, &sAudioFormat,
|
2021-07-07 18:31:46 +00:00
|
|
|
SDL_AUDIO_ALLOW_ANY_CHANGE);
|
2020-12-20 23:23:22 +00:00
|
|
|
|
|
|
|
if (sAudioDevice == 0) {
|
2020-12-23 16:56:21 +00:00
|
|
|
LOG(LogError) << "Unable to open audio device: " << SDL_GetError();
|
2020-12-31 13:27:31 +00:00
|
|
|
sHasAudioDevice = false;
|
2020-12-20 23:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sAudioFormat.freq != sRequestedAudioFormat.freq) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogDebug) << "AudioManager::init(): Requested sample rate "
|
|
|
|
<< std::to_string(sRequestedAudioFormat.freq)
|
|
|
|
<< " could not be set, obtained " << std::to_string(sAudioFormat.freq);
|
2020-12-20 23:23:22 +00:00
|
|
|
}
|
|
|
|
if (sAudioFormat.format != sRequestedAudioFormat.format) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogDebug) << "AudioManager::init(): Requested format "
|
|
|
|
<< std::to_string(sRequestedAudioFormat.format)
|
|
|
|
<< " could not be set, obtained " << std::to_string(sAudioFormat.format);
|
2020-12-20 23:23:22 +00:00
|
|
|
}
|
|
|
|
if (sAudioFormat.channels != sRequestedAudioFormat.channels) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogDebug) << "AudioManager::init(): Requested channel count "
|
|
|
|
<< std::to_string(sRequestedAudioFormat.channels)
|
|
|
|
<< " could not be set, obtained " << std::to_string(sAudioFormat.channels);
|
2020-12-20 23:23:22 +00:00
|
|
|
}
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(_WIN64) || defined(__APPLE__)
|
2020-12-23 14:32:10 +00:00
|
|
|
// Beats me why the buffer size is not divided by the channel count on some operating systems.
|
2020-12-20 23:23:22 +00:00
|
|
|
if (sAudioFormat.samples != sRequestedAudioFormat.samples) {
|
2021-07-07 18:31:46 +00:00
|
|
|
#else
|
2020-12-22 22:27:23 +00:00
|
|
|
if (sAudioFormat.samples != sRequestedAudioFormat.samples / sRequestedAudioFormat.channels) {
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
|
|
|
LOG(LogDebug) << "AudioManager::init(): Requested sample buffer size "
|
|
|
|
<< std::to_string(sRequestedAudioFormat.samples /
|
|
|
|
sRequestedAudioFormat.channels)
|
|
|
|
<< " could not be set, obtained " << std::to_string(sAudioFormat.samples);
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
2020-12-20 15:41:58 +00:00
|
|
|
|
2021-06-16 17:05:24 +00:00
|
|
|
// Just in case someone changed the es_settings.xml file manually to invalid values.
|
2020-12-20 15:41:58 +00:00
|
|
|
if (Settings::getInstance()->getInt("SoundVolumeNavigation") > 100)
|
|
|
|
Settings::getInstance()->setInt("SoundVolumeNavigation", 100);
|
|
|
|
if (Settings::getInstance()->getInt("SoundVolumeNavigation") < 0)
|
|
|
|
Settings::getInstance()->setInt("SoundVolumeNavigation", 0);
|
|
|
|
if (Settings::getInstance()->getInt("SoundVolumeVideos") > 100)
|
|
|
|
Settings::getInstance()->setInt("SoundVolumeVideos", 100);
|
|
|
|
if (Settings::getInstance()->getInt("SoundVolumeVideos") < 0)
|
|
|
|
Settings::getInstance()->setInt("SoundVolumeVideos", 0);
|
2020-12-22 22:27:23 +00:00
|
|
|
|
2021-05-09 20:47:46 +00:00
|
|
|
setupAudioStream(sRequestedAudioFormat.freq);
|
2013-05-14 19:31:39 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 08:40:01 +00:00
|
|
|
void AudioManager::deinit()
|
2013-05-14 19:31:39 +00:00
|
|
|
{
|
2021-11-15 21:43:06 +00:00
|
|
|
SDL_LockAudioDevice(sAudioDevice);
|
|
|
|
SDL_FreeAudioStream(sConversionStream);
|
|
|
|
SDL_UnlockAudioDevice(sAudioDevice);
|
2021-06-26 20:47:21 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
SDL_CloseAudio();
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
2022-01-04 23:08:50 +00:00
|
|
|
|
|
|
|
sAudioDevice = 0;
|
2013-05-14 19:31:39 +00:00
|
|
|
}
|
2012-10-13 18:29:53 +00:00
|
|
|
|
2020-12-22 22:27:23 +00:00
|
|
|
void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
|
|
|
|
{
|
|
|
|
// Process navigation sounds.
|
|
|
|
bool stillPlaying = false;
|
|
|
|
|
|
|
|
// Initialize the buffer to "silence".
|
|
|
|
SDL_memset(stream, 0, len);
|
|
|
|
|
|
|
|
// Iterate through all our samples.
|
|
|
|
std::vector<std::shared_ptr<Sound>>::const_iterator soundIt = sSoundVector.cbegin();
|
|
|
|
while (soundIt != sSoundVector.cend()) {
|
|
|
|
std::shared_ptr<Sound> sound = *soundIt;
|
|
|
|
if (sound->isPlaying()) {
|
|
|
|
// Calculate rest length of current sample.
|
|
|
|
Uint32 restLength = (sound->getLength() - sound->getPosition());
|
|
|
|
if (restLength > static_cast<Uint32>(len)) {
|
|
|
|
// If stream length is smaller than sample length, clip it.
|
|
|
|
restLength = len;
|
|
|
|
}
|
|
|
|
// Mix sample into stream.
|
2021-07-07 18:31:46 +00:00
|
|
|
SDL_MixAudioFormat(
|
|
|
|
stream, &(sound->getData()[sound->getPosition()]), sAudioFormat.format, restLength,
|
|
|
|
static_cast<int>(Settings::getInstance()->getInt("SoundVolumeNavigation") * 1.28f));
|
2020-12-22 22:27:23 +00:00
|
|
|
if (sound->getPosition() + restLength < sound->getLength()) {
|
|
|
|
// Sample hasn't ended yet.
|
|
|
|
stillPlaying = true;
|
|
|
|
}
|
|
|
|
// Set new sound position. if this is at or beyond the end of the sample,
|
|
|
|
// it will stop automatically.
|
|
|
|
sound->setPosition(sound->getPosition() + restLength);
|
|
|
|
}
|
|
|
|
// Advance to next sound.
|
2021-11-17 16:48:49 +00:00
|
|
|
++soundIt;
|
2020-12-22 22:27:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 20:45:01 +00:00
|
|
|
// Process video stream audio generated by VideoFFmpegComponent.
|
2021-11-15 21:43:06 +00:00
|
|
|
int streamLength = SDL_AudioStreamAvailable(sConversionStream);
|
2021-05-12 20:45:01 +00:00
|
|
|
|
2021-11-15 21:43:06 +00:00
|
|
|
if (streamLength <= 0) {
|
2021-05-12 20:45:01 +00:00
|
|
|
// If nothing is playing, pause the device until there is more audio to output.
|
|
|
|
if (!stillPlaying)
|
2021-05-09 20:47:46 +00:00
|
|
|
SDL_PauseAudioDevice(sAudioDevice, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-12 20:45:01 +00:00
|
|
|
int chunkLength = 0;
|
2020-12-22 22:27:23 +00:00
|
|
|
|
2021-05-09 20:47:46 +00:00
|
|
|
// Cap the chunk length to the buffer size.
|
2021-05-12 20:45:01 +00:00
|
|
|
if (streamLength > len)
|
2021-05-09 20:47:46 +00:00
|
|
|
chunkLength = len;
|
2021-05-12 20:45:01 +00:00
|
|
|
else
|
|
|
|
chunkLength = streamLength;
|
2020-12-22 22:27:23 +00:00
|
|
|
|
2021-05-09 20:47:46 +00:00
|
|
|
std::vector<Uint8> converted(chunkLength);
|
2020-12-22 22:27:23 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
int processedLength =
|
|
|
|
SDL_AudioStreamGet(sConversionStream, static_cast<void*>(&converted.at(0)), chunkLength);
|
2020-12-22 22:27:23 +00:00
|
|
|
|
2021-05-29 08:52:40 +00:00
|
|
|
if (processedLength < 0) {
|
2021-05-12 20:45:01 +00:00
|
|
|
LOG(LogError) << "AudioManager::mixAudio(): Couldn't convert sound chunk:";
|
2021-05-09 20:47:46 +00:00
|
|
|
LOG(LogError) << SDL_GetError();
|
|
|
|
return;
|
|
|
|
}
|
2020-12-22 22:27:23 +00:00
|
|
|
|
2021-05-09 20:47:46 +00:00
|
|
|
// Enable only when needed, as this generates a lot of debug output.
|
2021-07-07 18:31:46 +00:00
|
|
|
// LOG(LogDebug) << "AudioManager::mixAudio(): chunkLength "
|
|
|
|
// "/ processedLength / streamLength: " << chunkLength << " / " <<
|
|
|
|
// " / " << processedLength << " / " << streamLength;
|
2020-12-22 22:27:23 +00:00
|
|
|
|
2021-05-29 08:52:40 +00:00
|
|
|
// This mute flag is used to make sure that the audio buffer already sent to the
|
|
|
|
// stream is not played when the video player has been stopped. Otherwise there would
|
|
|
|
// be a short time period when the audio would keep playing after the video was stopped
|
|
|
|
// and before the stream was cleared in clearStream().
|
2021-11-15 21:43:06 +00:00
|
|
|
bool muteStream = sMuteStream;
|
|
|
|
if (muteStream) {
|
2021-05-29 08:52:40 +00:00
|
|
|
SDL_MixAudioFormat(stream, &converted.at(0), sAudioFormat.format, processedLength, 0);
|
|
|
|
}
|
|
|
|
else {
|
2021-07-07 18:31:46 +00:00
|
|
|
SDL_MixAudioFormat(
|
|
|
|
stream, &converted.at(0), sAudioFormat.format, processedLength,
|
|
|
|
static_cast<int>(Settings::getInstance()->getInt("SoundVolumeVideos") * 1.28f));
|
2021-05-29 08:52:40 +00:00
|
|
|
}
|
2020-12-22 22:27:23 +00:00
|
|
|
|
2021-05-12 20:45:01 +00:00
|
|
|
// If nothing is playing, pause the device until there is more audio to output.
|
|
|
|
if (!stillPlaying && SDL_AudioStreamAvailable(sConversionStream) == 0)
|
2020-12-22 22:27:23 +00:00
|
|
|
SDL_PauseAudioDevice(sAudioDevice, 1);
|
|
|
|
}
|
|
|
|
|
2021-11-15 21:43:06 +00:00
|
|
|
void AudioManager::registerSound(std::shared_ptr<Sound> sound)
|
2013-05-14 19:31:39 +00:00
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
// Add sound to sound vector.
|
2020-06-21 12:25:28 +00:00
|
|
|
sSoundVector.push_back(sound);
|
2013-05-14 19:31:39 +00:00
|
|
|
}
|
2012-10-13 18:29:53 +00:00
|
|
|
|
2021-11-15 21:43:06 +00:00
|
|
|
void AudioManager::unregisterSound(std::shared_ptr<Sound> sound)
|
2013-05-14 19:31:39 +00:00
|
|
|
{
|
2021-11-17 16:48:49 +00:00
|
|
|
for (unsigned int i = 0; i < sSoundVector.size(); ++i) {
|
2020-07-13 18:58:25 +00:00
|
|
|
if (sSoundVector.at(i) == sound) {
|
2020-06-21 12:25:28 +00:00
|
|
|
sSoundVector[i]->stop();
|
|
|
|
sSoundVector.erase(sSoundVector.cbegin() + i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-03-19 17:37:56 +00:00
|
|
|
LOG(LogError) << "AudioManager - tried to unregister a sound that wasn't registered";
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
2013-05-14 19:31:39 +00:00
|
|
|
|
|
|
|
void AudioManager::play()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// Unpause audio, the mixer will figure out if samples need to be played...
|
2020-12-20 23:23:22 +00:00
|
|
|
SDL_PauseAudioDevice(sAudioDevice, 0);
|
2013-05-22 17:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioManager::stop()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// Stop playing all Sounds.
|
2021-11-17 16:48:49 +00:00
|
|
|
for (unsigned int i = 0; i < sSoundVector.size(); ++i) {
|
2020-12-23 14:32:10 +00:00
|
|
|
if (sSoundVector.at(i)->isPlaying())
|
2020-06-21 12:25:28 +00:00
|
|
|
sSoundVector[i]->stop();
|
|
|
|
}
|
2020-07-13 18:58:25 +00:00
|
|
|
// Pause audio.
|
2020-12-20 23:23:22 +00:00
|
|
|
SDL_PauseAudioDevice(sAudioDevice, 1);
|
2013-05-22 17:11:10 +00:00
|
|
|
}
|
2020-12-22 22:27:23 +00:00
|
|
|
|
2021-05-09 20:47:46 +00:00
|
|
|
void AudioManager::setupAudioStream(int sampleRate)
|
|
|
|
{
|
2021-05-16 18:17:11 +00:00
|
|
|
SDL_AudioStatus audioStatus = SDL_GetAudioDeviceStatus(sAudioDevice);
|
|
|
|
|
|
|
|
// It's very important to pause the audio device before setting up the stream,
|
|
|
|
// or we may get random crashes if attempting to play samples at the same time.
|
|
|
|
SDL_PauseAudioDevice(sAudioDevice, 1);
|
2021-05-09 20:47:46 +00:00
|
|
|
SDL_FreeAudioStream(sConversionStream);
|
|
|
|
|
|
|
|
// Used for streaming audio from videos.
|
|
|
|
sConversionStream = SDL_NewAudioStream(AUDIO_F32, 2, sampleRate, sAudioFormat.format,
|
2021-07-07 18:31:46 +00:00
|
|
|
sAudioFormat.channels, sAudioFormat.freq);
|
2021-05-09 20:47:46 +00:00
|
|
|
if (sConversionStream == nullptr) {
|
|
|
|
LOG(LogError) << "Failed to create audio conversion stream:";
|
|
|
|
LOG(LogError) << SDL_GetError();
|
|
|
|
}
|
2021-05-16 18:17:11 +00:00
|
|
|
|
|
|
|
// If the device was previously in a playing state, then restore it.
|
|
|
|
if (audioStatus == SDL_AUDIO_PLAYING)
|
|
|
|
SDL_PauseAudioDevice(sAudioDevice, 0);
|
2021-05-09 20:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioManager::processStream(const void* samples, unsigned count)
|
2020-12-22 22:27:23 +00:00
|
|
|
{
|
2021-11-15 21:43:06 +00:00
|
|
|
SDL_LockAudioDevice(sAudioDevice);
|
2021-05-29 08:52:40 +00:00
|
|
|
|
2020-12-22 22:27:23 +00:00
|
|
|
if (SDL_AudioStreamPut(sConversionStream, samples, count * sizeof(Uint8)) == -1) {
|
|
|
|
LOG(LogError) << "Failed to put samples in the conversion stream:";
|
|
|
|
LOG(LogError) << SDL_GetError();
|
2021-11-15 21:43:06 +00:00
|
|
|
SDL_UnlockAudioDevice(sAudioDevice);
|
2020-12-22 22:27:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-09 20:47:46 +00:00
|
|
|
if (count > 0)
|
|
|
|
SDL_PauseAudioDevice(sAudioDevice, 0);
|
2021-11-15 21:43:06 +00:00
|
|
|
|
|
|
|
SDL_UnlockAudioDevice(sAudioDevice);
|
2021-05-09 20:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioManager::clearStream()
|
|
|
|
{
|
2021-11-15 21:43:06 +00:00
|
|
|
SDL_LockAudioDevice(sAudioDevice);
|
|
|
|
SDL_AudioStreamClear(sConversionStream);
|
|
|
|
SDL_UnlockAudioDevice(sAudioDevice);
|
2020-12-22 22:27:23 +00:00
|
|
|
}
|