1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-25 20:22:30 +01:00

cellMic: add 16-bit PCM to 32-bit float conversion for DSP stream (#16030)

This commit is contained in:
snake 2024-09-03 23:25:24 +02:00 committed by GitHub
parent 2fb0c499ea
commit ad1eb93a05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 2 deletions

View File

@ -258,6 +258,29 @@ inline void microphone_device::variable_byteswap(const void* src, void* dst)
}
}
inline u32 microphone_device::convert_16_bit_pcm_to_float(const std::vector<u8>& buffer, u32 num_bytes)
{
static_assert((float_buf_size % sizeof(f32)) == 0);
float_buf.resize(float_buf_size, 0);
const u32 bytes_to_write = static_cast<u32>(num_bytes * (sizeof(f32) / sizeof(s16)));
ensure(bytes_to_write <= float_buf.size());
const be_t<s16>* src = utils::bless<const be_t<s16>>(buffer.data());
be_t<f32>* dst = reinterpret_cast<be_t<f32>*>(float_buf.data());
for (usz i = 0; i < num_bytes / sizeof(s16); i++)
{
const be_t<s16> sample = *src++;
const be_t<f32> normalized_sample_be = std::clamp(static_cast<f32>(sample) / std::numeric_limits<s16>::max(), -1.0f, 1.0f);
*dst++ = normalized_sample_be;
}
return bytes_to_write;
}
// Public functions
microphone_device::microphone_device(microphone_handler type)
@ -696,7 +719,17 @@ void microphone_device::get_dsp(const u32 num_samples)
const u32 bufsize = num_samples * sample_size;
ensure(bufsize <= buf.size());
rbuf_dsp.write_bytes(buf.data(), bufsize);
if (attr_dsptype != 0x01)
{
// Convert 16-bit PCM audio data to 32-bit float (DSP format)
const u32 bufsize_float = convert_16_bit_pcm_to_float(buf, bufsize);
rbuf_dsp.write_bytes(float_buf.data(), bufsize_float);
}
else
{
// The same as device RAW stream format, except that the data byte is always big-endian
rbuf_dsp.write_bytes(buf.data(), bufsize);
}
}
/// Initialization/Shutdown Functions

View File

@ -323,6 +323,7 @@ public:
private:
template <u32 bytesize>
static inline void variable_byteswap(const void* src, void* dst);
inline u32 convert_16_bit_pcm_to_float(const std::vector<u8>& buffer, u32 num_bytes);
u32 capture_audio();
@ -345,6 +346,7 @@ private:
std::vector<mic_device> devices;
std::vector<u8> temp_buf;
std::vector<u8> float_buf;
// Sampling information provided at opening of mic
u32 raw_samplingrate = 48000;
@ -358,9 +360,10 @@ private:
static constexpr u8 bit_resolution = 16;
static constexpr usz inbuf_size = 400000; // Default value unknown
static constexpr usz float_buf_size = inbuf_size * (sizeof(f32) / sizeof(s16));
simple_ringbuf<inbuf_size> rbuf_raw;
simple_ringbuf<inbuf_size> rbuf_dsp;
simple_ringbuf<float_buf_size> rbuf_dsp;
simple_ringbuf<inbuf_size> rbuf_aux;
};