blob: bf82f9d66d6c2e6f40c6cefefac64ffd0dfc1826 [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
Ali Tofighf3592cb2022-08-16 14:44:38 +020023#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "common_audio/channel_buffer.h"
25#include "common_audio/wav_file.h"
26#include "modules/audio_processing/include/audio_processing.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
Per Åhgren2507f8c2020-03-19 12:33:29 +010033// Encapsulates samples and metadata for an integer frame.
34struct 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
39 Int16FrameData() {
40 sample_rate_hz = 0;
41 num_channels = 0;
42 samples_per_channel = 0;
Per Åhgren2507f8c2020-03-19 12:33:29 +010043 data.fill(0);
44 }
45
46 void CopyFrom(const Int16FrameData& src) {
47 samples_per_channel = src.samples_per_channel;
48 sample_rate_hz = src.sample_rate_hz;
Per Åhgren2507f8c2020-03-19 12:33:29 +010049 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;
Per Åhgren2507f8c2020-03-19 12:33:29 +010059};
60
aluebsb0ad43b2015-11-20 00:11:53 -080061// Reads ChannelBuffers from a provided WavReader.
62class ChannelBufferWavReader final {
63 public:
kwiberg62eaacf2016-02-17 06:39:05 -080064 explicit ChannelBufferWavReader(std::unique_ptr<WavReader> file);
kwiberg942c8512016-08-29 13:10:29 -070065 ~ChannelBufferWavReader();
aluebsb0ad43b2015-11-20 00:11:53 -080066
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090067 ChannelBufferWavReader(const ChannelBufferWavReader&) = delete;
68 ChannelBufferWavReader& operator=(const ChannelBufferWavReader&) = delete;
69
Artem Titov0b489302021-07-28 20:50:03 +020070 // Reads data from the file according to the `buffer` format. Returns false if
aluebsb0ad43b2015-11-20 00:11:53 -080071 // a full buffer can't be read from the file.
72 bool Read(ChannelBuffer<float>* buffer);
73
74 private:
kwiberg62eaacf2016-02-17 06:39:05 -080075 std::unique_ptr<WavReader> file_;
aluebsb0ad43b2015-11-20 00:11:53 -080076 std::vector<float> interleaved_;
aluebsb0ad43b2015-11-20 00:11:53 -080077};
78
79// Writes ChannelBuffers to a provided WavWriter.
80class ChannelBufferWavWriter final {
81 public:
kwiberg62eaacf2016-02-17 06:39:05 -080082 explicit ChannelBufferWavWriter(std::unique_ptr<WavWriter> file);
kwiberg942c8512016-08-29 13:10:29 -070083 ~ChannelBufferWavWriter();
84
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090085 ChannelBufferWavWriter(const ChannelBufferWavWriter&) = delete;
86 ChannelBufferWavWriter& operator=(const ChannelBufferWavWriter&) = delete;
87
aluebsb0ad43b2015-11-20 00:11:53 -080088 void Write(const ChannelBuffer<float>& buffer);
89
90 private:
kwiberg62eaacf2016-02-17 06:39:05 -080091 std::unique_ptr<WavWriter> file_;
aluebsb0ad43b2015-11-20 00:11:53 -080092 std::vector<float> interleaved_;
aluebsb0ad43b2015-11-20 00:11:53 -080093};
94
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +020095// 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.
98class 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
Artem Titov0b489302021-07-28 20:50:03 +0200106 // Creates an interleaved copy of `buffer`, converts the samples to float S16
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +0200107 // 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
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000115// Exits on failure; do not use in unit tests.
Ali Tofighf3592cb2022-08-16 14:44:38 +0200116FILE* OpenFile(absl::string_view filename, absl::string_view mode);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000117
Per Åhgren2507f8c2020-03-19 12:33:29 +0100118void SetFrameSampleRate(Int16FrameData* frame, int sample_rate_hz);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000119
120template <typename T>
121void SetContainerFormat(int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800122 size_t num_channels,
Per Åhgren2507f8c2020-03-19 12:33:29 +0100123 Int16FrameData* frame,
kwiberg62eaacf2016-02-17 06:39:05 -0800124 std::unique_ptr<ChannelBuffer<T> >* cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000125 SetFrameSampleRate(frame, sample_rate_hz);
Per Åhgren2507f8c2020-03-19 12:33:29 +0100126 frame->num_channels = num_channels;
127 cb->reset(new ChannelBuffer<T>(frame->samples_per_channel, num_channels));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000128}
129
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000130template <typename T>
pkasting25702cb2016-01-08 13:50:27 -0800131float ComputeSNR(const T* ref, const T* test, size_t length, float* variance) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000132 float mse = 0;
133 float mean = 0;
134 *variance = 0;
pkasting25702cb2016-01-08 13:50:27 -0800135 for (size_t i = 0; i < length; ++i) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000136 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
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700152// Returns a vector<T> parsed from whitespace delimited values in to_parse,
153// or an empty vector if the string could not be parsed.
Yves Gerey665174f2018-06-19 15:03:05 +0200154template <typename T>
Ali Tofighf3592cb2022-08-16 14:44:38 +0200155std::vector<T> ParseList(absl::string_view to_parse) {
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700156 std::vector<T> values;
157
Ali Tofighf3592cb2022-08-16 14:44:38 +0200158 std::istringstream str( // no-presubmit-check TODO(webrtc:8982)
159 std::string{to_parse});
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700160 std::copy(
Yves Gerey665174f2018-06-19 15:03:05 +0200161 std::istream_iterator<T>(str), // no-presubmit-check TODO(webrtc:8982)
162 std::istream_iterator<T>(), // no-presubmit-check TODO(webrtc:8982)
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700163 std::back_inserter(values));
164
165 return values;
166}
167
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000168} // namespace webrtc
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700169
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200170#endif // MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_