1 /*
2 * Copyright (c) 2013 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 "modules/audio_processing/transient/transient_detector.h"
12
13 #include <memory>
14 #include <string>
15
16 #include "modules/audio_processing/transient/common.h"
17 #include "modules/audio_processing/transient/file_utils.h"
18 #include "rtc_base/strings/string_builder.h"
19 #include "rtc_base/system/file_wrapper.h"
20 #include "test/gtest.h"
21 #include "test/testsupport/file_utils.h"
22
23 namespace webrtc {
24
25 static const int kSampleRatesHz[] = {ts::kSampleRate8kHz, ts::kSampleRate16kHz,
26 ts::kSampleRate32kHz,
27 ts::kSampleRate48kHz};
28 static const size_t kNumberOfSampleRates =
29 sizeof(kSampleRatesHz) / sizeof(*kSampleRatesHz);
30
31 // This test is for the correctness of the transient detector.
32 // Checks the results comparing them with the ones stored in the detect files in
33 // the directory: resources/audio_processing/transient/
34 // The files contain all the results in double precision (Little endian).
35 // The audio files used with different sample rates are stored in the same
36 // directory.
37 #if defined(WEBRTC_IOS)
TEST(TransientDetectorTest,DISABLED_CorrectnessBasedOnFiles)38 TEST(TransientDetectorTest, DISABLED_CorrectnessBasedOnFiles) {
39 #else
40 TEST(TransientDetectorTest, CorrectnessBasedOnFiles) {
41 #endif
42 for (size_t i = 0; i < kNumberOfSampleRates; ++i) {
43 int sample_rate_hz = kSampleRatesHz[i];
44
45 // Prepare detect file.
46 rtc::StringBuilder detect_file_name;
47 detect_file_name << "audio_processing/transient/detect"
48 << (sample_rate_hz / 1000) << "kHz";
49
50 FileWrapper detect_file = FileWrapper::OpenReadOnly(
51 test::ResourcePath(detect_file_name.str(), "dat"));
52
53 bool file_opened = detect_file.is_open();
54 ASSERT_TRUE(file_opened) << "File could not be opened.\n"
55 << detect_file_name.str().c_str();
56
57 // Prepare audio file.
58 rtc::StringBuilder audio_file_name;
59 audio_file_name << "audio_processing/transient/audio"
60 << (sample_rate_hz / 1000) << "kHz";
61
62 FileWrapper audio_file = FileWrapper::OpenReadOnly(
63 test::ResourcePath(audio_file_name.str(), "pcm"));
64
65 // Create detector.
66 TransientDetector detector(sample_rate_hz);
67
68 const size_t buffer_length = sample_rate_hz * ts::kChunkSizeMs / 1000;
69 std::unique_ptr<float[]> buffer(new float[buffer_length]);
70
71 const float kTolerance = 0.02f;
72
73 size_t frames_read = 0;
74
75 while (ReadInt16FromFileToFloatBuffer(&audio_file, buffer_length,
76 buffer.get()) == buffer_length) {
77 ++frames_read;
78
79 float detector_value =
80 detector.Detect(buffer.get(), buffer_length, NULL, 0);
81 double file_value;
82 ASSERT_EQ(1u, ReadDoubleBufferFromFile(&detect_file, 1, &file_value))
83 << "Detect test file is malformed.\n";
84
85 // Compare results with data from the matlab test file.
86 EXPECT_NEAR(file_value, detector_value, kTolerance)
87 << "Frame: " << frames_read;
88 }
89
90 detect_file.Close();
91 audio_file.Close();
92 }
93 }
94
95 } // namespace webrtc
96