From 0e8a2c7222b978507f62d7e0b83187b16532eae8 Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
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 6d418a05b..43414f197 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<Stream>(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 a86499d10..962360d09 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<Stream>;
-
 /**
  * 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<u8>&& data);
 
 private:
-    /// Active audio streams on the interface
+    SinkPtr sink;
     std::vector<StreamPtr> streams;
 };
 
diff --git a/src/audio_core/buffer.h b/src/audio_core/buffer.h
index 874ec787e..4bf5fd58a 100644
--- a/src/audio_core/buffer.h
+++ b/src/audio_core/buffer.h
@@ -4,6 +4,7 @@
 
 #pragma once
 
+#include <memory>
 #include <vector>
 
 #include "common/common_types.h"
@@ -34,4 +35,6 @@ private:
     std::vector<u8> data;
 };
 
+using BufferPtr = std::shared_ptr<Buffer>;
+
 } // namespace AudioCore
diff --git a/src/audio_core/stream.cpp b/src/audio_core/stream.cpp
index 82bff4b9e..63edc6c8d 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<u32>(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<u64>(num_samples) * 1000000) / sample_rate);
 }
 
diff --git a/src/audio_core/stream.h b/src/audio_core/stream.h
index 5f43b0798..5c1005899 100644
--- a/src/audio_core/stream.h
+++ b/src/audio_core/stream.h
@@ -16,8 +16,6 @@
 
 namespace AudioCore {
 
-using BufferPtr = std::shared_ptr<Buffer>;
-
 /**
  * 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<BufferPtr> released_buffers; ///< Buffers recently released from the stream
 };
 
+using StreamPtr = std::shared_ptr<Stream>;
+
 } // namespace AudioCore