2017-03-22 17:09:59 -06:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-03-22 17:09:59 -06:00
|
|
|
|
|
|
|
#include <cubeb/cubeb.h>
|
|
|
|
|
|
|
|
#include "AudioCommon/CubebStream.h"
|
2017-04-24 00:55:37 -06:00
|
|
|
#include "AudioCommon/CubebUtils.h"
|
2017-03-22 17:09:59 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/Thread.h"
|
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
|
|
|
|
// ~10 ms - needs to be at least 240 for surround
|
|
|
|
constexpr u32 BUFFER_SAMPLES = 512;
|
|
|
|
|
|
|
|
long CubebStream::DataCallback(cubeb_stream* stream, void* user_data, const void* /*input_buffer*/,
|
|
|
|
void* output_buffer, long num_frames)
|
|
|
|
{
|
|
|
|
auto* self = static_cast<CubebStream*>(user_data);
|
|
|
|
|
|
|
|
if (self->m_stereo)
|
|
|
|
self->m_mixer->Mix(static_cast<short*>(output_buffer), num_frames);
|
|
|
|
else
|
2017-04-23 19:05:21 -06:00
|
|
|
self->m_mixer->MixSurround(static_cast<float*>(output_buffer), num_frames);
|
2017-03-22 17:09:59 -06:00
|
|
|
|
|
|
|
return num_frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CubebStream::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-10-21 17:23:40 -06:00
|
|
|
bool CubebStream::Init()
|
2017-03-22 17:09:59 -06:00
|
|
|
{
|
2017-04-24 00:55:37 -06:00
|
|
|
m_ctx = CubebUtils::GetContext();
|
|
|
|
if (!m_ctx)
|
2017-03-22 17:09:59 -06:00
|
|
|
return false;
|
|
|
|
|
2019-11-11 04:03:36 -07:00
|
|
|
m_stereo = !SConfig::GetInstance().ShouldUseDPL2Decoder();
|
2017-03-22 17:09:59 -06:00
|
|
|
|
|
|
|
cubeb_stream_params params;
|
|
|
|
params.rate = m_mixer->GetSampleRate();
|
|
|
|
if (m_stereo)
|
|
|
|
{
|
|
|
|
params.channels = 2;
|
|
|
|
params.format = CUBEB_SAMPLE_S16NE;
|
|
|
|
params.layout = CUBEB_LAYOUT_STEREO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
params.channels = 6;
|
|
|
|
params.format = CUBEB_SAMPLE_FLOAT32NE;
|
|
|
|
params.layout = CUBEB_LAYOUT_3F2_LFE;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 minimum_latency = 0;
|
2017-10-21 15:13:53 -06:00
|
|
|
if (cubeb_get_min_latency(m_ctx.get(), ¶ms, &minimum_latency) != CUBEB_OK)
|
2020-10-21 11:32:25 -06:00
|
|
|
ERROR_LOG_FMT(AUDIO, "Error getting minimum latency");
|
|
|
|
INFO_LOG_FMT(AUDIO, "Minimum latency: {} frames", minimum_latency);
|
2017-03-22 17:09:59 -06:00
|
|
|
|
2017-10-21 17:23:40 -06:00
|
|
|
return cubeb_stream_init(m_ctx.get(), &m_stream, "Dolphin Audio Output", nullptr, nullptr,
|
|
|
|
nullptr, ¶ms, std::max(BUFFER_SAMPLES, minimum_latency),
|
|
|
|
DataCallback, StateCallback, this) == CUBEB_OK;
|
2017-03-22 17:09:59 -06:00
|
|
|
}
|
|
|
|
|
2017-10-21 17:23:40 -06:00
|
|
|
bool CubebStream::SetRunning(bool running)
|
2017-03-22 17:09:59 -06:00
|
|
|
{
|
2017-10-21 13:49:34 -06:00
|
|
|
if (running)
|
2017-10-21 17:23:40 -06:00
|
|
|
return cubeb_stream_start(m_stream) == CUBEB_OK;
|
2017-10-21 13:49:34 -06:00
|
|
|
else
|
2017-10-21 17:23:40 -06:00
|
|
|
return cubeb_stream_stop(m_stream) == CUBEB_OK;
|
2017-10-21 13:49:34 -06:00
|
|
|
}
|
|
|
|
|
2017-10-21 17:23:40 -06:00
|
|
|
CubebStream::~CubebStream()
|
2017-10-21 13:49:34 -06:00
|
|
|
{
|
|
|
|
SetRunning(false);
|
2017-03-22 17:09:59 -06:00
|
|
|
cubeb_stream_destroy(m_stream);
|
2017-04-24 00:55:37 -06:00
|
|
|
m_ctx.reset();
|
2017-03-22 17:09:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void CubebStream::SetVolume(int volume)
|
|
|
|
{
|
|
|
|
cubeb_stream_set_volume(m_stream, volume / 100.0f);
|
|
|
|
}
|