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 215af25372
commit a3747ef5b0
4 changed files with 19 additions and 11 deletions

View File

@ -44,7 +44,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()
@ -59,12 +59,12 @@ static void state_callback(cubeb_stream* stream, void* user_data, cubeb_state st
{
}
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(
@ -80,7 +80,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;
@ -248,6 +248,12 @@ Microphone::FloatType Microphone::ComputeGain(FloatType relative_db) const
return m_loudness.ComputeGain(relative_db);
}
void Microphone::SetSamplingRate(u32 sampling_rate)
{
StopStream();
StreamStart(sampling_rate);
}
const Microphone::FloatType Microphone::Loudness::DB_MIN =
20 * std::log10(FloatType(1) / MAX_AMPLTIUDE);
const Microphone::FloatType Microphone::Loudness::DB_MAX = 20 * std::log10(FloatType(1));

View File

@ -37,6 +37,7 @@ public:
void UpdateLoudness(std::ranges::input_range auto&& samples);
const WiiSpeakState& GetSampler() const;
FloatType ComputeGain(FloatType relative_db) const;
void SetSamplingRate(u32 sampling_rate);
private:
static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
@ -44,12 +45,11 @@ private:
void StreamInit();
void StreamTerminate();
void StreamStart();
void StreamStart(u32 sampling_rate);
void StopStream();
static constexpr u32 SAMPLING_RATE = 8000;
using SampleType = s16;
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{};
@ -96,8 +96,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_AMPLTIUDE = std::numeric_limits<UnsignedSampleType>::max() / 2;

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

@ -21,6 +21,8 @@ struct WiiSpeakState
int gain;
bool ec_reset;
bool sp_on;
static constexpr u32 DEFAULT_SAMPLING_RATE = 16000;
};
class WiiSpeak final : public Device