Replace raudio with SDL-Mixer-X for looping support

This commit is contained in:
Zachary Hall 2023-07-10 12:45:24 -07:00
parent 4f9f62ee8e
commit 01edf59446
8 changed files with 58 additions and 41 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ build*
subprojects/*/
.cache
compile_commands.json
!subprojects/SDL-Mixer-X/

12
.gitmodules vendored
View file

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

View file

@ -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) {

View file

@ -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)

View file

@ -1,43 +1,56 @@
#include "playback.h"
extern "C" {
#include "raudio.h"
}
#include "SDL_mixer.h"
#include <SDL_audio.h>
#include <exception>
#include <thread>
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() {

View file

@ -1,4 +1,5 @@
#pragma once
#include "SDL_mixer.h"
#include <thread>
#include <string>
#include <atomic>
@ -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();

1
raudio

@ -1 +0,0 @@
Subproject commit 8f0a255549524b5a8ea130371cabfbe77c94c7d5

@ -0,0 +1 @@
Subproject commit 22aed1f6bcfa6912a34d3241edf3bd90498a6bc2