blob: ca66520cb561ab9d5a8fe9b74a4941a06933523e [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
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070011#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
12#define WEBRTC_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>
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070018#include <string>
19#include <vector>
20
21#include "webrtc/base/constructormagic.h"
kjellander@webrtc.org035e9122015-01-28 19:57:00 +000022#include "webrtc/common_audio/channel_buffer.h"
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000023#include "webrtc/common_audio/wav_file.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000024#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010025#include "webrtc/modules/include/module_common_types.h"
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000026
27namespace webrtc {
28
29static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
30#define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000031
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070032class RawFile final {
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000033 public:
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070034 explicit RawFile(const std::string& filename);
35 ~RawFile();
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000036
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070037 void WriteSamples(const int16_t* samples, size_t num_samples);
38 void WriteSamples(const float* samples, size_t num_samples);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000039
40 private:
41 FILE* file_handle_;
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070042
henrikg3c089d72015-09-16 05:37:44 -070043 RTC_DISALLOW_COPY_AND_ASSIGN(RawFile);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000044};
45
aluebsb0ad43b2015-11-20 00:11:53 -080046// Reads ChannelBuffers from a provided WavReader.
47class ChannelBufferWavReader final {
48 public:
kwiberg62eaacf2016-02-17 06:39:05 -080049 explicit ChannelBufferWavReader(std::unique_ptr<WavReader> file);
kwiberg942c8512016-08-29 13:10:29 -070050 ~ChannelBufferWavReader();
aluebsb0ad43b2015-11-20 00:11:53 -080051
52 // Reads data from the file according to the |buffer| format. Returns false if
53 // a full buffer can't be read from the file.
54 bool Read(ChannelBuffer<float>* buffer);
55
56 private:
kwiberg62eaacf2016-02-17 06:39:05 -080057 std::unique_ptr<WavReader> file_;
aluebsb0ad43b2015-11-20 00:11:53 -080058 std::vector<float> interleaved_;
59
60 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavReader);
61};
62
63// Writes ChannelBuffers to a provided WavWriter.
64class ChannelBufferWavWriter final {
65 public:
kwiberg62eaacf2016-02-17 06:39:05 -080066 explicit ChannelBufferWavWriter(std::unique_ptr<WavWriter> file);
kwiberg942c8512016-08-29 13:10:29 -070067 ~ChannelBufferWavWriter();
68
aluebsb0ad43b2015-11-20 00:11:53 -080069 void Write(const ChannelBuffer<float>& buffer);
70
71 private:
kwiberg62eaacf2016-02-17 06:39:05 -080072 std::unique_ptr<WavWriter> file_;
aluebsb0ad43b2015-11-20 00:11:53 -080073 std::vector<float> interleaved_;
74
75 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavWriter);
76};
77
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070078void WriteIntData(const int16_t* data,
79 size_t length,
80 WavWriter* wav_file,
81 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000082
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070083void WriteFloatData(const float* const* data,
pkasting25702cb2016-01-08 13:50:27 -080084 size_t samples_per_channel,
Peter Kasting69558702016-01-12 16:26:35 -080085 size_t num_channels,
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070086 WavWriter* wav_file,
87 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000088
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000089// Exits on failure; do not use in unit tests.
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070090FILE* OpenFile(const std::string& filename, const char* mode);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000091
pkasting25702cb2016-01-08 13:50:27 -080092size_t SamplesFromRate(int rate);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000093
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070094void SetFrameSampleRate(AudioFrame* frame,
95 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.
133template<typename T>
134std::vector<T> ParseList(const std::string& to_parse) {
135 std::vector<T> values;
136
137 std::istringstream str(to_parse);
138 std::copy(
139 std::istream_iterator<T>(str),
140 std::istream_iterator<T>(),
141 std::back_inserter(values));
142
143 return values;
144}
145
146// Parses the array geometry from the command line.
147//
148// If a vector with size != num_mics is returned, an error has occurred and an
149// appropriate error message has been printed to stdout.
150std::vector<Point> ParseArrayGeometry(const std::string& mic_positions,
151 size_t num_mics);
152
aluebsb0ad43b2015-11-20 00:11:53 -0800153// Same as above, but without the num_mics check for when it isn't available.
154std::vector<Point> ParseArrayGeometry(const std::string& mic_positions);
155
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000156} // namespace webrtc
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700157
158#endif // WEBRTC_MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_