blob: f53d8ac81c242bc454e4d9f82a5698e5619c4de8 [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>
17#include <string>
18#include <vector>
19
20#include "webrtc/base/constructormagic.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000021#include "webrtc/base/scoped_ptr.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"
25#include "webrtc/modules/interface/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
andrewbdafe312015-10-29 23:42:54 -070046// Reads ChannelBuffers from a provided WavReader.
47class ChannelBufferWavReader final {
48 public:
49 explicit ChannelBufferWavReader(rtc::scoped_ptr<WavReader> file);
50
51 // Reads data from the file according to the |buffer| format. Returns false if
52 // a full buffer can't be read from the file.
53 bool Read(ChannelBuffer<float>* buffer);
54
55 private:
56 rtc::scoped_ptr<WavReader> file_;
57 std::vector<float> interleaved_;
58
59 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavReader);
60};
61
62// Writes ChannelBuffers to a provided WavWriter.
63class ChannelBufferWavWriter final {
64 public:
65 explicit ChannelBufferWavWriter(rtc::scoped_ptr<WavWriter> file);
66 void Write(const ChannelBuffer<float>& buffer);
67
68 private:
69 rtc::scoped_ptr<WavWriter> file_;
70 std::vector<float> interleaved_;
71
72 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelBufferWavWriter);
73};
74
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070075void WriteIntData(const int16_t* data,
76 size_t length,
77 WavWriter* wav_file,
78 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000079
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070080void WriteFloatData(const float* const* data,
81 int samples_per_channel,
82 int num_channels,
83 WavWriter* wav_file,
84 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000085
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000086// Exits on failure; do not use in unit tests.
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070087FILE* OpenFile(const std::string& filename, const char* mode);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000088
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070089int SamplesFromRate(int rate);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000090
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070091void SetFrameSampleRate(AudioFrame* frame,
92 int sample_rate_hz);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000093
94template <typename T>
95void SetContainerFormat(int sample_rate_hz,
96 int num_channels,
97 AudioFrame* frame,
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000098 rtc::scoped_ptr<ChannelBuffer<T> >* cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000099 SetFrameSampleRate(frame, sample_rate_hz);
100 frame->num_channels_ = num_channels;
101 cb->reset(new ChannelBuffer<T>(frame->samples_per_channel_, num_channels));
102}
103
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700104AudioProcessing::ChannelLayout LayoutFromChannels(int num_channels);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000105
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +0000106template <typename T>
107float ComputeSNR(const T* ref, const T* test, int length, float* variance) {
108 float mse = 0;
109 float mean = 0;
110 *variance = 0;
111 for (int i = 0; i < length; ++i) {
112 T error = ref[i] - test[i];
113 mse += error * error;
114 *variance += ref[i] * ref[i];
115 mean += ref[i];
116 }
117 mse /= length;
118 *variance /= length;
119 mean /= length;
120 *variance -= mean * mean;
121
122 float snr = 100; // We assign 100 dB to the zero-error case.
123 if (mse > 0)
124 snr = 10 * log10(*variance / mse);
125 return snr;
126}
127
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700128// Returns a vector<T> parsed from whitespace delimited values in to_parse,
129// or an empty vector if the string could not be parsed.
130template<typename T>
131std::vector<T> ParseList(const std::string& to_parse) {
132 std::vector<T> values;
133
134 std::istringstream str(to_parse);
135 std::copy(
136 std::istream_iterator<T>(str),
137 std::istream_iterator<T>(),
138 std::back_inserter(values));
139
140 return values;
141}
142
143// Parses the array geometry from the command line.
144//
145// If a vector with size != num_mics is returned, an error has occurred and an
146// appropriate error message has been printed to stdout.
147std::vector<Point> ParseArrayGeometry(const std::string& mic_positions,
148 size_t num_mics);
149
andrewbdafe312015-10-29 23:42:54 -0700150// Same as above, but without the num_mics check for when it isn't available.
151std::vector<Point> ParseArrayGeometry(const std::string& mic_positions);
152
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000153} // namespace webrtc
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700154
155#endif // WEBRTC_MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_