From 0e8a2c7222b978507f62d7e0b83187b16532eae8 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 28 Jul 2018 13:35:22 -0400 Subject: [PATCH] audio_core: Misc. improvements to stream/buffer/audio_out. --- src/audio_core/audio_out.cpp | 4 ++-- src/audio_core/audio_out.h | 6 ++---- src/audio_core/buffer.h | 3 +++ src/audio_core/stream.cpp | 24 ++++++++++++------------ src/audio_core/stream.h | 15 +++++++++++++-- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/audio_core/audio_out.cpp b/src/audio_core/audio_out.cpp index 6d418a05b1..43414f197d 100644 --- a/src/audio_core/audio_out.cpp +++ b/src/audio_core/audio_out.cpp @@ -9,7 +9,7 @@ namespace AudioCore { /// Returns the stream format from the specified number of channels -static Stream::Format ChannelsToStreamFormat(int num_channels) { +static Stream::Format ChannelsToStreamFormat(u32 num_channels) { switch (num_channels) { case 1: return Stream::Format::Mono16; @@ -24,7 +24,7 @@ static Stream::Format ChannelsToStreamFormat(int num_channels) { return {}; } -StreamPtr AudioOut::OpenStream(int sample_rate, int num_channels, +StreamPtr AudioOut::OpenStream(u32 sample_rate, u32 num_channels, Stream::ReleaseCallback&& release_callback) { streams.push_back(std::make_shared(sample_rate, ChannelsToStreamFormat(num_channels), std::move(release_callback))); diff --git a/src/audio_core/audio_out.h b/src/audio_core/audio_out.h index a86499d103..962360d09f 100644 --- a/src/audio_core/audio_out.h +++ b/src/audio_core/audio_out.h @@ -13,15 +13,13 @@ namespace AudioCore { -using StreamPtr = std::shared_ptr; - /** * Represents an audio playback interface, used to open and play audio streams */ class AudioOut { public: /// Opens a new audio stream - StreamPtr OpenStream(int sample_rate, int num_channels, + StreamPtr OpenStream(u32 sample_rate, u32 num_channels, Stream::ReleaseCallback&& release_callback); /// Returns a vector of recently released buffers specified by tag for the specified stream @@ -37,7 +35,7 @@ public: bool QueueBuffer(StreamPtr stream, Buffer::Tag tag, std::vector&& data); private: - /// Active audio streams on the interface + SinkPtr sink; std::vector streams; }; diff --git a/src/audio_core/buffer.h b/src/audio_core/buffer.h index 874ec787e2..4bf5fd58a7 100644 --- a/src/audio_core/buffer.h +++ b/src/audio_core/buffer.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "common/common_types.h" @@ -34,4 +35,6 @@ private: std::vector data; }; +using BufferPtr = std::shared_ptr; + } // namespace AudioCore diff --git a/src/audio_core/stream.cpp b/src/audio_core/stream.cpp index 82bff4b9ef..63edc6c8da 100644 --- a/src/audio_core/stream.cpp +++ b/src/audio_core/stream.cpp @@ -13,24 +13,24 @@ namespace AudioCore { constexpr size_t MaxAudioBufferCount{32}; -/// Returns the sample size for the specified audio stream format -static size_t SampleSizeFromFormat(Stream::Format format) { +u32 Stream::GetNumChannels() const { switch (format) { - case Stream::Format::Mono16: + case Format::Mono16: + return 1; + case Format::Stereo16: return 2; - case Stream::Format::Stereo16: - return 4; - case Stream::Format::Multi51Channel16: - return 12; - }; - + case Format::Multi51Channel16: + return 6; + } LOG_CRITICAL(Audio, "Unimplemented format={}", static_cast(format)); UNREACHABLE(); return {}; } -Stream::Stream(int sample_rate, Format format, ReleaseCallback&& release_callback) - : sample_rate{sample_rate}, format{format}, release_callback{std::move(release_callback)} { +u32 Stream::GetSampleSize() const { + return GetNumChannels() * 2; +} + release_event = CoreTiming::RegisterEvent( "Stream::Release", [this](u64 userdata, int cycles_late) { ReleaseActiveBuffer(); }); } @@ -45,7 +45,7 @@ void Stream::Stop() { } s64 Stream::GetBufferReleaseCycles(const Buffer& buffer) const { - const size_t num_samples{buffer.GetData().size() / SampleSizeFromFormat(format)}; + const size_t num_samples{buffer.GetData().size() / GetSampleSize()}; return CoreTiming::usToCycles((static_cast(num_samples) * 1000000) / sample_rate); } diff --git a/src/audio_core/stream.h b/src/audio_core/stream.h index 5f43b0798f..5c10058998 100644 --- a/src/audio_core/stream.h +++ b/src/audio_core/stream.h @@ -16,8 +16,6 @@ namespace AudioCore { -using BufferPtr = std::shared_ptr; - /** * Represents an audio stream, which is a sequence of queued buffers, to be outputed by AudioOut */ @@ -60,6 +58,17 @@ public: return queued_buffers.size(); } + /// Gets the sample rate + u32 GetSampleRate() const { + return sample_rate; + } + + /// Gets the number of channels + u32 GetNumChannels() const; + + /// Gets the sample size in bytes + u32 GetSampleSize() const; + private: /// Current state of the stream enum class State { @@ -86,4 +95,6 @@ private: std::queue released_buffers; ///< Buffers recently released from the stream }; +using StreamPtr = std::shared_ptr; + } // namespace AudioCore