• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2014 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 #include "webrtc/modules/audio_coding/neteq/audio_classifier.h"
12 
13 #include <math.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <string>
18 
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "webrtc/test/testsupport/fileutils.h"
21 
22 namespace webrtc {
23 
24 static const size_t kFrameSize = 960;
25 
TEST(AudioClassifierTest,AllZeroInput)26 TEST(AudioClassifierTest, AllZeroInput) {
27   int16_t in_mono[kFrameSize] = {0};
28 
29   // Test all-zero vectors and let the classifier converge from its default
30   // to the expected value.
31   AudioClassifier zero_classifier;
32   for (int i = 0; i < 100; ++i) {
33     zero_classifier.Analysis(in_mono, kFrameSize, 1);
34   }
35   EXPECT_TRUE(zero_classifier.is_music());
36 }
37 
RunAnalysisTest(const std::string & audio_filename,const std::string & data_filename,size_t channels)38 void RunAnalysisTest(const std::string& audio_filename,
39                      const std::string& data_filename,
40                      size_t channels) {
41   AudioClassifier classifier;
42   rtc::scoped_ptr<int16_t[]> in(new int16_t[channels * kFrameSize]);
43   bool is_music_ref;
44 
45   FILE* audio_file = fopen(audio_filename.c_str(), "rb");
46   ASSERT_TRUE(audio_file != NULL) << "Failed to open file " << audio_filename
47                                   << std::endl;
48   FILE* data_file = fopen(data_filename.c_str(), "rb");
49   ASSERT_TRUE(audio_file != NULL) << "Failed to open file " << audio_filename
50                                   << std::endl;
51   while (fread(in.get(), sizeof(int16_t), channels * kFrameSize, audio_file) ==
52          channels * kFrameSize) {
53     bool is_music =
54         classifier.Analysis(in.get(), channels * kFrameSize, channels);
55     EXPECT_EQ(is_music, classifier.is_music());
56     ASSERT_EQ(1u, fread(&is_music_ref, sizeof(is_music_ref), 1, data_file));
57     EXPECT_EQ(is_music_ref, is_music);
58   }
59   fclose(audio_file);
60   fclose(data_file);
61 }
62 
TEST(AudioClassifierTest,DoAnalysisMono)63 TEST(AudioClassifierTest, DoAnalysisMono) {
64   RunAnalysisTest(test::ResourcePath("short_mixed_mono_48", "pcm"),
65                   test::ResourcePath("short_mixed_mono_48", "dat"),
66                   1);
67 }
68 
TEST(AudioClassifierTest,DoAnalysisStereo)69 TEST(AudioClassifierTest, DoAnalysisStereo) {
70   RunAnalysisTest(test::ResourcePath("short_mixed_stereo_48", "pcm"),
71                   test::ResourcePath("short_mixed_stereo_48", "dat"),
72                   2);
73 }
74 
75 }  // namespace webrtc
76