2018-07-26 20:01:37 -04:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-30 23:57:53 -04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
2018-07-26 20:01:37 -04:00
|
|
|
|
2018-07-28 13:44:50 -04:00
|
|
|
#include "audio_core/sink.h"
|
|
|
|
#include "audio_core/sink_details.h"
|
2018-09-14 12:06:00 -04:00
|
|
|
#include "audio_core/sink_stream.h"
|
2018-07-26 20:01:37 -04:00
|
|
|
#include "audio_core/stream.h"
|
2018-07-30 23:57:53 -04:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
2021-04-14 19:07:40 -04:00
|
|
|
#include "common/settings.h"
|
2018-07-30 23:57:53 -04:00
|
|
|
#include "core/core_timing.h"
|
2018-07-26 20:01:37 -04:00
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
2018-09-15 09:21:06 -04:00
|
|
|
constexpr std::size_t MaxAudioBufferCount{32};
|
2018-07-26 20:01:37 -04:00
|
|
|
|
2018-07-28 13:35:22 -04:00
|
|
|
u32 Stream::GetNumChannels() const {
|
2018-07-26 20:01:37 -04:00
|
|
|
switch (format) {
|
2018-07-28 13:35:22 -04:00
|
|
|
case Format::Mono16:
|
|
|
|
return 1;
|
|
|
|
case Format::Stereo16:
|
2018-07-26 20:01:37 -04:00
|
|
|
return 2;
|
2018-07-28 13:35:22 -04:00
|
|
|
case Format::Multi51Channel16:
|
|
|
|
return 6;
|
|
|
|
}
|
2018-12-28 14:04:44 -05:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented format={}", static_cast<u32>(format));
|
2018-07-26 20:01:37 -04:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-11-25 15:21:03 -05:00
|
|
|
Stream::Stream(Core::Timing::CoreTiming& core_timing_, u32 sample_rate_, Format format_,
|
|
|
|
ReleaseCallback&& release_callback_, SinkStream& sink_stream_, std::string&& name_)
|
|
|
|
: sample_rate{sample_rate_}, format{format_}, release_callback{std::move(release_callback_)},
|
|
|
|
sink_stream{sink_stream_}, core_timing{core_timing_}, name{std::move(name_)} {
|
2020-07-27 19:00:41 -04:00
|
|
|
release_event =
|
|
|
|
Core::Timing::CreateEvent(name, [this](std::uintptr_t, std::chrono::nanoseconds ns_late) {
|
|
|
|
ReleaseActiveBuffer(ns_late);
|
|
|
|
});
|
2018-07-26 20:01:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::Play() {
|
|
|
|
state = State::Playing;
|
|
|
|
PlayNextBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::Stop() {
|
2018-09-23 08:32:01 -04:00
|
|
|
state = State::Stopped;
|
2018-12-28 14:04:44 -05:00
|
|
|
UNIMPLEMENTED();
|
2018-07-26 20:01:37 -04:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:13:34 -05:00
|
|
|
bool Stream::Flush() {
|
|
|
|
const bool had_buffers = !queued_buffers.empty();
|
|
|
|
while (!queued_buffers.empty()) {
|
|
|
|
queued_buffers.pop();
|
|
|
|
}
|
|
|
|
return had_buffers;
|
|
|
|
}
|
|
|
|
|
2019-06-16 05:06:33 -04:00
|
|
|
void Stream::SetVolume(float volume) {
|
|
|
|
game_volume = volume;
|
|
|
|
}
|
|
|
|
|
2018-09-23 20:01:02 -04:00
|
|
|
Stream::State Stream::GetState() const {
|
|
|
|
return state;
|
2018-09-23 08:32:01 -04:00
|
|
|
}
|
|
|
|
|
2020-07-15 18:30:06 -04:00
|
|
|
std::chrono::nanoseconds Stream::GetBufferReleaseNS(const Buffer& buffer) const {
|
2018-09-15 09:21:06 -04:00
|
|
|
const std::size_t num_samples{buffer.GetSamples().size() / GetNumChannels()};
|
2020-07-15 18:30:06 -04:00
|
|
|
return std::chrono::nanoseconds((static_cast<u64>(num_samples) * 1000000000ULL) / sample_rate);
|
2018-07-26 20:01:37 -04:00
|
|
|
}
|
|
|
|
|
2019-06-16 05:06:33 -04:00
|
|
|
static void VolumeAdjustSamples(std::vector<s16>& samples, float game_volume) {
|
2020-06-25 15:05:03 -04:00
|
|
|
const float volume{std::clamp(Settings::Volume() - (1.0f - game_volume), 0.0f, 1.0f)};
|
2018-07-30 23:57:53 -04:00
|
|
|
|
|
|
|
if (volume == 1.0f) {
|
2018-08-04 00:03:12 -04:00
|
|
|
return;
|
2018-07-30 23:57:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation of a volume slider with a dynamic range of 60 dB
|
2019-01-26 08:53:58 -05:00
|
|
|
const float volume_scale_factor = volume == 0 ? 0 : std::exp(6.90775f * volume) * 0.001f;
|
2018-07-30 23:57:53 -04:00
|
|
|
for (auto& sample : samples) {
|
|
|
|
sample = static_cast<s16>(sample * volume_scale_factor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 19:14:21 -04:00
|
|
|
void Stream::PlayNextBuffer(std::chrono::nanoseconds ns_late) {
|
2018-07-26 20:01:37 -04:00
|
|
|
if (!IsPlaying()) {
|
|
|
|
// Ensure we are in playing state before playing the next buffer
|
2018-09-12 13:07:16 -04:00
|
|
|
sink_stream.Flush();
|
2018-07-26 20:01:37 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active_buffer) {
|
|
|
|
// Do not queue a new buffer if we are already playing a buffer
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queued_buffers.empty()) {
|
|
|
|
// No queued buffers - we are effectively paused
|
2018-09-12 13:07:16 -04:00
|
|
|
sink_stream.Flush();
|
2018-07-26 20:01:37 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
active_buffer = queued_buffers.front();
|
|
|
|
queued_buffers.pop();
|
|
|
|
|
2019-06-16 05:06:33 -04:00
|
|
|
VolumeAdjustSamples(active_buffer->GetSamples(), game_volume);
|
2018-08-23 08:33:03 -04:00
|
|
|
|
2018-08-04 00:03:12 -04:00
|
|
|
sink_stream.EnqueueSamples(GetNumChannels(), active_buffer->GetSamples());
|
2018-07-28 13:44:50 -04:00
|
|
|
|
2021-02-02 10:59:59 -05:00
|
|
|
const auto buffer_release_ns = GetBufferReleaseNS(*active_buffer);
|
|
|
|
|
|
|
|
// If ns_late is higher than the update rate ignore the delay
|
|
|
|
if (ns_late > buffer_release_ns) {
|
|
|
|
ns_late = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
core_timing.ScheduleEvent(buffer_release_ns - ns_late, release_event, {});
|
2018-07-26 20:01:37 -04:00
|
|
|
}
|
|
|
|
|
2020-07-15 19:14:21 -04:00
|
|
|
void Stream::ReleaseActiveBuffer(std::chrono::nanoseconds ns_late) {
|
2018-08-02 18:27:22 -04:00
|
|
|
ASSERT(active_buffer);
|
2018-07-26 20:01:37 -04:00
|
|
|
released_buffers.push(std::move(active_buffer));
|
|
|
|
release_callback();
|
2020-07-15 19:14:21 -04:00
|
|
|
PlayNextBuffer(ns_late);
|
2018-07-26 20:01:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Stream::QueueBuffer(BufferPtr&& buffer) {
|
|
|
|
if (queued_buffers.size() < MaxAudioBufferCount) {
|
|
|
|
queued_buffers.push(std::move(buffer));
|
|
|
|
PlayNextBuffer();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-25 15:21:03 -05:00
|
|
|
bool Stream::ContainsBuffer([[maybe_unused]] Buffer::Tag tag) const {
|
2018-12-28 14:04:44 -05:00
|
|
|
UNIMPLEMENTED();
|
2018-07-26 20:01:37 -04:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-09-15 09:21:06 -04:00
|
|
|
std::vector<Buffer::Tag> Stream::GetTagsAndReleaseBuffers(std::size_t max_count) {
|
2018-07-26 20:01:37 -04:00
|
|
|
std::vector<Buffer::Tag> tags;
|
2018-09-15 09:21:06 -04:00
|
|
|
for (std::size_t count = 0; count < max_count && !released_buffers.empty(); ++count) {
|
2020-12-26 19:05:56 -05:00
|
|
|
if (released_buffers.front()) {
|
|
|
|
tags.push_back(released_buffers.front()->GetTag());
|
|
|
|
} else {
|
|
|
|
ASSERT_MSG(false, "Invalid tag in released_buffers!");
|
|
|
|
}
|
2018-07-26 20:01:37 -04:00
|
|
|
released_buffers.pop();
|
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
2020-11-16 22:14:29 -05:00
|
|
|
std::vector<Buffer::Tag> Stream::GetTagsAndReleaseBuffers() {
|
|
|
|
std::vector<Buffer::Tag> tags;
|
2020-11-16 23:40:19 -05:00
|
|
|
tags.reserve(released_buffers.size());
|
2020-11-16 22:14:29 -05:00
|
|
|
while (!released_buffers.empty()) {
|
2020-12-26 19:05:56 -05:00
|
|
|
if (released_buffers.front()) {
|
|
|
|
tags.push_back(released_buffers.front()->GetTag());
|
|
|
|
} else {
|
|
|
|
ASSERT_MSG(false, "Invalid tag in released_buffers!");
|
|
|
|
}
|
2020-11-16 22:14:29 -05:00
|
|
|
released_buffers.pop();
|
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
2018-07-26 20:01:37 -04:00
|
|
|
} // namespace AudioCore
|