mic: Refactor microphone state and management. (#7134)

This commit is contained in:
Steveice10 2023-11-12 13:03:07 -08:00 committed by GitHub
parent 831c9c4a38
commit 5118798c30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 137 additions and 145 deletions

View File

@ -19,7 +19,7 @@ struct CubebInput::Impl {
cubeb* ctx = nullptr; cubeb* ctx = nullptr;
cubeb_stream* stream = nullptr; cubeb_stream* stream = nullptr;
std::unique_ptr<SampleQueue> sample_queue{}; SampleQueue sample_queue{};
u8 sample_size_in_bytes = 0; u8 sample_size_in_bytes = 0;
static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
@ -28,38 +28,34 @@ struct CubebInput::Impl {
}; };
CubebInput::CubebInput(std::string device_id) CubebInput::CubebInput(std::string device_id)
: impl(std::make_unique<Impl>()), device_id(std::move(device_id)) { : impl(std::make_unique<Impl>()), device_id(std::move(device_id)) {}
if (cubeb_init(&impl->ctx, "Citra Input", nullptr) != CUBEB_OK) {
LOG_ERROR(Audio, "cubeb_init failed! Mic will not work properly");
return;
}
impl->sample_queue = std::make_unique<SampleQueue>();
}
CubebInput::~CubebInput() { CubebInput::~CubebInput() {
if (impl->stream) { StopSampling();
if (cubeb_stream_stop(impl->stream) != CUBEB_OK) {
LOG_ERROR(Audio, "Error stopping cubeb input stream.");
}
cubeb_stream_destroy(impl->stream);
}
if (impl->ctx) {
cubeb_destroy(impl->ctx);
}
} }
void CubebInput::StartSampling(const InputParameters& params) { void CubebInput::StartSampling(const InputParameters& params) {
if (IsSampling()) {
return;
}
// Cubeb apparently only supports signed 16 bit PCM (and float32 which the 3ds doesn't support) // Cubeb apparently only supports signed 16 bit PCM (and float32 which the 3ds doesn't support)
// TODO: Resample the input stream. // TODO: Resample the input stream.
if (params.sign == Signedness::Unsigned) { if (params.sign == Signedness::Unsigned) {
LOG_ERROR(Audio, LOG_WARNING(
"Application requested unsupported unsigned pcm format. Falling back to signed."); Audio,
"Application requested unsupported unsigned pcm format. Falling back to signed.");
} }
parameters = params; parameters = params;
impl->sample_size_in_bytes = params.sample_size / 8; impl->sample_size_in_bytes = params.sample_size / 8;
auto init_result = cubeb_init(&impl->ctx, "Citra Input", nullptr);
if (init_result != CUBEB_OK) {
LOG_CRITICAL(Audio, "cubeb_init failed: {}", init_result);
return;
}
cubeb_devid input_device = nullptr; cubeb_devid input_device = nullptr;
if (device_id != auto_device_name && !device_id.empty()) { if (device_id != auto_device_name && !device_id.empty()) {
cubeb_device_collection collection; cubeb_device_collection collection;
@ -87,25 +83,28 @@ void CubebInput::StartSampling(const InputParameters& params) {
}; };
u32 latency_frames = 512; // Firefox default u32 latency_frames = 512; // Firefox default
if (cubeb_get_min_latency(impl->ctx, &input_params, &latency_frames) != CUBEB_OK) { auto latency_result = cubeb_get_min_latency(impl->ctx, &input_params, &latency_frames);
LOG_WARNING(Audio, "Error getting minimum input latency, falling back to default latency."); if (latency_result != CUBEB_OK) {
LOG_WARNING(
Audio, "cubeb_get_min_latency failed, falling back to default latency of {} frames: {}",
latency_frames, latency_result);
} }
if (cubeb_stream_init(impl->ctx, &impl->stream, "Citra Microphone", input_device, &input_params, auto stream_init_result = cubeb_stream_init(
nullptr, nullptr, latency_frames, Impl::DataCallback, Impl::StateCallback, impl->ctx, &impl->stream, "Citra Microphone", input_device, &input_params, nullptr, nullptr,
impl.get()) != CUBEB_OK) { latency_frames, Impl::DataCallback, Impl::StateCallback, impl.get());
LOG_CRITICAL(Audio, "Error creating cubeb input stream."); if (stream_init_result != CUBEB_OK) {
LOG_CRITICAL(Audio, "cubeb_stream_init failed: {}", stream_init_result);
StopSampling();
return; return;
} }
if (cubeb_stream_start(impl->stream) != CUBEB_OK) { auto start_result = cubeb_stream_start(impl->stream);
LOG_CRITICAL(Audio, "Error starting cubeb input stream."); if (start_result != CUBEB_OK) {
cubeb_stream_destroy(impl->stream); LOG_CRITICAL(Audio, "cubeb_stream_start failed: {}", start_result);
impl->stream = nullptr; StopSampling();
return; return;
} }
is_sampling = true;
} }
void CubebInput::StopSampling() { void CubebInput::StopSampling() {
@ -114,11 +113,18 @@ void CubebInput::StopSampling() {
cubeb_stream_destroy(impl->stream); cubeb_stream_destroy(impl->stream);
impl->stream = nullptr; impl->stream = nullptr;
} }
is_sampling = false; if (impl->ctx) {
cubeb_destroy(impl->ctx);
impl->ctx = nullptr;
}
}
bool CubebInput::IsSampling() {
return impl->ctx && impl->stream;
} }
void CubebInput::AdjustSampleRate(u32 sample_rate) { void CubebInput::AdjustSampleRate(u32 sample_rate) {
if (!is_sampling) { if (!IsSampling()) {
return; return;
} }
@ -129,9 +135,13 @@ void CubebInput::AdjustSampleRate(u32 sample_rate) {
} }
Samples CubebInput::Read() { Samples CubebInput::Read() {
if (!IsSampling()) {
return {};
}
Samples samples{}; Samples samples{};
Samples queue; Samples queue;
while (impl->sample_queue->Pop(queue)) { while (impl->sample_queue.Pop(queue)) {
samples.insert(samples.end(), queue.begin(), queue.end()); samples.insert(samples.end(), queue.begin(), queue.end());
} }
return samples; return samples;
@ -162,7 +172,7 @@ long CubebInput::Impl::DataCallback(cubeb_stream* stream, void* user_data, const
const u8* data = reinterpret_cast<const u8*>(input_buffer); const u8* data = reinterpret_cast<const u8*>(input_buffer);
samples.insert(samples.begin(), data, data + num_frames * impl->sample_size_in_bytes); samples.insert(samples.begin(), data, data + num_frames * impl->sample_size_in_bytes);
} }
impl->sample_queue->Push(samples); impl->sample_queue.Push(samples);
// returning less than num_frames here signals cubeb to stop sampling // returning less than num_frames here signals cubeb to stop sampling
return num_frames; return num_frames;

View File

@ -17,11 +17,9 @@ public:
~CubebInput() override; ~CubebInput() override;
void StartSampling(const InputParameters& params) override; void StartSampling(const InputParameters& params) override;
void StopSampling() override; void StopSampling() override;
bool IsSampling() override;
void AdjustSampleRate(u32 sample_rate) override; void AdjustSampleRate(u32 sample_rate) override;
Samples Read() override; Samples Read() override;
private: private:

View File

@ -37,12 +37,8 @@ public:
/// Stops the microphone. Called by Core /// Stops the microphone. Called by Core
virtual void StopSampling() = 0; virtual void StopSampling() = 0;
/** /// Checks whether the microphone is currently sampling.
* Called from the actual event timing at a constant period under a given sample rate. virtual bool IsSampling() = 0;
* When sampling is enabled this function is expected to return a buffer of 16 samples in ideal
* conditions, but can be lax if the data is coming in from another source like a real mic.
*/
virtual Samples Read() = 0;
/** /**
* Adjusts the Parameters. Implementations should update the parameters field in addition to * Adjusts the Parameters. Implementations should update the parameters field in addition to
@ -50,36 +46,15 @@ public:
*/ */
virtual void AdjustSampleRate(u32 sample_rate) = 0; virtual void AdjustSampleRate(u32 sample_rate) = 0;
/// Value from 0 - 100 to adjust the mic gain setting. Called by Core /**
virtual void SetGain(u8 mic_gain) { * Called from the actual event timing at a constant period under a given sample rate.
gain = mic_gain; * When sampling is enabled this function is expected to return a buffer of 16 samples in ideal
} * conditions, but can be lax if the data is coming in from another source like a real mic.
*/
u8 GetGain() const { virtual Samples Read() = 0;
return gain;
}
void SetPower(bool power) {
powered = power;
}
bool GetPower() const {
return powered;
}
bool IsSampling() const {
return is_sampling;
}
const InputParameters& GetParameters() const {
return parameters;
}
protected: protected:
InputParameters parameters; InputParameters parameters;
u8 gain = 0;
bool is_sampling = false;
bool powered = false;
}; };
} // namespace AudioCore } // namespace AudioCore

View File

@ -23,13 +23,18 @@ public:
is_sampling = false; is_sampling = false;
} }
void AdjustSampleRate(u32 sample_rate) override { bool IsSampling() override {
parameters.sample_rate = sample_rate; return is_sampling;
} }
void AdjustSampleRate(u32 sample_rate) override {}
Samples Read() override { Samples Read() override {
return {}; return {};
} }
private:
bool is_sampling = false;
}; };
} // namespace AudioCore } // namespace AudioCore

View File

@ -26,7 +26,7 @@ OpenALInput::~OpenALInput() {
} }
void OpenALInput::StartSampling(const InputParameters& params) { void OpenALInput::StartSampling(const InputParameters& params) {
if (is_sampling) { if (IsSampling()) {
return; return;
} }
@ -45,19 +45,20 @@ void OpenALInput::StartSampling(const InputParameters& params) {
impl->device = alcCaptureOpenDevice( impl->device = alcCaptureOpenDevice(
device_id != auto_device_name && !device_id.empty() ? device_id.c_str() : nullptr, device_id != auto_device_name && !device_id.empty() ? device_id.c_str() : nullptr,
params.sample_rate, format, static_cast<ALsizei>(params.buffer_size)); params.sample_rate, format, static_cast<ALsizei>(params.buffer_size));
if (!impl->device) { auto open_error = alcGetError(impl->device);
LOG_CRITICAL(Audio, "alcCaptureOpenDevice failed."); if (impl->device == nullptr || open_error != ALC_NO_ERROR) {
LOG_CRITICAL(Audio, "alcCaptureOpenDevice failed: {}", open_error);
StopSampling();
return; return;
} }
alcCaptureStart(impl->device); alcCaptureStart(impl->device);
auto error = alcGetError(impl->device); auto capture_error = alcGetError(impl->device);
if (error != ALC_NO_ERROR) { if (capture_error != ALC_NO_ERROR) {
LOG_CRITICAL(Audio, "alcCaptureStart failed: {}", error); LOG_CRITICAL(Audio, "alcCaptureStart failed: {}", capture_error);
StopSampling();
return; return;
} }
is_sampling = true;
} }
void OpenALInput::StopSampling() { void OpenALInput::StopSampling() {
@ -66,11 +67,14 @@ void OpenALInput::StopSampling() {
alcCaptureCloseDevice(impl->device); alcCaptureCloseDevice(impl->device);
impl->device = nullptr; impl->device = nullptr;
} }
is_sampling = false; }
bool OpenALInput::IsSampling() {
return impl->device != nullptr;
} }
void OpenALInput::AdjustSampleRate(u32 sample_rate) { void OpenALInput::AdjustSampleRate(u32 sample_rate) {
if (!is_sampling) { if (!IsSampling()) {
return; return;
} }
@ -81,7 +85,7 @@ void OpenALInput::AdjustSampleRate(u32 sample_rate) {
} }
Samples OpenALInput::Read() { Samples OpenALInput::Read() {
if (!is_sampling) { if (!IsSampling()) {
return {}; return {};
} }

View File

@ -17,11 +17,9 @@ public:
~OpenALInput() override; ~OpenALInput() override;
void StartSampling(const InputParameters& params) override; void StartSampling(const InputParameters& params) override;
void StopSampling() override; void StopSampling() override;
bool IsSampling() override;
void AdjustSampleRate(u32 sample_rate) override; void AdjustSampleRate(u32 sample_rate) override;
Samples Read() override; Samples Read() override;
private: private:

View File

@ -19,24 +19,4 @@ StaticInput::StaticInput()
: CACHE_8_BIT{NOISE_SAMPLE_8_BIT.begin(), NOISE_SAMPLE_8_BIT.end()}, : CACHE_8_BIT{NOISE_SAMPLE_8_BIT.begin(), NOISE_SAMPLE_8_BIT.end()},
CACHE_16_BIT{NOISE_SAMPLE_16_BIT.begin(), NOISE_SAMPLE_16_BIT.end()} {} CACHE_16_BIT{NOISE_SAMPLE_16_BIT.begin(), NOISE_SAMPLE_16_BIT.end()} {}
StaticInput::~StaticInput() = default;
void StaticInput::StartSampling(const InputParameters& params) {
sample_rate = params.sample_rate;
sample_size = params.sample_size;
parameters = params;
is_sampling = true;
}
void StaticInput::StopSampling() {
is_sampling = false;
}
void StaticInput::AdjustSampleRate(u32 sample_rate) {}
Samples StaticInput::Read() {
return (sample_size == 8) ? CACHE_8_BIT : CACHE_16_BIT;
}
} // namespace AudioCore } // namespace AudioCore

View File

@ -15,17 +15,29 @@ namespace AudioCore {
class StaticInput final : public Input { class StaticInput final : public Input {
public: public:
StaticInput(); StaticInput();
~StaticInput() override; ~StaticInput() = default;
void StartSampling(const InputParameters& params) override; void StartSampling(const InputParameters& params) {
void StopSampling() override; parameters = params;
void AdjustSampleRate(u32 sample_rate) override; is_sampling = true;
}
Samples Read() override; void StopSampling() {
is_sampling = false;
}
bool IsSampling() {
return is_sampling;
}
void AdjustSampleRate(u32 sample_rate) {}
Samples Read() {
return (parameters.sample_size == 8) ? CACHE_8_BIT : CACHE_16_BIT;
}
private: private:
u16 sample_rate = 0; bool is_sampling = false;
u8 sample_size = 0;
std::vector<u8> CACHE_8_BIT; std::vector<u8> CACHE_8_BIT;
std::vector<u8> CACHE_16_BIT; std::vector<u8> CACHE_16_BIT;
}; };

View File

@ -76,6 +76,8 @@ struct State {
u32 initial_offset = 0; u32 initial_offset = 0;
bool looped_buffer = false; bool looped_buffer = false;
u8 sample_size = 0; u8 sample_size = 0;
u8 gain = 0;
bool power = false;
SampleRate sample_rate = SampleRate::Rate16360; SampleRate sample_rate = SampleRate::Rate16360;
void WriteSamples(std::span<const u8> samples) { void WriteSamples(std::span<const u8> samples) {
@ -124,6 +126,8 @@ private:
ar& initial_offset; ar& initial_offset;
ar& looped_buffer; ar& looped_buffer;
ar& sample_size; ar& sample_size;
ar& gain;
ar& power;
ar& sample_rate; ar& sample_rate;
sharedmem_buffer = _memory_ref ? _memory_ref->GetPointer() : nullptr; sharedmem_buffer = _memory_ref ? _memory_ref->GetPointer() : nullptr;
} }
@ -167,13 +171,14 @@ struct MIC_U::Impl {
} }
void UpdateSharedMemBuffer(std::uintptr_t user_data, s64 cycles_late) { void UpdateSharedMemBuffer(std::uintptr_t user_data, s64 cycles_late) {
// If the event was scheduled before the application requested the mic to stop sampling
if (!mic || !mic->IsSampling()) {
return;
}
if (change_mic_impl_requested.exchange(false)) { if (change_mic_impl_requested.exchange(false)) {
CreateMic(); CreateMic();
} }
// If the event was scheduled before the application requested the mic to stop sampling
if (!mic->IsSampling()) {
return;
}
AudioCore::Samples samples = mic->Read(); AudioCore::Samples samples = mic->Read();
if (!samples.empty()) { if (!samples.empty()) {
@ -204,10 +209,11 @@ struct MIC_U::Impl {
u32 audio_buffer_size = rp.Pop<u32>(); u32 audio_buffer_size = rp.Pop<u32>();
bool audio_buffer_loop = rp.Pop<bool>(); bool audio_buffer_loop = rp.Pop<bool>();
if (mic->IsSampling()) { if (mic && mic->IsSampling()) {
LOG_CRITICAL(Service_MIC, LOG_CRITICAL(Service_MIC,
"Application started sampling again before stopping sampling"); "Application started sampling again before stopping sampling");
mic->StopSampling(); mic->StopSampling();
mic.reset();
} }
u8 sample_size = encoding == Encoding::PCM8Signed || encoding == Encoding::PCM8 ? 8 : 16; u8 sample_size = encoding == Encoding::PCM8Signed || encoding == Encoding::PCM8 ? 8 : 16;
@ -218,6 +224,7 @@ struct MIC_U::Impl {
state.looped_buffer = audio_buffer_loop; state.looped_buffer = audio_buffer_loop;
state.size = audio_buffer_size; state.size = audio_buffer_size;
CreateMic();
StartSampling(); StartSampling();
timing.ScheduleEvent(GetBufferUpdatePeriod(state.sample_rate), buffer_write_event); timing.ScheduleEvent(GetBufferUpdatePeriod(state.sample_rate), buffer_write_event);
@ -233,7 +240,10 @@ struct MIC_U::Impl {
void AdjustSampling(Kernel::HLERequestContext& ctx) { void AdjustSampling(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx); IPC::RequestParser rp(ctx);
SampleRate sample_rate = rp.PopEnum<SampleRate>(); SampleRate sample_rate = rp.PopEnum<SampleRate>();
mic->AdjustSampleRate(GetSampleRateInHz(sample_rate)); state.sample_rate = sample_rate;
if (mic) {
mic->AdjustSampleRate(GetSampleRateInHz(sample_rate));
}
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -245,8 +255,11 @@ struct MIC_U::Impl {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
mic->StopSampling();
timing.RemoveEvent(buffer_write_event); timing.RemoveEvent(buffer_write_event);
if (mic) {
mic->StopSampling();
mic.reset();
}
LOG_TRACE(Service_MIC, "called"); LOG_TRACE(Service_MIC, "called");
} }
@ -255,7 +268,7 @@ struct MIC_U::Impl {
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
bool is_sampling = mic->IsSampling(); bool is_sampling = mic && mic->IsSampling();
rb.Push<bool>(is_sampling); rb.Push<bool>(is_sampling);
LOG_TRACE(Service_MIC, "IsSampling: {}", is_sampling); LOG_TRACE(Service_MIC, "IsSampling: {}", is_sampling);
} }
@ -272,7 +285,7 @@ struct MIC_U::Impl {
void SetGain(Kernel::HLERequestContext& ctx) { void SetGain(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx); IPC::RequestParser rp(ctx);
u8 gain = rp.Pop<u8>(); u8 gain = rp.Pop<u8>();
mic->SetGain(gain); state.gain = gain;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -284,15 +297,14 @@ struct MIC_U::Impl {
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
u8 gain = mic->GetGain(); rb.Push<u8>(state.gain);
rb.Push<u8>(gain);
LOG_TRACE(Service_MIC, "gain={}", gain); LOG_TRACE(Service_MIC, "gain={}", gain);
} }
void SetPower(Kernel::HLERequestContext& ctx) { void SetPower(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx); IPC::RequestParser rp(ctx);
bool power = rp.Pop<bool>(); bool power = rp.Pop<bool>();
mic->SetPower(power); state.power = power;
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -304,8 +316,7 @@ struct MIC_U::Impl {
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
bool mic_power = mic->GetPower(); rb.Push<u8>(state.power);
rb.Push<u8>(mic_power);
LOG_TRACE(Service_MIC, "called"); LOG_TRACE(Service_MIC, "called");
} }
@ -358,21 +369,18 @@ struct MIC_U::Impl {
} }
void CreateMic() { void CreateMic() {
std::unique_ptr<AudioCore::Input> new_mic = AudioCore::CreateInputFromID( const auto was_sampling = mic && mic->IsSampling();
Settings::values.input_type.GetValue(), Settings::values.input_device.GetValue()); if (was_sampling) {
mic->StopSampling();
// If theres already a mic, copy over any data to the new mic impl mic.reset();
if (mic) { }
new_mic->SetGain(mic->GetGain());
new_mic->SetPower(mic->GetPower()); mic = AudioCore::CreateInputFromID(Settings::values.input_type.GetValue(),
auto params = mic->GetParameters(); Settings::values.input_device.GetValue());
if (mic->IsSampling()) { if (was_sampling) {
mic->StopSampling(); StartSampling();
new_mic->StartSampling(params);
}
} }
mic = std::move(new_mic);
change_mic_impl_requested.store(false); change_mic_impl_requested.store(false);
} }
@ -503,12 +511,14 @@ MIC_U::MIC_U(Core::System& system)
// clang-format on // clang-format on
}; };
impl->CreateMic();
RegisterHandlers(functions); RegisterHandlers(functions);
} }
MIC_U::~MIC_U() { MIC_U::~MIC_U() {
impl->mic->StopSampling(); if (impl->mic) {
impl->mic->StopSampling();
impl->mic.reset();
}
} }
void MIC_U::ReloadMic() { void MIC_U::ReloadMic() {

View File

@ -17,7 +17,7 @@ namespace Service::MIC {
class MIC_U final : public ServiceFramework<MIC_U> { class MIC_U final : public ServiceFramework<MIC_U> {
public: public:
explicit MIC_U(Core::System& system); explicit MIC_U(Core::System& system);
~MIC_U(); ~MIC_U() override;
void ReloadMic(); void ReloadMic();