audio_core: Misc. improvements to stream/buffer/audio_out.
This commit is contained in:
		@@ -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)));
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user