From 695d4775788cc685d69f371f6d25222f9108e7f1 Mon Sep 17 00:00:00 2001 From: mrdude2478 Date: Fri, 6 Oct 2023 12:30:52 +0100 Subject: [PATCH] auto switch between wav and other audio formats --- include/util/util.hpp | 3 ++- source/ThemeInstall.cpp | 3 +-- source/ui/ThemeInstPage.cpp | 19 +++++++++++++++++++ source/ui/mainPage.cpp | 5 +---- source/util/util.cpp | 37 ++++++++++++++++++++++++++++++++++++- 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/include/util/util.hpp b/include/util/util.hpp index 1c502dc..b61f23e 100644 --- a/include/util/util.hpp +++ b/include/util/util.hpp @@ -19,7 +19,8 @@ namespace inst::util { std::vector setClockSpeed(int deviceToClock, uint32_t clockSpeed); std::string getIPAddress(); int getUsbState(); - void playAudio(std::string audioPath); + void playAudio(std::string audioPath); //for mp3/ogg/mod etc + void playWav(std::string audioPath); //for wav files std::vector checkForAppUpdate(); std::string SplitFilename(const std::string& str); } \ No newline at end of file diff --git a/source/ThemeInstall.cpp b/source/ThemeInstall.cpp index 88de6db..45441c6 100644 --- a/source/ThemeInstall.cpp +++ b/source/ThemeInstall.cpp @@ -27,7 +27,6 @@ SOFTWARE. #include #include #include -#include #include #include "ThemeInstall.hpp" #include "util/error.hpp" @@ -204,7 +203,7 @@ namespace ThemeInstStuff { else { if (url[url.size() - 1] != '/') //does this line even do anything? - //First try and stream the links + //First try and see if we have any links for zip files response = inst::curl::downloadToBuffer(url); //If the above fails we probably have an html page - try to download it instead. diff --git a/source/ui/ThemeInstPage.cpp b/source/ui/ThemeInstPage.cpp index 94765b4..f577b37 100644 --- a/source/ui/ThemeInstPage.cpp +++ b/source/ui/ThemeInstPage.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #define COLOR(hex) pu::ui::Color::FromHex(hex) @@ -209,6 +210,8 @@ namespace inst::ui { inst::ui::mainApp->ThemeinstPage->setInstBarPerc(0); ourPath = inst::config::appDir + "/temp_download.zip"; installing = 1; + //to do put download on a seperate thread. + //https://www.geeksforgeeks.org/multithreading-in-cpp/ bool didDownload = inst::curl::downloadFile(selectedUrls[0], ourPath.c_str(), 0, true); bool didExtract = false; if (didDownload) { @@ -221,6 +224,14 @@ namespace inst::ui { } } else { + if (inst::config::useSound) { + std::string audioPath = ""; + std::string fail = inst::config::appDir + "audio.fail"_theme; + if (inst::config::useTheme && std::filesystem::exists(inst::config::appDir + "/theme/theme.json") && std::filesystem::exists(fail)) audioPath = (fail); + else audioPath = "romfs:/audio/ohno.wav"; + std::thread audioThread(inst::util::playAudio, audioPath); + audioThread.join(); + } inst::ui::mainApp->ThemeinstPage->pageInfoText->SetText("theme.failed"_lang); installing = 0; inst::ui::mainApp->ThemeinstPage->setInstBarPerc(0); @@ -235,6 +246,14 @@ namespace inst::ui { } std::filesystem::remove(ourPath); if (didExtract) { + if (inst::config::useSound) { + std::string audioPath = ""; + std::string pass = inst::config::appDir + "audio.pass"_theme; + if (inst::config::useTheme && std::filesystem::exists(inst::config::appDir + "/theme/theme.json") && std::filesystem::exists(pass)) audioPath = (pass); + else audioPath = "romfs:/audio/yipee.wav"; + std::thread audioThread(inst::util::playAudio, audioPath); + audioThread.join(); + } inst::ui::mainApp->ThemeinstPage->pageInfoText->SetText("theme.extracted"_lang); std::string good = "romfs:/images/icons/good.png"; if (inst::config::useTheme && std::filesystem::exists(inst::config::appDir + "/theme/theme.json") && std::filesystem::exists(inst::config::appDir + "icons_others.good"_theme)) { diff --git a/source/ui/mainPage.cpp b/source/ui/mainPage.cpp index 085cbae..3f50d10 100644 --- a/source/ui/mainPage.cpp +++ b/source/ui/mainPage.cpp @@ -142,9 +142,6 @@ namespace inst::ui { void playmusic() { SDL_Init(SDL_INIT_AUDIO); - Mix_Init(MIX_INIT_MP3); //enable mp3 support - Mix_Init(MIX_INIT_FLAC); //enable flac support - Mix_Init(MIX_INIT_OGG); //enable ogg support Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096); std::string loadsound = "romfs:/bluesong.mod"; if (inst::config::useTheme && std::filesystem::exists(inst::config::appDir + "/theme/theme.json") && std::filesystem::exists(inst::config::appDir + "audio.music"_theme)) { @@ -153,7 +150,7 @@ namespace inst::ui { const char* x = loadsound.c_str(); audio = Mix_LoadMUS(x); if (audio != NULL) { - Mix_PlayMusic(audio, 100); //Play the audio file 100 loops + Mix_PlayMusic(audio, -1); //loop "infinitely" } } diff --git a/source/util/util.cpp b/source/util/util.cpp index 413d1a3..1bc4659 100644 --- a/source/util/util.cpp +++ b/source/util/util.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -18,6 +19,11 @@ #include "util/json.hpp" #include "nx/usbhdd.h" +// Include sdl2 headers +#include +#include +Mix_Music* music = NULL; + namespace inst::util { void initApp() { if (!std::filesystem::exists("sdmc:/switch")) std::filesystem::create_directory("sdmc:/switch"); @@ -293,6 +299,36 @@ namespace inst::util { } void playAudio(std::string audioPath) { + //check to make sure we aren't trying to play a wav file... + std::string wav("wav"); + std::size_t found = audioPath.find(wav); + if (found != std::string::npos) { + playWav(audioPath); + return; + } + //if not wav try to play + SDL_Init(SDL_INIT_AUDIO); + Mix_Init(MIX_INIT_MP3); //enable mp3 support + Mix_Init(MIX_INIT_FLAC); //enable flac support + Mix_Init(MIX_INIT_OGG); //enable ogg support + Mix_Init(MIX_INIT_MID); + Mix_Init(MIX_INIT_OPUS); + Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096); + const char* x = audioPath.c_str(); + music = Mix_LoadMUS(x); + if (music != NULL) { + Mix_PlayMusic(music, 1); + } + else { + Mix_HaltChannel(-1); + Mix_FreeMusic(music); + Mix_CloseAudio(); + Mix_Quit(); + return; + } + } + + void playWav(std::string audioPath) { int audio_rate = 22050; Uint16 audio_format = AUDIO_S16SYS; int audio_channels = 2; @@ -319,7 +355,6 @@ namespace inst::util { Mix_FreeChunk(sound); Mix_CloseAudio(); - return; }