2014-01-07 17:45:09 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#ifndef MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
|
|
|
|
|
#define MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
|
2015-05-07 22:17:51 -07:00
|
|
|
|
2014-10-31 04:58:14 +00:00
|
|
|
#include <math.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2015-05-07 22:17:51 -07:00
|
|
|
#include <iterator>
|
2014-09-04 18:12:00 +00:00
|
|
|
#include <limits>
|
2016-02-17 06:39:05 -08:00
|
|
|
#include <memory>
|
2018-09-13 10:07:07 +02:00
|
|
|
#include <sstream> // no-presubmit-check TODO(webrtc:8982)
|
2015-05-07 22:17:51 -07:00
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2014-09-04 18:12:00 +00:00
|
|
|
|
2022-08-16 14:44:38 +02:00
|
|
|
#include "absl/strings/string_view.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "common_audio/channel_buffer.h"
|
|
|
|
|
#include "common_audio/wav_file.h"
|
|
|
|
|
#include "modules/audio_processing/include/audio_processing.h"
|
2014-03-10 22:26:12 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
static const AudioProcessing::Error kNoErr = AudioProcessing::kNoError;
|
|
|
|
|
#define EXPECT_NOERR(expr) EXPECT_EQ(kNoErr, (expr))
|
2014-01-07 17:45:09 +00:00
|
|
|
|
2020-03-19 12:33:29 +01:00
|
|
|
// Encapsulates samples and metadata for an integer frame.
|
|
|
|
|
struct Int16FrameData {
|
|
|
|
|
// Max data size that matches the data size of the AudioFrame class, providing
|
|
|
|
|
// storage for 8 channels of 96 kHz data.
|
|
|
|
|
static const int kMaxDataSizeSamples = 7680;
|
|
|
|
|
|
|
|
|
|
Int16FrameData() {
|
|
|
|
|
sample_rate_hz = 0;
|
|
|
|
|
num_channels = 0;
|
|
|
|
|
samples_per_channel = 0;
|
|
|
|
|
data.fill(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CopyFrom(const Int16FrameData& src) {
|
|
|
|
|
samples_per_channel = src.samples_per_channel;
|
|
|
|
|
sample_rate_hz = src.sample_rate_hz;
|
|
|
|
|
num_channels = src.num_channels;
|
|
|
|
|
|
|
|
|
|
const size_t length = samples_per_channel * num_channels;
|
|
|
|
|
RTC_CHECK_LE(length, kMaxDataSizeSamples);
|
|
|
|
|
memcpy(data.data(), src.data.data(), sizeof(int16_t) * length);
|
|
|
|
|
}
|
|
|
|
|
std::array<int16_t, kMaxDataSizeSamples> data;
|
|
|
|
|
int32_t sample_rate_hz;
|
|
|
|
|
size_t num_channels;
|
|
|
|
|
size_t samples_per_channel;
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-20 00:11:53 -08:00
|
|
|
// Reads ChannelBuffers from a provided WavReader.
|
|
|
|
|
class ChannelBufferWavReader final {
|
|
|
|
|
public:
|
2016-02-17 06:39:05 -08:00
|
|
|
explicit ChannelBufferWavReader(std::unique_ptr<WavReader> file);
|
2016-08-29 13:10:29 -07:00
|
|
|
~ChannelBufferWavReader();
|
2015-11-20 00:11:53 -08:00
|
|
|
|
2022-01-21 09:49:39 +09:00
|
|
|
ChannelBufferWavReader(const ChannelBufferWavReader&) = delete;
|
|
|
|
|
ChannelBufferWavReader& operator=(const ChannelBufferWavReader&) = delete;
|
|
|
|
|
|
2021-07-28 20:50:03 +02:00
|
|
|
// Reads data from the file according to the `buffer` format. Returns false if
|
2015-11-20 00:11:53 -08:00
|
|
|
// a full buffer can't be read from the file.
|
|
|
|
|
bool Read(ChannelBuffer<float>* buffer);
|
|
|
|
|
|
|
|
|
|
private:
|
2016-02-17 06:39:05 -08:00
|
|
|
std::unique_ptr<WavReader> file_;
|
2015-11-20 00:11:53 -08:00
|
|
|
std::vector<float> interleaved_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Writes ChannelBuffers to a provided WavWriter.
|
|
|
|
|
class ChannelBufferWavWriter final {
|
|
|
|
|
public:
|
2016-02-17 06:39:05 -08:00
|
|
|
explicit ChannelBufferWavWriter(std::unique_ptr<WavWriter> file);
|
2016-08-29 13:10:29 -07:00
|
|
|
~ChannelBufferWavWriter();
|
|
|
|
|
|
2022-01-21 09:49:39 +09:00
|
|
|
ChannelBufferWavWriter(const ChannelBufferWavWriter&) = delete;
|
|
|
|
|
ChannelBufferWavWriter& operator=(const ChannelBufferWavWriter&) = delete;
|
|
|
|
|
|
2015-11-20 00:11:53 -08:00
|
|
|
void Write(const ChannelBuffer<float>& buffer);
|
|
|
|
|
|
|
|
|
|
private:
|
2016-02-17 06:39:05 -08:00
|
|
|
std::unique_ptr<WavWriter> file_;
|
2015-11-20 00:11:53 -08:00
|
|
|
std::vector<float> interleaved_;
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-12 09:57:01 +02:00
|
|
|
// Takes a pointer to a vector. Allows appending the samples of channel buffers
|
|
|
|
|
// to the given vector, by interleaving the samples and converting them to float
|
|
|
|
|
// S16.
|
|
|
|
|
class ChannelBufferVectorWriter final {
|
|
|
|
|
public:
|
|
|
|
|
explicit ChannelBufferVectorWriter(std::vector<float>* output);
|
|
|
|
|
ChannelBufferVectorWriter(const ChannelBufferVectorWriter&) = delete;
|
|
|
|
|
ChannelBufferVectorWriter& operator=(const ChannelBufferVectorWriter&) =
|
|
|
|
|
delete;
|
|
|
|
|
~ChannelBufferVectorWriter();
|
|
|
|
|
|
2021-07-28 20:50:03 +02:00
|
|
|
// Creates an interleaved copy of `buffer`, converts the samples to float S16
|
2019-08-12 09:57:01 +02:00
|
|
|
// and appends the result to output_.
|
|
|
|
|
void Write(const ChannelBuffer<float>& buffer);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<float> interleaved_buffer_;
|
|
|
|
|
std::vector<float>* output_;
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-10 22:26:12 +00:00
|
|
|
// Exits on failure; do not use in unit tests.
|
2022-08-16 14:44:38 +02:00
|
|
|
FILE* OpenFile(absl::string_view filename, absl::string_view mode);
|
2014-03-10 22:26:12 +00:00
|
|
|
|
2020-03-19 12:33:29 +01:00
|
|
|
void SetFrameSampleRate(Int16FrameData* frame, int sample_rate_hz);
|
2014-03-10 22:26:12 +00:00
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
void SetContainerFormat(int sample_rate_hz,
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
size_t num_channels,
|
2020-03-19 12:33:29 +01:00
|
|
|
Int16FrameData* frame,
|
2016-02-17 06:39:05 -08:00
|
|
|
std::unique_ptr<ChannelBuffer<T> >* cb) {
|
2014-03-10 22:26:12 +00:00
|
|
|
SetFrameSampleRate(frame, sample_rate_hz);
|
2020-03-19 12:33:29 +01:00
|
|
|
frame->num_channels = num_channels;
|
|
|
|
|
cb->reset(new ChannelBuffer<T>(frame->samples_per_channel, num_channels));
|
2014-03-10 22:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-31 04:58:14 +00:00
|
|
|
template <typename T>
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
float ComputeSNR(const T* ref, const T* test, size_t length, float* variance) {
|
2014-10-31 04:58:14 +00:00
|
|
|
float mse = 0;
|
|
|
|
|
float mean = 0;
|
|
|
|
|
*variance = 0;
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2014-10-31 04:58:14 +00:00
|
|
|
T error = ref[i] - test[i];
|
|
|
|
|
mse += error * error;
|
|
|
|
|
*variance += ref[i] * ref[i];
|
|
|
|
|
mean += ref[i];
|
|
|
|
|
}
|
|
|
|
|
mse /= length;
|
|
|
|
|
*variance /= length;
|
|
|
|
|
mean /= length;
|
|
|
|
|
*variance -= mean * mean;
|
|
|
|
|
|
|
|
|
|
float snr = 100; // We assign 100 dB to the zero-error case.
|
|
|
|
|
if (mse > 0)
|
|
|
|
|
snr = 10 * log10(*variance / mse);
|
|
|
|
|
return snr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-07 22:17:51 -07:00
|
|
|
// Returns a vector<T> parsed from whitespace delimited values in to_parse,
|
|
|
|
|
// or an empty vector if the string could not be parsed.
|
|
|
|
|
template <typename T>
|
2022-08-16 14:44:38 +02:00
|
|
|
std::vector<T> ParseList(absl::string_view to_parse) {
|
2015-05-07 22:17:51 -07:00
|
|
|
std::vector<T> values;
|
|
|
|
|
|
2022-08-16 14:44:38 +02:00
|
|
|
std::istringstream str( // no-presubmit-check TODO(webrtc:8982)
|
|
|
|
|
std::string{to_parse});
|
2015-05-07 22:17:51 -07:00
|
|
|
std::copy(
|
|
|
|
|
std::istream_iterator<T>(str), // no-presubmit-check TODO(webrtc:8982)
|
|
|
|
|
std::istream_iterator<T>(), // no-presubmit-check TODO(webrtc:8982)
|
|
|
|
|
std::back_inserter(values));
|
|
|
|
|
|
|
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-10 22:26:12 +00:00
|
|
|
} // namespace webrtc
|
2015-05-07 22:17:51 -07:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#endif // MODULES_AUDIO_PROCESSING_TEST_TEST_UTILS_H_
|