Browse Source
SDL_mixer can only stream a single music track SDL_audiolib has unlimited streams. With this change, we finally have streaming sounds (respecting sfx_STREAM). Audio options can now also be set via diablo.ini, which should help us better diagnose the static noise issues.pull/1595/head
30 changed files with 488 additions and 528 deletions
@ -0,0 +1,35 @@
|
||||
include(FetchContent_MakeAvailableExcludeFromAll) |
||||
|
||||
if(DEVILUTIONX_STATIC_SDL_AUDIOLIB) |
||||
set(BUILD_SHARED_LIBS OFF) |
||||
else() |
||||
set(BUILD_SHARED_LIBS ON) |
||||
endif() |
||||
|
||||
# No need for the libsamplerate resampler: |
||||
set(USE_RESAMP_SRC OFF) |
||||
|
||||
# No need for the SOX resampler: |
||||
set(USE_RESAMP_SOXR OFF) |
||||
|
||||
# We do not need any of the audio formats except WAV: |
||||
set(USE_DEC_DRWAV ON) |
||||
set(USE_DEC_DRFLAC OFF) |
||||
set(USE_DEC_OPENMPT OFF) |
||||
set(USE_DEC_XMP OFF) |
||||
set(USE_DEC_MODPLUG OFF) |
||||
set(USE_DEC_MPG123 OFF) |
||||
set(USE_DEC_SNDFILE OFF) |
||||
set(USE_DEC_LIBVORBIS OFF) |
||||
set(USE_DEC_LIBOPUSFILE OFF) |
||||
set(USE_DEC_MUSEPACK OFF) |
||||
set(USE_DEC_FLUIDSYNTH OFF) |
||||
set(USE_DEC_BASSMIDI OFF) |
||||
set(USE_DEC_WILDMIDI OFF) |
||||
set(USE_DEC_ADLMIDI OFF) |
||||
|
||||
include(FetchContent) |
||||
FetchContent_Declare(SDL_audiolib |
||||
URL https://github.com/realnc/SDL_audiolib/archive/f7c605cb9578916355a5a6770db76a1c0ca84a30.zip |
||||
URL_HASH MD5=9ed7ecac15988247650480656bf2929a) |
||||
FetchContent_MakeAvailableExcludeFromAll(SDL_audiolib) |
||||
@ -1,6 +1,5 @@
|
||||
brew "cmake" |
||||
brew "fmt" |
||||
brew "sdl2_mixer" |
||||
brew "sdl2_ttf" |
||||
brew "libsodium" |
||||
brew "pkg-config" |
||||
|
||||
@ -1,197 +0,0 @@
|
||||
# - Find SDL2_mixer |
||||
# Find the SDL2 headers and libraries |
||||
# |
||||
# SDL2::SDL2_mixer - Imported target |
||||
# |
||||
# SDL2_mixer_FOUND - True if SDL2_mixer was found. |
||||
# SDL2_mixer_DYNAMIC - If we found a DLL version of SDL2_mixer |
||||
# |
||||
# Modified for SDL2_mixer of FindSDL2.cmake |
||||
# Original Author: |
||||
# 2015 Ryan Pavlik <ryan.pavlik@gmail.com> <abiryan@ryand.net> |
||||
# |
||||
# Copyright Sensics, Inc. 2015. |
||||
# Distributed under the Boost Software License, Version 1.0. |
||||
# (See accompanying file LICENSE_1_0.txt or copy at |
||||
# http://www.boost.org/LICENSE_1_0.txt) |
||||
|
||||
# Set up architectures (for windows) and prefixes (for mingw builds) |
||||
if(WIN32) |
||||
if(MINGW) |
||||
include(MinGWSearchPathExtras OPTIONAL) |
||||
if(MINGWSEARCH_TARGET_TRIPLE) |
||||
set(SDL2_mixer_PREFIX ${MINGWSEARCH_TARGET_TRIPLE}) |
||||
endif() |
||||
endif() |
||||
if(CMAKE_SIZEOF_VOID_P EQUAL 8) |
||||
set(SDL2_mixer_LIB_PATH_SUFFIX lib/x64) |
||||
if(NOT MSVC AND NOT SDL2_mixer_PREFIX) |
||||
set(SDL2_mixer_PREFIX x86_64-w64-mingw32) |
||||
endif() |
||||
else() |
||||
set(SDL2_mixer_LIB_PATH_SUFFIX lib/x86) |
||||
if(NOT MSVC AND NOT SDL2_mixer_PREFIX) |
||||
set(SDL2_mixer_PREFIX i686-w64-mingw32) |
||||
endif() |
||||
endif() |
||||
endif() |
||||
|
||||
if(SDL2_mixer_PREFIX) |
||||
set(SDL2_mixer_ORIGPREFIXPATH ${CMAKE_PREFIX_PATH}) |
||||
if(SDL2_mixer_ROOT_DIR) |
||||
list(APPEND CMAKE_PREFIX_PATH "${SDL2_mixer_ROOT_DIR}") |
||||
endif() |
||||
if(CMAKE_PREFIX_PATH) |
||||
foreach(_prefix ${CMAKE_PREFIX_PATH}) |
||||
list(APPEND CMAKE_PREFIX_PATH "${_prefix}/${SDL2_mixer_PREFIX}") |
||||
endforeach() |
||||
endif() |
||||
if(MINGWSEARCH_PREFIXES) |
||||
list(APPEND CMAKE_PREFIX_PATH ${MINGWSEARCH_PREFIXES}) |
||||
endif() |
||||
endif() |
||||
|
||||
# Invoke pkgconfig for hints |
||||
find_package(PkgConfig QUIET) |
||||
set(SDL2_mixer_INCLUDE_HINTS) |
||||
set(SDL2_mixer_LIB_HINTS) |
||||
if(PKG_CONFIG_FOUND) |
||||
pkg_search_module(SDL2_mixerPC QUIET SDL2_mixer) |
||||
if(SDL2_mixerPC_INCLUDE_DIRS) |
||||
set(SDL2_mixer_INCLUDE_HINTS ${SDL2_mixerPC_INCLUDE_DIRS}) |
||||
endif() |
||||
if(SDL2_mixerPC_LIBRARY_DIRS) |
||||
set(SDL2_mixer_LIB_HINTS ${SDL2_mixerPC_LIBRARY_DIRS}) |
||||
endif() |
||||
endif() |
||||
|
||||
include(FindPackageHandleStandardArgs) |
||||
|
||||
find_library(SDL2_mixer_LIBRARY |
||||
NAMES |
||||
SDL2_mixer |
||||
HINTS |
||||
${SDL2_mixer_LIB_HINTS} |
||||
PATHS |
||||
${SDL2_mixer_ROOT_DIR} |
||||
ENV SDL2DIR |
||||
PATH_SUFFIXES lib SDL2 ${SDL2_mixer_LIB_PATH_SUFFIX}) |
||||
|
||||
set(_sdl2_framework FALSE) |
||||
# Some special-casing if we've found/been given a framework. |
||||
# Handles whether we're given the library inside the framework or the framework itself. |
||||
if(APPLE AND "${SDL2_mixer_LIBRARY}" MATCHES "(/[^/]+)*.framework(/.*)?$") |
||||
set(_sdl2_framework TRUE) |
||||
set(SDL2_mixer_FRAMEWORK "${SDL2_mixer_LIBRARY}") |
||||
# Move up in the directory tree as required to get the framework directory. |
||||
while("${SDL2_mixer_FRAMEWORK}" MATCHES "(/[^/]+)*.framework(/.*)$" AND NOT "${SDL2_mixer_FRAMEWORK}" MATCHES "(/[^/]+)*.framework$") |
||||
get_filename_component(SDL2_mixer_FRAMEWORK "${SDL2_mixer_FRAMEWORK}" DIRECTORY) |
||||
endwhile() |
||||
if("${SDL2_mixer_FRAMEWORK}" MATCHES "(/[^/]+)*.framework$") |
||||
set(SDL2_mixer_FRAMEWORK_NAME ${CMAKE_MATCH_1}) |
||||
# If we found a framework, do a search for the header ahead of time that will be more likely to get the framework header. |
||||
find_path(SDL2_mixer_INCLUDE_DIR |
||||
NAMES |
||||
SDL_mixer.h |
||||
HINTS |
||||
"${SDL2_mixer_FRAMEWORK}/Headers/") |
||||
else() |
||||
# For some reason we couldn't get the framework directory itself. |
||||
# Shouldn't happen, but might if something is weird. |
||||
unset(SDL2_mixer_FRAMEWORK) |
||||
endif() |
||||
endif() |
||||
|
||||
find_path(SDL2_mixer_INCLUDE_DIR |
||||
NAMES |
||||
SDL_mixer.h |
||||
HINTS |
||||
${SDL2_mixer_INCLUDE_HINTS} |
||||
PATHS |
||||
${SDL2_mixer_ROOT_DIR} |
||||
ENV SDL2DIR |
||||
PATH_SUFFIXES include include/sdl2 include/SDL2 SDL2) |
||||
|
||||
if(WIN32 AND SDL2_mixer_LIBRARY) |
||||
find_file(SDL2_mixer_RUNTIME_LIBRARY |
||||
NAMES |
||||
SDL2_mixer.dll |
||||
libSDL2_mixer.dll |
||||
HINTS |
||||
${SDL2_mixer_LIB_HINTS} |
||||
PATHS |
||||
${SDL2_mixer_ROOT_DIR} |
||||
ENV SDL2DIR |
||||
PATH_SUFFIXES bin lib ${SDL2_mixer_LIB_PATH_SUFFIX}) |
||||
endif() |
||||
|
||||
if(MINGW AND NOT SDL2_mixerPC_FOUND) |
||||
find_library(SDL2_mixer_MINGW_LIBRARY mingw32) |
||||
find_library(SDL2_mixer_MWINDOWS_LIBRARY mwindows) |
||||
endif() |
||||
|
||||
if(SDL2_mixer_PREFIX) |
||||
# Restore things the way they used to be. |
||||
set(CMAKE_PREFIX_PATH ${SDL2_mixer_ORIGPREFIXPATH}) |
||||
endif() |
||||
|
||||
# handle the QUIETLY and REQUIRED arguments and set QUATLIB_FOUND to TRUE if |
||||
# all listed variables are TRUE |
||||
include(FindPackageHandleStandardArgs) |
||||
find_package_handle_standard_args(SDL2_mixer |
||||
DEFAULT_MSG |
||||
SDL2_mixer_LIBRARY |
||||
SDL2_mixer_INCLUDE_DIR |
||||
${SDL2_mixer_EXTRA_REQUIRED}) |
||||
|
||||
if(SDL2_mixer_FOUND) |
||||
if(NOT TARGET SDL2::SDL2_mixer) |
||||
# Create SDL2::SDL2_mixer |
||||
if(WIN32 AND SDL2_mixer_RUNTIME_LIBRARY) |
||||
set(SDL2_mixer_DYNAMIC TRUE) |
||||
add_library(SDL2::SDL2_mixer SHARED IMPORTED) |
||||
set_target_properties(SDL2::SDL2_mixer |
||||
PROPERTIES |
||||
IMPORTED_IMPLIB "${SDL2_mixer_LIBRARY}" |
||||
IMPORTED_LOCATION "${SDL2_mixer_RUNTIME_LIBRARY}" |
||||
INTERFACE_INCLUDE_DIRECTORIES "${SDL2_mixer_INCLUDE_DIR}" |
||||
) |
||||
else() |
||||
add_library(SDL2::SDL2_mixer UNKNOWN IMPORTED) |
||||
if(SDL2_mixer_FRAMEWORK AND SDL2_mixer_FRAMEWORK_NAME) |
||||
# Handle the case that SDL2_mixer is a framework and we were able to decompose it above. |
||||
set_target_properties(SDL2::SDL2_mixer PROPERTIES |
||||
IMPORTED_LOCATION "${SDL2_mixer_FRAMEWORK}/${SDL2_mixer_FRAMEWORK_NAME}") |
||||
elseif(_sdl2_framework AND SDL2_mixer_LIBRARY MATCHES "(/[^/]+)*.framework$") |
||||
# Handle the case that SDL2_mixer is a framework and SDL_LIBRARY is just the framework itself. |
||||
|
||||
# This takes the basename of the framework, without the extension, |
||||
# and sets it (as a child of the framework) as the imported location for the target. |
||||
# This is the library symlink inside of the framework. |
||||
set_target_properties(SDL2::SDL2_mixer PROPERTIES |
||||
IMPORTED_LOCATION "${SDL2_mixer_LIBRARY}/${CMAKE_MATCH_1}") |
||||
else() |
||||
# Handle non-frameworks (including non-Mac), as well as the case that we're given the library inside of the framework |
||||
set_target_properties(SDL2::SDL2_mixer PROPERTIES |
||||
IMPORTED_LOCATION "${SDL2_mixer_LIBRARY}") |
||||
endif() |
||||
set_target_properties(SDL2::SDL2_mixer |
||||
PROPERTIES |
||||
INTERFACE_INCLUDE_DIRECTORIES "${SDL2_mixer_INCLUDE_DIR}" |
||||
) |
||||
endif() |
||||
endif() |
||||
mark_as_advanced(SDL2_mixer_ROOT_DIR) |
||||
endif() |
||||
|
||||
mark_as_advanced(SDL2_mixer_LIBRARY |
||||
SDL2_mixer_RUNTIME_LIBRARY |
||||
SDL2_mixer_INCLUDE_DIR |
||||
SDL2_mixer_SDLMAIN_LIBRARY |
||||
SDL2_mixer_COCOA_LIBRARY |
||||
SDL2_mixer_MINGW_LIBRARY |
||||
SDL2_mixer_MWINDOWS_LIBRARY) |
||||
|
||||
find_package(SDL2 REQUIRED) |
||||
set_property(TARGET SDL2::SDL2_mixer APPEND PROPERTY |
||||
INTERFACE_LINK_LIBRARIES SDL2::SDL2) |
||||
@ -0,0 +1,105 @@
|
||||
#include "push_aulib_decoder.h" |
||||
|
||||
#include <algorithm> |
||||
#include <cstring> |
||||
#include <limits> |
||||
|
||||
#include <aulib.h> |
||||
|
||||
#include "appfat.h" |
||||
|
||||
namespace devilution { |
||||
|
||||
void PushAulibDecoder::PushSamples(const std::int16_t *data, unsigned size) noexcept |
||||
{ |
||||
AudioQueueItem item; |
||||
item.data.reset(new std::int16_t[size]); |
||||
std::memcpy(item.data.get(), data, size * sizeof(data[0])); |
||||
item.len = size; |
||||
item.pos = item.data.get(); |
||||
queue_.push(std::move(item)); |
||||
} |
||||
|
||||
void PushAulibDecoder::PushSamples(const std::uint8_t *data, unsigned size) noexcept |
||||
{ |
||||
AudioQueueItem item; |
||||
item.data.reset(new std::int16_t[size]); |
||||
constexpr std::int16_t Center = 128; |
||||
constexpr std::int16_t Scale = 256; |
||||
for (unsigned i = 0; i < size; ++i) |
||||
item.data[i] = static_cast<std::int16_t>((data[i] - Center) * Scale); |
||||
item.len = size; |
||||
item.pos = item.data.get(); |
||||
queue_.push(std::move(item)); |
||||
} |
||||
|
||||
void PushAulibDecoder::DiscardPendingSamples() noexcept |
||||
{ |
||||
SDLMutexLockGuard lock(queue_mutex_.get()); |
||||
queue_ = {}; |
||||
} |
||||
|
||||
bool PushAulibDecoder::open([[maybe_unused]] SDL_RWops *rwops) |
||||
{ |
||||
assert(rwops == nullptr); |
||||
return true; |
||||
} |
||||
|
||||
bool PushAulibDecoder::rewind() |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
std::chrono::microseconds PushAulibDecoder::duration() const |
||||
{ |
||||
return {}; |
||||
} |
||||
|
||||
bool PushAulibDecoder::seekToTime([[maybe_unused]] std::chrono::microseconds pos) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
int PushAulibDecoder::doDecoding(float buf[], int len, bool &callAgain) |
||||
{ |
||||
callAgain = false; |
||||
|
||||
const auto writeFloats = [&buf](const std::int16_t *samples, unsigned count) { |
||||
constexpr float Scale = std::numeric_limits<std::int16_t>::max() + 1; |
||||
for (unsigned i = 0; i < count; ++i) { |
||||
buf[i] = static_cast<float>(samples[i]) / Scale; |
||||
} |
||||
}; |
||||
|
||||
unsigned remaining = len; |
||||
{ |
||||
SDLMutexLockGuard lock(queue_mutex_.get()); |
||||
AudioQueueItem *item; |
||||
while ((item = Next()) != nullptr) { |
||||
if (static_cast<unsigned>(remaining) <= item->len) { |
||||
writeFloats(item->pos, remaining); |
||||
item->pos += remaining; |
||||
item->len -= remaining; |
||||
return len; |
||||
} |
||||
|
||||
writeFloats(item->pos, item->len); |
||||
buf += item->len; |
||||
remaining -= static_cast<int>(item->len); |
||||
queue_.pop(); |
||||
} |
||||
} |
||||
std::memset(buf, 0, remaining * sizeof(buf[0])); |
||||
return len; |
||||
} |
||||
|
||||
PushAulibDecoder::AudioQueueItem *PushAulibDecoder::Next() |
||||
{ |
||||
while (!queue_.empty() && queue_.front().len == 0) |
||||
queue_.pop(); |
||||
if (queue_.empty()) |
||||
return nullptr; |
||||
return &queue_.front(); |
||||
} |
||||
|
||||
} // namespace devilution
|
||||
@ -0,0 +1,67 @@
|
||||
#pragma once |
||||
|
||||
#include <chrono> |
||||
#include <cstdint> |
||||
#include <memory> |
||||
#include <queue> |
||||
|
||||
#include <Aulib/Decoder.h> |
||||
#include <SDL_mutex.h> |
||||
|
||||
#include "utils/sdl_mutex.h" |
||||
|
||||
namespace devilution { |
||||
|
||||
/**
|
||||
* @brief A Decoder interface implementations that simply has the samples pushed into it by the user. |
||||
*/ |
||||
class PushAulibDecoder final : public ::Aulib::Decoder { |
||||
public: |
||||
PushAulibDecoder(int numChannels, int sampleRate) |
||||
: numChannels_(numChannels) |
||||
, sampleRate_(sampleRate) |
||||
, queue_mutex_(SDL_CreateMutex()) |
||||
{ |
||||
} |
||||
|
||||
void PushSamples(const std::uint8_t *data, unsigned size) noexcept; |
||||
void PushSamples(const std::int16_t *data, unsigned size) noexcept; |
||||
void DiscardPendingSamples() noexcept; |
||||
|
||||
bool open(SDL_RWops *rwops) override; |
||||
|
||||
[[nodiscard]] int getChannels() const override |
||||
{ |
||||
return numChannels_; |
||||
} |
||||
|
||||
[[nodiscard]] int getRate() const override |
||||
{ |
||||
return sampleRate_; |
||||
} |
||||
|
||||
bool rewind() override; |
||||
[[nodiscard]] std::chrono::microseconds duration() const override; |
||||
bool seekToTime(std::chrono::microseconds pos) override; |
||||
|
||||
protected: |
||||
int doDecoding(float buf[], int len, bool &callAgain) override; |
||||
|
||||
private: |
||||
struct AudioQueueItem { |
||||
std::unique_ptr<std::int16_t[]> data; |
||||
unsigned len; |
||||
const std::int16_t *pos; |
||||
}; |
||||
|
||||
const int numChannels_; |
||||
const int sampleRate_; |
||||
|
||||
// Requires holding the queue_mutex_.
|
||||
AudioQueueItem *Next(); |
||||
|
||||
std::queue<AudioQueueItem> queue_; |
||||
SDLMutexUniquePtr queue_mutex_; |
||||
}; |
||||
|
||||
} // namespace devilution
|
||||
@ -0,0 +1,43 @@
|
||||
#pragma once |
||||
|
||||
#include <memory> |
||||
|
||||
#include <SDL_mutex.h> |
||||
|
||||
namespace devilution { |
||||
|
||||
/**
|
||||
* @brief Deletes the SDL mutex using `SDL_DestroyMutex`. |
||||
*/ |
||||
struct SDLMutexDeleter { |
||||
void operator()(SDL_mutex *mutex) const |
||||
{ |
||||
SDL_DestroyMutex(mutex); |
||||
} |
||||
}; |
||||
|
||||
using SDLMutexUniquePtr = std::unique_ptr<SDL_mutex, SDLMutexDeleter>; |
||||
|
||||
struct SDLMutexLockGuard { |
||||
public: |
||||
explicit SDLMutexLockGuard(SDL_mutex *mutex) |
||||
: mutex_(mutex) |
||||
{ |
||||
SDL_LockMutex(mutex_); |
||||
} |
||||
|
||||
~SDLMutexLockGuard() |
||||
{ |
||||
SDL_UnlockMutex(mutex_); |
||||
} |
||||
|
||||
SDLMutexLockGuard(const SDLMutexLockGuard &) = delete; |
||||
SDLMutexLockGuard(SDLMutexLockGuard &&) = delete; |
||||
SDLMutexLockGuard &operator=(const SDLMutexLockGuard &) = delete; |
||||
SDLMutexLockGuard &operator=(SDLMutexLockGuard &&) = delete; |
||||
|
||||
private: |
||||
SDL_mutex *mutex_; |
||||
}; |
||||
|
||||
} // namespace devilution
|
||||
@ -1,14 +1,15 @@
|
||||
#pragma once |
||||
|
||||
#ifdef __has_include |
||||
#if defined(__cplusplus) && __cplusplus >= 201606L && __has_include(<optional>) |
||||
#include <optional> // IWYU pragma: export |
||||
#elif __has_include(<experimental/optional>) |
||||
#include <experimental/optional> // IWYU pragma: export |
||||
#define optional experimental::optional |
||||
#else |
||||
#error "Missing support for <optional> or <experimental/optional>" |
||||
#endif |
||||
#else |
||||
#error "__has_include unavailable" |
||||
#endif |
||||
#pragma once |
||||
|
||||
#ifdef __has_include |
||||
#if defined(__cplusplus) && __cplusplus >= 201606L && __has_include(<optional>) |
||||
#include <optional> // IWYU pragma: export |
||||
#elif __has_include(<experimental/optional>) |
||||
#include <experimental/optional> // IWYU pragma: export |
||||
#define optional experimental::optional |
||||
#define nullopt experimental::nullopt |
||||
#else |
||||
#error "Missing support for <optional> or <experimental/optional>" |
||||
#endif |
||||
#else |
||||
#error "__has_include unavailable" |
||||
#endif |
||||
|
||||
Loading…
Reference in new issue