diff --git a/.clang-format b/.clang-format index ff27be35d..15b53c893 100644 --- a/.clang-format +++ b/.clang-format @@ -6,6 +6,8 @@ UseTab: Always TabWidth: 4 ColumnLimit: 120 AccessModifierOffset: -4 +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false AllowShortBlocksOnASingleLine: false AllowShortCaseLabelsOnASingleLine: false AllowShortEnumsOnASingleLine: false @@ -13,10 +15,11 @@ AllowShortFunctionsOnASingleLine: InlineOnly AllowShortIfStatementsOnASingleLine: false AllowShortLambdasOnASingleLine: All AllowShortLoopsOnASingleLine: false +AlignAfterOpenBracket: BlockIndent AlwaysBreakTemplateDeclarations: Yes BinPackArguments: false BinPackParameters: false -BreakBeforeBraces: Attach # K&R/OTBS, braces on same line, Java style +BreakBeforeBraces: Allman # Always break before braces, to match existing SRB2 code BreakConstructorInitializers: BeforeComma CompactNamespaces: true ConstructorInitializerAllOnOneLineOrOnePerLine: true diff --git a/src/audio/chunk_load.cpp b/src/audio/chunk_load.cpp index 79900ec17..c076c1bdb 100644 --- a/src/audio/chunk_load.cpp +++ b/src/audio/chunk_load.cpp @@ -30,20 +30,26 @@ using std::size_t; using namespace srb2::audio; using namespace srb2; -namespace { +namespace +{ // Utility for leveraging Resampler... -class SoundChunkSource : public Source<1> { +class SoundChunkSource : public Source<1> +{ public: explicit SoundChunkSource(std::unique_ptr&& chunk) - : chunk_(std::forward>(chunk)) {} + : chunk_(std::forward>(chunk)) + { + } - virtual size_t generate(tcb::span> buffer) override final { + virtual size_t generate(tcb::span> buffer) override final + { if (!chunk_) return 0; size_t written = 0; - for (; pos_ < chunk_->samples.size() && written < buffer.size(); pos_++) { + for (; pos_ < chunk_->samples.size() && written < buffer.size(); pos_++) + { buffer[written] = chunk_->samples[pos_]; written++; } @@ -56,13 +62,15 @@ private: }; template -std::vector> generate_to_vec(I& source, std::size_t estimate = 0) { +std::vector> generate_to_vec(I& source, std::size_t estimate = 0) +{ std::vector> generated; size_t total = 0; size_t read = 0; generated.reserve(estimate); - do { + do + { generated.resize(total + 4096); read = source.generate(tcb::span {generated.data() + total, 4096}); total += read; @@ -71,7 +79,8 @@ std::vector> generate_to_vec(I& source, std::size_t estimate = 0) { return generated; } -optional try_load_dmx(tcb::span data) { +optional try_load_dmx(tcb::span data) +{ io::SpanStream stream {data}; if (io::remaining(stream) < 8) @@ -90,14 +99,16 @@ optional try_load_dmx(tcb::span data) { stream.seek(io::SeekFrom::kCurrent, 16); std::vector> samples; - for (size_t i = 0; i < length; i++) { + for (size_t i = 0; i < length; i++) + { uint8_t doom_sample = io::read_uint8(stream); float float_sample = audio::sample_to_float(doom_sample); samples.push_back(Sample<1> {float_sample}); } size_t samples_len = samples.size(); - if (rate == 44100) { + if (rate == 44100) + { return SoundChunk {samples}; } @@ -110,7 +121,8 @@ optional try_load_dmx(tcb::span data) { size_t total = 0; size_t read = 0; resampled.reserve(samples_len * (static_cast(kSampleRate) / rate)); - do { + do + { resampled.resize(total + 4096); read = resampler.generate(tcb::span {resampled.data() + total, 4096}); total += read; @@ -120,34 +132,44 @@ optional try_load_dmx(tcb::span data) { return SoundChunk {std::move(resampled)}; } -optional try_load_wav(tcb::span data) { +optional try_load_wav(tcb::span data) +{ io::SpanStream stream {data}; audio::Wav wav; std::size_t sample_rate; - try { + try + { wav = audio::load_wav(stream); - } catch (const std::exception& ex) { + } + catch (const std::exception& ex) + { return nullopt; } sample_rate = wav.sample_rate(); - audio::Resampler<1> resampler(std::make_unique(std::move(wav)), - sample_rate / static_cast(kSampleRate)); + audio::Resampler<1> resampler( + std::make_unique(std::move(wav)), + sample_rate / static_cast(kSampleRate) + ); SoundChunk chunk {generate_to_vec(resampler)}; return chunk; } -optional try_load_ogg(tcb::span data) { +optional try_load_ogg(tcb::span data) +{ std::shared_ptr> player; - try { + try + { io::SpanStream data_stream {data}; audio::Ogg ogg = audio::load_ogg(data_stream); player = std::make_shared>(std::move(ogg)); - } catch (...) { + } + catch (...) + { return nullopt; } player->looping(false); @@ -161,19 +183,26 @@ optional try_load_ogg(tcb::span data) { return chunk; } -optional try_load_gme(tcb::span data) { +optional try_load_gme(tcb::span data) +{ std::shared_ptr> player; - try { - if (data[0] == std::byte {0x1F} && data[1] == std::byte {0x8B}) { + try + { + if (data[0] == std::byte {0x1F} && data[1] == std::byte {0x8B}) + { io::SpanStream stream {data}; audio::Gme gme = audio::load_gme(stream); player = std::make_shared>(std::move(gme)); - } else { + } + else + { io::ZlibInputStream stream {io::SpanStream(data)}; audio::Gme gme = audio::load_gme(stream); player = std::make_shared>(std::move(gme)); } - } catch (...) { + } + catch (...) + { return nullopt; } std::vector> samples {generate_to_vec(*player)}; @@ -183,7 +212,8 @@ optional try_load_gme(tcb::span data) { } // namespace -optional srb2::audio::try_load_chunk(tcb::span data) { +optional srb2::audio::try_load_chunk(tcb::span data) +{ optional ret; ret = try_load_dmx(data); diff --git a/src/audio/chunk_load.hpp b/src/audio/chunk_load.hpp index c97d559d2..675a327cf 100644 --- a/src/audio/chunk_load.hpp +++ b/src/audio/chunk_load.hpp @@ -17,7 +17,8 @@ #include "sound_chunk.hpp" -namespace srb2::audio { +namespace srb2::audio +{ /// @brief Try to load a chunk from the given byte span. std::optional try_load_chunk(tcb::span data); diff --git a/src/audio/expand_mono.cpp b/src/audio/expand_mono.cpp index ce7cf0dc3..ee4e20147 100644 --- a/src/audio/expand_mono.cpp +++ b/src/audio/expand_mono.cpp @@ -17,8 +17,10 @@ using namespace srb2::audio; ExpandMono::~ExpandMono() = default; -size_t ExpandMono::filter(tcb::span> input_buffer, tcb::span> buffer) { - for (size_t i = 0; i < std::min(input_buffer.size(), buffer.size()); i++) { +size_t ExpandMono::filter(tcb::span> input_buffer, tcb::span> buffer) +{ + for (size_t i = 0; i < std::min(input_buffer.size(), buffer.size()); i++) + { buffer[i].amplitudes[0] = input_buffer[i].amplitudes[0]; buffer[i].amplitudes[1] = input_buffer[i].amplitudes[0]; } diff --git a/src/audio/expand_mono.hpp b/src/audio/expand_mono.hpp index f3686704e..d06a95a7c 100644 --- a/src/audio/expand_mono.hpp +++ b/src/audio/expand_mono.hpp @@ -14,9 +14,11 @@ #include "filter.hpp" -namespace srb2::audio { +namespace srb2::audio +{ -class ExpandMono : public Filter<1, 2> { +class ExpandMono : public Filter<1, 2> +{ public: virtual ~ExpandMono(); virtual std::size_t filter(tcb::span> input_buffer, tcb::span> buffer) override final; diff --git a/src/audio/filter.cpp b/src/audio/filter.cpp index 8bb09bdfb..b68e0a7b9 100644 --- a/src/audio/filter.cpp +++ b/src/audio/filter.cpp @@ -17,7 +17,8 @@ using srb2::audio::Sample; using srb2::audio::Source; template -size_t Filter::generate(tcb::span> buffer) { +size_t Filter::generate(tcb::span> buffer) +{ input_buffer_.clear(); input_buffer_.resize(buffer.size()); @@ -27,7 +28,8 @@ size_t Filter::generate(tcb::span> buffer) { } template -void Filter::bind(const shared_ptr>& input) { +void Filter::bind(const shared_ptr>& input) +{ input_ = input; } diff --git a/src/audio/filter.hpp b/src/audio/filter.hpp index 59dce6e1e..56b2eef18 100644 --- a/src/audio/filter.hpp +++ b/src/audio/filter.hpp @@ -18,10 +18,12 @@ #include "source.hpp" -namespace srb2::audio { +namespace srb2::audio +{ template -class Filter : public Source { +class Filter : public Source +{ public: virtual std::size_t generate(tcb::span> buffer) override; diff --git a/src/audio/gain.cpp b/src/audio/gain.cpp index 59839bb69..8db17d4e8 100644 --- a/src/audio/gain.cpp +++ b/src/audio/gain.cpp @@ -20,9 +20,11 @@ using srb2::audio::Sample; constexpr const float kGainInterpolationAlpha = 0.8f; template -size_t Gain::filter(tcb::span> input_buffer, tcb::span> buffer) { +size_t Gain::filter(tcb::span> input_buffer, tcb::span> buffer) +{ size_t written = std::min(buffer.size(), input_buffer.size()); - for (size_t i = 0; i < written; i++) { + for (size_t i = 0; i < written; i++) + { buffer[i] = input_buffer[i]; buffer[i] *= gain_; gain_ += (new_gain_ - gain_) * kGainInterpolationAlpha; @@ -32,7 +34,8 @@ size_t Gain::filter(tcb::span> input_buffer, tcb::span> b } template -void Gain::gain(float new_gain) { +void Gain::gain(float new_gain) +{ new_gain_ = std::clamp(new_gain, 0.0f, 1.0f); } diff --git a/src/audio/gain.hpp b/src/audio/gain.hpp index ef4dd0d53..42055466e 100644 --- a/src/audio/gain.hpp +++ b/src/audio/gain.hpp @@ -14,10 +14,12 @@ #include "filter.hpp" -namespace srb2::audio { +namespace srb2::audio +{ template -class Gain : public Filter { +class Gain : public Filter +{ public: virtual std::size_t filter(tcb::span> input_buffer, tcb::span> buffer) override final; void gain(float new_gain); diff --git a/src/audio/gme.cpp b/src/audio/gme.cpp index 38279dd98..8b6ea928b 100644 --- a/src/audio/gme.cpp +++ b/src/audio/gme.cpp @@ -17,37 +17,45 @@ using namespace srb2; using namespace srb2::audio; -Gme::Gme() : memory_data_(), instance_(nullptr) { +Gme::Gme() : memory_data_(), instance_(nullptr) +{ } -Gme::Gme(Gme&& rhs) noexcept : memory_data_(), instance_(nullptr) { +Gme::Gme(Gme&& rhs) noexcept : memory_data_(), instance_(nullptr) +{ std::swap(memory_data_, rhs.memory_data_); std::swap(instance_, rhs.instance_); } -Gme::Gme(std::vector&& data) : memory_data_(std::move(data)), instance_(nullptr) { +Gme::Gme(std::vector&& data) : memory_data_(std::move(data)), instance_(nullptr) +{ _init_with_data(); } -Gme::Gme(tcb::span data) : memory_data_(data.begin(), data.end()), instance_(nullptr) { +Gme::Gme(tcb::span data) : memory_data_(data.begin(), data.end()), instance_(nullptr) +{ _init_with_data(); } -Gme& Gme::operator=(Gme&& rhs) noexcept { +Gme& Gme::operator=(Gme&& rhs) noexcept +{ std::swap(memory_data_, rhs.memory_data_); std::swap(instance_, rhs.instance_); return *this; } -Gme::~Gme() { - if (instance_) { +Gme::~Gme() +{ + if (instance_) + { gme_delete(instance_); instance_ = nullptr; } } -std::size_t Gme::get_samples(tcb::span buffer) { +std::size_t Gme::get_samples(tcb::span buffer) +{ SRB2_ASSERT(instance_ != nullptr); gme_err_t err = gme_play(instance_, buffer.size(), buffer.data()); @@ -57,13 +65,15 @@ std::size_t Gme::get_samples(tcb::span buffer) { return buffer.size(); } -void Gme::seek(int sample) { +void Gme::seek(int sample) +{ SRB2_ASSERT(instance_ != nullptr); gme_seek_samples(instance_, sample); } -std::optional Gme::duration_seconds() const { +std::optional Gme::duration_seconds() const +{ SRB2_ASSERT(instance_ != nullptr); gme_info_t* info = nullptr; @@ -79,7 +89,8 @@ std::optional Gme::duration_seconds() const { return static_cast(info->length) / 1000.f; } -std::optional Gme::loop_point_seconds() const { +std::optional Gme::loop_point_seconds() const +{ SRB2_ASSERT(instance_ != nullptr); gme_info_t* info = nullptr; @@ -95,7 +106,8 @@ std::optional Gme::loop_point_seconds() const { return loop_point_ms / 44100.f; } -float Gme::position_seconds() const { +float Gme::position_seconds() const +{ SRB2_ASSERT(instance_ != nullptr); gme_info_t* info = nullptr; @@ -117,8 +129,10 @@ float Gme::position_seconds() const { return position / 1000.f; } -void Gme::_init_with_data() { - if (instance_) { +void Gme::_init_with_data() +{ + if (instance_) + { return; } diff --git a/src/audio/gme.hpp b/src/audio/gme.hpp index 34f2c2769..453f5cdda 100644 --- a/src/audio/gme.hpp +++ b/src/audio/gme.hpp @@ -24,9 +24,11 @@ #include "../io/streams.hpp" -namespace srb2::audio { +namespace srb2::audio +{ -class GmeException : public std::exception { +class GmeException : public std::exception +{ std::string msg_; public: @@ -35,7 +37,8 @@ public: virtual const char* what() const noexcept override { return msg_.c_str(); } }; -class Gme { +class Gme +{ std::vector memory_data_; Music_Emu* instance_; @@ -64,7 +67,8 @@ private: }; template , int> = 0> -inline Gme load_gme(I& stream) { +inline Gme load_gme(I& stream) +{ std::vector data = srb2::io::read_to_vec(stream); return Gme {std::move(data)}; } diff --git a/src/audio/gme_player.cpp b/src/audio/gme_player.cpp index 229c99676..a3f0f08f8 100644 --- a/src/audio/gme_player.cpp +++ b/src/audio/gme_player.cpp @@ -13,7 +13,8 @@ using namespace srb2; using namespace srb2::audio; template -GmePlayer::GmePlayer(Gme&& gme) : gme_(std::forward(gme)), buf_() { +GmePlayer::GmePlayer(Gme&& gme) : gme_(std::forward(gme)), buf_() +{ } template @@ -26,17 +27,22 @@ template GmePlayer::~GmePlayer() = default; template -std::size_t GmePlayer::generate(tcb::span> buffer) { +std::size_t GmePlayer::generate(tcb::span> buffer) +{ buf_.clear(); buf_.resize(buffer.size() * 2); std::size_t read = gme_.get_samples(tcb::make_span(buf_)); buf_.resize(read); std::size_t new_samples = std::min((read / 2), buffer.size()); - for (std::size_t i = 0; i < new_samples; i++) { - if constexpr (C == 1) { + for (std::size_t i = 0; i < new_samples; i++) + { + if constexpr (C == 1) + { buffer[i].amplitudes[0] = (buf_[i * 2] / 32768.f + buf_[i * 2 + 1] / 32768.f) / 2.f; - } else if constexpr (C == 2) { + } + else if constexpr (C == 2) + { buffer[i].amplitudes[0] = buf_[i * 2] / 32768.f; buffer[i].amplitudes[1] = buf_[i * 2 + 1] / 32768.f; } @@ -45,27 +51,32 @@ std::size_t GmePlayer::generate(tcb::span> buffer) { } template -void GmePlayer::seek(float position_seconds) { +void GmePlayer::seek(float position_seconds) +{ gme_.seek(static_cast(position_seconds * 44100.f)); } template -void GmePlayer::reset() { +void GmePlayer::reset() +{ gme_.seek(0); } template -std::optional GmePlayer::duration_seconds() const { +std::optional GmePlayer::duration_seconds() const +{ return gme_.duration_seconds(); } template -std::optional GmePlayer::loop_point_seconds() const { +std::optional GmePlayer::loop_point_seconds() const +{ return gme_.loop_point_seconds(); } template -float GmePlayer::position_seconds() const { +float GmePlayer::position_seconds() const +{ return gme_.position_seconds(); } diff --git a/src/audio/gme_player.hpp b/src/audio/gme_player.hpp index a2f555c08..1d23cde6e 100644 --- a/src/audio/gme_player.hpp +++ b/src/audio/gme_player.hpp @@ -15,10 +15,12 @@ #include "gme.hpp" #include "source.hpp" -namespace srb2::audio { +namespace srb2::audio +{ template -class GmePlayer : public Source { +class GmePlayer : public Source +{ Gme gme_; std::vector buf_; diff --git a/src/audio/mixer.cpp b/src/audio/mixer.cpp index d4b718ffa..133cda16f 100644 --- a/src/audio/mixer.cpp +++ b/src/audio/mixer.cpp @@ -18,16 +18,20 @@ using srb2::audio::Mixer; using srb2::audio::Sample; using srb2::audio::Source; -namespace { +namespace +{ template -void default_init_sample_buffer(Sample* buffer, size_t size) { +void default_init_sample_buffer(Sample* buffer, size_t size) +{ std::for_each(buffer, buffer + size, [](auto& i) { i = Sample {}; }); } template -void mix_sample_buffers(Sample* dst, size_t size, Sample* src, size_t src_size) { - for (size_t i = 0; i < size && i < src_size; i++) { +void mix_sample_buffers(Sample* dst, size_t size, Sample* src, size_t src_size) +{ + for (size_t i = 0; i < size && i < src_size; i++) + { dst[i] += src[i]; } } @@ -35,12 +39,14 @@ void mix_sample_buffers(Sample* dst, size_t size, Sample* src, size_t src_ } // namespace template -size_t Mixer::generate(tcb::span> buffer) { +size_t Mixer::generate(tcb::span> buffer) +{ buffer_.resize(buffer.size()); default_init_sample_buffer(buffer.data(), buffer.size()); - for (auto& source : sources_) { + for (auto& source : sources_) + { size_t read = source->generate(buffer_); mix_sample_buffers(buffer.data(), buffer.size(), buffer_.data(), read); @@ -51,7 +57,8 @@ size_t Mixer::generate(tcb::span> buffer) { } template -void Mixer::add_source(const shared_ptr>& source) { +void Mixer::add_source(const shared_ptr>& source) +{ sources_.push_back(source); } diff --git a/src/audio/mixer.hpp b/src/audio/mixer.hpp index 75c8ba2f9..06e143cc8 100644 --- a/src/audio/mixer.hpp +++ b/src/audio/mixer.hpp @@ -17,10 +17,12 @@ #include "source.hpp" -namespace srb2::audio { +namespace srb2::audio +{ template -class Mixer : public Source { +class Mixer : public Source +{ public: virtual std::size_t generate(tcb::span> buffer) override final; diff --git a/src/audio/music_player.cpp b/src/audio/music_player.cpp index bb85a01de..5433f2100 100644 --- a/src/audio/music_player.cpp +++ b/src/audio/music_player.cpp @@ -43,12 +43,14 @@ using srb2::audio::Sample; using srb2::audio::Source; using namespace srb2; -class MusicPlayer::Impl { +class MusicPlayer::Impl +{ public: Impl() = default; Impl(tcb::span data) : Impl() { _load(data); } - size_t generate(tcb::span> buffer) { + size_t generate(tcb::span> buffer) + { if (!resampler_) return 0; @@ -57,12 +59,14 @@ public: size_t total_written = 0; - while (total_written < buffer.size()) { + while (total_written < buffer.size()) + { const size_t generated = resampler_->generate(buffer.subspan(total_written)); // To avoid a branch preventing optimizations, we're always going to apply // the fade gain, even if it would clamp anyway. - for (std::size_t i = 0; i < generated; i++) { + for (std::size_t i = 0; i < generated; i++) + { const float alpha = 1.0 - (gain_samples_target_ - std::min(gain_samples_ + i, gain_samples_target_)) / static_cast(gain_samples_target_); const float fade_gain = (gain_target_ - gain_) * std::clamp(alpha, 0.f, 1.f) + gain_; @@ -71,7 +75,8 @@ public: gain_samples_ = std::min(gain_samples_ + generated, gain_samples_target_); - if (gain_samples_ >= gain_samples_target_) { + if (gain_samples_ >= gain_samples_target_) + { fading_ = false; gain_samples_ = gain_samples_target_; gain_ = gain_target_; @@ -79,7 +84,8 @@ public: total_written += generated; - if (generated == 0) { + if (generated == 0) + { playing_ = false; break; } @@ -88,53 +94,68 @@ public: return total_written; } - void _load(tcb::span data) { + void _load(tcb::span data) + { ogg_inst_ = nullptr; gme_inst_ = nullptr; xmp_inst_ = nullptr; resampler_ = std::nullopt; - try { + try + { io::SpanStream stream {data}; audio::Ogg ogg = audio::load_ogg(stream); ogg_inst_ = std::make_shared>(std::move(ogg)); ogg_inst_->looping(looping_); resampler_ = Resampler<2>(ogg_inst_, ogg_inst_->sample_rate() / 44100.f); - } catch (const std::exception& ex) { + } + catch (const std::exception& ex) + { // it's probably not ogg ogg_inst_ = nullptr; resampler_ = std::nullopt; } - if (!resampler_) { - try { - if (data[0] == std::byte {0x1F} && data[1] == std::byte {0x8B}) { + if (!resampler_) + { + try + { + if (data[0] == std::byte {0x1F} && data[1] == std::byte {0x8B}) + { io::ZlibInputStream stream {io::SpanStream(data)}; audio::Gme gme = audio::load_gme(stream); gme_inst_ = std::make_shared>(std::move(gme)); - } else { + } + else + { io::SpanStream stream {data}; audio::Gme gme = audio::load_gme(stream); gme_inst_ = std::make_shared>(std::move(gme)); } resampler_ = Resampler<2>(gme_inst_, 1.f); - } catch (const std::exception& ex) { + } + catch (const std::exception& ex) + { // it's probably not gme gme_inst_ = nullptr; resampler_ = std::nullopt; } } - if (!resampler_) { - try { + if (!resampler_) + { + try + { io::SpanStream stream {data}; audio::Xmp<2> xmp = audio::load_xmp<2>(stream); xmp_inst_ = std::make_shared>(std::move(xmp)); xmp_inst_->looping(looping_); resampler_ = Resampler<2>(xmp_inst_, 1.f); - } catch (const std::exception& ex) { + } + catch (const std::exception& ex) + { // it's probably not xmp xmp_inst_ = nullptr; resampler_ = std::nullopt; @@ -146,74 +167,103 @@ public: internal_gain(1.f); } - void play(bool looping) { - if (ogg_inst_) { + void play(bool looping) + { + if (ogg_inst_) + { ogg_inst_->looping(looping); ogg_inst_->playing(true); playing_ = true; ogg_inst_->reset(); - } else if (gme_inst_) { + } + else if (gme_inst_) + { playing_ = true; gme_inst_->reset(); - } else if (xmp_inst_) { + } + else if (xmp_inst_) + { xmp_inst_->looping(looping); playing_ = true; xmp_inst_->reset(); } } - void unpause() { - if (ogg_inst_) { + void unpause() + { + if (ogg_inst_) + { ogg_inst_->playing(true); playing_ = true; - } else if (gme_inst_) { + } + else if (gme_inst_) + { playing_ = true; - } else if (xmp_inst_) { + } + else if (xmp_inst_) + { playing_ = true; } } - void pause() { - if (ogg_inst_) { + void pause() + { + if (ogg_inst_) + { ogg_inst_->playing(false); playing_ = false; - } else if (gme_inst_) { + } + else if (gme_inst_) + { playing_ = false; - } else if (xmp_inst_) { + } + else if (xmp_inst_) + { playing_ = false; } } - void stop() { - if (ogg_inst_) { + void stop() + { + if (ogg_inst_) + { ogg_inst_->reset(); ogg_inst_->playing(false); playing_ = false; - } else if (gme_inst_) { + } + else if (gme_inst_) + { gme_inst_->reset(); playing_ = false; - } else if (xmp_inst_) { + } + else if (xmp_inst_) + { xmp_inst_->reset(); playing_ = false; } } - void seek(float position_seconds) { - if (ogg_inst_) { + void seek(float position_seconds) + { + if (ogg_inst_) + { ogg_inst_->seek(position_seconds); return; } - if (gme_inst_) { + if (gme_inst_) + { gme_inst_->seek(position_seconds); return; } - if (xmp_inst_) { + if (xmp_inst_) + { xmp_inst_->seek(position_seconds); return; } } - bool playing() const { + bool playing() const + { if (ogg_inst_) return ogg_inst_->playing(); else if (gme_inst_) @@ -224,7 +274,8 @@ public: return false; } - std::optional music_type() const { + std::optional music_type() const + { if (ogg_inst_) return audio::MusicType::kOgg; else if (gme_inst_) @@ -235,7 +286,8 @@ public: return std::nullopt; } - std::optional duration_seconds() const { + std::optional duration_seconds() const + { if (ogg_inst_) return ogg_inst_->duration_seconds(); if (gme_inst_) @@ -246,7 +298,8 @@ public: return std::nullopt; } - std::optional loop_point_seconds() const { + std::optional loop_point_seconds() const + { if (ogg_inst_) return ogg_inst_->loop_point_seconds(); if (gme_inst_) @@ -255,7 +308,8 @@ public: return std::nullopt; } - std::optional position_seconds() const { + std::optional position_seconds() const + { if (ogg_inst_) return ogg_inst_->position_seconds(); if (gme_inst_) @@ -266,13 +320,14 @@ public: void fade_to(float gain, float seconds) { fade_from_to(gain_target_, gain, seconds); } - void fade_from_to(float from, float to, float seconds) { + void fade_from_to(float from, float to, float seconds) + { fading_ = true; gain_ = from; gain_target_ = to; // Gain samples target must always be at least 1 to avoid a div-by-zero. - gain_samples_target_ = std::max( - static_cast(seconds * 44100.f), UINT64_C(1)); // UINT64_C generates a uint64_t literal + gain_samples_target_ = + std::max(static_cast(seconds * 44100.f), UINT64_C(1)); // UINT64_C generates a uint64_t literal gain_samples_ = 0; } @@ -280,12 +335,14 @@ public: void stop_fade() { internal_gain(gain_target_); } - void loop_point_seconds(float loop_point) { + void loop_point_seconds(float loop_point) + { if (ogg_inst_) ogg_inst_->loop_point_seconds(loop_point); } - void internal_gain(float gain) { + void internal_gain(float gain) + { fading_ = false; gain_ = gain; gain_target_ = gain; @@ -310,112 +367,131 @@ private: }; // The special member functions MUST be declared in this unit, where Impl is complete. -MusicPlayer::MusicPlayer() : impl_(make_unique()) { +MusicPlayer::MusicPlayer() : impl_(make_unique()) +{ } -MusicPlayer::MusicPlayer(tcb::span data) : impl_(make_unique(data)) { +MusicPlayer::MusicPlayer(tcb::span data) : impl_(make_unique(data)) +{ } MusicPlayer::MusicPlayer(MusicPlayer&& rhs) noexcept = default; MusicPlayer& MusicPlayer::operator=(MusicPlayer&& rhs) noexcept = default; MusicPlayer::~MusicPlayer() = default; -void MusicPlayer::play(bool looping) { +void MusicPlayer::play(bool looping) +{ SRB2_ASSERT(impl_ != nullptr); return impl_->play(looping); } -void MusicPlayer::unpause() { +void MusicPlayer::unpause() +{ SRB2_ASSERT(impl_ != nullptr); return impl_->unpause(); } -void MusicPlayer::pause() { +void MusicPlayer::pause() +{ SRB2_ASSERT(impl_ != nullptr); return impl_->pause(); } -void MusicPlayer::stop() { +void MusicPlayer::stop() +{ SRB2_ASSERT(impl_ != nullptr); return impl_->stop(); } -void MusicPlayer::seek(float position_seconds) { +void MusicPlayer::seek(float position_seconds) +{ SRB2_ASSERT(impl_ != nullptr); return impl_->seek(position_seconds); } -bool MusicPlayer::playing() const { +bool MusicPlayer::playing() const +{ SRB2_ASSERT(impl_ != nullptr); return impl_->playing(); } -size_t MusicPlayer::generate(tcb::span> buffer) { +size_t MusicPlayer::generate(tcb::span> buffer) +{ SRB2_ASSERT(impl_ != nullptr); return impl_->generate(buffer); } -std::optional MusicPlayer::music_type() const { +std::optional MusicPlayer::music_type() const +{ SRB2_ASSERT(impl_ != nullptr); return impl_->music_type(); } -std::optional MusicPlayer::duration_seconds() const { +std::optional MusicPlayer::duration_seconds() const +{ SRB2_ASSERT(impl_ != nullptr); return impl_->duration_seconds(); } -std::optional MusicPlayer::loop_point_seconds() const { +std::optional MusicPlayer::loop_point_seconds() const +{ SRB2_ASSERT(impl_ != nullptr); return impl_->loop_point_seconds(); } -std::optional MusicPlayer::position_seconds() const { +std::optional MusicPlayer::position_seconds() const +{ SRB2_ASSERT(impl_ != nullptr); return impl_->position_seconds(); } -void MusicPlayer::fade_to(float gain, float seconds) { +void MusicPlayer::fade_to(float gain, float seconds) +{ SRB2_ASSERT(impl_ != nullptr); impl_->fade_to(gain, seconds); } -void MusicPlayer::fade_from_to(float from, float to, float seconds) { +void MusicPlayer::fade_from_to(float from, float to, float seconds) +{ SRB2_ASSERT(impl_ != nullptr); impl_->fade_from_to(from, to, seconds); } -void MusicPlayer::internal_gain(float gain) { +void MusicPlayer::internal_gain(float gain) +{ SRB2_ASSERT(impl_ != nullptr); impl_->internal_gain(gain); } -bool MusicPlayer::fading() const { +bool MusicPlayer::fading() const +{ SRB2_ASSERT(impl_ != nullptr); return impl_->fading(); } -void MusicPlayer::stop_fade() { +void MusicPlayer::stop_fade() +{ SRB2_ASSERT(impl_ != nullptr); impl_->stop_fade(); } -void MusicPlayer::loop_point_seconds(float loop_point) { +void MusicPlayer::loop_point_seconds(float loop_point) +{ SRB2_ASSERT(impl_ != nullptr); impl_->loop_point_seconds(loop_point); diff --git a/src/audio/music_player.hpp b/src/audio/music_player.hpp index 668724fa0..69f32dd6b 100644 --- a/src/audio/music_player.hpp +++ b/src/audio/music_player.hpp @@ -19,15 +19,18 @@ struct stb_vorbis; -namespace srb2::audio { +namespace srb2::audio +{ -enum class MusicType { +enum class MusicType +{ kOgg, kGme, kMod }; -class MusicPlayer : public Source<2> { +class MusicPlayer : public Source<2> +{ public: MusicPlayer(); MusicPlayer(tcb::span data); diff --git a/src/audio/ogg.cpp b/src/audio/ogg.cpp index 388fd37fe..b86e9c85c 100644 --- a/src/audio/ogg.cpp +++ b/src/audio/ogg.cpp @@ -16,11 +16,14 @@ using namespace srb2; using namespace srb2::audio; -StbVorbisException::StbVorbisException(int code) noexcept : code_(code) { +StbVorbisException::StbVorbisException(int code) noexcept : code_(code) +{ } -const char* StbVorbisException::what() const noexcept { - switch (code_) { +const char* StbVorbisException::what() const noexcept +{ + switch (code_) + { case VORBIS__no_error: return "No error"; case VORBIS_need_more_data: @@ -68,54 +71,73 @@ const char* StbVorbisException::what() const noexcept { } } -Ogg::Ogg() noexcept : memory_data_(), instance_(nullptr) { +Ogg::Ogg() noexcept : memory_data_(), instance_(nullptr) +{ } -Ogg::Ogg(std::vector data) : memory_data_(std::move(data)), instance_(nullptr) { +Ogg::Ogg(std::vector data) : memory_data_(std::move(data)), instance_(nullptr) +{ _init_with_data(); } -Ogg::Ogg(tcb::span data) : memory_data_(data.begin(), data.end()), instance_(nullptr) { +Ogg::Ogg(tcb::span data) : memory_data_(data.begin(), data.end()), instance_(nullptr) +{ _init_with_data(); } -Ogg::Ogg(Ogg&& rhs) noexcept : memory_data_(), instance_(nullptr) { +Ogg::Ogg(Ogg&& rhs) noexcept : memory_data_(), instance_(nullptr) +{ std::swap(memory_data_, rhs.memory_data_); std::swap(instance_, rhs.instance_); } -Ogg& Ogg::operator=(Ogg&& rhs) noexcept { +Ogg& Ogg::operator=(Ogg&& rhs) noexcept +{ std::swap(memory_data_, rhs.memory_data_); std::swap(instance_, rhs.instance_); return *this; } -Ogg::~Ogg() { - if (instance_) { +Ogg::~Ogg() +{ + if (instance_) + { stb_vorbis_close(instance_); instance_ = nullptr; } } -std::size_t Ogg::get_samples(tcb::span> buffer) { +std::size_t Ogg::get_samples(tcb::span> buffer) +{ SRB2_ASSERT(instance_ != nullptr); size_t read = stb_vorbis_get_samples_float_interleaved( - instance_, 1, reinterpret_cast(buffer.data()), buffer.size() * 1); + instance_, + 1, + reinterpret_cast(buffer.data()), + buffer.size() * 1 + ); return read; } -std::size_t Ogg::get_samples(tcb::span> buffer) { +std::size_t Ogg::get_samples(tcb::span> buffer) +{ SRB2_ASSERT(instance_ != nullptr); size_t read = stb_vorbis_get_samples_float_interleaved( - instance_, 2, reinterpret_cast(buffer.data()), buffer.size() * 2); + instance_, + 2, + reinterpret_cast(buffer.data()), + buffer.size() * 2 + ); stb_vorbis_info info = stb_vorbis_get_info(instance_); - if (info.channels == 1) { - for (auto& sample : buffer.subspan(0, read)) { + if (info.channels == 1) + { + for (auto& sample : buffer.subspan(0, read)) + { sample.amplitudes[1] = sample.amplitudes[0]; } } @@ -123,7 +145,8 @@ std::size_t Ogg::get_samples(tcb::span> buffer) { return read; } -OggComment Ogg::comment() const { +OggComment Ogg::comment() const +{ SRB2_ASSERT(instance_ != nullptr); stb_vorbis_comment c_comment = stb_vorbis_get_comment(instance_); @@ -133,50 +156,59 @@ OggComment Ogg::comment() const { std::vector(c_comment.comment_list, c_comment.comment_list + c_comment.comment_list_length)}; } -std::size_t Ogg::sample_rate() const { +std::size_t Ogg::sample_rate() const +{ SRB2_ASSERT(instance_ != nullptr); stb_vorbis_info info = stb_vorbis_get_info(instance_); return info.sample_rate; } -void Ogg::seek(std::size_t sample) { +void Ogg::seek(std::size_t sample) +{ SRB2_ASSERT(instance_ != nullptr); stb_vorbis_seek(instance_, sample); } -std::size_t Ogg::position() const { +std::size_t Ogg::position() const +{ SRB2_ASSERT(instance_ != nullptr); return stb_vorbis_get_sample_offset(instance_); } -float Ogg::position_seconds() const { +float Ogg::position_seconds() const +{ return position() / static_cast(sample_rate()); } -std::size_t Ogg::duration_samples() const { +std::size_t Ogg::duration_samples() const +{ SRB2_ASSERT(instance_ != nullptr); return stb_vorbis_stream_length_in_samples(instance_); } -float Ogg::duration_seconds() const { +float Ogg::duration_seconds() const +{ SRB2_ASSERT(instance_ != nullptr); return stb_vorbis_stream_length_in_seconds(instance_); } -std::size_t Ogg::channels() const { +std::size_t Ogg::channels() const +{ SRB2_ASSERT(instance_ != nullptr); stb_vorbis_info info = stb_vorbis_get_info(instance_); return info.channels; } -void Ogg::_init_with_data() { - if (instance_) { +void Ogg::_init_with_data() +{ + if (instance_) + { return; } @@ -187,7 +219,11 @@ void Ogg::_init_with_data() { int vorbis_result; instance_ = stb_vorbis_open_memory( - reinterpret_cast(memory_data_.data()), memory_data_.size(), &vorbis_result, NULL); + reinterpret_cast(memory_data_.data()), + memory_data_.size(), + &vorbis_result, + NULL + ); if (vorbis_result != VORBIS__no_error) throw StbVorbisException(vorbis_result); diff --git a/src/audio/ogg.hpp b/src/audio/ogg.hpp index d4b8b9275..bd8c9a86c 100644 --- a/src/audio/ogg.hpp +++ b/src/audio/ogg.hpp @@ -20,9 +20,11 @@ #include "../io/streams.hpp" #include "source.hpp" -namespace srb2::audio { +namespace srb2::audio +{ -class StbVorbisException final : public std::exception { +class StbVorbisException final : public std::exception +{ int code_; public: @@ -31,12 +33,14 @@ public: virtual const char* what() const noexcept; }; -struct OggComment { +struct OggComment +{ std::string vendor; std::vector comments; }; -class Ogg final { +class Ogg final +{ std::vector memory_data_; stb_vorbis* instance_; @@ -71,7 +75,8 @@ private: }; template , int> = 0> -inline Ogg load_ogg(I& stream) { +inline Ogg load_ogg(I& stream) +{ std::vector data = srb2::io::read_to_vec(stream); return Ogg {std::move(data)}; } diff --git a/src/audio/ogg_player.cpp b/src/audio/ogg_player.cpp index d9028dedb..e865d334e 100644 --- a/src/audio/ogg_player.cpp +++ b/src/audio/ogg_player.cpp @@ -18,35 +18,46 @@ using namespace srb2; using namespace srb2::audio; -namespace { +namespace +{ -std::optional find_loop_point(const Ogg& ogg) { +std::optional find_loop_point(const Ogg& ogg) +{ OggComment comment = ogg.comment(); std::size_t rate = ogg.sample_rate(); - for (auto& comment : comment.comments) { - if (comment.find("LOOPPOINT=") == 0) { + for (auto& comment : comment.comments) + { + if (comment.find("LOOPPOINT=") == 0) + { std::string_view comment_view(comment); comment_view.remove_prefix(10); std::string copied {comment_view}; - try { + try + { int loop_point = std::stoi(copied); return loop_point; - } catch (...) { + } + catch (...) + { } } - if (comment.find("LOOPMS=") == 0) { + if (comment.find("LOOPMS=") == 0) + { std::string_view comment_view(comment); comment_view.remove_prefix(7); std::string copied {comment_view}; - try { + try + { int loop_ms = std::stoi(copied); int loop_point = std::round(static_cast(loop_ms) / (rate / 1000.)); return loop_point; - } catch (...) { + } + catch (...) + { } } } @@ -58,7 +69,8 @@ std::optional find_loop_point(const Ogg& ogg) { template OggPlayer::OggPlayer(Ogg&& ogg) noexcept - : playing_(false), looping_(false), loop_point_(std::nullopt), ogg_(std::forward(ogg)) { + : playing_(false), looping_(false), loop_point_(std::nullopt), ogg_(std::forward(ogg)) +{ loop_point_ = find_loop_point(ogg_); } @@ -72,25 +84,30 @@ template OggPlayer::~OggPlayer() = default; template -std::size_t OggPlayer::generate(tcb::span> buffer) { +std::size_t OggPlayer::generate(tcb::span> buffer) +{ if (!playing_) return 0; std::size_t total = 0; - do { + do + { std::size_t read = ogg_.get_samples(buffer.subspan(total)); total += read; - if (read == 0 && !looping_) { + if (read == 0 && !looping_) + { playing_ = false; break; } - if (read == 0 && loop_point_) { + if (read == 0 && loop_point_) + { ogg_.seek(*loop_point_); } - if (read == 0 && !loop_point_) { + if (read == 0 && !loop_point_) + { ogg_.seek(0); } } while (total < buffer.size()); @@ -99,33 +116,39 @@ std::size_t OggPlayer::generate(tcb::span> buffer) { } template -void OggPlayer::seek(float position_seconds) { +void OggPlayer::seek(float position_seconds) +{ ogg_.seek(static_cast(position_seconds * sample_rate())); } template -void OggPlayer::loop_point_seconds(float loop_point) { +void OggPlayer::loop_point_seconds(float loop_point) +{ std::size_t rate = sample_rate(); loop_point = static_cast(std::round(loop_point * rate)); } template -void OggPlayer::reset() { +void OggPlayer::reset() +{ ogg_.seek(0); } template -std::size_t OggPlayer::sample_rate() const { +std::size_t OggPlayer::sample_rate() const +{ return ogg_.sample_rate(); } template -float OggPlayer::duration_seconds() const { +float OggPlayer::duration_seconds() const +{ return ogg_.duration_seconds(); } template -std::optional OggPlayer::loop_point_seconds() const { +std::optional OggPlayer::loop_point_seconds() const +{ if (!loop_point_) return std::nullopt; @@ -133,7 +156,8 @@ std::optional OggPlayer::loop_point_seconds() const { } template -float OggPlayer::position_seconds() const { +float OggPlayer::position_seconds() const +{ return ogg_.position_seconds(); } diff --git a/src/audio/ogg_player.hpp b/src/audio/ogg_player.hpp index 049d39862..f80ae7734 100644 --- a/src/audio/ogg_player.hpp +++ b/src/audio/ogg_player.hpp @@ -25,10 +25,12 @@ #include "ogg.hpp" #include "source.hpp" -namespace srb2::audio { +namespace srb2::audio +{ template -class OggPlayer final : public Source { +class OggPlayer final : public Source +{ bool playing_; bool looping_; std::optional loop_point_; diff --git a/src/audio/resample.cpp b/src/audio/resample.cpp index a64d0af13..d82282ba6 100644 --- a/src/audio/resample.cpp +++ b/src/audio/resample.cpp @@ -23,7 +23,8 @@ using namespace srb2::audio; template Resampler::Resampler(std::shared_ptr>&& source, float ratio) - : source_(std::forward>>(source)), ratio_(ratio) { + : source_(std::forward>>(source)), ratio_(ratio) +{ } template @@ -36,11 +37,13 @@ template Resampler& Resampler::operator=(Resampler&& r) = default; template -size_t Resampler::generate(tcb::span> buffer) { +size_t Resampler::generate(tcb::span> buffer) +{ if (!source_) return 0; - if (ratio_ == 1.f) { + if (ratio_ == 1.f) + { // fast path - generate directly from source size_t source_read = source_->generate(buffer); return source_read; @@ -48,21 +51,25 @@ size_t Resampler::generate(tcb::span> buffer) { size_t written = 0; - while (written < buffer.size()) { + while (written < buffer.size()) + { // do we need a refill? - if (buf_.size() == 0 || pos_ >= static_cast(buf_.size() - 1)) { + if (buf_.size() == 0 || pos_ >= static_cast(buf_.size() - 1)) + { pos_ -= buf_.size(); last_ = buf_.size() == 0 ? Sample {} : buf_.back(); buf_.clear(); buf_.resize(512); size_t source_read = source_->generate(buf_); buf_.resize(source_read); - if (source_read == 0) { + if (source_read == 0) + { break; } } - if (pos_ < 0) { + if (pos_ < 0) + { buffer[written] = (buf_[0] - last_) * pos_frac_ + last_; advance(ratio_); written++; diff --git a/src/audio/resample.hpp b/src/audio/resample.hpp index 7aab4f674..0936db7b8 100644 --- a/src/audio/resample.hpp +++ b/src/audio/resample.hpp @@ -21,10 +21,12 @@ #include "sound_chunk.hpp" #include "source.hpp" -namespace srb2::audio { +namespace srb2::audio +{ template -class Resampler : public Source { +class Resampler : public Source +{ public: Resampler(std::shared_ptr>&& source_, float ratio); Resampler(const Resampler& r) = delete; @@ -44,7 +46,8 @@ private: int pos_ {0}; float pos_frac_ {0.f}; - void advance(float samples) { + void advance(float samples) + { pos_frac_ += samples; float integer; std::modf(pos_frac_, &integer); diff --git a/src/audio/sample.hpp b/src/audio/sample.hpp index b1f0298b5..7f671bc7a 100644 --- a/src/audio/sample.hpp +++ b/src/audio/sample.hpp @@ -12,21 +12,27 @@ #include -namespace srb2::audio { +namespace srb2::audio +{ template -struct Sample { +struct Sample +{ std::array amplitudes; - constexpr Sample& operator+=(const Sample& rhs) noexcept { - for (std::size_t i = 0; i < C; i++) { + constexpr Sample& operator+=(const Sample& rhs) noexcept + { + for (std::size_t i = 0; i < C; i++) + { amplitudes[i] += rhs.amplitudes[i]; } return *this; } - constexpr Sample& operator*=(float rhs) noexcept { - for (std::size_t i = 0; i < C; i++) { + constexpr Sample& operator*=(float rhs) noexcept + { + for (std::size_t i = 0; i < C; i++) + { amplitudes[i] *= rhs; } return *this; @@ -34,27 +40,33 @@ struct Sample { }; template -constexpr Sample operator+(const Sample& lhs, const Sample& rhs) noexcept { +constexpr Sample operator+(const Sample& lhs, const Sample& rhs) noexcept +{ Sample out; - for (std::size_t i = 0; i < C; i++) { + for (std::size_t i = 0; i < C; i++) + { out.amplitudes[i] = lhs.amplitudes[i] + rhs.amplitudes[i]; } return out; } template -constexpr Sample operator-(const Sample& lhs, const Sample& rhs) noexcept { +constexpr Sample operator-(const Sample& lhs, const Sample& rhs) noexcept +{ Sample out; - for (std::size_t i = 0; i < C; i++) { + for (std::size_t i = 0; i < C; i++) + { out.amplitudes[i] = lhs.amplitudes[i] - rhs.amplitudes[i]; } return out; } template -constexpr Sample operator*(const Sample& lhs, float rhs) noexcept { +constexpr Sample operator*(const Sample& lhs, float rhs) noexcept +{ Sample out; - for (std::size_t i = 0; i < C; i++) { + for (std::size_t i = 0; i < C; i++) + { out.amplitudes[i] = lhs.amplitudes[i] * rhs; } return out; @@ -64,12 +76,14 @@ template static constexpr float sample_to_float(T sample) noexcept; template <> -constexpr float sample_to_float(uint8_t sample) noexcept { +constexpr float sample_to_float(uint8_t sample) noexcept +{ return (sample / 128.f) - 1.f; } template <> -constexpr float sample_to_float(int16_t sample) noexcept { +constexpr float sample_to_float(int16_t sample) noexcept +{ return sample / 32768.f; } diff --git a/src/audio/sound_chunk.hpp b/src/audio/sound_chunk.hpp index 7fc0a45eb..8bae962da 100644 --- a/src/audio/sound_chunk.hpp +++ b/src/audio/sound_chunk.hpp @@ -14,9 +14,11 @@ #include "source.hpp" -namespace srb2::audio { +namespace srb2::audio +{ -struct SoundChunk { +struct SoundChunk +{ std::vector> samples; }; diff --git a/src/audio/sound_effect_player.cpp b/src/audio/sound_effect_player.cpp index a038ee3d8..620a22688 100644 --- a/src/audio/sound_effect_player.cpp +++ b/src/audio/sound_effect_player.cpp @@ -20,15 +20,18 @@ using srb2::audio::Sample; using srb2::audio::SoundEffectPlayer; using srb2::audio::Source; -size_t SoundEffectPlayer::generate(tcb::span> buffer) { +size_t SoundEffectPlayer::generate(tcb::span> buffer) +{ if (!chunk_) return 0; - if (position_ >= chunk_->samples.size()) { + if (position_ >= chunk_->samples.size()) + { return 0; } size_t written = 0; - for (; position_ < chunk_->samples.size() && written < buffer.size(); position_++) { + for (; position_ < chunk_->samples.size() && written < buffer.size(); position_++) + { float mono_sample = chunk_->samples[position_].amplitudes[0]; float sep_pan = ((sep_ + 1.f) / 2.f) * (3.14159 / 2.f); @@ -41,23 +44,27 @@ size_t SoundEffectPlayer::generate(tcb::span> buffer) { return written; } -void SoundEffectPlayer::start(const SoundChunk* chunk, float volume, float sep) { +void SoundEffectPlayer::start(const SoundChunk* chunk, float volume, float sep) +{ this->update(volume, sep); position_ = 0; chunk_ = chunk; } -void SoundEffectPlayer::update(float volume, float sep) { +void SoundEffectPlayer::update(float volume, float sep) +{ volume_ = volume; sep_ = sep; } -void SoundEffectPlayer::reset() { +void SoundEffectPlayer::reset() +{ position_ = 0; chunk_ = nullptr; } -bool SoundEffectPlayer::finished() const { +bool SoundEffectPlayer::finished() const +{ if (!chunk_) return true; if (position_ >= chunk_->samples.size()) @@ -65,7 +72,8 @@ bool SoundEffectPlayer::finished() const { return false; } -bool SoundEffectPlayer::is_playing_chunk(const SoundChunk* chunk) const { +bool SoundEffectPlayer::is_playing_chunk(const SoundChunk* chunk) const +{ return chunk_ == chunk; } diff --git a/src/audio/sound_effect_player.hpp b/src/audio/sound_effect_player.hpp index 99f5edb9e..6fce34700 100644 --- a/src/audio/sound_effect_player.hpp +++ b/src/audio/sound_effect_player.hpp @@ -17,9 +17,11 @@ #include "sound_chunk.hpp" #include "source.hpp" -namespace srb2::audio { +namespace srb2::audio +{ -class SoundEffectPlayer : public Source<2> { +class SoundEffectPlayer : public Source<2> +{ public: virtual std::size_t generate(tcb::span> buffer) override final; diff --git a/src/audio/source.hpp b/src/audio/source.hpp index ea4be8761..9f9471f94 100644 --- a/src/audio/source.hpp +++ b/src/audio/source.hpp @@ -16,10 +16,12 @@ #include "sample.hpp" -namespace srb2::audio { +namespace srb2::audio +{ template -class Source { +class Source +{ public: virtual std::size_t generate(tcb::span> buffer) = 0; diff --git a/src/audio/wav.cpp b/src/audio/wav.cpp index 751c87b41..c1bcd7977 100644 --- a/src/audio/wav.cpp +++ b/src/audio/wav.cpp @@ -18,7 +18,8 @@ using namespace srb2; using srb2::audio::Wav; -namespace { +namespace +{ constexpr const uint32_t kMagicRIFF = 0x46464952; constexpr const uint32_t kMagicWAVE = 0x45564157; @@ -29,17 +30,20 @@ constexpr const uint16_t kFormatPcm = 1; constexpr const std::size_t kRiffHeaderLength = 8; -struct RiffHeader { +struct RiffHeader +{ uint32_t magic; std::size_t filesize; }; -struct TagHeader { +struct TagHeader +{ uint32_t type; std::size_t length; }; -struct FmtTag { +struct FmtTag +{ uint16_t format; uint16_t channels; uint32_t rate; @@ -48,9 +52,12 @@ struct FmtTag { uint16_t bit_width; }; -struct DataTag {}; +struct DataTag +{ +}; -RiffHeader parse_riff_header(io::SpanStream& stream) { +RiffHeader parse_riff_header(io::SpanStream& stream) +{ if (io::remaining(stream) < kRiffHeaderLength) throw std::runtime_error("insufficient bytes remaining in stream"); @@ -60,7 +67,8 @@ RiffHeader parse_riff_header(io::SpanStream& stream) { return ret; } -TagHeader parse_tag_header(io::SpanStream& stream) { +TagHeader parse_tag_header(io::SpanStream& stream) +{ if (io::remaining(stream) < 8) throw std::runtime_error("insufficient bytes remaining in stream"); @@ -70,7 +78,8 @@ TagHeader parse_tag_header(io::SpanStream& stream) { return header; } -FmtTag parse_fmt_tag(io::SpanStream& stream) { +FmtTag parse_fmt_tag(io::SpanStream& stream) +{ if (io::remaining(stream) < 16) throw std::runtime_error("insufficient bytes in stream"); @@ -86,14 +95,16 @@ FmtTag parse_fmt_tag(io::SpanStream& stream) { } template -void visit_tag(Visitor& visitor, io::SpanStream& stream, const TagHeader& header) { +void visit_tag(Visitor& visitor, io::SpanStream& stream, const TagHeader& header) +{ if (io::remaining(stream) < header.length) throw std::runtime_error("insufficient bytes in stream"); const io::StreamSize start = stream.seek(io::SeekFrom::kCurrent, 0); const io::StreamSize dest = start + header.length; - switch (header.type) { + switch (header.type) + { case kMagicFmt: { FmtTag fmt_tag {parse_fmt_tag(stream)}; @@ -114,19 +125,23 @@ void visit_tag(Visitor& visitor, io::SpanStream& stream, const TagHeader& header stream.seek(io::SeekFrom::kStart, dest); } -std::vector read_uint8_samples_from_stream(io::SpanStream& stream, std::size_t count) { +std::vector read_uint8_samples_from_stream(io::SpanStream& stream, std::size_t count) +{ std::vector samples; samples.reserve(count); - for (std::size_t i = 0; i < count; i++) { + for (std::size_t i = 0; i < count; i++) + { samples.push_back(io::read_uint8(stream)); } return samples; } -std::vector read_int16_samples_from_stream(io::SpanStream& stream, std::size_t count) { +std::vector read_int16_samples_from_stream(io::SpanStream& stream, std::size_t count) +{ std::vector samples; samples.reserve(count); - for (std::size_t i = 0; i < count; i++) { + for (std::size_t i = 0; i < count; i++) + { samples.push_back(io::read_int16(stream)); } return samples; @@ -136,57 +151,70 @@ std::vector read_int16_samples_from_stream(io::SpanStream& stream, std: Wav::Wav() = default; -Wav::Wav(tcb::span data) { +Wav::Wav(tcb::span data) +{ io::SpanStream stream {data}; auto [magic, filesize] = parse_riff_header(stream); - if (magic != kMagicRIFF) { + if (magic != kMagicRIFF) + { throw std::runtime_error("invalid RIFF magic"); } - if (io::remaining(stream) < filesize) { + if (io::remaining(stream) < filesize) + { throw std::runtime_error("insufficient data in stream for RIFF's reported filesize"); } const io::StreamSize riff_end = stream.seek(io::SeekFrom::kCurrent, 0) + filesize; uint32_t type = io::read_uint32(stream); - if (type != kMagicWAVE) { + if (type != kMagicWAVE) + { throw std::runtime_error("RIFF in stream is not a WAVE"); } std::optional read_fmt; std::variant, std::vector> interleaved_samples; - while (stream.seek(io::SeekFrom::kCurrent, 0) < riff_end) { + while (stream.seek(io::SeekFrom::kCurrent, 0) < riff_end) + { TagHeader tag_header {parse_tag_header(stream)}; - if (io::remaining(stream) < tag_header.length) { + if (io::remaining(stream) < tag_header.length) + { throw std::runtime_error("WAVE tag length exceeds stream length"); } auto tag_visitor = srb2::Overload { - [&](const FmtTag& fmt) { - if (read_fmt) { + [&](const FmtTag& fmt) + { + if (read_fmt) + { throw std::runtime_error("WAVE has multiple 'fmt' tags"); } - if (fmt.format != kFormatPcm) { + if (fmt.format != kFormatPcm) + { throw std::runtime_error("Unsupported WAVE format (only PCM is supported)"); } read_fmt = fmt; }, - [&](const DataTag& data) { - if (!read_fmt) { + [&](const DataTag& data) + { + if (!read_fmt) + { throw std::runtime_error("unable to read data tag because no fmt tag was read"); } - if (tag_header.length % read_fmt->bytes_per_sample != 0) { + if (tag_header.length % read_fmt->bytes_per_sample != 0) + { throw std::runtime_error("data tag length not divisible by bytes_per_sample"); } const std::size_t sample_count = tag_header.length / read_fmt->bytes_per_sample; - switch (read_fmt->bit_width) { + switch (read_fmt->bit_width) + { case 8: interleaved_samples = std::move(read_uint8_samples_from_stream(stream, sample_count)); break; @@ -201,7 +229,8 @@ Wav::Wav(tcb::span data) { visit_tag(tag_visitor, stream, tag_header); } - if (!read_fmt) { + if (!read_fmt) + { throw std::runtime_error("WAVE did not have a fmt tag"); } @@ -210,27 +239,34 @@ Wav::Wav(tcb::span data) { sample_rate_ = read_fmt->rate; } -namespace { +namespace +{ template -std::size_t read_samples(std::size_t channels, - std::size_t offset, - const std::vector& samples, - tcb::span> buffer) noexcept { +std::size_t read_samples( + std::size_t channels, + std::size_t offset, + const std::vector& samples, + tcb::span> buffer +) noexcept +{ const std::size_t offset_interleaved = offset * channels; const std::size_t samples_size = samples.size(); const std::size_t buffer_size = buffer.size(); - if (offset_interleaved >= samples_size) { + if (offset_interleaved >= samples_size) + { return 0; } const std::size_t remainder = (samples_size - offset_interleaved) / channels; const std::size_t samples_to_read = std::min(buffer_size, remainder); - for (std::size_t i = 0; i < samples_to_read; i++) { + for (std::size_t i = 0; i < samples_to_read; i++) + { buffer[i].amplitudes[0] = 0.f; - for (std::size_t j = 0; j < channels; j++) { + for (std::size_t j = 0; j < channels; j++) + { buffer[i].amplitudes[0] += audio::sample_to_float(samples[i * channels + j + offset_interleaved]); } buffer[i].amplitudes[0] /= static_cast(channels); @@ -241,18 +277,20 @@ std::size_t read_samples(std::size_t channels, } // namespace -std::size_t Wav::get_samples(std::size_t offset, tcb::span> buffer) const noexcept { +std::size_t Wav::get_samples(std::size_t offset, tcb::span> buffer) const noexcept +{ auto samples_visitor = srb2::Overload { [&](const std::vector& samples) { return read_samples(channels(), offset, samples, buffer); }, - [&](const std::vector& samples) { - return read_samples(channels(), offset, samples, buffer); - }}; + [&](const std::vector& samples) + { return read_samples(channels(), offset, samples, buffer); }}; return std::visit(samples_visitor, interleaved_samples_); } -std::size_t Wav::interleaved_length() const noexcept { - auto samples_visitor = srb2::Overload {[](const std::vector& samples) { return samples.size(); }, - [](const std::vector& samples) { return samples.size(); }}; +std::size_t Wav::interleaved_length() const noexcept +{ + auto samples_visitor = srb2::Overload { + [](const std::vector& samples) { return samples.size(); }, + [](const std::vector& samples) { return samples.size(); }}; return std::visit(samples_visitor, interleaved_samples_); } diff --git a/src/audio/wav.hpp b/src/audio/wav.hpp index e571969e7..1dd2504d8 100644 --- a/src/audio/wav.hpp +++ b/src/audio/wav.hpp @@ -21,9 +21,11 @@ #include "../io/streams.hpp" #include "sample.hpp" -namespace srb2::audio { +namespace srb2::audio +{ -class Wav final { +class Wav final +{ std::variant, std::vector> interleaved_samples_; std::size_t channels_ = 1; std::size_t sample_rate_ = 44100; @@ -41,7 +43,8 @@ public: }; template , int> = 0> -inline Wav load_wav(I& stream) { +inline Wav load_wav(I& stream) +{ std::vector data = srb2::io::read_to_vec(stream); return Wav {data}; } diff --git a/src/audio/wav_player.cpp b/src/audio/wav_player.cpp index 64f9831f0..61bb95fea 100644 --- a/src/audio/wav_player.cpp +++ b/src/audio/wav_player.cpp @@ -13,7 +13,8 @@ using namespace srb2; using srb2::audio::WavPlayer; -WavPlayer::WavPlayer() : WavPlayer(audio::Wav {}) { +WavPlayer::WavPlayer() : WavPlayer(audio::Wav {}) +{ } WavPlayer::WavPlayer(const WavPlayer& rhs) = default; @@ -24,20 +25,25 @@ WavPlayer& WavPlayer::operator=(const WavPlayer& rhs) = default; WavPlayer& WavPlayer::operator=(WavPlayer&& rhs) noexcept = default; -WavPlayer::WavPlayer(audio::Wav&& wav) noexcept : wav_(std::forward(wav)), position_(0), looping_(false) { +WavPlayer::WavPlayer(audio::Wav&& wav) noexcept : wav_(std::forward(wav)), position_(0), looping_(false) +{ } -std::size_t WavPlayer::generate(tcb::span> buffer) { +std::size_t WavPlayer::generate(tcb::span> buffer) +{ std::size_t samples_read = 0; - while (samples_read < buffer.size()) { + while (samples_read < buffer.size()) + { const std::size_t read_this_time = wav_.get_samples(position_, buffer.subspan(samples_read)); position_ += read_this_time; samples_read += read_this_time; - if (position_ > wav_.length() && looping_) { + if (position_ > wav_.length() && looping_) + { position_ = 0; } - if (read_this_time == 0 && !looping_) { + if (read_this_time == 0 && !looping_) + { break; } } diff --git a/src/audio/wav_player.hpp b/src/audio/wav_player.hpp index dc6a98864..d7b12a23b 100644 --- a/src/audio/wav_player.hpp +++ b/src/audio/wav_player.hpp @@ -17,9 +17,11 @@ #include "source.hpp" #include "wav.hpp" -namespace srb2::audio { +namespace srb2::audio +{ -class WavPlayer final : public Source<1> { +class WavPlayer final : public Source<1> +{ Wav wav_; std::size_t position_; bool looping_; diff --git a/src/audio/xmp.cpp b/src/audio/xmp.cpp index 9e88f2a7f..6c1031a9a 100644 --- a/src/audio/xmp.cpp +++ b/src/audio/xmp.cpp @@ -16,11 +16,14 @@ using namespace srb2; using namespace srb2::audio; -XmpException::XmpException(int code) : code_(code) { +XmpException::XmpException(int code) : code_(code) +{ } -const char* XmpException::what() const noexcept { - switch (code_) { +const char* XmpException::what() const noexcept +{ + switch (code_) + { case -XMP_ERROR_INTERNAL: return "XMP_ERROR_INTERNAL"; case -XMP_ERROR_FORMAT: @@ -41,23 +44,27 @@ const char* XmpException::what() const noexcept { } template -Xmp::Xmp() : data_(), instance_(nullptr), module_loaded_(false), looping_(false) { +Xmp::Xmp() : data_(), instance_(nullptr), module_loaded_(false), looping_(false) +{ } template Xmp::Xmp(std::vector data) - : data_(std::move(data)), instance_(nullptr), module_loaded_(false), looping_(false) { + : data_(std::move(data)), instance_(nullptr), module_loaded_(false), looping_(false) +{ _init(); } template Xmp::Xmp(tcb::span data) - : data_(data.begin(), data.end()), instance_(nullptr), module_loaded_(false), looping_(false) { + : data_(data.begin(), data.end()), instance_(nullptr), module_loaded_(false), looping_(false) +{ _init(); } template -Xmp::Xmp(Xmp&& rhs) noexcept : Xmp() { +Xmp::Xmp(Xmp&& rhs) noexcept : Xmp() +{ std::swap(data_, rhs.data_); std::swap(instance_, rhs.instance_); std::swap(module_loaded_, rhs.module_loaded_); @@ -65,7 +72,8 @@ Xmp::Xmp(Xmp&& rhs) noexcept : Xmp() { } template -Xmp& Xmp::operator=(Xmp&& rhs) noexcept { +Xmp& Xmp::operator=(Xmp&& rhs) noexcept +{ std::swap(data_, rhs.data_); std::swap(instance_, rhs.instance_); std::swap(module_loaded_, rhs.module_loaded_); @@ -75,15 +83,18 @@ Xmp& Xmp::operator=(Xmp&& rhs) noexcept { }; template -Xmp::~Xmp() { - if (instance_) { +Xmp::~Xmp() +{ + if (instance_) + { xmp_free_context(instance_); instance_ = nullptr; } } template -std::size_t Xmp::play_buffer(tcb::span> buffer) { +std::size_t Xmp::play_buffer(tcb::span> buffer) +{ SRB2_ASSERT(instance_ != nullptr); SRB2_ASSERT(module_loaded_ == true); @@ -99,7 +110,8 @@ std::size_t Xmp::play_buffer(tcb::span> buffer) { } template -void Xmp::reset() { +void Xmp::reset() +{ SRB2_ASSERT(instance_ != nullptr); SRB2_ASSERT(module_loaded_ == true); @@ -107,7 +119,8 @@ void Xmp::reset() { } template -float Xmp::duration_seconds() const { +float Xmp::duration_seconds() const +{ SRB2_ASSERT(instance_ != nullptr); SRB2_ASSERT(module_loaded_ == true); @@ -117,7 +130,8 @@ float Xmp::duration_seconds() const { } template -void Xmp::seek(int position_ms) { +void Xmp::seek(int position_ms) +{ SRB2_ASSERT(instance_ != nullptr); SRB2_ASSERT(module_loaded_ == true); @@ -127,7 +141,8 @@ void Xmp::seek(int position_ms) { } template -void Xmp::_init() { +void Xmp::_init() +{ if (instance_) return; @@ -137,12 +152,14 @@ void Xmp::_init() { throw std::logic_error("Insufficient data from stream"); instance_ = xmp_create_context(); - if (instance_ == nullptr) { + if (instance_ == nullptr) + { throw std::bad_alloc(); } int result = xmp_load_module_from_memory(instance_, data_.data(), data_.size()); - if (result != 0) { + if (result != 0) + { xmp_free_context(instance_); instance_ = nullptr; throw XmpException(result); @@ -150,11 +167,13 @@ void Xmp::_init() { module_loaded_ = true; int flags = 0; - if constexpr (C == 1) { + if constexpr (C == 1) + { flags |= XMP_FORMAT_MONO; } result = xmp_start_player(instance_, 44100, flags); - if (result != 0) { + if (result != 0) + { xmp_release_module(instance_); module_loaded_ = false; xmp_free_context(instance_); diff --git a/src/audio/xmp.hpp b/src/audio/xmp.hpp index a5b443bfa..dc22550c4 100644 --- a/src/audio/xmp.hpp +++ b/src/audio/xmp.hpp @@ -7,7 +7,7 @@ // See the 'LICENSE' file for more details. //----------------------------------------------------------------------------- - #ifndef __SRB2_AUDIO_XMP_HPP__ +#ifndef __SRB2_AUDIO_XMP_HPP__ #define __SRB2_AUDIO_XMP_HPP__ #include @@ -22,9 +22,11 @@ #include "../io/streams.hpp" -namespace srb2::audio { +namespace srb2::audio +{ -class XmpException : public std::exception { +class XmpException : public std::exception +{ int code_; public: @@ -33,7 +35,8 @@ public: }; template -class Xmp final { +class Xmp final +{ std::vector data_; xmp_context instance_; bool module_loaded_; @@ -68,7 +71,8 @@ extern template class Xmp<1>; extern template class Xmp<2>; template , int> = 0> -inline Xmp load_xmp(I& stream) { +inline Xmp load_xmp(I& stream) +{ std::vector data = srb2::io::read_to_vec(stream); return Xmp {std::move(data)}; } diff --git a/src/audio/xmp_player.cpp b/src/audio/xmp_player.cpp index a08ee8bb5..2d869857a 100644 --- a/src/audio/xmp_player.cpp +++ b/src/audio/xmp_player.cpp @@ -15,7 +15,8 @@ using namespace srb2; using namespace srb2::audio; template -XmpPlayer::XmpPlayer(Xmp&& xmp) : xmp_(std::move(xmp)), buf_() { +XmpPlayer::XmpPlayer(Xmp&& xmp) : xmp_(std::move(xmp)), buf_() +{ } template @@ -28,14 +29,17 @@ template XmpPlayer::~XmpPlayer() = default; template -std::size_t XmpPlayer::generate(tcb::span> buffer) { +std::size_t XmpPlayer::generate(tcb::span> buffer) +{ buf_.resize(buffer.size()); std::size_t read = xmp_.play_buffer(tcb::make_span(buf_)); buf_.resize(read); std::size_t ret = std::min(buffer.size(), buf_.size()); - for (std::size_t i = 0; i < ret; i++) { - for (std::size_t j = 0; j < C; j++) { + for (std::size_t i = 0; i < ret; i++) + { + for (std::size_t j = 0; j < C; j++) + { buffer[i].amplitudes[j] = buf_[i][j] / 32768.f; } } @@ -44,12 +48,14 @@ std::size_t XmpPlayer::generate(tcb::span> buffer) { } template -float XmpPlayer::duration_seconds() const { +float XmpPlayer::duration_seconds() const +{ return xmp_.duration_seconds(); } template -void XmpPlayer::seek(float position_seconds) { +void XmpPlayer::seek(float position_seconds) +{ xmp_.seek(static_cast(std::round(position_seconds * 1000.f))); } diff --git a/src/audio/xmp_player.hpp b/src/audio/xmp_player.hpp index 5829dbd8a..865eabb5e 100644 --- a/src/audio/xmp_player.hpp +++ b/src/audio/xmp_player.hpp @@ -13,10 +13,12 @@ #include "source.hpp" #include "xmp.hpp" -namespace srb2::audio { +namespace srb2::audio +{ template -class XmpPlayer final : public Source { +class XmpPlayer final : public Source +{ Xmp xmp_; std::vector> buf_; diff --git a/src/cxxutil.hpp b/src/cxxutil.hpp index 82131768a..06f6f1adc 100644 --- a/src/cxxutil.hpp +++ b/src/cxxutil.hpp @@ -62,10 +62,12 @@ struct SourceLocation { class IErrorAssertHandler { public: static void handle(const SourceLocation& source_location, const char* expression) { - I_Error("Assertion failed at %s:%u: %s != true", - source_location.file_name, - source_location.line_number, - expression); + I_Error( + "Assertion failed at %s:%u: %s != true", + source_location.file_name, + source_location.line_number, + expression + ); } }; @@ -102,8 +104,10 @@ class NotNull final { T ptr_; public: - static_assert(std::is_convertible_v() != nullptr), bool>, - "T is not comparable with nullptr_t"); + static_assert( + std::is_convertible_v() != nullptr), bool>, + "T is not comparable with nullptr_t" + ); /// @brief Move-construct from the pointer value U, asserting that it is not null. Allows construction of a /// NotNull from any compatible pointer U, for example with polymorphic classes. diff --git a/src/sdl/new_sound.cpp b/src/sdl/new_sound.cpp index 0415778f1..1a2d25b1c 100644 --- a/src/sdl/new_sound.cpp +++ b/src/sdl/new_sound.cpp @@ -59,7 +59,8 @@ static vector> sound_effect_channels; static void (*music_fade_callback)(); -void* I_GetSfx(sfxinfo_t* sfx) { +void* I_GetSfx(sfxinfo_t* sfx) +{ if (sfx->lumpnum == LUMPERROR) sfx->lumpnum = S_GetSfxLumpNum(sfx); sfx->length = W_LumpLength(sfx->lumpnum); @@ -78,14 +79,18 @@ void* I_GetSfx(sfxinfo_t* sfx) { return heap_chunk; } -void I_FreeSfx(sfxinfo_t* sfx) { - if (sfx->data) { +void I_FreeSfx(sfxinfo_t* sfx) +{ + if (sfx->data) + { SoundChunk* chunk = static_cast(sfx->data); auto _ = srb2::finally([chunk]() { delete chunk; }); // Stop any channels playing this chunk - for (auto& player : sound_effect_channels) { - if (player->is_playing_chunk(chunk)) { + for (auto& player : sound_effect_channels) + { + if (player->is_playing_chunk(chunk)) + { player->reset(); } } @@ -94,22 +99,27 @@ void I_FreeSfx(sfxinfo_t* sfx) { sfx->lumpnum = LUMPERROR; } -namespace { +namespace +{ -class SdlAudioLockHandle { +class SdlAudioLockHandle +{ public: SdlAudioLockHandle() { SDL_LockAudio(); } ~SdlAudioLockHandle() { SDL_UnlockAudio(); } }; -void audio_callback(void* userdata, Uint8* buffer, int len) { +void audio_callback(void* userdata, Uint8* buffer, int len) +{ // The SDL Audio lock is implied to be held during callback. - try { + try + { Sample<2>* float_buffer = reinterpret_cast*>(buffer); size_t float_len = len / 8; - for (size_t i = 0; i < float_len; i++) { + for (size_t i = 0; i < float_len; i++) + { float_buffer[i] = Sample<2> {0.f, 0.f}; } @@ -118,20 +128,25 @@ void audio_callback(void* userdata, Uint8* buffer, int len) { master->generate(tcb::span {float_buffer, float_len}); - for (size_t i = 0; i < float_len; i++) { + for (size_t i = 0; i < float_len; i++) + { float_buffer[i] = { std::clamp(float_buffer[i].amplitudes[0], -1.f, 1.f), std::clamp(float_buffer[i].amplitudes[1], -1.f, 1.f), }; } - } catch (...) { + } + catch (...) + { } return; } -void initialize_sound() { - if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { +void initialize_sound() +{ + if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) + { CONS_Alert(CONS_ERROR, "Error initializing SDL Audio: %s\n", SDL_GetError()); return; } @@ -143,7 +158,8 @@ void initialize_sound() { desired.freq = 44100; desired.callback = audio_callback; - if (SDL_OpenAudio(&desired, NULL) < 0) { + if (SDL_OpenAudio(&desired, NULL) < 0) + { CONS_Alert(CONS_ERROR, "Failed to open SDL Audio device: %s\n", SDL_GetError()); SDL_QuitSubSystem(SDL_INIT_AUDIO); return; @@ -165,7 +181,8 @@ void initialize_sound() { master->add_source(gain_sound_effects); master->add_source(gain_music); mixer_music->add_source(music_player); - for (size_t i = 0; i < static_cast(cv_numChannels.value); i++) { + for (size_t i = 0; i < static_cast(cv_numChannels.value); i++) + { shared_ptr player = make_shared(); sound_effect_channels.push_back(player); mixer_sound_effects->add_source(player); @@ -177,26 +194,31 @@ void initialize_sound() { } // namespace -void I_StartupSound(void) { +void I_StartupSound(void) +{ if (!sound_started) initialize_sound(); } -void I_ShutdownSound(void) { +void I_ShutdownSound(void) +{ SdlAudioLockHandle _; - for (auto& channel : sound_effect_channels) { + for (auto& channel : sound_effect_channels) + { if (channel) *channel = audio::SoundEffectPlayer(); } } -void I_UpdateSound(void) { +void I_UpdateSound(void) +{ // The SDL audio lock is re-entrant, so it is safe to lock twice // for the "fade to stop music" callback later. SdlAudioLockHandle _; - if (music_fade_callback && !music_player->fading()) { + if (music_fade_callback && !music_player->fading()) + { auto old_callback = music_fade_callback; music_fade_callback = nullptr; (old_callback()); @@ -208,7 +230,8 @@ void I_UpdateSound(void) { // SFX I/O // -INT32 I_StartSound(sfxenum_t id, UINT8 vol, UINT8 sep, UINT8 pitch, UINT8 priority, INT32 channel) { +INT32 I_StartSound(sfxenum_t id, UINT8 vol, UINT8 sep, UINT8 pitch, UINT8 priority, INT32 channel) +{ (void) pitch; (void) priority; @@ -218,16 +241,21 @@ INT32 I_StartSound(sfxenum_t id, UINT8 vol, UINT8 sep, UINT8 pitch, UINT8 priori return -1; shared_ptr player_channel; - if (channel < 0) { + if (channel < 0) + { // find a free sfx channel - for (size_t i = 0; i < sound_effect_channels.size(); i++) { - if (sound_effect_channels[i]->finished()) { + for (size_t i = 0; i < sound_effect_channels.size(); i++) + { + if (sound_effect_channels[i]->finished()) + { player_channel = sound_effect_channels[i]; channel = i; break; } } - } else { + } + else + { player_channel = sound_effect_channels[channel]; } @@ -246,7 +274,8 @@ INT32 I_StartSound(sfxenum_t id, UINT8 vol, UINT8 sep, UINT8 pitch, UINT8 priori return channel; } -void I_StopSound(INT32 handle) { +void I_StopSound(INT32 handle) +{ SdlAudioLockHandle _; if (sound_effect_channels.empty()) @@ -263,7 +292,8 @@ void I_StopSound(INT32 handle) { sound_effect_channels[index]->reset(); } -boolean I_SoundIsPlaying(INT32 handle) { +boolean I_SoundIsPlaying(INT32 handle) +{ SdlAudioLockHandle _; // Handle is channel index @@ -281,7 +311,8 @@ boolean I_SoundIsPlaying(INT32 handle) { return sound_effect_channels[index]->finished() ? 0 : 1; } -void I_UpdateSoundParams(INT32 handle, UINT8 vol, UINT8 sep, UINT8 pitch) { +void I_UpdateSoundParams(INT32 handle, UINT8 vol, UINT8 sep, UINT8 pitch) +{ (void) pitch; SdlAudioLockHandle _; @@ -298,18 +329,21 @@ void I_UpdateSoundParams(INT32 handle, UINT8 vol, UINT8 sep, UINT8 pitch) { return; shared_ptr& channel = sound_effect_channels[index]; - if (!channel->finished()) { + if (!channel->finished()) + { float vol_float = static_cast(vol) / 255.f; float sep_float = static_cast(sep) / 127.f - 1.f; channel->update(vol_float, sep_float); } } -void I_SetSfxVolume(int volume) { +void I_SetSfxVolume(int volume) +{ SdlAudioLockHandle _; float vol = static_cast(volume) / 100.f; - if (gain_sound_effects) { + if (gain_sound_effects) + { gain_sound_effects->gain(vol * vol * vol); } } @@ -318,7 +352,8 @@ void I_SetSfxVolume(int volume) { // MUSIC SYSTEM /// ------------------------ -void I_InitMusic(void) { +void I_InitMusic(void) +{ if (!sound_started) initialize_sound(); @@ -327,7 +362,8 @@ void I_InitMusic(void) { *music_player = audio::MusicPlayer(); } -void I_ShutdownMusic(void) { +void I_ShutdownMusic(void) +{ SdlAudioLockHandle _; if (music_player) @@ -338,7 +374,8 @@ void I_ShutdownMusic(void) { // MUSIC PROPERTIES /// ------------------------ -musictype_t I_SongType(void) { +musictype_t I_SongType(void) +{ if (!music_player) return MU_NONE; @@ -346,11 +383,13 @@ musictype_t I_SongType(void) { std::optional music_type = music_player->music_type(); - if (music_type == std::nullopt) { + if (music_type == std::nullopt) + { return MU_NONE; } - switch (*music_type) { + switch (*music_type) + { case audio::MusicType::kOgg: return MU_OGG; case audio::MusicType::kGme: @@ -362,7 +401,8 @@ musictype_t I_SongType(void) { } } -boolean I_SongPlaying(void) { +boolean I_SongPlaying(void) +{ if (!music_player) return false; @@ -371,7 +411,8 @@ boolean I_SongPlaying(void) { return music_player->music_type().has_value(); } -boolean I_SongPaused(void) { +boolean I_SongPaused(void) +{ if (!music_player) return false; @@ -384,7 +425,8 @@ boolean I_SongPaused(void) { // MUSIC EFFECTS /// ------------------------ -boolean I_SetSongSpeed(float speed) { +boolean I_SetSongSpeed(float speed) +{ (void) speed; return false; } @@ -393,7 +435,8 @@ boolean I_SetSongSpeed(float speed) { // MUSIC SEEKING /// ------------------------ -UINT32 I_GetSongLength(void) { +UINT32 I_GetSongLength(void) +{ if (!music_player) return 0; @@ -407,13 +450,15 @@ UINT32 I_GetSongLength(void) { return static_cast(std::round(*duration * 1000.f)); } -boolean I_SetSongLoopPoint(UINT32 looppoint) { +boolean I_SetSongLoopPoint(UINT32 looppoint) +{ if (!music_player) return 0; SdlAudioLockHandle _; - if (music_player->music_type() == audio::MusicType::kOgg) { + if (music_player->music_type() == audio::MusicType::kOgg) + { music_player->loop_point_seconds(looppoint / 1000.f); return true; } @@ -421,7 +466,8 @@ boolean I_SetSongLoopPoint(UINT32 looppoint) { return false; } -UINT32 I_GetSongLoopPoint(void) { +UINT32 I_GetSongLoopPoint(void) +{ if (!music_player) return 0; @@ -435,7 +481,8 @@ UINT32 I_GetSongLoopPoint(void) { return static_cast(std::round(*loop_point_seconds * 1000.f)); } -boolean I_SetSongPosition(UINT32 position) { +boolean I_SetSongPosition(UINT32 position) +{ if (!music_player) return false; @@ -445,7 +492,8 @@ boolean I_SetSongPosition(UINT32 position) { return true; } -UINT32 I_GetSongPosition(void) { +UINT32 I_GetSongPosition(void) +{ if (!music_player) return 0; @@ -459,50 +507,66 @@ UINT32 I_GetSongPosition(void) { return static_cast(std::round(*position_seconds * 1000.f)); } -void I_UpdateSongLagThreshold(void) { +void I_UpdateSongLagThreshold(void) +{ } -void I_UpdateSongLagConditions(void) { +void I_UpdateSongLagConditions(void) +{ } /// ------------------------ // MUSIC PLAYBACK /// ------------------------ -namespace { -void print_walk_ex_stack(const std::exception& ex) { +namespace +{ +void print_walk_ex_stack(const std::exception& ex) +{ CONS_Alert(CONS_WARNING, " Caused by: %s\n", ex.what()); - try { + try + { std::rethrow_if_nested(ex); - } catch (const std::exception& ex) { + } + catch (const std::exception& ex) + { print_walk_ex_stack(ex); } } -void print_ex(const std::exception& ex) { +void print_ex(const std::exception& ex) +{ CONS_Alert(CONS_WARNING, "Exception loading music: %s\n", ex.what()); - try { + try + { std::rethrow_if_nested(ex); - } catch (const std::exception& ex) { + } + catch (const std::exception& ex) + { print_walk_ex_stack(ex); } } } // namespace -boolean I_LoadSong(char* data, size_t len) { +boolean I_LoadSong(char* data, size_t len) +{ if (!music_player) return false; tcb::span data_span(reinterpret_cast(data), len); audio::MusicPlayer new_player; - try { + try + { new_player = audio::MusicPlayer {data_span}; - } catch (const std::exception& ex) { + } + catch (const std::exception& ex) + { print_ex(ex); return false; } - if (music_fade_callback && music_player->fading()) { + if (music_fade_callback && music_player->fading()) + { auto old_callback = music_fade_callback; music_fade_callback = nullptr; (old_callback)(); @@ -510,9 +574,12 @@ boolean I_LoadSong(char* data, size_t len) { SdlAudioLockHandle _; - try { + try + { *music_player = std::move(new_player); - } catch (const std::exception& ex) { + } + catch (const std::exception& ex) + { print_ex(ex); return false; } @@ -520,11 +587,13 @@ boolean I_LoadSong(char* data, size_t len) { return true; } -void I_UnloadSong(void) { +void I_UnloadSong(void) +{ if (!music_player) return; - if (music_fade_callback && music_player->fading()) { + if (music_fade_callback && music_player->fading()) + { auto old_callback = music_fade_callback; music_fade_callback = nullptr; (old_callback)(); @@ -535,7 +604,8 @@ void I_UnloadSong(void) { *music_player = audio::MusicPlayer(); } -boolean I_PlaySong(boolean looping) { +boolean I_PlaySong(boolean looping) +{ if (!music_player) return false; @@ -546,7 +616,8 @@ boolean I_PlaySong(boolean looping) { return true; } -void I_StopSong(void) { +void I_StopSong(void) +{ if (!music_player) return; @@ -555,7 +626,8 @@ void I_StopSong(void) { music_player->stop(); } -void I_PauseSong(void) { +void I_PauseSong(void) +{ if (!music_player) return; @@ -564,7 +636,8 @@ void I_PauseSong(void) { music_player->pause(); } -void I_ResumeSong(void) { +void I_ResumeSong(void) +{ if (!music_player) return; @@ -573,15 +646,18 @@ void I_ResumeSong(void) { music_player->unpause(); } -void I_SetMusicVolume(int volume) { +void I_SetMusicVolume(int volume) +{ float vol = static_cast(volume) / 100.f; - if (gain_music) { + if (gain_music) + { gain_music->gain(vol * vol * vol); } } -boolean I_SetSongTrack(int track) { +boolean I_SetSongTrack(int track) +{ (void) track; return false; } @@ -590,7 +666,8 @@ boolean I_SetSongTrack(int track) { // MUSIC FADING /// ------------------------ -void I_SetInternalMusicVolume(UINT8 volume) { +void I_SetInternalMusicVolume(UINT8 volume) +{ if (!music_player) return; @@ -600,7 +677,8 @@ void I_SetInternalMusicVolume(UINT8 volume) { music_player->internal_gain(gain); } -void I_StopFadingSong(void) { +void I_StopFadingSong(void) +{ if (!music_player) return; @@ -609,7 +687,8 @@ void I_StopFadingSong(void) { music_player->stop_fade(); } -boolean I_FadeSongFromVolume(UINT8 target_volume, UINT8 source_volume, UINT32 ms, void (*callback)(void)) { +boolean I_FadeSongFromVolume(UINT8 target_volume, UINT8 source_volume, UINT32 ms, void (*callback)(void)) +{ if (!music_player) return false; @@ -628,7 +707,8 @@ boolean I_FadeSongFromVolume(UINT8 target_volume, UINT8 source_volume, UINT32 ms return true; } -boolean I_FadeSong(UINT8 target_volume, UINT32 ms, void (*callback)(void)) { +boolean I_FadeSong(UINT8 target_volume, UINT32 ms, void (*callback)(void)) +{ if (!music_player) return false; @@ -646,7 +726,8 @@ boolean I_FadeSong(UINT8 target_volume, UINT32 ms, void (*callback)(void)) { return true; } -static void stop_song_cb(void) { +static void stop_song_cb(void) +{ if (!music_player) return; @@ -655,11 +736,13 @@ static void stop_song_cb(void) { music_player->stop(); } -boolean I_FadeOutStopSong(UINT32 ms) { +boolean I_FadeOutStopSong(UINT32 ms) +{ return I_FadeSong(0.f, ms, stop_song_cb); } -boolean I_FadeInPlaySong(UINT32 ms, boolean looping) { +boolean I_FadeInPlaySong(UINT32 ms, boolean looping) +{ if (I_PlaySong(looping)) return I_FadeSongFromVolume(100, 0, ms, nullptr); else