blob: 7ad462c0957e321cebb5d808eea7d41ee756f069 [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
43 DISALLOW_COPY_AND_ASSIGN(RawFile);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000044};
45
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070046void WriteIntData(const int16_t* data,
47 size_t length,
48 WavWriter* wav_file,
49 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000050
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070051void WriteFloatData(const float* const* data,
52 int samples_per_channel,
53 int num_channels,
54 WavWriter* wav_file,
55 RawFile* raw_file);
aluebs@webrtc.org021e76f2014-09-04 18:12:00 +000056
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000057// Exits on failure; do not use in unit tests.
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070058FILE* OpenFile(const std::string& filename, const char* mode);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000059
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070060int SamplesFromRate(int rate);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000061
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070062void SetFrameSampleRate(AudioFrame* frame,
63 int sample_rate_hz);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000064
65template <typename T>
66void SetContainerFormat(int sample_rate_hz,
67 int num_channels,
68 AudioFrame* frame,
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000069 rtc::scoped_ptr<ChannelBuffer<T> >* cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000070 SetFrameSampleRate(frame, sample_rate_hz);
71 frame->num_channels_ = num_channels;
72 cb->reset(new ChannelBuffer<T>(frame->samples_per_channel_, num_channels));
73}
74
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070075AudioProcessing::ChannelLayout LayoutFromChannels(int num_channels);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000076
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +000077template <typename T>
78float ComputeSNR(const T* ref, const T* test, int length, float* variance) {
79 float mse = 0;
80 float mean = 0;
81 *variance = 0;
82 for (int i = 0; i < length; ++i) {
83 T error = ref[i] - test[i];
84 mse += error * error;
85 *variance += ref[i] * ref[i];
86 mean += ref[i];
87 }
88 mse /= length;
89 *variance /= length;
90 mean /= length;
91 *variance -= mean * mean;
92
93 float snr = 100; // We assign 100 dB to the zero-error case.
94 if (mse > 0)
95 snr = 10 * log10(*variance / mse);
96 return snr;
97}
98
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070099// Returns a vector<T> parsed from whitespace delimited values in to_parse,
100// or an empty vector if the string could not be parsed.
101template<typename T>
102std::vector<T> ParseList(const std::string& to_parse) {
103 std::vector<T> values;
104
105 std::istringstream str(to_parse);
106 std::copy(
107 std::istream_iterator<T>(str),
108 std::istream_iterator<T>(),
109 std::back_inserter(values));
110
111 return values;
112}
113
114// Parses the array geometry from the command line.
115//
116// If a vector with size != num_mics is returned, an error has occurred and an
117// appropriate error message has been printed to stdout.
118std::vector<Point> ParseArrayGeometry(const std::string& mic_positions,
119 size_t num_mics);
120
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000121} // namespace webrtc
Andrew MacDonaldcb05b722015-05-07 22:17:51 -0700122
123#endif // WEBRTC_MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_