IOS/USB: Report Wii Speak packet size properly

This commit is contained in:
Sepalani 2024-05-14 01:52:52 +04:00
parent 69a315ecb5
commit 78f37429ac
3 changed files with 23 additions and 24 deletions

View file

@ -207,7 +207,7 @@ long Microphone::DataCallback(cubeb_stream* stream, void* user_data, const void*
return nframes; return nframes;
} }
void Microphone::ReadIntoBuffer(u8* dst, u32 size) u16 Microphone::ReadIntoBuffer(u8* ptr, u32 size)
{ {
static constexpr u32 SINGLE_READ_SIZE = BUFF_SIZE_SAMPLES * sizeof(SampleType); static constexpr u32 SINGLE_READ_SIZE = BUFF_SIZE_SAMPLES * sizeof(SampleType);
@ -217,22 +217,20 @@ void Microphone::ReadIntoBuffer(u8* dst, u32 size)
std::lock_guard lk(m_ring_lock); std::lock_guard lk(m_ring_lock);
for (u8* end = dst + size; dst < end; dst += SINGLE_READ_SIZE, size -= SINGLE_READ_SIZE) u8* begin = ptr;
for (u8* end = begin + size; ptr < end; ptr += SINGLE_READ_SIZE, size -= SINGLE_READ_SIZE)
{ {
if (size < SINGLE_READ_SIZE || m_samples_avail < BUFF_SIZE_SAMPLES) if (size < SINGLE_READ_SIZE || m_samples_avail < BUFF_SIZE_SAMPLES)
break; break;
SampleType* last_buffer = &m_stream_buffer[m_stream_rpos]; SampleType* last_buffer = &m_stream_buffer[m_stream_rpos];
std::memcpy(dst, last_buffer, SINGLE_READ_SIZE); std::memcpy(ptr, last_buffer, SINGLE_READ_SIZE);
m_samples_avail -= BUFF_SIZE_SAMPLES; m_samples_avail -= BUFF_SIZE_SAMPLES;
m_stream_rpos += BUFF_SIZE_SAMPLES; m_stream_rpos += BUFF_SIZE_SAMPLES;
m_stream_rpos %= STREAM_SIZE; m_stream_rpos %= STREAM_SIZE;
} }
if (size != 0) return static_cast<u16>(ptr - begin);
{
std::memset(dst, 0, size);
}
} }
bool Microphone::HasData() const bool Microphone::HasData() const

View file

@ -24,7 +24,7 @@ public:
~Microphone(); ~Microphone();
bool HasData() const; bool HasData() const;
void ReadIntoBuffer(u8* dst, u32 size); u16 ReadIntoBuffer(u8* ptr, u32 size);
const WiiSpeakState& GetSampler() const; const WiiSpeakState& GetSampler() const;
private: private:

View file

@ -3,6 +3,8 @@
#include "Core/IOS/USB/Emulated/WiiSpeak.h" #include "Core/IOS/USB/Emulated/WiiSpeak.h"
#include <algorithm>
#include "Core/Config/MainSettings.h" #include "Core/Config/MainSettings.h"
#include "Core/HW/Memmap.h" #include "Core/HW/Memmap.h"
#include "Core/System.h" #include "Core/System.h"
@ -177,10 +179,18 @@ int WiiSpeak::SubmitTransfer(std::unique_ptr<IsoMessage> cmd)
switch (cmd->endpoint) switch (cmd->endpoint)
{ {
case ENDPOINT_AUDIO_IN: case ENDPOINT_AUDIO_IN:
{
// Transfer: Wii Speak -> Wii // Transfer: Wii Speak -> Wii
u16 size = 0;
if (m_microphone && m_microphone->HasData()) if (m_microphone && m_microphone->HasData())
m_microphone->ReadIntoBuffer(packets, cmd->length); size = m_microphone->ReadIntoBuffer(packets, cmd->length);
for (std::size_t i = 0; i < cmd->num_packets; i++)
{
cmd->SetPacketReturnValue(i, std::min(size, cmd->packet_sizes[i]));
size = (size > cmd->packet_sizes[i]) ? (size - cmd->packet_sizes[i]) : 0;
}
break; break;
}
case ENDPOINT_AUDIO_OUT: case ENDPOINT_AUDIO_OUT:
// Transfer: Wii -> Wii Speak // Transfer: Wii -> Wii Speak
break; break;
@ -203,22 +213,13 @@ int WiiSpeak::SubmitTransfer(std::unique_ptr<IsoMessage> cmd)
// (i.e. 128 samples in 16-bit mono) per frame transfer. The Microphone class is using cubeb // (i.e. 128 samples in 16-bit mono) per frame transfer. The Microphone class is using cubeb
// configured with a sample rate of 8000. // configured with a sample rate of 8000.
// //
// Based on these numbers, here are some theoretical speeds: // Based on USB sniffing using Wireshark + Dolphin USB passthrough:
// - fastest transfer speed would be 8000 samples in 63 ms (i.e. 8000 * 1/128 = 62.5) // - 125 frames are received per second (i.e. timing 8 ms per frame)
// * using a timing of 1 ms per frame of 128 samples // - however, the cmd->length=0x80 which doesn't match the HLE emulation
// * here cubeb sample rate is the bottleneck // - each frame having 8 packets of 0x10 bytes
// - slowest transfer speed would be 8000 samples in 1000 ms (i.e. 128 * 1000/8000 = 16)
// * using a timing of 16 ms per frame of 128 samples
// * matching cubeb sampling rate
// //
// A decent timing would be 16ms. However, it seems that the Wii Speak Channel record feature is // Let's sample at a reasonable speed.
// broken if the timing is less than 32ms and that the record playback is broken when around 34ms const u32 transfer_timing = 2000;
// and above. Monster Hunter 3 doesn't seem affected by this timing issue.
//
// TODO: Investigate and ensure that's the proper way to fix it.
// Maybe it's related to the Wii DSP and its stereo/mono mode?
// If so, what would be the impact of using DSP LLE/HLE in mono/stereo?
const u32 transfer_timing = 32000;
cmd->ScheduleTransferCompletion(IPC_SUCCESS, transfer_timing); cmd->ScheduleTransferCompletion(IPC_SUCCESS, transfer_timing);
return IPC_SUCCESS; return IPC_SUCCESS;
} }