2018-07-27 00:01:37 +00:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-31 03:57:53 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
2018-07-27 00:01:37 +00:00
|
|
|
|
2018-07-28 17:44:50 +00:00
|
|
|
#include "audio_core/sink.h"
|
|
|
|
#include "audio_core/sink_details.h"
|
2018-09-14 16:06:00 +00:00
|
|
|
#include "audio_core/sink_stream.h"
|
2018-07-27 00:01:37 +00:00
|
|
|
#include "audio_core/stream.h"
|
2018-07-31 03:57:53 +00:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
2018-09-04 09:02:59 +00:00
|
|
|
#include "common/microprofile.h"
|
2018-07-31 03:57:53 +00:00
|
|
|
#include "core/core_timing.h"
|
|
|
|
#include "core/core_timing_util.h"
|
|
|
|
#include "core/settings.h"
|
2018-07-27 00:01:37 +00:00
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
constexpr std::size_t MaxAudioBufferCount{32};
|
2018-07-27 00:01:37 +00:00
|
|
|
|
2018-07-28 17:35:22 +00:00
|
|
|
u32 Stream::GetNumChannels() const {
|
2018-07-27 00:01:37 +00:00
|
|
|
switch (format) {
|
2018-07-28 17:35:22 +00:00
|
|
|
case Format::Mono16:
|
|
|
|
return 1;
|
|
|
|
case Format::Stereo16:
|
2018-07-27 00:01:37 +00:00
|
|
|
return 2;
|
2018-07-28 17:35:22 +00:00
|
|
|
case Format::Multi51Channel16:
|
|
|
|
return 6;
|
|
|
|
}
|
2018-07-27 00:01:37 +00:00
|
|
|
LOG_CRITICAL(Audio, "Unimplemented format={}", static_cast<u32>(format));
|
|
|
|
UNREACHABLE();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-07-28 17:44:50 +00:00
|
|
|
Stream::Stream(u32 sample_rate, Format format, ReleaseCallback&& release_callback,
|
2018-08-02 22:27:22 +00:00
|
|
|
SinkStream& sink_stream, std::string&& name_)
|
2018-07-28 17:44:50 +00:00
|
|
|
: sample_rate{sample_rate}, format{format}, release_callback{std::move(release_callback)},
|
2018-08-02 22:27:22 +00:00
|
|
|
sink_stream{sink_stream}, name{std::move(name_)} {
|
2018-07-28 17:44:50 +00:00
|
|
|
|
2018-07-27 00:01:37 +00:00
|
|
|
release_event = CoreTiming::RegisterEvent(
|
2018-08-02 22:27:22 +00:00
|
|
|
name, [this](u64 userdata, int cycles_late) { ReleaseActiveBuffer(); });
|
2018-07-27 00:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::Play() {
|
|
|
|
state = State::Playing;
|
|
|
|
PlayNextBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::Stop() {
|
2018-09-23 12:32:01 +00:00
|
|
|
state = State::Stopped;
|
2018-07-27 00:01:37 +00:00
|
|
|
ASSERT_MSG(false, "Unimplemented");
|
|
|
|
}
|
|
|
|
|
2018-09-23 12:32:01 +00:00
|
|
|
u32 Stream::GetState() const {
|
|
|
|
return static_cast<u32>(state);
|
|
|
|
}
|
|
|
|
|
2018-07-27 00:01:37 +00:00
|
|
|
s64 Stream::GetBufferReleaseCycles(const Buffer& buffer) const {
|
2018-09-15 13:21:06 +00:00
|
|
|
const std::size_t num_samples{buffer.GetSamples().size() / GetNumChannels()};
|
2018-07-27 00:01:37 +00:00
|
|
|
return CoreTiming::usToCycles((static_cast<u64>(num_samples) * 1000000) / sample_rate);
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:03:12 +00:00
|
|
|
static void VolumeAdjustSamples(std::vector<s16>& samples) {
|
2018-07-31 03:57:53 +00:00
|
|
|
const float volume{std::clamp(Settings::values.volume, 0.0f, 1.0f)};
|
|
|
|
|
|
|
|
if (volume == 1.0f) {
|
2018-08-04 04:03:12 +00:00
|
|
|
return;
|
2018-07-31 03:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation of a volume slider with a dynamic range of 60 dB
|
|
|
|
const float volume_scale_factor{std::exp(6.90775f * volume) * 0.001f};
|
|
|
|
for (auto& sample : samples) {
|
|
|
|
sample = static_cast<s16>(sample * volume_scale_factor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 00:01:37 +00:00
|
|
|
void Stream::PlayNextBuffer() {
|
|
|
|
if (!IsPlaying()) {
|
|
|
|
// Ensure we are in playing state before playing the next buffer
|
2018-09-12 17:07:16 +00:00
|
|
|
sink_stream.Flush();
|
2018-07-27 00:01:37 +00: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 17:07:16 +00:00
|
|
|
sink_stream.Flush();
|
2018-07-27 00:01:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
active_buffer = queued_buffers.front();
|
|
|
|
queued_buffers.pop();
|
|
|
|
|
2018-08-04 04:03:12 +00:00
|
|
|
VolumeAdjustSamples(active_buffer->Samples());
|
2018-08-23 12:33:03 +00:00
|
|
|
|
2018-08-04 04:03:12 +00:00
|
|
|
sink_stream.EnqueueSamples(GetNumChannels(), active_buffer->GetSamples());
|
2018-07-28 17:44:50 +00:00
|
|
|
|
2018-07-27 00:01:37 +00:00
|
|
|
CoreTiming::ScheduleEventThreadsafe(GetBufferReleaseCycles(*active_buffer), release_event, {});
|
|
|
|
}
|
|
|
|
|
2018-09-04 09:02:59 +00:00
|
|
|
MICROPROFILE_DEFINE(AudioOutput, "Audio", "ReleaseActiveBuffer", MP_RGB(100, 100, 255));
|
|
|
|
|
2018-07-27 00:01:37 +00:00
|
|
|
void Stream::ReleaseActiveBuffer() {
|
2018-09-04 09:02:59 +00:00
|
|
|
MICROPROFILE_SCOPE(AudioOutput);
|
2018-08-02 22:27:22 +00:00
|
|
|
ASSERT(active_buffer);
|
2018-07-27 00:01:37 +00: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 {
|
|
|
|
ASSERT_MSG(false, "Unimplemented");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::vector<Buffer::Tag> Stream::GetTagsAndReleaseBuffers(std::size_t max_count) {
|
2018-07-27 00:01:37 +00:00
|
|
|
std::vector<Buffer::Tag> tags;
|
2018-09-15 13:21:06 +00:00
|
|
|
for (std::size_t count = 0; count < max_count && !released_buffers.empty(); ++count) {
|
2018-07-27 00:01:37 +00:00
|
|
|
tags.push_back(released_buffers.front()->GetTag());
|
|
|
|
released_buffers.pop();
|
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AudioCore
|