mirror of
https://git.suyu.dev/suyu/suyu.git
synced 2024-11-22 10:51:50 -05:00
audio_core: Misc. improvements to stream/buffer/audio_out.
This commit is contained in:
parent
eaf66b4c9f
commit
0e8a2c7222
5 changed files with 32 additions and 20 deletions
|
@ -9,7 +9,7 @@
|
||||||
namespace AudioCore {
|
namespace AudioCore {
|
||||||
|
|
||||||
/// Returns the stream format from the specified number of channels
|
/// 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) {
|
switch (num_channels) {
|
||||||
case 1:
|
case 1:
|
||||||
return Stream::Format::Mono16;
|
return Stream::Format::Mono16;
|
||||||
|
@ -24,7 +24,7 @@ static Stream::Format ChannelsToStreamFormat(int num_channels) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamPtr AudioOut::OpenStream(int sample_rate, int num_channels,
|
StreamPtr AudioOut::OpenStream(u32 sample_rate, u32 num_channels,
|
||||||
Stream::ReleaseCallback&& release_callback) {
|
Stream::ReleaseCallback&& release_callback) {
|
||||||
streams.push_back(std::make_shared<Stream>(sample_rate, ChannelsToStreamFormat(num_channels),
|
streams.push_back(std::make_shared<Stream>(sample_rate, ChannelsToStreamFormat(num_channels),
|
||||||
std::move(release_callback)));
|
std::move(release_callback)));
|
||||||
|
|
|
@ -13,15 +13,13 @@
|
||||||
|
|
||||||
namespace AudioCore {
|
namespace AudioCore {
|
||||||
|
|
||||||
using StreamPtr = std::shared_ptr<Stream>;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an audio playback interface, used to open and play audio streams
|
* Represents an audio playback interface, used to open and play audio streams
|
||||||
*/
|
*/
|
||||||
class AudioOut {
|
class AudioOut {
|
||||||
public:
|
public:
|
||||||
/// Opens a new audio stream
|
/// 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);
|
Stream::ReleaseCallback&& release_callback);
|
||||||
|
|
||||||
/// Returns a vector of recently released buffers specified by tag for the specified stream
|
/// 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);
|
bool QueueBuffer(StreamPtr stream, Buffer::Tag tag, std::vector<u8>&& data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Active audio streams on the interface
|
SinkPtr sink;
|
||||||
std::vector<StreamPtr> streams;
|
std::vector<StreamPtr> streams;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
|
@ -34,4 +35,6 @@ private:
|
||||||
std::vector<u8> data;
|
std::vector<u8> data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using BufferPtr = std::shared_ptr<Buffer>;
|
||||||
|
|
||||||
} // namespace AudioCore
|
} // namespace AudioCore
|
||||||
|
|
|
@ -13,24 +13,24 @@ namespace AudioCore {
|
||||||
|
|
||||||
constexpr size_t MaxAudioBufferCount{32};
|
constexpr size_t MaxAudioBufferCount{32};
|
||||||
|
|
||||||
/// Returns the sample size for the specified audio stream format
|
u32 Stream::GetNumChannels() const {
|
||||||
static size_t SampleSizeFromFormat(Stream::Format format) {
|
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case Stream::Format::Mono16:
|
case Format::Mono16:
|
||||||
|
return 1;
|
||||||
|
case Format::Stereo16:
|
||||||
return 2;
|
return 2;
|
||||||
case Stream::Format::Stereo16:
|
case Format::Multi51Channel16:
|
||||||
return 4;
|
return 6;
|
||||||
case Stream::Format::Multi51Channel16:
|
}
|
||||||
return 12;
|
|
||||||
};
|
|
||||||
|
|
||||||
LOG_CRITICAL(Audio, "Unimplemented format={}", static_cast<u32>(format));
|
LOG_CRITICAL(Audio, "Unimplemented format={}", static_cast<u32>(format));
|
||||||
UNREACHABLE();
|
UNREACHABLE();
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Stream::Stream(int sample_rate, Format format, ReleaseCallback&& release_callback)
|
u32 Stream::GetSampleSize() const {
|
||||||
: sample_rate{sample_rate}, format{format}, release_callback{std::move(release_callback)} {
|
return GetNumChannels() * 2;
|
||||||
|
}
|
||||||
|
|
||||||
release_event = CoreTiming::RegisterEvent(
|
release_event = CoreTiming::RegisterEvent(
|
||||||
"Stream::Release", [this](u64 userdata, int cycles_late) { ReleaseActiveBuffer(); });
|
"Stream::Release", [this](u64 userdata, int cycles_late) { ReleaseActiveBuffer(); });
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ void Stream::Stop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
s64 Stream::GetBufferReleaseCycles(const Buffer& buffer) const {
|
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);
|
return CoreTiming::usToCycles((static_cast<u64>(num_samples) * 1000000) / sample_rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,8 +16,6 @@
|
||||||
|
|
||||||
namespace AudioCore {
|
namespace AudioCore {
|
||||||
|
|
||||||
using BufferPtr = std::shared_ptr<Buffer>;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an audio stream, which is a sequence of queued buffers, to be outputed by AudioOut
|
* 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();
|
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:
|
private:
|
||||||
/// Current state of the stream
|
/// Current state of the stream
|
||||||
enum class State {
|
enum class State {
|
||||||
|
@ -86,4 +95,6 @@ private:
|
||||||
std::queue<BufferPtr> released_buffers; ///< Buffers recently released from the stream
|
std::queue<BufferPtr> released_buffers; ///< Buffers recently released from the stream
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using StreamPtr = std::shared_ptr<Stream>;
|
||||||
|
|
||||||
} // namespace AudioCore
|
} // namespace AudioCore
|
||||||
|
|
Loading…
Reference in a new issue