2018-07-28 20:44:50 +03:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
2018-09-12 20:07:16 +03:00
|
|
|
#include <atomic>
|
2018-07-28 20:44:50 +03:00
|
|
|
#include <cstring>
|
|
|
|
#include "audio_core/cubeb_sink.h"
|
|
|
|
#include "audio_core/stream.h"
|
2018-09-08 18:49:04 +03:00
|
|
|
#include "audio_core/time_stretch.h"
|
2020-02-24 20:34:00 +03:00
|
|
|
#include "common/assert.h"
|
2018-07-28 20:44:50 +03:00
|
|
|
#include "common/logging/log.h"
|
2018-09-08 16:55:11 +03:00
|
|
|
#include "common/ring_buffer.h"
|
2018-09-08 18:49:04 +03:00
|
|
|
#include "core/settings.h"
|
2018-07-28 20:44:50 +03:00
|
|
|
|
2019-03-10 02:05:58 +03:00
|
|
|
#ifdef _WIN32
|
2019-02-28 00:07:06 +03:00
|
|
|
#include <objbase.h>
|
|
|
|
#endif
|
|
|
|
|
2018-07-28 20:44:50 +03:00
|
|
|
namespace AudioCore {
|
|
|
|
|
2018-09-08 16:55:11 +03:00
|
|
|
class CubebSinkStream final : public SinkStream {
|
2018-07-28 20:44:50 +03:00
|
|
|
public:
|
2018-09-08 16:55:11 +03:00
|
|
|
CubebSinkStream(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device,
|
|
|
|
const std::string& name)
|
2018-09-09 11:16:48 +03:00
|
|
|
: ctx{ctx}, num_channels{std::min(num_channels_, 2u)}, time_stretch{sample_rate,
|
|
|
|
num_channels} {
|
2018-08-03 21:50:53 +03:00
|
|
|
|
|
|
|
cubeb_stream_params params{};
|
|
|
|
params.rate = sample_rate;
|
|
|
|
params.channels = num_channels;
|
2018-07-28 20:44:50 +03:00
|
|
|
params.format = CUBEB_SAMPLE_S16NE;
|
2018-08-03 21:50:53 +03:00
|
|
|
params.layout = num_channels == 1 ? CUBEB_LAYOUT_MONO : CUBEB_LAYOUT_STEREO;
|
2018-07-28 20:44:50 +03:00
|
|
|
|
2018-08-03 21:50:53 +03:00
|
|
|
u32 minimum_latency{};
|
2018-07-28 20:44:50 +03:00
|
|
|
if (cubeb_get_min_latency(ctx, ¶ms, &minimum_latency) != CUBEB_OK) {
|
|
|
|
LOG_CRITICAL(Audio_Sink, "Error getting minimum latency");
|
|
|
|
}
|
|
|
|
|
2018-08-03 01:54:25 +03:00
|
|
|
if (cubeb_stream_init(ctx, &stream_backend, name.c_str(), nullptr, nullptr, output_device,
|
|
|
|
¶ms, std::max(512u, minimum_latency),
|
2018-09-08 16:55:11 +03:00
|
|
|
&CubebSinkStream::DataCallback, &CubebSinkStream::StateCallback,
|
2018-07-28 20:44:50 +03:00
|
|
|
this) != CUBEB_OK) {
|
|
|
|
LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cubeb_stream_start(stream_backend) != CUBEB_OK) {
|
|
|
|
LOG_CRITICAL(Audio_Sink, "Error starting cubeb stream");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:38:24 +03:00
|
|
|
~CubebSinkStream() override {
|
2018-07-28 20:44:50 +03:00
|
|
|
if (!ctx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cubeb_stream_stop(stream_backend) != CUBEB_OK) {
|
|
|
|
LOG_CRITICAL(Audio_Sink, "Error stopping cubeb stream");
|
|
|
|
}
|
|
|
|
|
|
|
|
cubeb_stream_destroy(stream_backend);
|
|
|
|
}
|
|
|
|
|
2018-09-09 11:16:48 +03:00
|
|
|
void EnqueueSamples(u32 source_num_channels, const std::vector<s16>& samples) override {
|
|
|
|
if (source_num_channels > num_channels) {
|
2018-07-28 20:44:50 +03:00
|
|
|
// Downsample 6 channels to 2
|
2020-02-24 20:34:00 +03:00
|
|
|
ASSERT_MSG(source_num_channels == 6, "Channel count must be 6");
|
|
|
|
|
2018-09-08 16:55:11 +03:00
|
|
|
std::vector<s16> buf;
|
2018-09-09 11:16:48 +03:00
|
|
|
buf.reserve(samples.size() * num_channels / source_num_channels);
|
2018-09-15 16:21:06 +03:00
|
|
|
for (std::size_t i = 0; i < samples.size(); i += source_num_channels) {
|
2020-02-24 20:34:00 +03:00
|
|
|
// Downmixing implementation taken from the ATSC standard
|
|
|
|
const s16 left{samples[i + 0]};
|
|
|
|
const s16 right{samples[i + 1]};
|
|
|
|
const s16 center{samples[i + 2]};
|
|
|
|
const s16 surround_left{samples[i + 4]};
|
|
|
|
const s16 surround_right{samples[i + 5]};
|
|
|
|
// Not used in the ATSC reference implementation
|
|
|
|
[[maybe_unused]] const s16 low_frequency_effects { samples[i + 3] };
|
|
|
|
|
|
|
|
constexpr s32 clev{707}; // center mixing level coefficient
|
|
|
|
constexpr s32 slev{707}; // surround mixing level coefficient
|
|
|
|
|
|
|
|
buf.push_back(left + (clev * center / 1000) + (slev * surround_left / 1000));
|
|
|
|
buf.push_back(right + (clev * center / 1000) + (slev * surround_right / 1000));
|
2018-07-28 20:44:50 +03:00
|
|
|
}
|
2018-09-08 16:55:11 +03:00
|
|
|
queue.Push(buf);
|
|
|
|
return;
|
2018-07-28 20:44:50 +03:00
|
|
|
}
|
2018-09-08 16:55:11 +03:00
|
|
|
|
|
|
|
queue.Push(samples);
|
2018-07-28 20:44:50 +03:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:28:48 +03:00
|
|
|
std::size_t SamplesInQueue(u32 channel_count) const override {
|
2018-08-23 15:33:03 +03:00
|
|
|
if (!ctx)
|
|
|
|
return 0;
|
|
|
|
|
2019-02-25 17:28:48 +03:00
|
|
|
return queue.Size() / channel_count;
|
2018-08-23 15:33:03 +03:00
|
|
|
}
|
|
|
|
|
2018-09-12 20:07:16 +03:00
|
|
|
void Flush() override {
|
|
|
|
should_flush = true;
|
|
|
|
}
|
|
|
|
|
2018-07-28 20:44:50 +03:00
|
|
|
u32 GetNumChannels() const {
|
2018-08-03 21:50:53 +03:00
|
|
|
return num_channels;
|
2018-07-28 20:44:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::string> device_list;
|
|
|
|
|
|
|
|
cubeb* ctx{};
|
|
|
|
cubeb_stream* stream_backend{};
|
2018-08-03 21:50:53 +03:00
|
|
|
u32 num_channels{};
|
2018-07-28 20:44:50 +03:00
|
|
|
|
2018-09-08 16:55:11 +03:00
|
|
|
Common::RingBuffer<s16, 0x10000> queue;
|
2019-02-25 17:40:34 +03:00
|
|
|
std::array<s16, 2> last_frame{};
|
2018-09-12 20:07:16 +03:00
|
|
|
std::atomic<bool> should_flush{};
|
2018-09-08 18:49:04 +03:00
|
|
|
TimeStretcher time_stretch;
|
2018-07-28 20:44:50 +03:00
|
|
|
|
|
|
|
static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
|
|
|
|
void* output_buffer, long num_frames);
|
|
|
|
static void StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state);
|
|
|
|
};
|
|
|
|
|
2018-12-14 00:23:31 +03:00
|
|
|
CubebSink::CubebSink(std::string_view target_device_name) {
|
2019-02-28 00:07:06 +03:00
|
|
|
// Cubeb requires COM to be initialized on the thread calling cubeb_init on Windows
|
2019-03-10 02:05:58 +03:00
|
|
|
#ifdef _WIN32
|
2019-02-28 00:07:06 +03:00
|
|
|
com_init_result = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
|
|
|
#endif
|
|
|
|
|
2018-07-28 20:44:50 +03:00
|
|
|
if (cubeb_init(&ctx, "yuzu", nullptr) != CUBEB_OK) {
|
|
|
|
LOG_CRITICAL(Audio_Sink, "cubeb_init failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target_device_name != auto_device_name && !target_device_name.empty()) {
|
|
|
|
cubeb_device_collection collection;
|
|
|
|
if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) != CUBEB_OK) {
|
|
|
|
LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported");
|
|
|
|
} else {
|
|
|
|
const auto collection_end{collection.device + collection.count};
|
2018-09-14 19:20:48 +03:00
|
|
|
const auto device{
|
|
|
|
std::find_if(collection.device, collection_end, [&](const cubeb_device_info& info) {
|
2018-10-26 07:32:41 +03:00
|
|
|
return info.friendly_name != nullptr &&
|
|
|
|
target_device_name == info.friendly_name;
|
2018-09-14 19:20:48 +03:00
|
|
|
})};
|
2018-07-28 20:44:50 +03:00
|
|
|
if (device != collection_end) {
|
|
|
|
output_device = device->devid;
|
|
|
|
}
|
|
|
|
cubeb_device_collection_destroy(ctx, &collection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CubebSink::~CubebSink() {
|
|
|
|
if (!ctx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& sink_stream : sink_streams) {
|
|
|
|
sink_stream.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
cubeb_destroy(ctx);
|
2019-02-28 00:07:06 +03:00
|
|
|
|
2019-03-10 02:05:58 +03:00
|
|
|
#ifdef _WIN32
|
2019-02-28 00:07:06 +03:00
|
|
|
if (SUCCEEDED(com_init_result)) {
|
|
|
|
CoUninitialize();
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-28 20:44:50 +03:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:54:25 +03:00
|
|
|
SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels,
|
|
|
|
const std::string& name) {
|
2018-08-03 21:50:53 +03:00
|
|
|
sink_streams.push_back(
|
2018-09-08 16:55:11 +03:00
|
|
|
std::make_unique<CubebSinkStream>(ctx, sample_rate, num_channels, output_device, name));
|
2018-07-28 20:44:50 +03:00
|
|
|
return *sink_streams.back();
|
|
|
|
}
|
|
|
|
|
2018-09-08 16:55:11 +03:00
|
|
|
long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
|
2018-09-08 18:49:04 +03:00
|
|
|
void* output_buffer, long num_frames) {
|
2018-09-08 16:55:11 +03:00
|
|
|
CubebSinkStream* impl = static_cast<CubebSinkStream*>(user_data);
|
2018-07-28 20:44:50 +03:00
|
|
|
u8* buffer = reinterpret_cast<u8*>(output_buffer);
|
|
|
|
|
|
|
|
if (!impl) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-09-15 16:21:06 +03:00
|
|
|
const std::size_t num_channels = impl->GetNumChannels();
|
|
|
|
const std::size_t samples_to_write = num_channels * num_frames;
|
|
|
|
std::size_t samples_written;
|
2018-09-08 18:49:04 +03:00
|
|
|
|
|
|
|
if (Settings::values.enable_audio_stretching) {
|
|
|
|
const std::vector<s16> in{impl->queue.Pop()};
|
2018-09-15 16:21:06 +03:00
|
|
|
const std::size_t num_in{in.size() / num_channels};
|
2018-09-08 18:49:04 +03:00
|
|
|
s16* const out{reinterpret_cast<s16*>(buffer)};
|
2018-09-15 16:21:06 +03:00
|
|
|
const std::size_t out_frames =
|
|
|
|
impl->time_stretch.Process(in.data(), num_in, out, num_frames);
|
2018-09-08 18:49:04 +03:00
|
|
|
samples_written = out_frames * num_channels;
|
2018-09-12 20:07:16 +03:00
|
|
|
|
|
|
|
if (impl->should_flush) {
|
|
|
|
impl->time_stretch.Flush();
|
|
|
|
impl->should_flush = false;
|
|
|
|
}
|
2018-09-08 18:49:04 +03:00
|
|
|
} else {
|
|
|
|
samples_written = impl->queue.Pop(buffer, samples_to_write);
|
|
|
|
}
|
2018-07-28 20:44:50 +03:00
|
|
|
|
2018-09-08 18:15:40 +03:00
|
|
|
if (samples_written >= num_channels) {
|
|
|
|
std::memcpy(&impl->last_frame[0], buffer + (samples_written - num_channels) * sizeof(s16),
|
|
|
|
num_channels * sizeof(s16));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill the rest of the frames with last_frame
|
2018-09-15 16:21:06 +03:00
|
|
|
for (std::size_t i = samples_written; i < samples_to_write; i += num_channels) {
|
2018-09-08 18:15:40 +03:00
|
|
|
std::memcpy(buffer + i * sizeof(s16), &impl->last_frame[0], num_channels * sizeof(s16));
|
2018-07-28 20:44:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return num_frames;
|
|
|
|
}
|
|
|
|
|
2018-09-08 16:55:11 +03:00
|
|
|
void CubebSinkStream::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {}
|
2018-07-28 20:44:50 +03:00
|
|
|
|
|
|
|
std::vector<std::string> ListCubebSinkDevices() {
|
|
|
|
std::vector<std::string> device_list;
|
|
|
|
cubeb* ctx;
|
|
|
|
|
2018-09-14 19:18:06 +03:00
|
|
|
if (cubeb_init(&ctx, "yuzu Device Enumerator", nullptr) != CUBEB_OK) {
|
2018-07-28 20:44:50 +03:00
|
|
|
LOG_CRITICAL(Audio_Sink, "cubeb_init failed");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
cubeb_device_collection collection;
|
|
|
|
if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) != CUBEB_OK) {
|
|
|
|
LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported");
|
|
|
|
} else {
|
2018-09-15 16:21:06 +03:00
|
|
|
for (std::size_t i = 0; i < collection.count; i++) {
|
2018-07-28 20:44:50 +03:00
|
|
|
const cubeb_device_info& device = collection.device[i];
|
|
|
|
if (device.friendly_name) {
|
|
|
|
device_list.emplace_back(device.friendly_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cubeb_device_collection_destroy(ctx, &collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
cubeb_destroy(ctx);
|
|
|
|
return device_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AudioCore
|