xref: /aosp_15_r20/external/webrtc/modules/audio_processing/test/test_utils.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
12 #define MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
13 
14 #include <math.h>
15 
16 #include <iterator>
17 #include <limits>
18 #include <memory>
19 #include <sstream>  // no-presubmit-check TODO(webrtc:8982)
20 #include <string>
21 #include <vector>
22 
23 #include "absl/strings/string_view.h"
24 #include "common_audio/channel_buffer.h"
25 #include "common_audio/wav_file.h"
26 #include "modules/audio_processing/include/audio_processing.h"
27 
28 namespace webrtc {
29 
30 static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
31 #define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
32 
33 // Encapsulates samples and metadata for an integer frame.
34 struct Int16FrameData {
35   // Max data size that matches the data size of the AudioFrame class, providing
36   // storage for 8 channels of 96 kHz data.
37   static const int kMaxDataSizeSamples = 7680;
38 
Int16FrameDataInt16FrameData39   Int16FrameData() {
40     sample_rate_hz = 0;
41     num_channels = 0;
42     samples_per_channel = 0;
43     data.fill(0);
44   }
45 
CopyFromInt16FrameData46   void CopyFrom(const Int16FrameData& src) {
47     samples_per_channel = src.samples_per_channel;
48     sample_rate_hz = src.sample_rate_hz;
49     num_channels = src.num_channels;
50 
51     const size_t length = samples_per_channel * num_channels;
52     RTC_CHECK_LE(length, kMaxDataSizeSamples);
53     memcpy(data.data(), src.data.data(), sizeof(int16_t) * length);
54   }
55   std::array<int16_t, kMaxDataSizeSamples> data;
56   int32_t sample_rate_hz;
57   size_t num_channels;
58   size_t samples_per_channel;
59 };
60 
61 // Reads ChannelBuffers from a provided WavReader.
62 class ChannelBufferWavReader final {
63  public:
64   explicit ChannelBufferWavReader(std::unique_ptr<WavReader> file);
65   ~ChannelBufferWavReader();
66 
67   ChannelBufferWavReader(const ChannelBufferWavReader&) = delete;
68   ChannelBufferWavReader& operator=(const ChannelBufferWavReader&) = delete;
69 
70   // Reads data from the file according to the `buffer` format. Returns false if
71   // a full buffer can't be read from the file.
72   bool Read(ChannelBuffer<float>* buffer);
73 
74  private:
75   std::unique_ptr<WavReader> file_;
76   std::vector<float> interleaved_;
77 };
78 
79 // Writes ChannelBuffers to a provided WavWriter.
80 class ChannelBufferWavWriter final {
81  public:
82   explicit ChannelBufferWavWriter(std::unique_ptr<WavWriter> file);
83   ~ChannelBufferWavWriter();
84 
85   ChannelBufferWavWriter(const ChannelBufferWavWriter&) = delete;
86   ChannelBufferWavWriter& operator=(const ChannelBufferWavWriter&) = delete;
87 
88   void Write(const ChannelBuffer<float>& buffer);
89 
90  private:
91   std::unique_ptr<WavWriter> file_;
92   std::vector<float> interleaved_;
93 };
94 
95 // Takes a pointer to a vector. Allows appending the samples of channel buffers
96 // to the given vector, by interleaving the samples and converting them to float
97 // S16.
98 class ChannelBufferVectorWriter final {
99  public:
100   explicit ChannelBufferVectorWriter(std::vector<float>* output);
101   ChannelBufferVectorWriter(const ChannelBufferVectorWriter&) = delete;
102   ChannelBufferVectorWriter& operator=(const ChannelBufferVectorWriter&) =
103       delete;
104   ~ChannelBufferVectorWriter();
105 
106   // Creates an interleaved copy of `buffer`, converts the samples to float S16
107   // and appends the result to output_.
108   void Write(const ChannelBuffer<float>& buffer);
109 
110  private:
111   std::vector<float> interleaved_buffer_;
112   std::vector<float>* output_;
113 };
114 
115 // Exits on failure; do not use in unit tests.
116 FILE* OpenFile(absl::string_view filename, absl::string_view mode);
117 
118 void SetFrameSampleRate(Int16FrameData* frame, int sample_rate_hz);
119 
120 template <typename T>
SetContainerFormat(int sample_rate_hz,size_t num_channels,Int16FrameData * frame,std::unique_ptr<ChannelBuffer<T>> * cb)121 void SetContainerFormat(int sample_rate_hz,
122                         size_t num_channels,
123                         Int16FrameData* frame,
124                         std::unique_ptr<ChannelBuffer<T> >* cb) {
125   SetFrameSampleRate(frame, sample_rate_hz);
126   frame->num_channels = num_channels;
127   cb->reset(new ChannelBuffer<T>(frame->samples_per_channel, num_channels));
128 }
129 
130 template <typename T>
ComputeSNR(const T * ref,const T * test,size_t length,float * variance)131 float ComputeSNR(const T* ref, const T* test, size_t length, float* variance) {
132   float mse = 0;
133   float mean = 0;
134   *variance = 0;
135   for (size_t i = 0; i < length; ++i) {
136     T error = ref[i] - test[i];
137     mse += error * error;
138     *variance += ref[i] * ref[i];
139     mean += ref[i];
140   }
141   mse /= length;
142   *variance /= length;
143   mean /= length;
144   *variance -= mean * mean;
145 
146   float snr = 100;  // We assign 100 dB to the zero-error case.
147   if (mse > 0)
148     snr = 10 * log10(*variance / mse);
149   return snr;
150 }
151 
152 // Returns a vector<T> parsed from whitespace delimited values in to_parse,
153 // or an empty vector if the string could not be parsed.
154 template <typename T>
ParseList(absl::string_view to_parse)155 std::vector<T> ParseList(absl::string_view to_parse) {
156   std::vector<T> values;
157 
158   std::istringstream str(  // no-presubmit-check TODO(webrtc:8982)
159       std::string{to_parse});
160   std::copy(
161       std::istream_iterator<T>(str),  // no-presubmit-check TODO(webrtc:8982)
162       std::istream_iterator<T>(),     // no-presubmit-check TODO(webrtc:8982)
163       std::back_inserter(values));
164 
165   return values;
166 }
167 
168 }  // namespace webrtc
169 
170 #endif  // MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
171