1
0
Fork 0

Change resampling s16 -> u8 to respect aliasing rules

This commit is contained in:
James Rowe 2019-03-20 09:25:44 -06:00
parent 9739e2b6fd
commit b4d538468d
1 changed files with 9 additions and 3 deletions

View File

@ -111,13 +111,19 @@ long CubebInput::Impl::DataCallback(cubeb_stream* stream, void* user_data, const
return 0; return 0;
} }
constexpr auto resample_s16_s8 = [](s16 sample) {
return static_cast<u8>(static_cast<u16>(sample) >> 8);
};
std::vector<u8> samples{}; std::vector<u8> samples{};
samples.reserve(num_frames * impl->sample_size_in_bytes); samples.reserve(num_frames * impl->sample_size_in_bytes);
if (impl->sample_size_in_bytes == 1) { if (impl->sample_size_in_bytes == 1) {
// If the sample format is 8bit, then resample back to 8bit before passing back to core // If the sample format is 8bit, then resample back to 8bit before passing back to core
const s16* data = reinterpret_cast<const s16*>(input_buffer); for (std::size_t i; i < num_frames; i++) {
std::transform(data, data + num_frames, std::back_inserter(samples), s16 data;
[](s16 sample) { return static_cast<u8>(static_cast<u16>(sample) >> 8); }); std::memcpy(&data, static_cast<const u8*>(input_buffer) + i * 2, 2);
samples.push_back(resample_s16_s8(data));
}
} else { } else {
// Otherwise copy all of the samples to the buffer (which will be treated as s16 by core) // Otherwise copy all of the samples to the buffer (which will be treated as s16 by core)
const u8* data = reinterpret_cast<const u8*>(input_buffer); const u8* data = reinterpret_cast<const u8*>(input_buffer);