Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Clean up Audio code, Add Mono/Stereo switch #20

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions es-app/src/ApiSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,8 @@ bool ApiSystem::setAudioOutputDevice(std::string selected)
oss << "set-audio set" << " '" << selected << "'";
int exitcode = system(oss.str().c_str());

Sound::get("/usr/share/emulationstation/resources/checksound.ogg")->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::get("/usr/share/emulationstation/resources/checksound.ogg")->play();

return exitcode == 0;
}
Expand Down Expand Up @@ -746,7 +747,8 @@ bool ApiSystem::setAudioOutputPath(std::string selected)
oss << "set-audio esset" << " '" << selected << "'";
int exitcode = system(oss.str().c_str());

Sound::get("/usr/share/emulationstation/resources/checksound.ogg")->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::get("/usr/share/emulationstation/resources/checksound.ogg")->play();

return exitcode == 0;
}
Expand Down Expand Up @@ -797,7 +799,8 @@ bool ApiSystem::setAudioOutputProfile(std::string selected)
oss << "batocera-audio set-profile" << " '" << selected << "'";
int exitcode = system(oss.str().c_str());

Sound::get("/usr/share/emulationstation/resources/checksound.ogg")->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::get("/usr/share/emulationstation/resources/checksound.ogg")->play();

return exitcode == 0;
}
Expand Down
7 changes: 4 additions & 3 deletions es-app/src/SystemScreenSaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void SystemScreenSaver::startScreenSaver()

stopScreenSaver();

if (!loadingNext && Settings::getInstance()->getBool("StopMusicOnScreenSaver")) //(Settings::getInstance()->getBool("VideoAudio") && !Settings::getInstance()->getBool("ScreenSaverVideoMute")))
if (!loadingNext && !Settings::getInstance()->getBool("EnableSounds")) //(Settings::getInstance()->getBool("VideoAudio") && !Settings::getInstance()->getBool("ScreenSaverVideoMute")))
AudioManager::getInstance()->deinit();

std::string screensaver_behavior = Settings::getInstance()->getString("ScreenSaverBehavior");
Expand Down Expand Up @@ -180,9 +180,10 @@ void SystemScreenSaver::startScreenSaver()
}
}

// No videos. Just use a standard screensaver
// No videos. Just use a standard screensaver (black screen)
mState = STATE_SCREENSAVER_ACTIVE;
mCurrentGame = NULL;

}

void SystemScreenSaver::stopScreenSaver()
Expand Down Expand Up @@ -214,7 +215,7 @@ void SystemScreenSaver::stopScreenSaver()
PowerSaver::runningScreenSaver(false);

// Exiting screen saver -> Restore sound
if (isExitingScreenSaver && Settings::getInstance()->getBool("StopMusicOnScreenSaver")) //isVideoScreenSaver && Settings::getInstance()->getBool("VideoAudio") && !Settings::getInstance()->getBool("ScreenSaverVideoMute"))
if (isExitingScreenSaver && Settings::getInstance()->getBool("EnableSounds")) //isVideoScreenSaver && Settings::getInstance()->getBool("VideoAudio") && !Settings::getInstance()->getBool("ScreenSaverVideoMute"))
{
AudioManager::getInstance()->init();

Expand Down
3 changes: 2 additions & 1 deletion es-app/src/components/CarouselComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ void CarouselComponent::onCursorChanged(const CursorState& state)
return;

if (!mScrollSound.empty())
Sound::get(mScrollSound)->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::get(mScrollSound)->play();

int oldCursor = mLastCursor;

Expand Down
22 changes: 11 additions & 11 deletions es-app/src/guis/GuiGeneralScreensaverOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ GuiGeneralScreensaverOptions::GuiGeneralScreensaverOptions(Window* window, int s
});

// Screensaver stops music
if (Settings::getInstance()->getBool("audio.bgmusic"))
{
auto ctlStopMusic = std::make_shared<SwitchComponent>(mWindow);
ctlStopMusic->setState(Settings::getInstance()->getBool("StopMusicOnScreenSaver"));
addWithLabel(_("STOP MUSIC ON SCREENSAVER"), ctlStopMusic);
addSaveFunc([ctlStopMusic] { Settings::getInstance()->setBool("StopMusicOnScreenSaver", ctlStopMusic->getState()); });
}
// if (Settings::getInstance()->getBool("audio.bgmusic"))
// {
// auto ctlStopMusic = std::make_shared<SwitchComponent>(mWindow);
// ctlStopMusic->setState(Settings::getInstance()->getBool("EnableSounds"));
// addWithLabel(_("STOP MUSIC ON SCREENSAVER"), ctlStopMusic);
// addSaveFunc([ctlStopMusic] { Settings::getInstance()->setBool("EnableSounds", ctlStopMusic->getState()); });
// }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without any information about commented lines I don't find it very useful to keep them. Can you remove the lines?


if (ssBehavior == "random video")
addVideoScreensaverOptions(selectItem);
Expand Down Expand Up @@ -143,10 +143,10 @@ void GuiGeneralScreensaverOptions::addVideoScreensaverOptions(int selectItem)
addWithLabel(_("STRETCH VIDEOS"), stretch_screensaver);
addSaveFunc([stretch_screensaver] { Settings::getInstance()->setBool("StretchVideoOnScreenSaver", stretch_screensaver->getState()); });

auto ss_video_mute = std::make_shared<SwitchComponent>(mWindow);
ss_video_mute->setState(Settings::getInstance()->getBool("ScreenSaverVideoMute"));
addWithLabel(_("MUTE VIDEO AUDIO"), ss_video_mute);
addSaveFunc([ss_video_mute] { Settings::getInstance()->setBool("ScreenSaverVideoMute", ss_video_mute->getState()); });
//auto ss_video_mute = std::make_shared<SwitchComponent>(mWindow);
//ss_video_mute->setState(Settings::getInstance()->getBool("ScreenSaverVideoMute"));
//addWithLabel(_("MUTE VIDEO AUDIO"), ss_video_mute);
//addSaveFunc([ss_video_mute] { Settings::getInstance()->setBool("ScreenSaverVideoMute", ss_video_mute->getState()); });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without any information about commented lines I don't find it very useful to keep them. Can you remove the lines?


// Allow ScreenSaver Controls - ScreenSaverControls
auto controls = std::make_shared<SwitchComponent>(mWindow);
Expand Down
37 changes: 32 additions & 5 deletions es-app/src/guis/GuiMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3797,15 +3797,24 @@ void GuiMenu::openSoundSettings()
// disable sounds
auto sounds_enabled = std::make_shared<SwitchComponent>(mWindow);
sounds_enabled->setState(Settings::getInstance()->getBool("EnableSounds"));
s->addWithLabel(_("ENABLE NAVIGATION SOUNDS"), sounds_enabled);
s->addWithLabel(_("ENABLE SYSTEM SOUNDS"), sounds_enabled);
s->addSaveFunc([sounds_enabled]
{
if (sounds_enabled->getState() && !Settings::getInstance()->getBool("EnableSounds") && PowerSaver::getMode() == PowerSaver::INSTANT)
{
Settings::getInstance()->setPowerSaverMode("default");
PowerSaver::init();
}

if (sounds_enabled->getState())
/* user has toggled the sound option to on, so make sure audioManager is alive */
AudioManager::getInstance()->init();
else
/* user has toggled the sound option to off, so make sure audioManager is torn down */
AudioManager::getInstance()->deinit();

Settings::getInstance()->setBool("EnableSounds", sounds_enabled->getState());
Settings::getInstance()->saveFile();
});

auto batteryWarning = std::make_shared<SwitchComponent>(mWindow);
Expand All @@ -3817,12 +3826,30 @@ void GuiMenu::openSoundSettings()
SystemConf::getInstance()->set("system.battery.warning", batteryWarningEnabled ? "1" : "0");
});

auto video_audio = std::make_shared<SwitchComponent>(mWindow);
video_audio->setState(Settings::getInstance()->getBool("VideoAudio"));
s->addWithLabel(_("ENABLE VIDEO PREVIEW AUDIO"), video_audio);
s->addSaveFunc([video_audio] { Settings::getInstance()->setBool("VideoAudio", video_audio->getState()); });
// auto video_audio = std::make_shared<SwitchComponent>(mWindow);
// video_audio->setState(Settings::getInstance()->getBool("VideoAudio"));
// s->addWithLabel(_("ENABLE VIDEO PREVIEW AUDIO"), video_audio);
// s->addSaveFunc([video_audio] { Settings::getInstance()->setBool("VideoAudio", video_audio->getState()); });

const std::string audioVDriverScript = "/usr/bin/audio_vdriver.sh";
if (Utils::FileSystem::exists(audioVDriverScript)) {
auto optionsVAudioDriver = std::make_shared<OptionListComponent<std::string> >(mWindow, _("AUDIO PLAYBACK DRIVER"), false);
std::string selectedVAudioDriver = std::string(getShOutput(R"(/usr/bin/audio_vdriver.sh)"));

std::string a;
for(std::stringstream ss(getShOutput(R"(/usr/bin/audio_vdriver.sh --options)")); getline(ss, a, ' '); ) {
optionsVAudioDriver->add(a, a, a == selectedVAudioDriver);
}

s->addWithLabel(_("AUDIO PLAYBACK DRIVER"), optionsVAudioDriver);

Window *window = mWindow;
s->addSaveFunc([this, window, audioVDriverScript, optionsVAudioDriver, selectedVAudioDriver] {
if (optionsVAudioDriver->changed()) {
runSystemCommand(audioVDriverScript + " " + optionsVAudioDriver->getSelected(), "", nullptr);
}
});
}

mWindow->pushGui(s);
}
Expand Down
18 changes: 9 additions & 9 deletions es-app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,6 @@ void playVideo()
return;
}

Settings::getInstance()->setBool("VideoAudio", true);

bool exitLoop = false;

VideoVlcComponent vid(&window);
Expand Down Expand Up @@ -513,14 +511,16 @@ int main(int argc, char* argv[])
// Create a flag in temporary directory to signal READY state
ApiSystem::getInstance()->setReadyFlag();

// Play music
AudioManager::getInstance()->init();

if (ViewController::get()->getState().viewing == ViewController::GAME_LIST || ViewController::get()->getState().viewing == ViewController::SYSTEM_SELECT)
AudioManager::getInstance()->changePlaylist(ViewController::get()->getState().getSystem()->getTheme());
else
AudioManager::getInstance()->playRandomMusic();
// Play music (as part of ES startup)
if (Settings::getInstance()->getBool("EnableSounds"))
{
AudioManager::getInstance()->init();

if (ViewController::get()->getState().viewing == ViewController::GAME_LIST || ViewController::get()->getState().viewing == ViewController::SYSTEM_SELECT)
AudioManager::getInstance()->changePlaylist(ViewController::get()->getState().getSystem()->getTheme());
else
AudioManager::getInstance()->playRandomMusic();
}

int lastTime = SDL_GetTicks();
int ps_time = SDL_GetTicks();
Expand Down
3 changes: 2 additions & 1 deletion es-app/src/views/SystemView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,8 @@ void SystemView::onCursorChanged(const CursorState& state)
TextToSpeech::getInstance()->say(getSelected()->getFullName());

if (!mCarousel.scrollSound.empty())
Sound::get(mCarousel.scrollSound)->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::get(mCarousel.scrollSound)->play();

int oldCursor = mLastCursor;
mLastCursor = mCursor;
Expand Down
24 changes: 16 additions & 8 deletions es-app/src/views/gamelist/ISimpleGameListView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ bool ISimpleGameListView::input(InputConfig* config, Input input)
if (idx != nullptr && idx->hasRelevency())
return true;

Sound::getFromTheme(mTheme, getName(), "menuOpen")->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::getFromTheme(mTheme, getName(), "menuOpen")->play();
mWindow->pushGui(new GuiGamelistOptions(mWindow, this, this->mRoot->getSystem()));
return true;
}
Expand Down Expand Up @@ -267,7 +268,8 @@ bool ISimpleGameListView::input(InputConfig* config, Input input)

populateList(folder->getChildrenListToDisplay());
setCursor(top);
Sound::getFromTheme(getTheme(), getName(), "back")->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::getFromTheme(getTheme(), getName(), "back")->play();
}
else if (mPopupSelfReference)
{
Expand Down Expand Up @@ -320,7 +322,8 @@ void ISimpleGameListView::showSelectedGameOptions()
if (cursor == nullptr)
return;

Sound::getFromTheme(mTheme, getName(), "menuOpen")->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::getFromTheme(mTheme, getName(), "menuOpen")->play();
mWindow->pushGui(new GuiGameOptions(mWindow, cursor));
}

Expand All @@ -341,11 +344,13 @@ void ISimpleGameListView::showSelectedGameSaveSnapshots()

if (SaveStateRepository::isEnabled(cursor))
{
Sound::getFromTheme(mTheme, getName(), "menuOpen")->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::getFromTheme(mTheme, getName(), "menuOpen")->play();

mWindow->pushGui(new GuiSaveState(mWindow, cursor, [this, cursor](SaveState state)
{
Sound::getFromTheme(getTheme(), getName(), "launch")->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::getFromTheme(getTheme(), getName(), "launch")->play();

LaunchGameOptions options;
options.saveStateInfo = state;
Expand Down Expand Up @@ -375,7 +380,8 @@ void ISimpleGameListView::launchSelectedGame()

populateList(folder->getChildrenListToDisplay());
setCursor(top);
Sound::getFromTheme(getTheme(), getName(), "back")->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::getFromTheme(getTheme(), getName(), "back")->play();
}
else
{
Expand All @@ -386,7 +392,8 @@ void ISimpleGameListView::launchSelectedGame()
{
mWindow->pushGui(new GuiSaveState(mWindow, cursor, [this, cursor](SaveState state)
{
Sound::getFromTheme(getTheme(), getName(), "launch")->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::getFromTheme(getTheme(), getName(), "launch")->play();

LaunchGameOptions options;
options.saveStateInfo = state;
Expand All @@ -396,7 +403,8 @@ void ISimpleGameListView::launchSelectedGame()
}
else
{
Sound::getFromTheme(getTheme(), getName(), "launch")->play();
if (Settings::getInstance()->getBool("EnableSounds"))
Sound::getFromTheme(getTheme(), getName(), "launch")->play();
launch(cursor);
}
}
Expand Down
54 changes: 40 additions & 14 deletions es-core/src/AudioManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ bool AudioManager::isInitialized()
return sInstance->mInitialized;
}

int AudioManager::openMixerDevice()
{
return Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 4096);
}

void AudioManager::closeMixerDevice()
{
Mix_CloseAudio();
}

void AudioManager::init()
{
if (mInitialized)
Expand All @@ -66,42 +76,61 @@ void AudioManager::init()
return;
}

int mixerOpened;
// Open the audio device and pause
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) < 0)
if (mixerOpened = openMixerDevice() < 0)
{
LOG(LogError) << "MUSIC Error - Unable to open SDLMixer audio: " << SDL_GetError() << std::endl;
}
else
{
LOG(LogInfo) << "SDL AUDIO Initialized";
mInitialized = true;

// Reload known sounds
for (unsigned int i = 0; i < sSoundVector.size(); i++)
{
sSoundVector[i]->init();
}
Mix_HaltChannel(-1);
}
}

void AudioManager::freeSounds()
{
// Free known sounds from memory
for (unsigned int i = 0; i < sSoundVector.size(); i++)
sSoundVector[i]->deinit();
}

void AudioManager::deinit()
{
LOG(LogDebug) << "AudioManager::deinit";

freeSounds();
tearDown();

LOG(LogInfo) << "SDL AUDIO Deinitialized";
}

void AudioManager::tearDown()
{
if (!mInitialized)
return;

LOG(LogDebug) << "AudioManager::deinit";
LOG(LogDebug) << "AudioManager::tearDown";

mInitialized = false;

//stop all playback
stop();
stopSound();
stopMusic();

// Free known sounds from memory
for (unsigned int i = 0; i < sSoundVector.size(); i++)
sSoundVector[i]->deinit();

Mix_HookMusicFinished(nullptr);
Mix_HaltMusic();

//completely tear down SDL audio. else SDL hogs audio resources and emulators might fail to start...
Mix_CloseAudio();
closeMixerDevice();
SDL_QuitSubSystem(SDL_INIT_AUDIO);

LOG(LogInfo) << "SDL AUDIO Deinitialized";
Expand All @@ -128,17 +157,14 @@ void AudioManager::unregisterSound(std::shared_ptr<Sound> & sound)
LOG(LogWarning) << "AudioManager Error - tried to unregister a sound that wasn't registered!";
}

void AudioManager::play()
{
getInstance();
}

void AudioManager::stop()
void AudioManager::stopSound()
{
// Stop playing all Sounds
for (unsigned int i = 0; i < sSoundVector.size(); i++)
{
if (sSoundVector.at(i)->isPlaying())
sSoundVector[i]->stop();
}
}

// batocera
Expand Down
Loading