blob: 30674cb143eac1513e5356d6c7e80698dc47f002 [file] [log] [blame]
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
12#define MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000013
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070014#include <math.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070016#include <iterator>
17#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080018#include <memory>
Jonas Olsson941a07c2018-09-13 10:07:07 +020019#include <sstream> // no-presubmit-check TODO(webrtc:8982)
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070020#include <string>
21#include <vector>
22
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_audio/channel_buffer.h"
24#include "common_audio/wav_file.h"
25#include "modules/audio_processing/include/audio_processing.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/constructor_magic.h"
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000027
28namespace webrtc {
29
30static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
31#define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000032
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070033class RawFile final {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000034 public:
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070035 explicit RawFile(const std::string& filename);
36 ~RawFile();
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000037
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070038 void WriteSamples(const int16_t* samples, size_t num_samples);
39 void WriteSamples(const float* samples, size_t num_samples);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000040
41 private:
42 FILE* file_handle_;
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070043
henrikg3c089d72015-09-16 05:37:44 -070044 RTC_DISALLOW_COPY_AND_ASSIGN(RawFile);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000045};
46
Per Åhgren2507f8c2020-03-19 12:33:29 +010047// Encapsulates samples and metadata for an integer frame.
48struct Int16FrameData {
49 // Max data size that matches the data size of the AudioFrame class, providing
50 // storage for 8 channels of 96 kHz data.
51 static const int kMaxDataSizeSamples = 7680;
52
53 Int16FrameData() {
54 sample_rate_hz = 0;
55 num_channels = 0;
56 samples_per_channel = 0;
Per Åhgren2507f8c2020-03-19 12:33:29 +010057 data.fill(0);
58 }
59
60 void CopyFrom(const Int16FrameData& src) {
61 samples_per_channel = src.samples_per_channel;
62 sample_rate_hz = src.sample_rate_hz;
Per Åhgren2507f8c2020-03-19 12:33:29 +010063 num_channels = src.num_channels;
64
65 const size_t length = samples_per_channel * num_channels;
66 RTC_CHECK_LE(length, kMaxDataSizeSamples);
67 memcpy(data.data(), src.data.data(), sizeof(int16_t) * length);
68 }
69 std::array<int16_t, kMaxDataSizeSamples> data;
70 int32_t sample_rate_hz;
71 size_t num_channels;
72 size_t samples_per_channel;
Per Åhgren2507f8c2020-03-19 12:33:29 +010073};
74
aluebsb0ad43b2015-11-20 00:11:53 -080075// Reads ChannelBuffers from a provided WavReader.
76class ChannelBufferWavReader final {
77 public:
kwiberg62eaacf2016-02-17 06:39:05 -080078 explicit ChannelBufferWavReader(std::unique_ptr<WavReader> file);
kwiberg942c8512016-08-29 13:10:29 -070079 ~ChannelBufferWavReader();
aluebsb0ad43b2015-11-20 00:11:53 -080080
Artem Titov0b489302021-07-28 20:50:03 +020081 // Reads data from the file according to the `buffer` format. Returns false if
aluebsb0ad43b2015-11-20 00:11:53 -080082 // a full buffer can't be read from the file.
83 bool Read(ChannelBuffer<float>* buffer);
84
85 private:
kwiberg62eaacf2016-02-17 06:39:05 -080086 std::unique_ptr<WavReader> file_;
aluebsb0ad43b2015-11-20 00:11:53 -080087 std::vector<float> interleaved_;
88
89 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavReader);
90};
91
92// Writes ChannelBuffers to a provided WavWriter.
93class ChannelBufferWavWriter final {
94 public:
kwiberg62eaacf2016-02-17 06:39:05 -080095 explicit ChannelBufferWavWriter(std::unique_ptr<WavWriter> file);
kwiberg942c8512016-08-29 13:10:29 -070096 ~ChannelBufferWavWriter();
97
aluebsb0ad43b2015-11-20 00:11:53 -080098 void Write(const ChannelBuffer<float>& buffer);
99
100 private:
kwiberg62eaacf2016-02-17 06:39:05 -0800101 std::unique_ptr<WavWriter> file_;
aluebsb0ad43b2015-11-20 00:11:53 -0800102 std::vector<float> interleaved_;
103
104 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavWriter);
105};
106
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200107// Takes a pointer to a vector. Allows appending the samples of channel buffers
108// to the given vector, by interleaving the samples and converting them to float
109// S16.
110class ChannelBufferVectorWriter final {
111 public:
112 explicit ChannelBufferVectorWriter(std::vector<float>* output);
113 ChannelBufferVectorWriter(const ChannelBufferVectorWriter&) = delete;
114 ChannelBufferVectorWriter& operator=(const ChannelBufferVectorWriter&) =
115 delete;
116 ~ChannelBufferVectorWriter();
117
Artem Titov0b489302021-07-28 20:50:03 +0200118 // Creates an interleaved copy of `buffer`, converts the samples to float S16
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200119 // and appends the result to output_.
120 void Write(const ChannelBuffer<float>& buffer);
121
122 private:
123 std::vector<float> interleaved_buffer_;
124 std::vector<float>* output_;
125};
126
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700127void WriteIntData(const int16_t* data,
128 size_t length,
129 WavWriter* wav_file,
130 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000131
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700132void WriteFloatData(const float* const* data,
pkasting25702cb2016-01-08 13:50:27 -0800133 size_t samples_per_channel,
Peter Kasting69558702016-01-12 16:26:35 -0800134 size_t num_channels,
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700135 WavWriter* wav_file,
136 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000137
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000138// Exits on failure; do not use in unit tests.
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700139FILE* OpenFile(const std::string& filename, const char* mode);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000140
pkasting25702cb2016-01-08 13:50:27 -0800141size_t SamplesFromRate(int rate);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000142
Per Åhgren2507f8c2020-03-19 12:33:29 +0100143void SetFrameSampleRate(Int16FrameData* frame, int sample_rate_hz);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000144
145template <typename T>
146void SetContainerFormat(int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800147 size_t num_channels,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100148 Int16FrameData* frame,
kwiberg62eaacf2016-02-17 06:39:05 -0800149 std::unique_ptr<ChannelBuffer<T> >* cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000150 SetFrameSampleRate(frame, sample_rate_hz);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100151 frame->num_channels = num_channels;
152 cb->reset(new ChannelBuffer<T>(frame->samples_per_channel, num_channels));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000153}
154
Peter Kasting69558702016-01-12 16:26:35 -0800155AudioProcessing::ChannelLayout LayoutFromChannels(size_t num_channels);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000156
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000157template <typename T>
pkasting25702cb2016-01-08 13:50:27 -0800158float ComputeSNR(const T* ref, const T* test, size_t length, float* variance) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000159 float mse = 0;
160 float mean = 0;
161 *variance = 0;
pkasting25702cb2016-01-08 13:50:27 -0800162 for (size_t i = 0; i < length; ++i) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000163 T error = ref[i] - test[i];
164 mse += error * error;
165 *variance += ref[i] * ref[i];
166 mean += ref[i];
167 }
168 mse /= length;
169 *variance /= length;
170 mean /= length;
171 *variance -= mean * mean;
172
173 float snr = 100; // We assign 100 dB to the zero-error case.
174 if (mse > 0)
175 snr = 10 * log10(*variance / mse);
176 return snr;
177}
178
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700179// Returns a vector<T> parsed from whitespace delimited values in to_parse,
180// or an empty vector if the string could not be parsed.
Yves Gerey665174f2018-06-19 15:03:05 +0200181template <typename T>
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700182std::vector<T> ParseList(const std::string& to_parse) {
183 std::vector<T> values;
184
185 std::istringstream str(to_parse);
186 std::copy(
Yves Gerey665174f2018-06-19 15:03:05 +0200187 std::istream_iterator<T>(str), // no-presubmit-check TODO(webrtc:8982)
188 std::istream_iterator<T>(), // no-presubmit-check TODO(webrtc:8982)
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700189 std::back_inserter(values));
190
191 return values;
192}
193
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000194} // namespace webrtc
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700195
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200196#endif // MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_