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 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
12 #define WEBRTC_MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
13
14 #include <math.h>
15 #include <iterator>
16 #include <limits>
17 #include <string>
18 #include <vector>
19
20 #include "webrtc/base/constructormagic.h"
21 #include "webrtc/base/scoped_ptr.h"
22 #include "webrtc/common_audio/channel_buffer.h"
23 #include "webrtc/common_audio/wav_file.h"
24 #include "webrtc/modules/audio_processing/include/audio_processing.h"
25 #include "webrtc/modules/include/module_common_types.h"
26
27 namespace webrtc {
28
29 static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
30 #define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
31
32 class RawFile final {
33 public:
34 explicit RawFile(const std::string& filename);
35 ~RawFile();
36
37 void WriteSamples(const int16_t* samples, size_t num_samples);
38 void WriteSamples(const float* samples, size_t num_samples);
39
40 private:
41 FILE* file_handle_;
42
43 RTC_DISALLOW_COPY_AND_ASSIGN(RawFile);
44 };
45
46 // Reads ChannelBuffers from a provided WavReader.
47 class ChannelBufferWavReader final {
48 public:
49 explicit ChannelBufferWavReader(rtc::scoped_ptr<WavReader> file);
50
51 // Reads data from the file according to the |buffer| format. Returns false if
52 // a full buffer can't be read from the file.
53 bool Read(ChannelBuffer<float>* buffer);
54
55 private:
56 rtc::scoped_ptr<WavReader> file_;
57 std::vector<float> interleaved_;
58
59 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavReader);
60 };
61
62 // Writes ChannelBuffers to a provided WavWriter.
63 class ChannelBufferWavWriter final {
64 public:
65 explicit ChannelBufferWavWriter(rtc::scoped_ptr<WavWriter> file);
66 void Write(const ChannelBuffer<float>& buffer);
67
68 private:
69 rtc::scoped_ptr<WavWriter> file_;
70 std::vector<float> interleaved_;
71
72 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavWriter);
73 };
74
75 void WriteIntData(const int16_t* data,
76 size_t length,
77 WavWriter* wav_file,
78 RawFile* raw_file);
79
80 void WriteFloatData(const float* const* data,
81 size_t samples_per_channel,
82 size_t num_channels,
83 WavWriter* wav_file,
84 RawFile* raw_file);
85
86 // Exits on failure; do not use in unit tests.
87 FILE* OpenFile(const std::string& filename, const char* mode);
88
89 size_t SamplesFromRate(int rate);
90
91 void SetFrameSampleRate(AudioFrame* frame,
92 int sample_rate_hz);
93
94 template <typename T>
SetContainerFormat(int sample_rate_hz,size_t num_channels,AudioFrame * frame,rtc::scoped_ptr<ChannelBuffer<T>> * cb)95 void SetContainerFormat(int sample_rate_hz,
96 size_t num_channels,
97 AudioFrame* frame,
98 rtc::scoped_ptr<ChannelBuffer<T> >* cb) {
99 SetFrameSampleRate(frame, sample_rate_hz);
100 frame->num_channels_ = num_channels;
101 cb->reset(new ChannelBuffer<T>(frame->samples_per_channel_, num_channels));
102 }
103
104 AudioProcessing::ChannelLayout LayoutFromChannels(size_t num_channels);
105
106 template <typename T>
ComputeSNR(const T * ref,const T * test,size_t length,float * variance)107 float ComputeSNR(const T* ref, const T* test, size_t length, float* variance) {
108 float mse = 0;
109 float mean = 0;
110 *variance = 0;
111 for (size_t i = 0; i < length; ++i) {
112 T error = ref[i] - test[i];
113 mse += error * error;
114 *variance += ref[i] * ref[i];
115 mean += ref[i];
116 }
117 mse /= length;
118 *variance /= length;
119 mean /= length;
120 *variance -= mean * mean;
121
122 float snr = 100; // We assign 100 dB to the zero-error case.
123 if (mse > 0)
124 snr = 10 * log10(*variance / mse);
125 return snr;
126 }
127
128 // Returns a vector<T> parsed from whitespace delimited values in to_parse,
129 // or an empty vector if the string could not be parsed.
130 template<typename T>
ParseList(const std::string & to_parse)131 std::vector<T> ParseList(const std::string& to_parse) {
132 std::vector<T> values;
133
134 std::istringstream str(to_parse);
135 std::copy(
136 std::istream_iterator<T>(str),
137 std::istream_iterator<T>(),
138 std::back_inserter(values));
139
140 return values;
141 }
142
143 // Parses the array geometry from the command line.
144 //
145 // If a vector with size != num_mics is returned, an error has occurred and an
146 // appropriate error message has been printed to stdout.
147 std::vector<Point> ParseArrayGeometry(const std::string& mic_positions,
148 size_t num_mics);
149
150 // Same as above, but without the num_mics check for when it isn't available.
151 std::vector<Point> ParseArrayGeometry(const std::string& mic_positions);
152
153 } // namespace webrtc
154
155 #endif // WEBRTC_MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
156