blob: 341f2b2374a1bb722971c9e37688440bce318b81 [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
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020023#include "api/audio/audio_frame.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"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/constructor_magic.h"
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000028
29namespace webrtc {
30
31static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
32#define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000033
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070034class RawFile final {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000035 public:
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070036 explicit RawFile(const std::string& filename);
37 ~RawFile();
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000038
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070039 void WriteSamples(const int16_t* samples, size_t num_samples);
40 void WriteSamples(const float* samples, size_t num_samples);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000041
42 private:
43 FILE* file_handle_;
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070044
henrikg3c089d72015-09-16 05:37:44 -070045 RTC_DISALLOW_COPY_AND_ASSIGN(RawFile);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000046};
47
aluebsb0ad43b2015-11-20 00:11:53 -080048// Reads ChannelBuffers from a provided WavReader.
49class ChannelBufferWavReader final {
50 public:
kwiberg62eaacf2016-02-17 06:39:05 -080051 explicit ChannelBufferWavReader(std::unique_ptr<WavReader> file);
kwiberg942c8512016-08-29 13:10:29 -070052 ~ChannelBufferWavReader();
aluebsb0ad43b2015-11-20 00:11:53 -080053
54 // Reads data from the file according to the |buffer| format. Returns false if
55 // a full buffer can't be read from the file.
56 bool Read(ChannelBuffer<float>* buffer);
57
58 private:
kwiberg62eaacf2016-02-17 06:39:05 -080059 std::unique_ptr<WavReader> file_;
aluebsb0ad43b2015-11-20 00:11:53 -080060 std::vector<float> interleaved_;
61
62 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavReader);
63};
64
65// Writes ChannelBuffers to a provided WavWriter.
66class ChannelBufferWavWriter final {
67 public:
kwiberg62eaacf2016-02-17 06:39:05 -080068 explicit ChannelBufferWavWriter(std::unique_ptr<WavWriter> file);
kwiberg942c8512016-08-29 13:10:29 -070069 ~ChannelBufferWavWriter();
70
aluebsb0ad43b2015-11-20 00:11:53 -080071 void Write(const ChannelBuffer<float>& buffer);
72
73 private:
kwiberg62eaacf2016-02-17 06:39:05 -080074 std::unique_ptr<WavWriter> file_;
aluebsb0ad43b2015-11-20 00:11:53 -080075 std::vector<float> interleaved_;
76
77 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavWriter);
78};
79
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +020080// Takes a pointer to a vector. Allows appending the samples of channel buffers
81// to the given vector, by interleaving the samples and converting them to float
82// S16.
83class ChannelBufferVectorWriter final {
84 public:
85 explicit ChannelBufferVectorWriter(std::vector<float>* output);
86 ChannelBufferVectorWriter(const ChannelBufferVectorWriter&) = delete;
87 ChannelBufferVectorWriter& operator=(const ChannelBufferVectorWriter&) =
88 delete;
89 ~ChannelBufferVectorWriter();
90
91 // Creates an interleaved copy of |buffer|, converts the samples to float S16
92 // and appends the result to output_.
93 void Write(const ChannelBuffer<float>& buffer);
94
95 private:
96 std::vector<float> interleaved_buffer_;
97 std::vector<float>* output_;
98};
99
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700100void WriteIntData(const int16_t* data,
101 size_t length,
102 WavWriter* wav_file,
103 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000104
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700105void WriteFloatData(const float* const* data,
pkasting25702cb2016-01-08 13:50:27 -0800106 size_t samples_per_channel,
Peter Kasting69558702016-01-12 16:26:35 -0800107 size_t num_channels,
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700108 WavWriter* wav_file,
109 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +0000110
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000111// Exits on failure; do not use in unit tests.
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700112FILE* OpenFile(const std::string& filename, const char* mode);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000113
pkasting25702cb2016-01-08 13:50:27 -0800114size_t SamplesFromRate(int rate);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000115
Yves Gerey665174f2018-06-19 15:03:05 +0200116void SetFrameSampleRate(AudioFrame* frame, int sample_rate_hz);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000117
118template <typename T>
119void SetContainerFormat(int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800120 size_t num_channels,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000121 AudioFrame* frame,
kwiberg62eaacf2016-02-17 06:39:05 -0800122 std::unique_ptr<ChannelBuffer<T> >* cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000123 SetFrameSampleRate(frame, sample_rate_hz);
124 frame->num_channels_ = num_channels;
125 cb->reset(new ChannelBuffer<T>(frame->samples_per_channel_, num_channels));
126}
127
Peter Kasting69558702016-01-12 16:26:35 -0800128AudioProcessing::ChannelLayout LayoutFromChannels(size_t num_channels);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000129
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>
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700155std::vector<T> ParseList(const std::string& to_parse) {
156 std::vector<T> values;
157
158 std::istringstream str(to_parse);
159 std::copy(
Yves Gerey665174f2018-06-19 15:03:05 +0200160 std::istream_iterator<T>(str), // no-presubmit-check TODO(webrtc:8982)
161 std::istream_iterator<T>(), // no-presubmit-check TODO(webrtc:8982)
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700162 std::back_inserter(values));
163
164 return values;
165}
166
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000167} // namespace webrtc
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700168
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200169#endif // MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_