Skip to content
Snippets Groups Projects
Commit f8ba6bc8 authored by Philippe Gorley's avatar Philippe Gorley Committed by Adrien Béraud
Browse files

audio: clip audio signal when mixing

Blindly adding both signals results in over/underflows which show up in
the signal as noise, avoid that by clamping the result between min and
max for s16 samples.

Change-Id: Ibbb42d87497c662b5ba9085d4c9efb6f1e45a183
parent 2f78cf3f
No related branches found
No related tags found
No related merge requests found
...@@ -244,8 +244,16 @@ size_t AudioBuffer::mix(const AudioBuffer& other, bool up /* = true */) ...@@ -244,8 +244,16 @@ size_t AudioBuffer::mix(const AudioBuffer& other, bool up /* = true */)
for (unsigned i = 0; i < chan_num; i++) { for (unsigned i = 0; i < chan_num; i++) {
unsigned src_chan = upmix ? std::min<unsigned>(i, other.samples_.size() - 1) : i; unsigned src_chan = upmix ? std::min<unsigned>(i, other.samples_.size() - 1) : i;
for (unsigned j = 0; j < samp_num; j++) for (unsigned j = 0; j < samp_num; j++) {
samples_[i][j] += other.samples_[src_chan][j]; // clamp result to min/max
// result must be larger than 16 bits to check for over/underflow
int32_t n = static_cast<int32_t>(samples_[i][j]) + static_cast<int32_t>(other.samples_[src_chan][j]);
if (n < std::numeric_limits<AudioSample>::min())
n = std::numeric_limits<AudioSample>::min();
else if (n > std::numeric_limits<AudioSample>::max())
n = std::numeric_limits<AudioSample>::max();
samples_[i][j] = n;
}
} }
return samp_num; return samp_num;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment