| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- // Copyright 2018 yuzu Emulator Project
- // Licensed under GPLv2 or any later version
- // Refer to the license.txt file included.
- #include <algorithm>
- #include <atomic>
- #include <cstring>
- #include "audio_core/cubeb_sink.h"
- #include "audio_core/stream.h"
- #include "audio_core/time_stretch.h"
- #include "common/assert.h"
- #include "common/logging/log.h"
- #include "common/ring_buffer.h"
- #include "core/settings.h"
- #ifdef _WIN32
- #include <objbase.h>
- #endif
- namespace AudioCore {
- class CubebSinkStream final : public SinkStream {
- public:
- CubebSinkStream(cubeb* ctx_, u32 sample_rate, u32 num_channels_, cubeb_devid output_device,
- const std::string& name)
- : ctx{ctx_}, num_channels{std::min(num_channels_, 6u)}, time_stretch{sample_rate,
- num_channels} {
- cubeb_stream_params params{};
- params.rate = sample_rate;
- params.channels = num_channels;
- params.format = CUBEB_SAMPLE_S16NE;
- params.prefs = CUBEB_STREAM_PREF_PERSIST;
- switch (num_channels) {
- case 1:
- params.layout = CUBEB_LAYOUT_MONO;
- break;
- case 2:
- params.layout = CUBEB_LAYOUT_STEREO;
- break;
- case 6:
- params.layout = CUBEB_LAYOUT_3F2_LFE;
- break;
- }
- u32 minimum_latency{};
- if (cubeb_get_min_latency(ctx, ¶ms, &minimum_latency) != CUBEB_OK) {
- LOG_CRITICAL(Audio_Sink, "Error getting minimum latency");
- }
- if (cubeb_stream_init(ctx, &stream_backend, name.c_str(), nullptr, nullptr, output_device,
- ¶ms, std::max(512u, minimum_latency),
- &CubebSinkStream::DataCallback, &CubebSinkStream::StateCallback,
- 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;
- }
- }
- ~CubebSinkStream() override {
- if (!ctx) {
- return;
- }
- if (cubeb_stream_stop(stream_backend) != CUBEB_OK) {
- LOG_CRITICAL(Audio_Sink, "Error stopping cubeb stream");
- }
- cubeb_stream_destroy(stream_backend);
- }
- void EnqueueSamples(u32 source_num_channels, const std::vector<s16>& samples) override {
- if (source_num_channels > num_channels) {
- // Downsample 6 channels to 2
- ASSERT_MSG(source_num_channels == 6, "Channel count must be 6");
- std::vector<s16> buf;
- buf.reserve(samples.size() * num_channels / source_num_channels);
- for (std::size_t i = 0; i < samples.size(); i += source_num_channels) {
- // 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(static_cast<s16>(left + (clev * center / 1000) +
- (slev * surround_left / 1000)));
- buf.push_back(static_cast<s16>(right + (clev * center / 1000) +
- (slev * surround_right / 1000)));
- }
- queue.Push(buf);
- return;
- }
- queue.Push(samples);
- }
- std::size_t SamplesInQueue(u32 channel_count) const override {
- if (!ctx)
- return 0;
- return queue.Size() / channel_count;
- }
- void Flush() override {
- should_flush = true;
- }
- u32 GetNumChannels() const {
- return num_channels;
- }
- private:
- std::vector<std::string> device_list;
- cubeb* ctx{};
- cubeb_stream* stream_backend{};
- u32 num_channels{};
- Common::RingBuffer<s16, 0x10000> queue;
- std::array<s16, 2> last_frame{};
- std::atomic<bool> should_flush{};
- TimeStretcher time_stretch;
- 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);
- };
- CubebSink::CubebSink(std::string_view target_device_name) {
- // Cubeb requires COM to be initialized on the thread calling cubeb_init on Windows
- #ifdef _WIN32
- com_init_result = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
- #endif
- 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};
- const auto device{
- std::find_if(collection.device, collection_end, [&](const cubeb_device_info& info) {
- return info.friendly_name != nullptr &&
- target_device_name == info.friendly_name;
- })};
- 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);
- #ifdef _WIN32
- if (SUCCEEDED(com_init_result)) {
- CoUninitialize();
- }
- #endif
- }
- SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels,
- const std::string& name) {
- sink_streams.push_back(
- std::make_unique<CubebSinkStream>(ctx, sample_rate, num_channels, output_device, name));
- return *sink_streams.back();
- }
- long CubebSinkStream::DataCallback([[maybe_unused]] cubeb_stream* stream, void* user_data,
- [[maybe_unused]] const void* input_buffer, void* output_buffer,
- long num_frames) {
- auto* impl = static_cast<CubebSinkStream*>(user_data);
- auto* buffer = static_cast<u8*>(output_buffer);
- if (!impl) {
- return {};
- }
- const std::size_t num_channels = impl->GetNumChannels();
- const std::size_t samples_to_write = num_channels * num_frames;
- std::size_t samples_written;
- /*
- if (Settings::values.enable_audio_stretching.GetValue()) {
- const std::vector<s16> in{impl->queue.Pop()};
- const std::size_t num_in{in.size() / num_channels};
- s16* const out{reinterpret_cast<s16*>(buffer)};
- const std::size_t out_frames =
- impl->time_stretch.Process(in.data(), num_in, out, num_frames);
- samples_written = out_frames * num_channels;
- if (impl->should_flush) {
- impl->time_stretch.Flush();
- impl->should_flush = false;
- }
- } else {
- samples_written = impl->queue.Pop(buffer, samples_to_write);
- }*/
- samples_written = impl->queue.Pop(buffer, samples_to_write);
- 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
- for (std::size_t i = samples_written; i < samples_to_write; i += num_channels) {
- std::memcpy(buffer + i * sizeof(s16), &impl->last_frame[0], num_channels * sizeof(s16));
- }
- return num_frames;
- }
- void CubebSinkStream::StateCallback([[maybe_unused]] cubeb_stream* stream,
- [[maybe_unused]] void* user_data,
- [[maybe_unused]] cubeb_state state) {}
- std::vector<std::string> ListCubebSinkDevices() {
- std::vector<std::string> device_list;
- cubeb* ctx;
- if (cubeb_init(&ctx, "yuzu Device Enumerator", nullptr) != CUBEB_OK) {
- 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 {
- for (std::size_t i = 0; i < collection.count; i++) {
- 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
|