2018-07-27 01:01:37 +01:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-31 04:57:53 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
2018-07-27 01:01:37 +01:00
|
|
|
|
2018-07-28 18:44:50 +01:00
|
|
|
#include "audio_core/sink.h"
|
|
|
|
#include "audio_core/sink_details.h"
|
2018-09-14 17:06:00 +01:00
|
|
|
#include "audio_core/sink_stream.h"
|
2018-07-27 01:01:37 +01:00
|
|
|
#include "audio_core/stream.h"
|
2018-07-31 04:57:53 +01:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "core/core_timing.h"
|
|
|
|
#include "core/core_timing_util.h"
|
|
|
|
#include "core/settings.h"
|
2018-07-27 01:01:37 +01:00
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
2018-09-15 14:21:06 +01:00
|
|
|
constexpr std::size_t MaxAudioBufferCount{32};
|
2018-07-27 01:01:37 +01:00
|
|
|
|
2018-07-28 18:35:22 +01:00
|
|
|
u32 Stream::GetNumChannels() const {
|
2018-07-27 01:01:37 +01:00
|
|
|
switch (format) {
|
2018-07-28 18:35:22 +01:00
|
|
|
case Format::Mono16:
|
|
|
|
return 1;
|
|
|
|
case Format::Stereo16:
|
2018-07-27 01:01:37 +01:00
|
|
|
return 2;
|
2018-07-28 18:35:22 +01:00
|
|
|
case Format::Multi51Channel16:
|
|
|
|
return 6;
|
|
|
|
}
|
2018-12-28 19:04:44 +00:00
|
|
|
UNIMPLEMENTED_MSG("Unimplemented format={}", static_cast<u32>(format));
|
2018-07-27 01:01:37 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
Stream::Stream(Core::Timing::CoreTiming& core_timing, u32 sample_rate, Format format,
|
|
|
|
ReleaseCallback&& release_callback, SinkStream& sink_stream, std::string&& name_)
|
2018-07-28 18:44:50 +01:00
|
|
|
: sample_rate{sample_rate}, format{format}, release_callback{std::move(release_callback)},
|
2019-02-14 17:42:58 +00:00
|
|
|
sink_stream{sink_stream}, core_timing{core_timing}, name{std::move(name_)} {
|
2018-07-28 18:44:50 +01:00
|
|
|
|
2019-11-27 02:48:56 +00:00
|
|
|
release_event = Core::Timing::CreateEvent(
|
2019-03-24 22:11:32 +00:00
|
|
|
name, [this](u64 userdata, s64 cycles_late) { ReleaseActiveBuffer(); });
|
2018-07-27 01:01:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::Play() {
|
|
|
|
state = State::Playing;
|
|
|
|
PlayNextBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::Stop() {
|
2018-09-23 13:32:01 +01:00
|
|
|
state = State::Stopped;
|
2018-12-28 19:04:44 +00:00
|
|
|
UNIMPLEMENTED();
|
2018-07-27 01:01:37 +01:00
|
|
|
}
|
|
|
|
|
2019-06-16 10:06:33 +01:00
|
|
|
void Stream::SetVolume(float volume) {
|
|
|
|
game_volume = volume;
|
|
|
|
}
|
|
|
|
|
2018-09-24 01:01:02 +01:00
|
|
|
Stream::State Stream::GetState() const {
|
|
|
|
return state;
|
2018-09-23 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 15:51:33 +00:00
|
|
|
s64 Stream::GetBufferReleaseNS(const Buffer& buffer) const {
|
2018-09-15 14:21:06 +01:00
|
|
|
const std::size_t num_samples{buffer.GetSamples().size() / GetNumChannels()};
|
2020-02-25 15:51:33 +00:00
|
|
|
const auto ns =
|
|
|
|
std::chrono::nanoseconds((static_cast<u64>(num_samples) * 1000000000ULL) / sample_rate);
|
|
|
|
return ns.count();
|
2018-07-27 01:01:37 +01:00
|
|
|
}
|
|
|
|
|
2020-05-29 22:37:57 +01:00
|
|
|
s64 Stream::GetBufferReleaseNSHostTiming(const Buffer& buffer) const {
|
|
|
|
const std::size_t num_samples{buffer.GetSamples().size() / GetNumChannels()};
|
|
|
|
/// DSP signals before playing the last sample, in HLE we emulate this in this way
|
|
|
|
s64 base_samples = std::max<s64>(static_cast<s64>(num_samples) - 1, 0);
|
|
|
|
const auto ns =
|
|
|
|
std::chrono::nanoseconds((static_cast<u64>(base_samples) * 1000000000ULL) / sample_rate);
|
|
|
|
return ns.count();
|
|
|
|
}
|
|
|
|
|
2019-06-16 10:06:33 +01:00
|
|
|
static void VolumeAdjustSamples(std::vector<s16>& samples, float game_volume) {
|
2020-06-25 20:05:03 +01:00
|
|
|
const float volume{std::clamp(Settings::Volume() - (1.0f - game_volume), 0.0f, 1.0f)};
|
2018-07-31 04:57:53 +01:00
|
|
|
|
|
|
|
if (volume == 1.0f) {
|
2018-08-04 05:03:12 +01:00
|
|
|
return;
|
2018-07-31 04:57:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation of a volume slider with a dynamic range of 60 dB
|
2019-01-26 13:53:58 +00:00
|
|
|
const float volume_scale_factor = volume == 0 ? 0 : std::exp(6.90775f * volume) * 0.001f;
|
2018-07-31 04:57:53 +01:00
|
|
|
for (auto& sample : samples) {
|
|
|
|
sample = static_cast<s16>(sample * volume_scale_factor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 01:01:37 +01:00
|
|
|
void Stream::PlayNextBuffer() {
|
|
|
|
if (!IsPlaying()) {
|
|
|
|
// Ensure we are in playing state before playing the next buffer
|
2018-09-12 18:07:16 +01:00
|
|
|
sink_stream.Flush();
|
2018-07-27 01:01:37 +01: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 18:07:16 +01:00
|
|
|
sink_stream.Flush();
|
2018-07-27 01:01:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
active_buffer = queued_buffers.front();
|
|
|
|
queued_buffers.pop();
|
|
|
|
|
2019-06-16 10:06:33 +01:00
|
|
|
VolumeAdjustSamples(active_buffer->GetSamples(), game_volume);
|
2018-08-23 13:33:03 +01:00
|
|
|
|
2018-08-04 05:03:12 +01:00
|
|
|
sink_stream.EnqueueSamples(GetNumChannels(), active_buffer->GetSamples());
|
2018-07-28 18:44:50 +01:00
|
|
|
|
2020-05-29 22:37:57 +01:00
|
|
|
if (core_timing.IsHostTiming()) {
|
|
|
|
core_timing.ScheduleEvent(GetBufferReleaseNSHostTiming(*active_buffer), release_event, {});
|
|
|
|
} else {
|
|
|
|
core_timing.ScheduleEvent(GetBufferReleaseNS(*active_buffer), release_event, {});
|
|
|
|
}
|
2018-07-27 01:01:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::ReleaseActiveBuffer() {
|
2018-08-02 23:27:22 +01:00
|
|
|
ASSERT(active_buffer);
|
2018-07-27 01:01:37 +01:00
|
|
|
released_buffers.push(std::move(active_buffer));
|
|
|
|
release_callback();
|
|
|
|
PlayNextBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stream::QueueBuffer(BufferPtr&& buffer) {
|
|
|
|
if (queued_buffers.size() < MaxAudioBufferCount) {
|
|
|
|
queued_buffers.push(std::move(buffer));
|
|
|
|
PlayNextBuffer();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stream::ContainsBuffer(Buffer::Tag tag) const {
|
2018-12-28 19:04:44 +00:00
|
|
|
UNIMPLEMENTED();
|
2018-07-27 01:01:37 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-09-15 14:21:06 +01:00
|
|
|
std::vector<Buffer::Tag> Stream::GetTagsAndReleaseBuffers(std::size_t max_count) {
|
2018-07-27 01:01:37 +01:00
|
|
|
std::vector<Buffer::Tag> tags;
|
2018-09-15 14:21:06 +01:00
|
|
|
for (std::size_t count = 0; count < max_count && !released_buffers.empty(); ++count) {
|
2018-07-27 01:01:37 +01:00
|
|
|
tags.push_back(released_buffers.front()->GetTag());
|
|
|
|
released_buffers.pop();
|
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AudioCore
|