From 01edf594463470a200fbbf603ffaf4a9e8a7fba2 Mon Sep 17 00:00:00 2001 From: Zachary Hall Date: Mon, 10 Jul 2023 12:45:24 -0700 Subject: [PATCH] Replace raudio with SDL-Mixer-X for looping support --- .gitignore | 3 ++- .gitmodules | 12 +++------ main.cpp | 7 +++--- meson.build | 15 +++++++---- playback.cpp | 55 +++++++++++++++++++++++++---------------- playback.h | 5 ++++ raudio | 1 - subprojects/SDL-Mixer-X | 1 + 8 files changed, 58 insertions(+), 41 deletions(-) delete mode 160000 raudio create mode 160000 subprojects/SDL-Mixer-X diff --git a/.gitignore b/.gitignore index f5b810d..fb1e5d2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ build* .vscode subprojects/*/ .cache -compile_commands.json \ No newline at end of file +compile_commands.json +!subprojects/SDL-Mixer-X/ \ No newline at end of file diff --git a/.gitmodules b/.gitmodules index 361f5fe..2fd0a96 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,15 +1,9 @@ -[submodule "raygui"] - path = raygui - url = https://github.com/raysan5/raygui.git [submodule "IconFontCppHeaders"] path = IconFontCppHeaders url = https://github.com/juliettef/IconFontCppHeaders.git [submodule "imgui"] path = imgui url = https://github.com/ocornut/imgui.git -[submodule "subprojects/raylib"] - path = subprojects/raylib - url = https://github.com/raysan5/raylib.git -[submodule "raudio"] - path = raudio - url = https://github.com/raysan5/raudio.git +[submodule "subprojects/SDL-Mixer-X"] + path = subprojects/SDL-Mixer-X + url = https://github.com/WohlSoft/SDL-Mixer-X.git diff --git a/main.cpp b/main.cpp index 15c51e5..b5a4a57 100644 --- a/main.cpp +++ b/main.cpp @@ -287,7 +287,7 @@ int main(int, char**) playback->Seek(0.0); } ImGui::SameLine(); - ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - (ImGui::GetFontSize() * 22) - (ImGui::GetStyle().FramePadding.x * 10)); + ImGui::SetNextItemWidth(ImGui::GetWindowWidth() - (ImGui::GetFontSize() * (22 - 8)) - (ImGui::GetStyle().FramePadding.x * (10 - 2))); uint8_t components = TimeToComponentCount(playback->GetLength()); string time_str = TimeToString(position, components); if (ImGui::SliderFloat("##Seek", &position, 0.0f, playback->GetLength(), time_str.c_str(), ImGuiSliderFlags_NoRoundToFormat)) @@ -301,13 +301,12 @@ int main(int, char**) ImGui::SameLine(); if (ImGui::SliderFloat("##Volume", &playback->volume, 0.0, 100.0, ICON_FK_VOLUME_UP ": %.0f%%")) { playback->Update(); - } - ImGui::SameLine(); + }/* ImGui::SameLine(); ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); if (ImGui::SliderFloat("##Speed", &playback->speed, 0.25, 4.0, "Speed: %.2fx", ImGuiSliderFlags_Logarithmic)) { playback->Update(); - } + }*/ } ImGui::End(); if (prefs_window) { diff --git a/meson.build b/meson.build index 0112a03..aa960c6 100644 --- a/meson.build +++ b/meson.build @@ -2,18 +2,24 @@ project('neko-player', ['c', 'cpp'], version : '0.1', default_options : ['warning_level=3', 'cpp_std=c++20']) +cmake = import('cmake') if get_option('debug') add_global_arguments('-DDEBUG', language: 'cpp') endif -raudio_lib = static_library('raudio', 'raudio/src/raudio.c', c_args: ['-DRAUDIO_STANDALONE', '-DSUPPORT_MODULE_RAUDIO', '-DSUPPORT_FILEFORMAT_WAV', '-DSUPPORT_FILEFORMAT_OGG', '-DSUPPORT_FILEFORMAT_MP3', '-DSUPPORT_FILEFORMAT_QOA', '-DSUPPORT_FILEFORMAT_FLAC', '-DSUPPORT_FILEFORMAT_XM', '-DSUPPORT_FILEFORMAT_MOD', '-w']) -raudio_include = include_directories('raudio/src') -raudio_dep = declare_dependency(include_directories: raudio_include, link_with: raudio_lib) +smx_opts = cmake.subproject_options() +smx_opts.add_cmake_defines({'SDL_MIXER_X_STATIC': true, 'SDL_MIXER_X_SHARED': false}) +smx_opts.set_override_option('c_std', 'c99') +smx_subproj = cmake.subproject('SDL-Mixer-X', options: smx_opts) +#raudio_lib = static_library('raudio', 'raudio/src/raudio.c', c_args: ['-DRAUDIO_STANDALONE', '-DSUPPORT_MODULE_RAUDIO', '-DSUPPORT_FILEFORMAT_WAV', '-DSUPPORT_FILEFORMAT_OGG', '-DSUPPORT_FILEFORMAT_MP3', '-DSUPPORT_FILEFORMAT_QOA', '-DSUPPORT_FILEFORMAT_FLAC', '-DSUPPORT_FILEFORMAT_XM', '-DSUPPORT_FILEFORMAT_MOD', '-w']) +#raudio_include = include_directories('raudio/src') +#raudio_dep = declare_dependency(include_directories: raudio_include, link_with: raudio_lib) deps = [ dependency('sdl2'), dependency('sdl2_image'), dependency('gl'), dependency('jsoncpp'), - raudio_dep, + #raudio_dep, + smx_subproj.dependency('SDL2_mixer_ext_Static'), ] srcs = [ @@ -35,7 +41,6 @@ include_dirs = [ 'imgui-filebrowser', 'IconFontCppHeaders', 'assets', - 'raudio/src', ] res = run_command('./assets/update-assets.sh', check: true) diff --git a/playback.cpp b/playback.cpp index 257cdc0..c7cae20 100644 --- a/playback.cpp +++ b/playback.cpp @@ -1,43 +1,56 @@ #include "playback.h" -extern "C" { -#include "raudio.h" -} +#include "SDL_mixer.h" +#include +#include #include using namespace std::chrono; +Mix_Music *Playback::Load(const char *file) { + Mix_Music *output = Mix_LoadMUS(file); + if (!output) { + printf("Error loading music '%s': %s\n", file, Mix_GetError()); + throw std::exception(); + } + Mix_PlayMusicStream(output, -1); + length = Mix_MusicDuration(output); + update.store(true); + return output; +} +void Playback::Unload(Mix_Music *music) { + Mix_HaltMusicStream(music); + Mix_FreeMusic(music); +} void Playback::ThreadFunc() { - InitAudioDevice(); - Music music = LoadMusicStream(filePath.c_str()); - PlayMusicStream(music); - length = GetMusicTimeLength(music); + Mix_Init(MIX_INIT_FLAC|MIX_INIT_MID|MIX_INIT_MOD|MIX_INIT_MP3|MIX_INIT_OGG|MIX_INIT_OPUS|MIX_INIT_WAVPACK); + Mix_OpenAudioDevice(48000, AUDIO_S16SYS, 2, 4096, NULL, 0); + Mix_Music *music = Load(filePath.c_str()); + while (running) { if (file_changed.exchange(false)) { - StopMusicStream(music); - UnloadMusicStream(music); - music = LoadMusicStream(filePath.c_str()); - PlayMusicStream(music); - length = GetMusicTimeLength(music); + Unload(music); + music = Load(filePath.c_str()); } - UpdateMusicStream(music); if (flag_mutex.try_lock()) { if (seeking.exchange(false)) { - SeekMusicStream(music, position); + Mix_SetMusicPositionStream(music, position); } if (paused) { - PauseMusicStream(music); + Mix_PauseMusicStream(music); } else { - ResumeMusicStream(music); + Mix_ResumeMusicStream(music); } if (update.exchange(false)) { - SetMasterVolume(volume / 100.0); - SetMusicPitch(music, speed); + Mix_VolumeMusicStream(music, (volume / 100.0 * MIX_MAX_VOLUME)); } flag_mutex.unlock(); } - position = GetMusicTimePlayed(music); + position = Mix_GetMusicPosition(music); std::this_thread::sleep_for(20ms); } - UnloadMusicStream(music); - CloseAudioDevice(); + // ==== + Unload(music); + Mix_CloseAudio(); + Mix_Quit(); + free(buf); } Playback::Playback() { diff --git a/playback.h b/playback.h index 3378942..a0320d9 100644 --- a/playback.h +++ b/playback.h @@ -1,4 +1,5 @@ #pragma once +#include "SDL_mixer.h" #include #include #include @@ -15,6 +16,10 @@ private: double position; double length; bool paused; + Uint8 *buf; + size_t bufsize; + Mix_Music *Load(const char* file); + void Unload(Mix_Music* music); void ThreadFunc(); public: Playback(); diff --git a/raudio b/raudio deleted file mode 160000 index 8f0a255..0000000 --- a/raudio +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8f0a255549524b5a8ea130371cabfbe77c94c7d5 diff --git a/subprojects/SDL-Mixer-X b/subprojects/SDL-Mixer-X new file mode 160000 index 0000000..22aed1f --- /dev/null +++ b/subprojects/SDL-Mixer-X @@ -0,0 +1 @@ +Subproject commit 22aed1f6bcfa6912a34d3241edf3bd90498a6bc2