IOS/USB: Implement Wii Speak SAMPLER_FREQ register properly

Fix the default sampling rate which should be 16KHz
This commit is contained in:
Sepalani 2025-04-04 23:55:08 +04:00
parent 74a875e9d6
commit 4efbd35a5e
4 changed files with 20 additions and 12 deletions

View File

@ -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));

View File

@ -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<SampleType, STREAM_SIZE> 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 =

View File

@ -250,8 +250,6 @@ void WiiSpeak::SetRegister(const std::unique_ptr<CtrlMessage>& 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<CtrlMessage>& 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",

View File

@ -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