Home | History | Annotate | Download | only in agc
      1 /*
      2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 // Use CreateHistUnittestFile.m to generate the input file.
     12 
     13 #include "webrtc/modules/audio_processing/agc/histogram.h"
     14 
     15 #include <stdio.h>
     16 #include <cmath>
     17 
     18 #include "gtest/gtest.h"
     19 #include "webrtc/test/testsupport/fileutils.h"
     20 #include "webrtc/modules/audio_processing/agc/utility.h"
     21 
     22 namespace webrtc {
     23 
     24 struct InputOutput {
     25   double rms;
     26   double activity_probability;
     27   double audio_content;
     28   double loudness;
     29 };
     30 
     31 const double kRelativeErrTol = 1e-10;
     32 
     33 class HistogramTest : public ::testing::Test {
     34  protected:
     35   void RunTest(bool enable_circular_buff,
     36                const char* filename);
     37 
     38  private:
     39   void TestClean();
     40   rtc::scoped_ptr<Histogram> hist_;
     41 };
     42 
     43 void HistogramTest::TestClean() {
     44   EXPECT_EQ(hist_->CurrentRms(), 7.59621091765857e-02);
     45   EXPECT_EQ(hist_->AudioContent(), 0);
     46   EXPECT_EQ(hist_->num_updates(), 0);
     47 }
     48 
     49 void HistogramTest::RunTest(bool enable_circular_buff, const char* filename) {
     50   FILE* in_file = fopen(filename, "rb");
     51   ASSERT_TRUE(in_file != NULL);
     52   if (enable_circular_buff) {
     53     int buffer_size;
     54     EXPECT_EQ(fread(&buffer_size, sizeof(buffer_size), 1, in_file), 1u);
     55     hist_.reset(Histogram::Create(buffer_size));
     56   } else {
     57     hist_.reset(Histogram::Create());
     58   }
     59   TestClean();
     60 
     61   InputOutput io;
     62   int num_updates = 0;
     63   int num_reset = 0;
     64   while (fread(&io, sizeof(InputOutput), 1, in_file) == 1) {
     65     if (io.rms < 0) {
     66       // We have to reset.
     67       hist_->Reset();
     68       TestClean();
     69       num_updates = 0;
     70       num_reset++;
     71       // Read the next chunk of input.
     72       if (fread(&io, sizeof(InputOutput), 1, in_file) != 1)
     73         break;
     74     }
     75     hist_->Update(io.rms, io.activity_probability);
     76     num_updates++;
     77     EXPECT_EQ(hist_->num_updates(), num_updates);
     78     double audio_content = hist_->AudioContent();
     79 
     80     double abs_err = std::min(audio_content, io.audio_content) *
     81         kRelativeErrTol;
     82 
     83     ASSERT_NEAR(audio_content, io.audio_content, abs_err);
     84     double current_loudness = Linear2Loudness(hist_->CurrentRms());
     85     abs_err = std::min(fabs(current_loudness), fabs(io.loudness)) *
     86         kRelativeErrTol;
     87     ASSERT_NEAR(current_loudness, io.loudness, abs_err);
     88   }
     89   fclose(in_file);
     90 }
     91 
     92 TEST_F(HistogramTest, ActiveCircularBuffer) {
     93   RunTest(true,
     94           test::ResourcePath("audio_processing/agc/agc_with_circular_buffer",
     95                              "dat").c_str());
     96 }
     97 
     98 TEST_F(HistogramTest, InactiveCircularBuffer) {
     99   RunTest(false,
    100           test::ResourcePath("audio_processing/agc/agc_no_circular_buffer",
    101                              "dat").c_str());
    102 }
    103 
    104 }  // namespace webrtc
    105