Fix raw sample clamping

This commit is contained in:
Dr. Chat 2015-05-28 22:31:55 -05:00
parent fb787d4737
commit d303f4a5ce
1 changed files with 5 additions and 8 deletions

View File

@ -200,15 +200,12 @@ int AudioDecoder::DecodePacket(uint8_t* output, size_t output_offset, size_t out
for (int i = 0; i < decoded_frame_->nb_samples; i++) { for (int i = 0; i < decoded_frame_->nb_samples; i++) {
// Raw sample should be within [-1, 1] // Raw sample should be within [-1, 1]
float fRawSample = sample_array[i]; float fRawSample = sample_array[i];
float fScaledSample = fRawSample * ((1 << bits_) - 1);
// Clamp the sample in range // Clamp it, just in case.
int64_t range = (1 << bits_) * 2; fRawSample = std::min( 1.f, fRawSample);
if (fScaledSample > (range - 1)) { fRawSample = std::max(-1.f, fRawSample);
fScaledSample = (float)range;
} else if (fScaledSample < (-range + 1)) { float fScaledSample = fRawSample * ((1 << bits_) - 1);
fScaledSample = (float)-range;
}
// Convert the sample and output it in big endian // Convert the sample and output it in big endian
int sample = (int)fScaledSample; int sample = (int)fScaledSample;