2020-06-21 12:25:28 +00:00
|
|
|
//
|
|
|
|
// Sound.cpp
|
|
|
|
//
|
|
|
|
// Higher-level audio functions.
|
|
|
|
// Reading theme sound setings, playing audio samples etc.
|
|
|
|
//
|
|
|
|
|
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"
|
|
|
|
|
2020-06-06 11:10:33 +00:00
|
|
|
NavigationSounds* NavigationSounds::sInstance = nullptr;
|
|
|
|
|
2014-01-03 16:40:36 +00:00
|
|
|
std::map< std::string, std::shared_ptr<Sound> > Sound::sMap;
|
|
|
|
|
|
|
|
std::shared_ptr<Sound> Sound::get(const std::string& path)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
auto it = sMap.find(path);
|
|
|
|
if(it != sMap.cend())
|
|
|
|
return it->second;
|
2014-01-03 16:40:36 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
std::shared_ptr<Sound> sound = std::shared_ptr<Sound>(new Sound(path));
|
|
|
|
AudioManager::getInstance()->registerSound(sound);
|
|
|
|
sMap[path] = sound;
|
|
|
|
return sound;
|
2014-01-03 16:40:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
std::shared_ptr<Sound> Sound::getFromTheme(const std::shared_ptr<ThemeData>& theme,
|
|
|
|
const std::string& view, const std::string& element)
|
2014-01-03 16:40:36 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
LOG(LogInfo) << "Sound::getFromTheme() looking for [" << view << "." << element << "]";
|
2014-01-03 16:40:36 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "sound");
|
|
|
|
if(!elem || !elem->has("path")) {
|
|
|
|
LOG(LogInfo) << "[" << element << "] not found, won't load any sound file";
|
|
|
|
return get("");
|
|
|
|
}
|
2020-06-06 11:10:33 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
LOG(LogInfo) << "[" << element << "] found, ready to load sound file";
|
|
|
|
return get(elem->get<std::string>("path"));
|
2014-01-03 16:40:36 +00:00
|
|
|
}
|
2012-10-13 18:29:53 +00:00
|
|
|
|
2020-06-06 11:10:33 +00:00
|
|
|
NavigationSounds* NavigationSounds::getInstance()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (sInstance == nullptr)
|
|
|
|
sInstance = new NavigationSounds();
|
2020-06-06 11:10:33 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
return sInstance;
|
2020-06-06 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 20:13:07 +00:00
|
|
|
void NavigationSounds::deinit()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (sInstance)
|
|
|
|
delete sInstance;
|
2020-06-17 20:13:07 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
sInstance = nullptr;
|
2020-06-17 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 16:21:24 +00:00
|
|
|
void NavigationSounds::loadThemeNavigationSounds(const std::shared_ptr<ThemeData>& theme)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
navigationSounds.push_back(Sound::getFromTheme(theme, "all", "systembrowseSound"));
|
|
|
|
navigationSounds.push_back(Sound::getFromTheme(theme, "all", "quicksysselectSound"));
|
|
|
|
navigationSounds.push_back(Sound::getFromTheme(theme, "all", "selectSound"));
|
|
|
|
navigationSounds.push_back(Sound::getFromTheme(theme, "all", "backSound"));
|
|
|
|
navigationSounds.push_back(Sound::getFromTheme(theme, "all", "scrollSound"));
|
|
|
|
navigationSounds.push_back(Sound::getFromTheme(theme, "all", "favoriteSound"));
|
|
|
|
navigationSounds.push_back(Sound::getFromTheme(theme, "all", "launchSound"));
|
2020-05-15 16:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NavigationSounds::playThemeNavigationSound(NavigationSoundsID soundID)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
NavigationSounds::getInstance()->navigationSounds[soundID]->play();
|
2020-05-15 16:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NavigationSounds::isPlayingThemeNavigationSound(NavigationSoundsID soundID)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return NavigationSounds::getInstance()->navigationSounds[soundID]->isPlaying();
|
2020-05-15 16:21:24 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
Sound::Sound(
|
|
|
|
const std::string & path)
|
2020-06-28 16:39:18 +00:00
|
|
|
: mSampleData(nullptr),
|
2020-06-21 12:25:28 +00:00
|
|
|
mSamplePos(0),
|
|
|
|
mSampleLength(0),
|
|
|
|
playing(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
|
|
|
}
|
|
|
|
|
|
|
|
Sound::~Sound()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
deinit();
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 19:31:39 +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-06-28 16:39:18 +00:00
|
|
|
if(mSampleData != nullptr)
|
2020-06-21 12:25:28 +00:00
|
|
|
deinit();
|
2012-10-13 18:29:53 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if(mPath.empty())
|
|
|
|
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;
|
|
|
|
Uint8 * data = nullptr;
|
2013-05-14 19:31:39 +00:00
|
|
|
Uint32 dlen = 0;
|
2020-06-21 12:25:28 +00:00
|
|
|
if (SDL_LoadWAV(mPath.c_str(), &wave, &data, &dlen) == nullptr) {
|
|
|
|
LOG(LogError) << "Error loading sound file \"" << mPath << "\"!\n" << " " << SDL_GetError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Build conversion buffer.
|
|
|
|
SDL_AudioCVT cvt;
|
2013-05-14 19:31:39 +00:00
|
|
|
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 44100);
|
2020-06-21 12:25:28 +00:00
|
|
|
// Copy data to conversion buffer.
|
|
|
|
cvt.len = dlen;
|
2013-05-14 19:31:39 +00:00
|
|
|
cvt.buf = new Uint8[cvt.len * cvt.len_mult];
|
|
|
|
memcpy(cvt.buf, data, dlen);
|
2020-06-21 12:25:28 +00:00
|
|
|
// Convert buffer to stereo, 16bit, 44.1kHz.
|
2013-05-14 19:31:39 +00:00
|
|
|
if (SDL_ConvertAudio(&cvt) < 0) {
|
2020-06-21 12:25:28 +00:00
|
|
|
LOG(LogError) << "Error converting sound \"" << mPath <<
|
|
|
|
"\" to 44.1kHz, 16bit, stereo format!\n" << " " << SDL_GetError();
|
|
|
|
delete[] cvt.buf;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Worked. set up member data.
|
|
|
|
SDL_LockAudio();
|
|
|
|
mSampleData = cvt.buf;
|
|
|
|
mSampleLength = cvt.len_cvt;
|
|
|
|
mSamplePos = 0;
|
|
|
|
mSampleFormat.channels = 2;
|
|
|
|
mSampleFormat.freq = 44100;
|
|
|
|
mSampleFormat.format = AUDIO_S16;
|
|
|
|
SDL_UnlockAudio();
|
|
|
|
}
|
|
|
|
// Free WAV data now.
|
2013-05-14 19:31:39 +00:00
|
|
|
SDL_FreeWAV(data);
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::deinit()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
playing = false;
|
2013-05-14 19:31:39 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if(mSampleData != nullptr) {
|
|
|
|
SDL_LockAudio();
|
|
|
|
delete[] mSampleData;
|
|
|
|
mSampleData = nullptr;
|
|
|
|
mSampleLength = 0;
|
|
|
|
mSamplePos = 0;
|
|
|
|
SDL_UnlockAudio();
|
|
|
|
}
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::play()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if(mSampleData == nullptr)
|
|
|
|
return;
|
|
|
|
|
2020-07-08 15:06:34 +00:00
|
|
|
if(!Settings::getInstance()->getBool("EnableNavigationSounds"))
|
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
|
|
|
AudioManager::getInstance();
|
2013-08-07 05:41:55 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
SDL_LockAudio();
|
2017-09-14 01:26:33 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (playing)
|
|
|
|
// Replay from start. rewind the sample to the beginning.
|
|
|
|
mSamplePos = 0;
|
|
|
|
else
|
|
|
|
// Flag our sample as playing.
|
|
|
|
playing = true;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
SDL_UnlockAudio();
|
|
|
|
// Tell the AudioManager to start playing samples.
|
|
|
|
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
|
|
|
bool Sound::isPlaying() const
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return playing;
|
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.
|
|
|
|
SDL_LockAudio();
|
|
|
|
playing = false;
|
|
|
|
mSamplePos = 0;
|
|
|
|
SDL_UnlockAudio();
|
2012-10-13 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 19:31:39 +00:00
|
|
|
const Uint8 * Sound::getData() const
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return mSampleData;
|
2013-05-14 19:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Uint32 Sound::getPosition() const
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return mSamplePos;
|
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.
|
|
|
|
playing = false;
|
|
|
|
mSamplePos = 0;
|
|
|
|
}
|
2013-05-14 19:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Uint32 Sound::getLength() const
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return mSampleLength;
|
2013-08-06 13:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Uint32 Sound::getLengthMS() const
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// 44100 samples per second, 2 channels (stereo).
|
|
|
|
// I have no idea why the *0.75 is necessary, but otherwise it's inaccurate.
|
|
|
|
return (Uint32)((mSampleLength / 44100.0f / 2.0f * 0.75f) * 1000);
|
2013-08-06 13:15:20 +00:00
|
|
|
}
|