Skip to content

Commit

Permalink
Switched to miniaudio library from SDL_audio and SDL_mixer
Browse files Browse the repository at this point in the history
Changes:
- Added Miniaudio.
- Removed SDL_mixer.
- Implemented support for audio loop points.

Corrected reimplementation issues:
- The music shuffler sometimes did not start the selected track.
- Normalization of sound effects did not work.
  • Loading branch information
klei1984 committed Oct 7, 2023
1 parent 96af8c4 commit 08e0dd9
Show file tree
Hide file tree
Showing 10 changed files with 726 additions and 669 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
)

if(NOT BUILD_SHARED_LIBS)
set(${PROJECT_NAME}_deps SDL2::SDL2main Iconv::Iconv Freetype::Freetype SDL2::SDL2-static SDL2_net::SDL2_net-static SDL2_mixer::SDL2_mixer-static)
set(${PROJECT_NAME}_deps SDL2::SDL2main Iconv::Iconv Freetype::Freetype Miniaudio::Miniaudio SDL2::SDL2-static SDL2_net::SDL2_net-static)

target_link_options(${PROJECT_NAME} PUBLIC -static -static-libgcc -static-libstdc++)
target_link_libraries(${PROJECT_NAME} PRIVATE ${${PROJECT_NAME}_deps})
else()
set(${PROJECT_NAME}_deps SDL2::SDL2main Iconv::Iconv Freetype::Freetype SDL2::SDL2 SDL2_net::SDL2_net SDL2_mixer::SDL2_mixer)
set(${PROJECT_NAME}_deps SDL2::SDL2main Iconv::Iconv Freetype::Freetype Miniaudio::Miniaudio SDL2::SDL2 SDL2_net::SDL2_net)

target_link_libraries(${PROJECT_NAME} PRIVATE ${${PROJECT_NAME}_deps})
endif()

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/cmake/lib-iconv ${CMAKE_CURRENT_BINARY_DIR}/_deps/iconv-build)

include(sdl2)
include(sdl2_mixer)
include(sdl2_net)
include(freetype)
include(miniaudio)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_OBJCOPY} ARGS --only-keep-debug $<TARGET_FILE:${PROJECT_NAME}> ${PROJECT_NAME}.debug
Expand Down
33 changes: 33 additions & 0 deletions cmake/miniaudio.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
include(versions)
include(FetchContent)

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/dependencies/${MINIAUDIO_FILE})
file(${MINIAUDIO_HASH_TYPE} ${CMAKE_CURRENT_SOURCE_DIR}/dependencies/${MINIAUDIO_FILE} MINIAUDIO_FILE_HASH)

if(${MINIAUDIO_FILE_HASH} STREQUAL ${MINIAUDIO_HASH})
set(MINIAUDIO_URI file://${CMAKE_CURRENT_SOURCE_DIR}/dependencies/${MINIAUDIO_FILE})
endif()
endif()

FetchContent_Declare(
MINIAUDIO
TIMEOUT 60
URL ${MINIAUDIO_URI}
URL_HASH ${MINIAUDIO_HASH_TYPE}=${MINIAUDIO_HASH}
DOWNLOAD_EXTRACT_TIMESTAMP FALSE
OVERRIDE_FIND_PACKAGE
)

FetchContent_MakeAvailable(MINIAUDIO)

set(MINIAUDIO_BINARY_DIR ${PROJECT_BINARY_DIR}/_deps/miniaudio-build)
set(MINIAUDIO_SOURCE_DIR ${PROJECT_BINARY_DIR}/_deps/miniaudio-src)

file(COPY ${MINIAUDIO_SOURCE_DIR}/miniaudio.h DESTINATION ${MINIAUDIO_BINARY_DIR}/include)

add_library(Miniaudio INTERFACE)
set_property(TARGET Miniaudio APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MINIAUDIO_BINARY_DIR}/include")

if(NOT TARGET Miniaudio::Miniaudio)
add_library(Miniaudio::Miniaudio ALIAS Miniaudio)
endif()
32 changes: 0 additions & 32 deletions cmake/sdl2_mixer.cmake

This file was deleted.

10 changes: 8 additions & 2 deletions cmake/versions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ set(FREETYPE_URI http://prdownloads.sourceforge.net/freetype/${FREETYPE_FILE})
set(FREETYPE_HASH d99447cf00c5f2679918f66f2b5771f6)
set(FREETYPE_HASH_TYPE MD5)

set(MINIAUDIO_VERSION 0.11.18)
set(MINIAUDIO_FILE miniaudio-${MINIAUDIO_VERSION}.tar.gz)
set(MINIAUDIO_URI https://github.com/mackron/miniaudio/archive/refs/tags/${MINIAUDIO_VERSION}.tar.gz)
set(MINIAUDIO_HASH ea591ab39594a62b0c5893b5b47cc6ba)
set(MINIAUDIO_HASH_TYPE MD5)

set(SDL2_VERSION 2.28.3)
set(SDL2_FILE SDL2-${SDL2_VERSION}.tar.gz)
set(SDL2_URI https://github.com/libsdl-org/SDL/releases/download/release-${SDL2_VERSION}/${SDL2_FILE})
Expand All @@ -29,7 +35,7 @@ set(SDL2_NET_HASH ba1cc690ddde85ee90c80e15be9f3bf9)
set(SDL2_NET_HASH_TYPE MD5)

set(GTEST_VERSION 1.14.0)
set(GTEST_FILE v${GTEST_VERSION}.tar.gz)
set(GTEST_URI https://github.com/google/googletest/archive/refs/tags/${GTEST_FILE})
set(GTEST_FILE googletest-${GTEST_VERSION}.tar.gz)
set(GTEST_URI https://github.com/google/googletest/archive/refs/tags/v${GTEST_VERSION}.tar.gz)
set(GTEST_HASH c8340a482851ef6a3fe618a082304cfc)
set(GTEST_HASH_TYPE MD5)
2 changes: 1 addition & 1 deletion dependencies/dependencies.md5
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ c8340a482851ef6a3fe618a082304cfc *googletest-1.14.0.tar.gz
d99447cf00c5f2679918f66f2b5771f6 *freetype-2.13.2.tar.gz
d718cd5a59438be666d1575855be72c3 *libiconv-1.17.tar.gz
ba1cc690ddde85ee90c80e15be9f3bf9 *SDL2_net-2.2.0.tar.gz
42c50ace7099b43b630d09d871f054c3 *SDL2_mixer-2.6.3.tar.gz
ea591ab39594a62b0c5893b5b47cc6ba *miniaudio-0.11.18.tar.gz
2 changes: 1 addition & 1 deletion src/game_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3069,7 +3069,7 @@ void GameManager_ProcessCheatCodes() {
void GameManager_InitUnitsAndGameState() {
GameManager_IsCheater = false;

SoundManager_SetVolume(AUDIO_TYPE_MUSIC, ini_get_setting(INI_MUSIC_LEVEL) / 3);
SoundManager_SetVolume(AUDIO_TYPE_MUSIC, static_cast<float>(std::max(ini_get_setting(INI_MUSIC_LEVEL), 100)) / 100);

ResourceManager_FreeResources();
WindowManager_ScaleResources();
Expand Down
2 changes: 1 addition & 1 deletion src/optionsmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ void OptionsMenu::UpdateSlider(int32_t id) {
}

void OptionsMenu::SetVolume(int32_t id, int32_t audio_type, int32_t value) {
SoundManager_SetVolume(audio_type, value);
SoundManager_SetVolume(audio_type, static_cast<float>(value) / 100);
ini_set_setting(options_menu_buttons[id].ini_param_index, value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/resource_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ void ResourceManager_InitResources() {
}

void ResourceManager_InitSDL() {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) != 0) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
exit(1);
}
Expand Down
Loading

0 comments on commit 08e0dd9

Please sign in to comment.