blob: 36374318be09ea3a7c28bcfbe11b1d9530986d2e [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>
15#include <iterator>
16#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080017#include <memory>
Jonas Olsson941a07c2018-09-13 10:07:07 +020018#include <sstream> // no-presubmit-check TODO(webrtc:8982)
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070019#include <string>
20#include <vector>
21
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020022#include "api/audio/audio_frame.h"
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
aluebsb0ad43b2015-11-20 00:11:53 -080047// Reads ChannelBuffers from a provided WavReader.
48class ChannelBufferWavReader final {
49 public:
kwiberg62eaacf2016-02-17 06:39:05 -080050 explicit ChannelBufferWavReader(std::unique_ptr<WavReader> file);
kwiberg942c8512016-08-29 13:10:29 -070051 ~ChannelBufferWavReader();
aluebsb0ad43b2015-11-20 00:11:53 -080052
53 // Reads data from the file according to the |buffer| format. Returns false if
54 // a full buffer can't be read from the file.
55 bool Read(ChannelBuffer<float>* buffer);
56
57 private:
kwiberg62eaacf2016-02-17 06:39:05 -080058 std::unique_ptr<WavReader> file_;
aluebsb0ad43b2015-11-20 00:11:53 -080059 std::vector<float> interleaved_;
60
61 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavReader);
62};
63
64// Writes ChannelBuffers to a provided WavWriter.
65class ChannelBufferWavWriter final {
66 public:
kwiberg62eaacf2016-02-17 06:39:05 -080067 explicit ChannelBufferWavWriter(std::unique_ptr<WavWriter> file);
kwiberg942c8512016-08-29 13:10:29 -070068 ~ChannelBufferWavWriter();
69
aluebsb0ad43b2015-11-20 00:11:53 -080070 void Write(const ChannelBuffer<float>& buffer);
71
72 private:
kwiberg62eaacf2016-02-17 06:39:05 -080073 std::unique_ptr<WavWriter> file_;
aluebsb0ad43b2015-11-20 00:11:53 -080074 std::vector<float> interleaved_;
75
76 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavWriter);
77};
78
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070079void WriteIntData(const int16_t* data,
80 size_t length,
81 WavWriter* wav_file,
82 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000083
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070084void WriteFloatData(const float* const* data,
pkasting25702cb2016-01-08 13:50:27 -080085 size_t samples_per_channel,
Peter Kasting69558702016-01-12 16:26:35 -080086 size_t num_channels,
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070087 WavWriter* wav_file,
88 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000089
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000090// Exits on failure; do not use in unit tests.
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070091FILE* OpenFile(const std::string& filename, const char* mode);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000092
pkasting25702cb2016-01-08 13:50:27 -080093size_t SamplesFromRate(int rate);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094
Yves Gerey665174f2018-06-19 15:03:05 +020095void SetFrameSampleRate(AudioFrame* frame, int sample_rate_hz);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000096
97template <typename T>
98void SetContainerFormat(int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -080099 size_t num_channels,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000100 AudioFrame* frame,
kwiberg62eaacf2016-02-17 06:39:05 -0800101 std::unique_ptr<ChannelBuffer<T> >* cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000102 SetFrameSampleRate(frame, sample_rate_hz);
103 frame->num_channels_ = num_channels;
104 cb->reset(new ChannelBuffer<T>(frame->samples_per_channel_, num_channels));
105}
106
Peter Kasting69558702016-01-12 16:26:35 -0800107AudioProcessing::ChannelLayout LayoutFromChannels(size_t num_channels);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000108
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000109template <typename T>
pkasting25702cb2016-01-08 13:50:27 -0800110float ComputeSNR(const T* ref, const T* test, size_t length, float* variance) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000111 float mse = 0;
112 float mean = 0;
113 *variance = 0;
pkasting25702cb2016-01-08 13:50:27 -0800114 for (size_t i = 0; i < length; ++i) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000115 T error = ref[i] - test[i];
116 mse += error * error;
117 *variance += ref[i] * ref[i];
118 mean += ref[i];
119 }
120 mse /= length;
121 *variance /= length;
122 mean /= length;
123 *variance -= mean * mean;
124
125 float snr = 100; // We assign 100 dB to the zero-error case.
126 if (mse > 0)
127 snr = 10 * log10(*variance / mse);
128 return snr;
129}
130
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700131// Returns a vector<T> parsed from whitespace delimited values in to_parse,
132// or an empty vector if the string could not be parsed.
Yves Gerey665174f2018-06-19 15:03:05 +0200133template <typename T>
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700134std::vector<T> ParseList(const std::string& to_parse) {
135 std::vector<T> values;
136
137 std::istringstream str(to_parse);
138 std::copy(
Yves Gerey665174f2018-06-19 15:03:05 +0200139 std::istream_iterator<T>(str), // no-presubmit-check TODO(webrtc:8982)
140 std::istream_iterator<T>(), // no-presubmit-check TODO(webrtc:8982)
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700141 std::back_inserter(values));
142
143 return values;
144}
145
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000146} // namespace webrtc
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700147
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200148#endif // MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_