From d4d5be87819307ba88819e86af41b5681c1ee8d7 Mon Sep 17 00:00:00 2001 From: "turaj@webrtc.org" Date: Thu, 20 Feb 2014 20:55:21 +0000 Subject: [PATCH] Minor improvement in RoundToInt16 implementation. R=andrew@webrtc.org, bjornv@webrtc.org Review URL: https://webrtc-codereview.appspot.com/8959004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@5586 4adac7df-926f-26a2-2b94-8c16560cd09d --- webrtc/common_audio/audio_util_unittest.cc | 13 ++++++++----- webrtc/common_audio/include/audio_util.h | 13 ++++++++++--- .../common_audio/resampler/push_sinc_resampler.cc | 3 +-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/webrtc/common_audio/audio_util_unittest.cc b/webrtc/common_audio/audio_util_unittest.cc index c724e378f..7e8dee389 100644 --- a/webrtc/common_audio/audio_util_unittest.cc +++ b/webrtc/common_audio/audio_util_unittest.cc @@ -27,11 +27,14 @@ TEST(AudioUtilTest, Clamp) { } TEST(AudioUtilTest, Round) { - EXPECT_EQ(0, RoundToInt16(0.f)); - EXPECT_EQ(0, RoundToInt16(0.4f)); - EXPECT_EQ(1, RoundToInt16(0.5f)); - EXPECT_EQ(0, RoundToInt16(-0.4f)); - EXPECT_EQ(-1, RoundToInt16(-0.5f)); + const int kSize = 7; + const float kInput[kSize] = { + 0.f, 0.4f, 0.5f, -0.4f, -0.5f, 32768.f, -32769.f}; + const int16_t kReference[kSize] = {0, 0, 1, 0, -1, 32767, -32768}; + int16_t output[kSize]; + RoundToInt16(kInput, kSize, output); + for (int n = 0; n < kSize; ++n) + EXPECT_EQ(kReference[n], output[n]); } TEST(AudioUtilTest, InterleavingStereo) { diff --git a/webrtc/common_audio/include/audio_util.h b/webrtc/common_audio/include/audio_util.h index 5e86c1f06..1e8f8d617 100644 --- a/webrtc/common_audio/include/audio_util.h +++ b/webrtc/common_audio/include/audio_util.h @@ -23,10 +23,17 @@ static inline float ClampInt16(float value) { (value > kMaxInt16 ? kMaxInt16 : value); } -// Return a rounded int16_t of the floating |value|. Doesn't handle overflow; -// use ClampInt16 if necessary. +// Round |value| to the closest int16. static inline int16_t RoundToInt16(float value) { - return static_cast(value < 0.f ? value - 0.5f : value + 0.5f); + return static_cast( + value > 0 ? (value >= 32766.5 ? 32767 : value + 0.5f) + : (value <= -32767.5 ? -32768 : value - 0.5f)); +} + +// Round |size| elements of |src| to the closest int16 and writes to |dest|. +static inline void RoundToInt16(const float* src, int size, int16_t* dest) { + for (int i = 0; i < size; ++i) + dest[i] = RoundToInt16(src[i]); } // Deinterleave audio from |interleaved| to the channel buffers pointed to diff --git a/webrtc/common_audio/resampler/push_sinc_resampler.cc b/webrtc/common_audio/resampler/push_sinc_resampler.cc index 1fb72dc76..cbf121042 100644 --- a/webrtc/common_audio/resampler/push_sinc_resampler.cc +++ b/webrtc/common_audio/resampler/push_sinc_resampler.cc @@ -57,8 +57,7 @@ int PushSincResampler::Resample(const int16_t* source, resampler_->Resample(resampler_->ChunkSize(), float_buffer_.get()); resampler_->Resample(destination_frames_, float_buffer_.get()); - for (int i = 0; i < destination_frames_; ++i) - destination[i] = RoundToInt16(ClampInt16(float_buffer_[i])); + RoundToInt16(float_buffer_.get(), destination_frames_, destination); source_ptr_ = NULL; return destination_frames_; }