From b0338f588aa17bcae40c9e82a8a25feb54477053 Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 1 Jan 2023 17:48:51 -0600 Subject: [PATCH] add SDL_mixer --- .gitmodules | 3 +++ CMakeLists.txt | 12 +++++++++++ src/CMakeLists.txt | 3 +++ src/app/core/controller.cc | 6 ++++++ src/app/editor/music_editor.cc | 39 +++++++++++++++++++++++++++++----- src/app/editor/music_editor.h | 3 +++ src/lib/SDL_mixer | 1 + 7 files changed, 62 insertions(+), 5 deletions(-) create mode 160000 src/lib/SDL_mixer diff --git a/.gitmodules b/.gitmodules index 2cc439c6..f610c45d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -25,3 +25,6 @@ [submodule "src/lib/snes_spc"] path = src/lib/snes_spc url = https://github.com/blarggs-audio-libraries/snes_spc.git +[submodule "src/lib/SDL_mixer"] + path = src/lib/SDL_mixer + url = https://github.com/libsdl-org/SDL_mixer.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a12639b..1d4fa03c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,18 @@ if (UNIX) else() find_package(SDL2) endif() +set(SDL2MIXER_OPUS OFF) +set(SDL2MIXER_FLAC OFF) +set(SDL2MIXER_MOD OFF) +set(SDL2MIXER_MIDI_FLUIDSYNTH OFF) +find_library(SDL_MIXER_LIBRARY + NAMES SDL_mixer + HINTS + ENV SDLMIXERDIR + ENV SDLDIR + PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX} + ) +add_subdirectory(src/lib/SDL_mixer) # Asar ------------------------------------------------------------------------ add_subdirectory(src/lib/asar/src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5624947c..e3b147ad 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -66,6 +66,7 @@ target_include_directories( ${CMAKE_SOURCE_DIR}/src/ ${PNG_INCLUDE_DIRS} ${SDL2_INCLUDE_DIR} + lib/SDL_mixer/include/ ${GLEW_INCLUDE_DIRS} lib/asar/src/asar/ lib/snes_spc/snes_spc/ @@ -84,6 +85,8 @@ target_link_libraries( yaze PUBLIC ${ABSL_TARGETS} ${SDL_TARGETS} + ${SDLMIXER_LIBRARY} + SDL2_mixer ${PNG_LIBRARIES} ${GLEW_LIBRARIES} ${OPENGL_LIBRARIES} diff --git a/src/app/core/controller.cc b/src/app/core/controller.cc index 687d663f..65b66100 100644 --- a/src/app/core/controller.cc +++ b/src/app/core/controller.cc @@ -1,6 +1,7 @@ #include "controller.h" #include +#include #include #include #include @@ -165,6 +166,11 @@ absl::Status Controller::CreateWindow() { return absl::InternalError( absl::StrFormat("SDL_CreateWindow: %s\n", SDL_GetError())); } + // Initialize SDL_mixer + if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) { + printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", + Mix_GetError()); + } } return absl::OkStatus(); } diff --git a/src/app/editor/music_editor.cc b/src/app/editor/music_editor.cc index 7918c87c..a635f081 100644 --- a/src/app/editor/music_editor.cc +++ b/src/app/editor/music_editor.cc @@ -1,5 +1,6 @@ #include "music_editor.h" +#include #include #include "absl/strings/str_format.h" @@ -17,6 +18,8 @@ namespace editor { namespace { +#define BUF_SIZE 2048 + void PlaySPC() { /* Create emulator and filter */ SNES_SPC* snes_spc = spc_new(); @@ -43,9 +46,8 @@ void PlaySPC() { /* Record 20 seconds to wave file */ wave_open(spc_sample_rate, "out.wav"); wave_enable_stereo(); - while (wave_sample_count() < 20 * spc_sample_rate * 2) { -/* Play into buffer */ -#define BUF_SIZE 2048 + while (wave_sample_count() < 30 * spc_sample_rate * 2) { + /* Play into buffer */ short buf[BUF_SIZE]; error(spc_play(snes_spc, BUF_SIZE, buf)); @@ -216,11 +218,34 @@ void MusicEditor::DrawToolset() { static bool is_playing = false; static int selected_option = 0; static int current_volume = 0; + static bool has_loaded_song = false; const int MAX_VOLUME = 100; - if (is_playing) { - PlaySPC(); + if (!has_loaded_song) { + PlaySPC(); + current_song_ = Mix_LoadMUS("out.wav"); + Mix_PlayMusic(current_song_, -1); + has_loaded_song = true; + } + + // // If there is no music playing + // if (Mix_PlayingMusic() == 0) { + // Mix_PlayMusic(current_song_, -1); + // } + // // If music is being played + // else { + // // If the music is paused + // if (Mix_PausedMusic() == 1) { + // // Resume the music + // Mix_ResumeMusic(); + // } + // // If the music is playing + // else { + // // Pause the music + // Mix_PauseMusic(); + // } + // } } gui::ItemLabel("Select a song to edit: ", gui::ItemLabelFlags::Left); @@ -236,6 +261,10 @@ void MusicEditor::DrawToolset() { ImGui::TableNextColumn(); if (ImGui::Button(is_playing ? ICON_MD_STOP : ICON_MD_PLAY_ARROW)) { + if (is_playing) { + Mix_HaltMusic(); + has_loaded_song = false; + } is_playing = !is_playing; } diff --git a/src/app/editor/music_editor.h b/src/app/editor/music_editor.h index a5433c39..929d36ae 100644 --- a/src/app/editor/music_editor.h +++ b/src/app/editor/music_editor.h @@ -1,6 +1,7 @@ #ifndef YAZE_APP_EDITOR_MUSIC_EDITOR_H #define YAZE_APP_EDITOR_MUSIC_EDITOR_H +#include #include #include "absl/strings/str_format.h" @@ -62,6 +63,8 @@ class MusicEditor { void DrawSongToolset(); void DrawToolset(); + Mix_Music* current_song_ = NULL; + AssemblyEditor assembly_editor_; ImGuiTableFlags toolset_table_flags_ = ImGuiTableFlags_SizingFixedFit; ImGuiTableFlags music_editor_flags_ = ImGuiTableFlags_SizingFixedFit | diff --git a/src/lib/SDL_mixer b/src/lib/SDL_mixer new file mode 160000 index 00000000..7f73f724 --- /dev/null +++ b/src/lib/SDL_mixer @@ -0,0 +1 @@ +Subproject commit 7f73f724f2097e457cd7cfe3323a876670e79daf