aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/audio_processing/vad/standalone_vad_unittest.cc
blob: 1d1dcc70662db94b3cd0fc9ebbdd4df10e2e594b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/modules/audio_processing/vad/standalone_vad.h"

#include <string.h>

#include "testing/gtest/include/gtest/gtest.h"
#include "webrtc/base/scoped_ptr.h"
#include "webrtc/modules/include/module_common_types.h"
#include "webrtc/test/testsupport/fileutils.h"

namespace webrtc {

TEST(StandaloneVadTest, Api) {
  rtc::scoped_ptr<StandaloneVad> vad(StandaloneVad::Create());
  int16_t data[kLength10Ms] = {0};

  // Valid frame length (for 32 kHz rate), but not what the VAD is expecting.
  EXPECT_EQ(-1, vad->AddAudio(data, 320));

  const size_t kMaxNumFrames = 3;
  double p[kMaxNumFrames];
  for (size_t n = 0; n < kMaxNumFrames; n++)
    EXPECT_EQ(0, vad->AddAudio(data, kLength10Ms));

  // Pretend |p| is shorter that it should be.
  EXPECT_EQ(-1, vad->GetActivity(p, kMaxNumFrames - 1));

  EXPECT_EQ(0, vad->GetActivity(p, kMaxNumFrames));

  // Ask for activity when buffer is empty.
  EXPECT_EQ(-1, vad->GetActivity(p, kMaxNumFrames));

  // Should reset and result in one buffer.
  for (size_t n = 0; n < kMaxNumFrames + 1; n++)
    EXPECT_EQ(0, vad->AddAudio(data, kLength10Ms));
  EXPECT_EQ(0, vad->GetActivity(p, 1));

  // Wrong modes
  EXPECT_EQ(-1, vad->set_mode(-1));
  EXPECT_EQ(-1, vad->set_mode(4));

  // Valid mode.
  const int kMode = 2;
  EXPECT_EQ(0, vad->set_mode(kMode));
  EXPECT_EQ(kMode, vad->mode());
}

#if defined(WEBRTC_IOS)
TEST(StandaloneVadTest, DISABLED_ActivityDetection) {
#else
TEST(StandaloneVadTest, ActivityDetection) {
#endif
  rtc::scoped_ptr<StandaloneVad> vad(StandaloneVad::Create());
  const size_t kDataLength = kLength10Ms;
  int16_t data[kDataLength] = {0};

  FILE* pcm_file =
      fopen(test::ResourcePath("audio_processing/agc/agc_audio", "pcm").c_str(),
            "rb");
  ASSERT_TRUE(pcm_file != NULL);

  FILE* reference_file = fopen(
      test::ResourcePath("audio_processing/agc/agc_vad", "dat").c_str(), "rb");
  ASSERT_TRUE(reference_file != NULL);

  // Reference activities are prepared with 0 aggressiveness.
  ASSERT_EQ(0, vad->set_mode(0));

  // Stand-alone VAD can operate on 1, 2 or 3 frames of length 10 ms. The
  // reference file is created for 30 ms frame.
  const int kNumVadFramesToProcess = 3;
  int num_frames = 0;
  while (fread(data, sizeof(int16_t), kDataLength, pcm_file) == kDataLength) {
    vad->AddAudio(data, kDataLength);
    num_frames++;
    if (num_frames == kNumVadFramesToProcess) {
      num_frames = 0;
      int referece_activity;
      double p[kNumVadFramesToProcess];
      EXPECT_EQ(1u, fread(&referece_activity, sizeof(referece_activity), 1,
                          reference_file));
      int activity = vad->GetActivity(p, kNumVadFramesToProcess);
      EXPECT_EQ(referece_activity, activity);
      if (activity != 0) {
        // When active, probabilities are set to 0.5.
        for (int n = 0; n < kNumVadFramesToProcess; n++)
          EXPECT_EQ(0.5, p[n]);
      } else {
        // When inactive, probabilities are set to 0.01.
        for (int n = 0; n < kNumVadFramesToProcess; n++)
          EXPECT_EQ(0.01, p[n]);
      }
    }
  }
  fclose(reference_file);
  fclose(pcm_file);
}
}  // namespace webrtc