1
0
Fork 0

mic_u: Fix up logic for populating shared memory sample buffer. (#6669)

This commit is contained in:
Steveice10 2023-07-07 19:02:31 -07:00 committed by GitHub
parent e5310b25d4
commit cd3244f139
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 15 deletions

View File

@ -80,23 +80,29 @@ struct State {
void WriteSamples(std::span<const u8> samples) { void WriteSamples(std::span<const u8> samples) {
u32 bytes_total_written = 0; u32 bytes_total_written = 0;
const std::size_t remaining_space = size - offset; auto sample_buffer = sharedmem_buffer + initial_offset;
std::size_t bytes_to_write = std::min(samples.size(), remaining_space); // Do not let sampling buffer overrun shared memory space.
const auto sample_buffer_size =
std::min(size, sharedmem_size - initial_offset - sizeof(u32));
// Write as many samples as we can to the buffer. // Write samples in a loop until the input runs out
// TODO if the sample size is 16bit, this could theoretically cut a sample in the case where while (samples.size() > bytes_total_written) {
// the application configures an odd size // TODO: If the sample size is 16-bit, this could theoretically cut a sample in the case
std::memcpy(sharedmem_buffer + offset, samples.data(), bytes_to_write); // where the application configures an odd size.
std::size_t bytes_to_write =
std::min(samples.size() - bytes_total_written, sample_buffer_size - offset);
std::memcpy(sample_buffer + offset, samples.data() + bytes_total_written,
bytes_to_write);
offset += static_cast<u32>(bytes_to_write); offset += static_cast<u32>(bytes_to_write);
bytes_total_written += static_cast<u32>(bytes_to_write); bytes_total_written += static_cast<u32>(bytes_to_write);
// If theres any samples left to write after we looped, go ahead and write them now if (offset >= sample_buffer_size && looped_buffer) {
if (looped_buffer && samples.size() > bytes_total_written) { offset = 0;
offset = initial_offset; }
bytes_to_write = std::min(samples.size() - bytes_total_written, size);
std::memcpy(sharedmem_buffer + offset, samples.data() + bytes_total_written, if (!looped_buffer) {
bytes_to_write); break;
offset += static_cast<u32>(bytes_to_write); }
} }
// The last 4 bytes of the shared memory contains the latest offset // The last 4 bytes of the shared memory contains the latest offset
@ -205,7 +211,8 @@ struct MIC_U::Impl {
} }
u8 sample_size = encoding == Encoding::PCM8Signed || encoding == Encoding::PCM8 ? 8 : 16; u8 sample_size = encoding == Encoding::PCM8Signed || encoding == Encoding::PCM8 ? 8 : 16;
state.offset = state.initial_offset = audio_buffer_offset; state.offset = 0;
state.initial_offset = audio_buffer_offset;
state.sample_rate = sample_rate; state.sample_rate = sample_rate;
state.sample_size = sample_size; state.sample_size = sample_size;
state.looped_buffer = audio_buffer_loop; state.looped_buffer = audio_buffer_loop;