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
This commit is contained in:
turaj@webrtc.org 2014-02-20 20:55:21 +00:00
parent a0a6df3910
commit d4d5be8781
3 changed files with 19 additions and 10 deletions

View File

@ -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) {

View File

@ -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<int16_t>(value < 0.f ? value - 0.5f : value + 0.5f);
return static_cast<int16_t>(
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

View File

@ -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_;
}