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++) {
// Raw sample should be within [-1, 1]
float fRawSample = sample_array[i];
float fScaledSample = fRawSample * ((1 << bits_) - 1);
// Clamp the sample in range
int64_t range = (1 << bits_) * 2;
if (fScaledSample > (range - 1)) {
fScaledSample = (float)range;
} else if (fScaledSample < (-range + 1)) {
fScaledSample = (float)-range;
}
// Clamp it, just in case.
fRawSample = std::min( 1.f, fRawSample);
fRawSample = std::max(-1.f, fRawSample);
float fScaledSample = fRawSample * ((1 << bits_) - 1);
// Convert the sample and output it in big endian
int sample = (int)fScaledSample;