auto switch between wav and other audio formats

This commit is contained in:
mrdude2478 2023-10-06 12:30:52 +01:00
parent 3d63a8abf6
commit 695d477578
5 changed files with 59 additions and 8 deletions

View File

@ -19,7 +19,8 @@ namespace inst::util {
std::vector<uint32_t> 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<std::string> checkForAppUpdate();
std::string SplitFilename(const std::string& str);
}

View File

@ -27,7 +27,6 @@ SOFTWARE.
#include <fcntl.h>
#include <sstream>
#include <curl/curl.h>
#include <thread>
#include <switch.h>
#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.

View File

@ -14,6 +14,7 @@
#include <sstream>
#include <cstring>
#include <iostream>
#include <thread>
#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)) {

View File

@ -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"
}
}

View File

@ -2,6 +2,7 @@
#include <vector>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <unistd.h>
#include <curl/curl.h>
#include <regex>
@ -18,6 +19,11 @@
#include "util/json.hpp"
#include "nx/usbhdd.h"
// Include sdl2 headers
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
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;
}