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
|
|
|
// Sound.cpp
|
|
|
|
//
|
|
|
|
// Higher-level audio functions.
|
2022-12-22 16:15:15 +00:00
|
|
|
// Navigation sounds, audio sample playback etc.
|
2020-06-21 12:25:28 +00:00
|
|
|
//
|
|
|
|
|
2012-10-13 18:29:53 +00:00
|
|
|
#include "Sound.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2012-10-13 18:29:53 +00:00
|
|
|
#include "AudioManager.h"
|
2013-01-04 23:31:51 +00:00
|
|
|
#include "Log.h"
|
2013-08-07 05:41:55 +00:00
|
|
|
#include "Settings.h"
|
2014-01-03 16:40:36 +00:00
|
|
|
#include "ThemeData.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "resources/ResourceManager.h"
|
2014-01-03 16:40:36 +00:00
|
|
|
|
|
|
|
std::shared_ptr<Sound> Sound::get(const std::string& path)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
auto it = sMap.find(path);
|
2020-07-13 18:58:25 +00:00
|
|
|
if (it != sMap.cend())
|
2020-06-21 12:25:28 +00:00
|
|
|
return it->second;
|
2014-01-03 16:40:36 +00:00
|
|
|
|
2022-01-16 17:18:28 +00:00
|
|
|
std::shared_ptr<Sound> sound {std::shared_ptr<Sound>(new Sound(path))};
|
2021-11-15 21:43:06 +00:00
|
|
|
AudioManager::getInstance().registerSound(sound);
|
2020-06-21 12:25:28 +00:00
|
|
|
sMap[path] = sound;
|
|
|
|
return sound;
|
2014-01-03 16:40:36 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 21:43:06 +00:00
|
|
|
std::shared_ptr<Sound> Sound::getFromTheme(ThemeData* const theme,
|
2021-07-07 18:31:46 +00:00
|
|
|
const std::string& view,
|
|
|
|
const std::string& element)
|
2014-01-03 16:40:36 +00:00
|
|
|
{
|
2022-01-29 17:41:22 +00:00
|
|
|
std::string elemName {element.substr(6, std::string::npos)};
|
|
|
|
|
2021-11-15 21:43:06 +00:00
|
|
|
if (theme == nullptr) {
|
2022-01-29 17:41:22 +00:00
|
|
|
LOG(LogDebug) << "Sound::getFromTheme(): Using fallback sound file for \"" << elemName
|
2021-07-07 18:31:46 +00:00
|
|
|
<< "\"";
|
2022-01-29 17:41:22 +00:00
|
|
|
return get(ResourceManager::getInstance().getResourcePath(":/sounds/" + elemName + ".wav"));
|
2021-03-21 14:42:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 17:41:22 +00:00
|
|
|
LOG(LogDebug) << "Sound::getFromTheme(): Looking for tag <sound name=\"" << elemName << "\">";
|
2014-01-03 16:40:36 +00:00
|
|
|
|
2022-12-22 16:15:15 +00:00
|
|
|
const ThemeData::ThemeElement* elem {theme->getElement(view, element, "sound")};
|
2020-07-13 18:58:25 +00:00
|
|
|
if (!elem || !elem->has("path")) {
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogDebug) << "Sound::getFromTheme(): Tag not found, using fallback sound file";
|
2022-01-29 17:41:22 +00:00
|
|
|
return get(ResourceManager::getInstance().getResourcePath(":/sounds/" + elemName + ".wav"));
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
2020-06-06 11:10:33 +00:00
|
|
|
|
2022-12-22 16:15:15 +00:00
|
|
|
if (!Utils::FileSystem::exists(elem->get<std::string>("path"))) {
|
|
|
|
LOG(LogError) << "Sound::getFromTheme(): Navigation sound tag found but sound file does "
|
|
|
|
"not exist, falling back to default sound";
|
|
|
|
return get(ResourceManager::getInstance().getResourcePath(":/sounds/" + elemName + ".wav"));
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:28:20 +00:00
|
|
|
LOG(LogDebug) << "Sound::getFromTheme(): Tag found, ready to load theme sound file";
|
2020-06-21 12:25:28 +00:00
|
|
|
return get(elem->get<std::string>("path"));
|
2014-01-03 16:40:36 +00:00
|
|
|
}
|
2012-10-13 18:29:53 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
Sound::Sound(const std::string& path)
|
|
|
|
: mSampleData(nullptr)
|
|
|
|
, mSamplePos(0)
|
|
|
|
, mSampleLength(0)
|
2021-11-15 21:43:06 +00:00
|
|
|
, mPlaying(false)
|
2013-05-14 19:31:39 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
loadFile(path);
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 17:45:26 +00:00
|
|
|
void Sound::loadFile(const std::string& path)
|
2012-10-13 18:29:53 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mPath = path;
|
|
|
|
init();
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::init()
|
|
|
|
{
|
2020-07-13 18:58:25 +00:00
|
|
|
if (mSampleData != nullptr)
|
2020-06-21 12:25:28 +00:00
|
|
|
deinit();
|
2012-10-13 18:29:53 +00:00
|
|
|
|
2020-07-13 18:58:25 +00:00
|
|
|
if (mPath.empty())
|
2020-06-21 12:25:28 +00:00
|
|
|
return;
|
2012-10-13 18:29:53 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// Load WAV file via SDL.
|
|
|
|
SDL_AudioSpec wave;
|
2022-12-22 16:15:15 +00:00
|
|
|
Uint8* data {nullptr};
|
|
|
|
Uint32 dlen {0};
|
2020-06-21 12:25:28 +00:00
|
|
|
if (SDL_LoadWAV(mPath.c_str(), &wave, &data, &dlen) == nullptr) {
|
2022-01-29 17:41:22 +00:00
|
|
|
LOG(LogError) << "Failed to load theme navigation sound file: " << SDL_GetError();
|
2020-06-21 12:25:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-12-22 22:27:23 +00:00
|
|
|
|
|
|
|
// Convert sound file to the format required by ES-DE.
|
2022-12-22 16:15:15 +00:00
|
|
|
SDL_AudioStream* conversionStream {
|
2021-07-07 18:31:46 +00:00
|
|
|
SDL_NewAudioStream(wave.format, wave.channels, wave.freq, AudioManager::sAudioFormat.format,
|
2022-12-22 16:15:15 +00:00
|
|
|
AudioManager::sAudioFormat.channels, AudioManager::sAudioFormat.freq)};
|
2020-12-22 22:27:23 +00:00
|
|
|
|
|
|
|
if (conversionStream == nullptr) {
|
2022-01-29 17:41:22 +00:00
|
|
|
LOG(LogError) << "Failed to create sample conversion stream: " << SDL_GetError();
|
2020-12-22 22:27:23 +00:00
|
|
|
return;
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
2020-12-22 22:27:23 +00:00
|
|
|
|
|
|
|
if (SDL_AudioStreamPut(conversionStream, data, dlen) == -1) {
|
2022-01-29 17:41:22 +00:00
|
|
|
LOG(LogError) << "Failed to put samples in the conversion stream: " << SDL_GetError();
|
2020-12-22 22:27:23 +00:00
|
|
|
SDL_FreeAudioStream(conversionStream);
|
|
|
|
return;
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
2020-12-22 22:27:23 +00:00
|
|
|
|
2022-12-22 16:15:15 +00:00
|
|
|
int sampleLength {SDL_AudioStreamAvailable(conversionStream)};
|
2020-12-22 22:27:23 +00:00
|
|
|
|
2022-12-22 16:15:15 +00:00
|
|
|
Uint8* converted {new Uint8[sampleLength]};
|
2020-12-22 22:27:23 +00:00
|
|
|
if (SDL_AudioStreamGet(conversionStream, converted, sampleLength) == -1) {
|
2022-01-29 17:41:22 +00:00
|
|
|
LOG(LogError) << "Failed to convert sound file '" << mPath << "': " << SDL_GetError();
|
2020-12-22 22:27:23 +00:00
|
|
|
SDL_FreeAudioStream(conversionStream);
|
|
|
|
delete[] converted;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSampleData = converted;
|
|
|
|
mSampleLength = sampleLength;
|
|
|
|
mSamplePos = 0;
|
|
|
|
mSampleFormat.freq = AudioManager::sAudioFormat.freq;
|
|
|
|
mSampleFormat.channels = AudioManager::sAudioFormat.channels;
|
|
|
|
mSampleFormat.format = AudioManager::sAudioFormat.format;
|
|
|
|
SDL_FreeAudioStream(conversionStream);
|
2013-05-14 19:31:39 +00:00
|
|
|
SDL_FreeWAV(data);
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::deinit()
|
|
|
|
{
|
2021-11-15 21:43:06 +00:00
|
|
|
mPlaying = false;
|
2013-05-14 19:31:39 +00:00
|
|
|
|
2020-07-13 18:58:25 +00:00
|
|
|
if (mSampleData != nullptr) {
|
2020-12-20 23:23:22 +00:00
|
|
|
SDL_LockAudioDevice(AudioManager::sAudioDevice);
|
2020-06-21 12:25:28 +00:00
|
|
|
delete[] mSampleData;
|
|
|
|
mSampleData = nullptr;
|
|
|
|
mSampleLength = 0;
|
|
|
|
mSamplePos = 0;
|
2020-12-20 23:23:22 +00:00
|
|
|
SDL_UnlockAudioDevice(AudioManager::sAudioDevice);
|
2021-03-19 17:37:56 +00:00
|
|
|
sMap.erase(mPath);
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::play()
|
|
|
|
{
|
2020-07-13 18:58:25 +00:00
|
|
|
if (mSampleData == nullptr)
|
2020-06-21 12:25:28 +00:00
|
|
|
return;
|
|
|
|
|
2020-07-13 18:58:25 +00:00
|
|
|
if (!Settings::getInstance()->getBool("NavigationSounds"))
|
2020-06-21 12:25:28 +00:00
|
|
|
return;
|
2012-10-13 18:29:53 +00:00
|
|
|
|
2021-11-15 21:43:06 +00:00
|
|
|
if (!AudioManager::getInstance().getHasAudioDevice())
|
2020-12-31 13:27:31 +00:00
|
|
|
return;
|
|
|
|
|
2020-12-20 23:23:22 +00:00
|
|
|
SDL_LockAudioDevice(AudioManager::sAudioDevice);
|
2017-09-14 01:26:33 +00:00
|
|
|
|
2021-11-15 21:43:06 +00:00
|
|
|
if (mPlaying)
|
2020-06-21 12:25:28 +00:00
|
|
|
// Replay from start. rewind the sample to the beginning.
|
|
|
|
mSamplePos = 0;
|
|
|
|
else
|
|
|
|
// Flag our sample as playing.
|
2021-11-15 21:43:06 +00:00
|
|
|
mPlaying = true;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-12-20 23:23:22 +00:00
|
|
|
SDL_UnlockAudioDevice(AudioManager::sAudioDevice);
|
2020-06-21 12:25:28 +00:00
|
|
|
// Tell the AudioManager to start playing samples.
|
2021-11-15 21:43:06 +00:00
|
|
|
AudioManager::getInstance().play();
|
2013-05-14 19:31:39 +00:00
|
|
|
}
|
2012-10-13 18:29:53 +00:00
|
|
|
|
2013-05-14 19:31:39 +00:00
|
|
|
void Sound::stop()
|
2012-10-13 18:29:53 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// Flag our sample as not playing and rewind its position.
|
2020-12-20 23:23:22 +00:00
|
|
|
SDL_LockAudioDevice(AudioManager::sAudioDevice);
|
2021-11-15 21:43:06 +00:00
|
|
|
mPlaying = false;
|
2020-06-21 12:25:28 +00:00
|
|
|
mSamplePos = 0;
|
2020-12-20 23:23:22 +00:00
|
|
|
SDL_UnlockAudioDevice(AudioManager::sAudioDevice);
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 19:31:39 +00:00
|
|
|
void Sound::setPosition(Uint32 newPosition)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mSamplePos = newPosition;
|
|
|
|
if (mSamplePos >= mSampleLength) {
|
|
|
|
// Got to or beyond the end of the sample. stop playing.
|
2021-11-15 21:43:06 +00:00
|
|
|
mPlaying = false;
|
2020-06-21 12:25:28 +00:00
|
|
|
mSamplePos = 0;
|
|
|
|
}
|
2013-05-14 19:31:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 21:43:06 +00:00
|
|
|
NavigationSounds& NavigationSounds::getInstance()
|
2020-12-22 22:27:23 +00:00
|
|
|
{
|
2021-11-15 21:43:06 +00:00
|
|
|
static NavigationSounds instance;
|
|
|
|
return instance;
|
2020-12-22 22:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NavigationSounds::deinit()
|
|
|
|
{
|
2021-11-15 21:43:06 +00:00
|
|
|
for (auto sound : mNavigationSounds) {
|
|
|
|
AudioManager::getInstance().unregisterSound(sound);
|
|
|
|
sound->deinit();
|
2021-03-19 17:37:56 +00:00
|
|
|
}
|
2021-11-15 21:43:06 +00:00
|
|
|
mNavigationSounds.clear();
|
2020-12-22 22:27:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 21:43:06 +00:00
|
|
|
void NavigationSounds::loadThemeNavigationSounds(ThemeData* const theme)
|
2020-12-22 22:27:23 +00:00
|
|
|
{
|
2021-03-21 14:42:13 +00:00
|
|
|
if (theme) {
|
|
|
|
LOG(LogDebug) << "NavigationSounds::loadThemeNavigationSounds(): "
|
2023-08-14 20:40:32 +00:00
|
|
|
"Theme includes navigation sound support, loading custom sounds";
|
2021-03-21 14:42:13 +00:00
|
|
|
}
|
|
|
|
else {
|
2023-08-14 20:40:32 +00:00
|
|
|
LOG(LogDebug) << "NavigationSounds::loadThemeNavigationSounds(): "
|
|
|
|
"Theme does not include navigation sound support, using fallback sounds";
|
2021-03-21 14:42:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 17:41:22 +00:00
|
|
|
mNavigationSounds.push_back(Sound::getFromTheme(theme, "all", "sound_systembrowse"));
|
|
|
|
mNavigationSounds.push_back(Sound::getFromTheme(theme, "all", "sound_quicksysselect"));
|
|
|
|
mNavigationSounds.push_back(Sound::getFromTheme(theme, "all", "sound_select"));
|
|
|
|
mNavigationSounds.push_back(Sound::getFromTheme(theme, "all", "sound_back"));
|
|
|
|
mNavigationSounds.push_back(Sound::getFromTheme(theme, "all", "sound_scroll"));
|
|
|
|
mNavigationSounds.push_back(Sound::getFromTheme(theme, "all", "sound_favorite"));
|
|
|
|
mNavigationSounds.push_back(Sound::getFromTheme(theme, "all", "sound_launch"));
|
2020-12-22 22:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NavigationSounds::playThemeNavigationSound(NavigationSoundsID soundID)
|
2013-08-06 13:15:20 +00:00
|
|
|
{
|
2021-11-15 21:43:06 +00:00
|
|
|
NavigationSounds::getInstance().mNavigationSounds[soundID]->play();
|
2020-12-22 22:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NavigationSounds::isPlayingThemeNavigationSound(NavigationSoundsID soundID)
|
|
|
|
{
|
2021-11-15 21:43:06 +00:00
|
|
|
return NavigationSounds::getInstance().mNavigationSounds[soundID]->isPlaying();
|
2013-08-06 13:15:20 +00:00
|
|
|
}
|