blob: 0dd4a40bc914ee415e6883dbeef333af0268c4a0 [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
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070080void WriteIntData(const int16_t* data,
81 size_t length,
82 WavWriter* wav_file,
83 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000084
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070085void WriteFloatData(const float* const* data,
pkasting25702cb2016-01-08 13:50:27 -080086 size_t samples_per_channel,
Peter Kasting69558702016-01-12 16:26:35 -080087 size_t num_channels,
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070088 WavWriter* wav_file,
89 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000090
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000091// Exits on failure; do not use in unit tests.
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070092FILE* OpenFile(const std::string& filename, const char* mode);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000093
pkasting25702cb2016-01-08 13:50:27 -080094size_t SamplesFromRate(int rate);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000095
Yves Gerey665174f2018-06-19 15:03:05 +020096void SetFrameSampleRate(AudioFrame* frame, int sample_rate_hz);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000097
98template <typename T>
99void SetContainerFormat(int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800100 size_t num_channels,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000101 AudioFrame* frame,
kwiberg62eaacf2016-02-17 06:39:05 -0800102 std::unique_ptr<ChannelBuffer<T> >* cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000103 SetFrameSampleRate(frame, sample_rate_hz);
104 frame->num_channels_ = num_channels;
105 cb->reset(new ChannelBuffer<T>(frame->samples_per_channel_, num_channels));
106}
107
Peter Kasting69558702016-01-12 16:26:35 -0800108AudioProcessing::ChannelLayout LayoutFromChannels(size_t num_channels);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000109
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000110template <typename T>
pkasting25702cb2016-01-08 13:50:27 -0800111float ComputeSNR(const T* ref, const T* test, size_t length, float* variance) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000112 float mse = 0;
113 float mean = 0;
114 *variance = 0;
pkasting25702cb2016-01-08 13:50:27 -0800115 for (size_t i = 0; i < length; ++i) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000116 T error = ref[i] - test[i];
117 mse += error * error;
118 *variance += ref[i] * ref[i];
119 mean += ref[i];
120 }
121 mse /= length;
122 *variance /= length;
123 mean /= length;
124 *variance -= mean * mean;
125
126 float snr = 100; // We assign 100 dB to the zero-error case.
127 if (mse > 0)
128 snr = 10 * log10(*variance / mse);
129 return snr;
130}
131
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700132// Returns a vector<T> parsed from whitespace delimited values in to_parse,
133// or an empty vector if the string could not be parsed.
Yves Gerey665174f2018-06-19 15:03:05 +0200134template <typename T>
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700135std::vector<T> ParseList(const std::string& to_parse) {
136 std::vector<T> values;
137
138 std::istringstream str(to_parse);
139 std::copy(
Yves Gerey665174f2018-06-19 15:03:05 +0200140 std::istream_iterator<T>(str), // no-presubmit-check TODO(webrtc:8982)
141 std::istream_iterator<T>(), // no-presubmit-check TODO(webrtc:8982)
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700142 std::back_inserter(values));
143
144 return values;
145}
146
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000147} // namespace webrtc
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700148
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200149#endif // MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_