diff --git a/Source/Core/Core/IOS/USB/Emulated/Microphone.cpp b/Source/Core/Core/IOS/USB/Emulated/Microphone.cpp index 9ab0aaa0b2..be2bc57a42 100644 --- a/Source/Core/Core/IOS/USB/Emulated/Microphone.cpp +++ b/Source/Core/Core/IOS/USB/Emulated/Microphone.cpp @@ -47,7 +47,7 @@ void Microphone::StreamInit() { } -void Microphone::StreamStart() +void Microphone::StreamStart([[maybe_unused]] u32 sampling_rate) { } @@ -68,7 +68,7 @@ void Microphone::StreamInit() } // TODO: Not here but rather inside the WiiSpeak device if possible? - StreamStart(); + StreamStart(m_sampler.DEFAULT_SAMPLING_RATE); } void Microphone::StreamTerminate() @@ -83,12 +83,12 @@ static void StateCallback(cubeb_stream* stream, void* user_data, cubeb_state sta { } -void Microphone::StreamStart() +void Microphone::StreamStart(u32 sampling_rate) { if (!m_cubeb_ctx) return; - m_worker.Execute([this] { + m_worker.Execute([this, sampling_rate] { #ifdef ANDROID JNIEnv* env = IDCache::GetEnvForThread(); if (jboolean result = env->CallStaticBooleanMethod( @@ -104,7 +104,7 @@ void Microphone::StreamStart() cubeb_stream_params params{}; params.format = CUBEB_SAMPLE_S16LE; - params.rate = SAMPLING_RATE; + params.rate = sampling_rate; params.channels = 1; params.layout = CUBEB_LAYOUT_MONO; @@ -266,6 +266,12 @@ Microphone::FloatType Microphone::ComputeGain(FloatType relative_db) const return m_loudness.ComputeGain(relative_db); } +void Microphone::SetSamplingRate(u32 sampling_rate) +{ + StreamStop(); + StreamStart(sampling_rate); +} + const Microphone::FloatType Microphone::Loudness::DB_MIN = 20 * std::log10(FloatType(1) / MAX_AMPLITUDE); const Microphone::FloatType Microphone::Loudness::DB_MAX = 20 * std::log10(FloatType(1)); diff --git a/Source/Core/Core/IOS/USB/Emulated/Microphone.h b/Source/Core/Core/IOS/USB/Emulated/Microphone.h index 568bcd8f83..a40e6008aa 100644 --- a/Source/Core/Core/IOS/USB/Emulated/Microphone.h +++ b/Source/Core/Core/IOS/USB/Emulated/Microphone.h @@ -38,6 +38,7 @@ public: u16 ReadIntoBuffer(u8* ptr, u32 size); u16 GetLoudnessLevel() const; FloatType ComputeGain(FloatType relative_db) const; + void SetSamplingRate(u32 sampling_rate); private: #ifdef HAVE_CUBEB @@ -50,11 +51,10 @@ private: void StreamInit(); void StreamTerminate(); - void StreamStart(); + void StreamStart(u32 sampling_rate); void StreamStop(); - static constexpr u32 SAMPLING_RATE = 8000; - static constexpr u32 BUFF_SIZE_SAMPLES = 16; + static constexpr u32 BUFF_SIZE_SAMPLES = 32; static constexpr u32 STREAM_SIZE = BUFF_SIZE_SAMPLES * 500; std::array m_stream_buffer{}; @@ -83,8 +83,8 @@ private: void Reset(); void LogStats(); - // Samples used to compute the loudness level - static constexpr u16 SAMPLES_NEEDED = SAMPLING_RATE / 125; + // Samples used to compute the loudness level (arbitrarily chosen) + static constexpr u16 SAMPLES_NEEDED = 128; static_assert((SAMPLES_NEEDED % BUFF_SIZE_SAMPLES) == 0); static constexpr FloatType MAX_AMPLITUDE = diff --git a/Source/Core/Core/IOS/USB/Emulated/WiiSpeak.cpp b/Source/Core/Core/IOS/USB/Emulated/WiiSpeak.cpp index 1cea22c89b..686906fd55 100644 --- a/Source/Core/Core/IOS/USB/Emulated/WiiSpeak.cpp +++ b/Source/Core/Core/IOS/USB/Emulated/WiiSpeak.cpp @@ -250,8 +250,6 @@ void WiiSpeak::SetRegister(const std::unique_ptr& cmd) m_sampler.sample_on = !!arg1; break; case SAMPLER_FREQ: - WARN_LOG_FMT(IOS_USB, "Wii Speak SAMPLER_FREQ set (arg1={:04x}, arg2={:04x}) not implemented", - arg1, arg2); switch (arg1) { case FREQ_8KHZ: @@ -271,6 +269,8 @@ void WiiSpeak::SetRegister(const std::unique_ptr& cmd) m_sampler.freq = 16000; break; } + if (m_microphone) + m_microphone->SetSamplingRate(m_sampler.freq); break; case SAMPLER_GAIN: WARN_LOG_FMT(IOS_USB, "Wii Speak SAMPLER_GAIN set (arg1={:04x}, arg2={:04x}) not implemented", diff --git a/Source/Core/Core/IOS/USB/Emulated/WiiSpeak.h b/Source/Core/Core/IOS/USB/Emulated/WiiSpeak.h index 9d620a97b1..a58ddb4ffd 100644 --- a/Source/Core/Core/IOS/USB/Emulated/WiiSpeak.h +++ b/Source/Core/Core/IOS/USB/Emulated/WiiSpeak.h @@ -22,6 +22,8 @@ struct WiiSpeakState int gain; bool ec_reset; bool sp_on; + + static constexpr u32 DEFAULT_SAMPLING_RATE = 16000; }; class WiiSpeak final : public Device