OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/audio_silence_detector.h" | 5 #include "remoting/host/audio_silence_detector.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include <stdint.h> |
| 8 |
| 9 #include "base/macros.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
9 | 11 |
10 namespace remoting { | 12 namespace remoting { |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
14 const int kSamplingRate = 1000; | 16 const int kSamplingRate = 1000; |
15 | 17 |
16 void TestSilenceDetector(AudioSilenceDetector* target, | 18 void TestSilenceDetector(AudioSilenceDetector* target, |
17 const int16* samples, int samples_count, | 19 const int16_t* samples, |
| 20 int samples_count, |
18 bool silence_expected) { | 21 bool silence_expected) { |
19 target->Reset(kSamplingRate, 1); | 22 target->Reset(kSamplingRate, 1); |
20 bool silence_started = false; | 23 bool silence_started = false; |
21 int threshold_length = 0; | 24 int threshold_length = 0; |
22 for (int i = 0; i < 3 * kSamplingRate / samples_count; ++i) { | 25 for (int i = 0; i < 3 * kSamplingRate / samples_count; ++i) { |
23 bool result = target->IsSilence(samples, samples_count); | 26 bool result = target->IsSilence(samples, samples_count); |
24 if (silence_started) { | 27 if (silence_started) { |
25 ASSERT_TRUE(result); | 28 ASSERT_TRUE(result); |
26 } else if (result) { | 29 } else if (result) { |
27 silence_started = true; | 30 silence_started = true; |
28 threshold_length = i * samples_count; | 31 threshold_length = i * samples_count; |
29 } | 32 } |
30 } | 33 } |
31 | 34 |
32 // Check that the silence was detected if it was expected. | 35 // Check that the silence was detected if it was expected. |
33 EXPECT_EQ(silence_expected, silence_started); | 36 EXPECT_EQ(silence_expected, silence_started); |
34 | 37 |
35 if (silence_expected) { | 38 if (silence_expected) { |
36 // Check that silence threshold is between 0.5 and 2 seconds. | 39 // Check that silence threshold is between 0.5 and 2 seconds. |
37 EXPECT_GE(threshold_length, kSamplingRate / 2); | 40 EXPECT_GE(threshold_length, kSamplingRate / 2); |
38 EXPECT_LE(threshold_length, kSamplingRate * 2); | 41 EXPECT_LE(threshold_length, kSamplingRate * 2); |
39 } | 42 } |
40 } | 43 } |
41 | 44 |
42 } // namespace | 45 } // namespace |
43 | 46 |
44 TEST(AudioSilenceDetectorTest, Silence) { | 47 TEST(AudioSilenceDetectorTest, Silence) { |
45 const int16 kSamples[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | 48 const int16_t kSamples[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
46 | 49 |
47 AudioSilenceDetector target(0); | 50 AudioSilenceDetector target(0); |
48 TestSilenceDetector(&target, kSamples, arraysize(kSamples), true); | 51 TestSilenceDetector(&target, kSamples, arraysize(kSamples), true); |
49 } | 52 } |
50 | 53 |
51 TEST(AudioSilenceDetectorTest, Sound) { | 54 TEST(AudioSilenceDetectorTest, Sound) { |
52 const int16 kSamples[] = {65, 73, 83, 89, 92, -1, 5, 9, 123, 0}; | 55 const int16_t kSamples[] = {65, 73, 83, 89, 92, -1, 5, 9, 123, 0}; |
53 | 56 |
54 AudioSilenceDetector target(0); | 57 AudioSilenceDetector target(0); |
55 TestSilenceDetector(&target, kSamples, arraysize(kSamples), false); | 58 TestSilenceDetector(&target, kSamples, arraysize(kSamples), false); |
56 } | 59 } |
57 | 60 |
58 TEST(AudioSilenceDetectorTest, Threshold) { | 61 TEST(AudioSilenceDetectorTest, Threshold) { |
59 const int16 kSamples[] = {0, 0, 0, 0, 1, 0, 0, -1, 0, 0}; | 62 const int16_t kSamples[] = {0, 0, 0, 0, 1, 0, 0, -1, 0, 0}; |
60 | 63 |
61 AudioSilenceDetector target1(0); | 64 AudioSilenceDetector target1(0); |
62 TestSilenceDetector(&target1, kSamples, arraysize(kSamples), false); | 65 TestSilenceDetector(&target1, kSamples, arraysize(kSamples), false); |
63 | 66 |
64 AudioSilenceDetector target2(1); | 67 AudioSilenceDetector target2(1); |
65 TestSilenceDetector(&target2, kSamples, arraysize(kSamples), true); | 68 TestSilenceDetector(&target2, kSamples, arraysize(kSamples), true); |
66 } | 69 } |
67 | 70 |
68 } // namespace remoting | 71 } // namespace remoting |
OLD | NEW |