2018-04-19 17:56:55 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "modules/audio_processing/agc2/rnn_vad/test_utils.h"
|
|
|
|
|
|
2020-11-11 12:54:39 +01:00
|
|
|
#include <algorithm>
|
2020-12-07 17:02:22 +01:00
|
|
|
#include <fstream>
|
2019-09-17 17:06:18 +02:00
|
|
|
#include <memory>
|
2020-12-07 17:02:22 +01:00
|
|
|
#include <type_traits>
|
|
|
|
|
#include <vector>
|
2019-09-17 17:06:18 +02:00
|
|
|
|
2018-04-19 17:56:55 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2020-10-29 20:50:13 +01:00
|
|
|
#include "rtc_base/numerics/safe_compare.h"
|
2018-04-19 17:56:55 +02:00
|
|
|
#include "test/gtest.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "test/testsupport/file_utils.h"
|
2018-04-19 17:56:55 +02:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2018-04-27 16:44:11 +02:00
|
|
|
namespace rnn_vad {
|
2018-04-19 17:56:55 +02:00
|
|
|
namespace {
|
|
|
|
|
|
2020-12-07 17:02:22 +01:00
|
|
|
// File reader for binary files that contain a sequence of values with
|
|
|
|
|
// arithmetic type `T`. The values of type `T` that are read are cast to float.
|
|
|
|
|
template <typename T>
|
|
|
|
|
class FloatFileReader : public FileReader {
|
|
|
|
|
public:
|
|
|
|
|
static_assert(std::is_arithmetic<T>::value, "");
|
|
|
|
|
FloatFileReader(const std::string& filename)
|
|
|
|
|
: is_(filename, std::ios::binary | std::ios::ate),
|
|
|
|
|
size_(is_.tellg() / sizeof(T)) {
|
|
|
|
|
RTC_CHECK(is_);
|
|
|
|
|
SeekBeginning();
|
|
|
|
|
}
|
|
|
|
|
FloatFileReader(const FloatFileReader&) = delete;
|
|
|
|
|
FloatFileReader& operator=(const FloatFileReader&) = delete;
|
|
|
|
|
~FloatFileReader() = default;
|
|
|
|
|
|
|
|
|
|
int size() const override { return size_; }
|
|
|
|
|
bool ReadChunk(rtc::ArrayView<float> dst) override {
|
|
|
|
|
const std::streamsize bytes_to_read = dst.size() * sizeof(T);
|
|
|
|
|
if (std::is_same<T, float>::value) {
|
|
|
|
|
is_.read(reinterpret_cast<char*>(dst.data()), bytes_to_read);
|
|
|
|
|
} else {
|
|
|
|
|
buffer_.resize(dst.size());
|
|
|
|
|
is_.read(reinterpret_cast<char*>(buffer_.data()), bytes_to_read);
|
|
|
|
|
std::transform(buffer_.begin(), buffer_.end(), dst.begin(),
|
|
|
|
|
[](const T& v) -> float { return static_cast<float>(v); });
|
|
|
|
|
}
|
|
|
|
|
return is_.gcount() == bytes_to_read;
|
|
|
|
|
}
|
|
|
|
|
bool ReadValue(float& dst) override { return ReadChunk({&dst, 1}); }
|
|
|
|
|
void SeekForward(int hop) override { is_.seekg(hop * sizeof(T), is_.cur); }
|
|
|
|
|
void SeekBeginning() override { is_.seekg(0, is_.beg); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::ifstream is_;
|
|
|
|
|
const int size_;
|
|
|
|
|
std::vector<T> buffer_;
|
|
|
|
|
};
|
2018-04-19 17:56:55 +02:00
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2018-04-27 16:44:11 +02:00
|
|
|
using webrtc::test::ResourcePath;
|
|
|
|
|
|
2018-05-08 11:10:45 +02:00
|
|
|
void ExpectEqualFloatArray(rtc::ArrayView<const float> expected,
|
|
|
|
|
rtc::ArrayView<const float> computed) {
|
|
|
|
|
ASSERT_EQ(expected.size(), computed.size());
|
2020-10-29 20:50:13 +01:00
|
|
|
for (int i = 0; rtc::SafeLt(i, expected.size()); ++i) {
|
2018-05-08 11:10:45 +02:00
|
|
|
SCOPED_TRACE(i);
|
|
|
|
|
EXPECT_FLOAT_EQ(expected[i], computed[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 17:56:55 +02:00
|
|
|
void ExpectNearAbsolute(rtc::ArrayView<const float> expected,
|
|
|
|
|
rtc::ArrayView<const float> computed,
|
2018-04-27 16:44:11 +02:00
|
|
|
float tolerance) {
|
2018-04-19 17:56:55 +02:00
|
|
|
ASSERT_EQ(expected.size(), computed.size());
|
2020-10-29 20:50:13 +01:00
|
|
|
for (int i = 0; rtc::SafeLt(i, expected.size()); ++i) {
|
2018-04-19 17:56:55 +02:00
|
|
|
SCOPED_TRACE(i);
|
|
|
|
|
EXPECT_NEAR(expected[i], computed[i], tolerance);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-07 17:02:22 +01:00
|
|
|
std::unique_ptr<FileReader> CreatePcmSamplesReader() {
|
|
|
|
|
return std::make_unique<FloatFileReader<int16_t>>(
|
|
|
|
|
/*filename=*/test::ResourcePath("audio_processing/agc2/rnn_vad/samples",
|
|
|
|
|
"pcm"));
|
2018-05-15 15:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-07 17:02:22 +01:00
|
|
|
ChunksFileReader CreatePitchBuffer24kHzReader() {
|
|
|
|
|
auto reader = std::make_unique<FloatFileReader<float>>(
|
|
|
|
|
/*filename=*/test::ResourcePath(
|
|
|
|
|
"audio_processing/agc2/rnn_vad/pitch_buf_24k", "dat"));
|
|
|
|
|
const int num_chunks = rtc::CheckedDivExact(reader->size(), kBufSize24kHz);
|
|
|
|
|
return {/*chunk_size=*/kBufSize24kHz, num_chunks, std::move(reader)};
|
2018-04-19 17:56:55 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-07 17:02:22 +01:00
|
|
|
ChunksFileReader CreateLpResidualAndPitchInfoReader() {
|
|
|
|
|
constexpr int kPitchInfoSize = 2; // Pitch period and strength.
|
|
|
|
|
constexpr int kChunkSize = kBufSize24kHz + kPitchInfoSize;
|
|
|
|
|
auto reader = std::make_unique<FloatFileReader<float>>(
|
|
|
|
|
/*filename=*/test::ResourcePath(
|
|
|
|
|
"audio_processing/agc2/rnn_vad/pitch_lp_res", "dat"));
|
|
|
|
|
const int num_chunks = rtc::CheckedDivExact(reader->size(), kChunkSize);
|
|
|
|
|
return {kChunkSize, num_chunks, std::move(reader)};
|
2018-04-19 17:56:55 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-08 11:26:05 +01:00
|
|
|
std::unique_ptr<FileReader> CreateGruInputReader() {
|
|
|
|
|
return std::make_unique<FloatFileReader<float>>(
|
|
|
|
|
/*filename=*/test::ResourcePath("audio_processing/agc2/rnn_vad/gru_in",
|
|
|
|
|
"dat"));
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-07 17:02:22 +01:00
|
|
|
std::unique_ptr<FileReader> CreateVadProbsReader() {
|
|
|
|
|
return std::make_unique<FloatFileReader<float>>(
|
|
|
|
|
/*filename=*/test::ResourcePath("audio_processing/agc2/rnn_vad/vad_prob",
|
|
|
|
|
"dat"));
|
Reland "Reland "AGC2 RNN VAD: Recurrent Neural Network impl""
This reverts commit 3c9f47434f0af3b16f1b8f43cd4500be6fd2ac17.
Reason for revert: downstream projects fixed
Original change's description:
> Revert "Reland "AGC2 RNN VAD: Recurrent Neural Network impl""
>
> This reverts commit e0bba68edea74ca33f4c492eba290c089f233f6b.
>
> Reason for revert: <INSERT REASONING HERE>
>
> Original change's description:
> > Reland "AGC2 RNN VAD: Recurrent Neural Network impl"
> >
> > This reverts commit 97e349ace7a3fd64fff270f0d780e02bb708f503.
> >
> > Reason for revert: downstream projects fixed
> >
> > Original change's description:
> > > Revert "AGC2 RNN VAD: Recurrent Neural Network impl"
> > >
> > > This reverts commit 2491cb73820fe82923b848dfcab6772b4b0addb0.
> > >
> > > Reason for revert: broke internal build
> > >
> > > Original change's description:
> > > > AGC2 RNN VAD: Recurrent Neural Network impl
> > > >
> > > > RNN implementation for the AGC2 VAD that includes a fully connected
> > > > layer and a gated recurrent unit layer.
> > > >
> > > > Bug: webrtc:9076
> > > > Change-Id: Ibb8b0b4e9213f09eb9dbe118bbdc94d7e8e4f91b
> > > > Reviewed-on: https://webrtc-review.googlesource.com/72060
> > > > Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
> > > > Reviewed-by: Alex Loiko <aleloi@webrtc.org>
> > > > Reviewed-by: Ivo Creusen <ivoc@webrtc.org>
> > > > Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
> > > > Cr-Commit-Position: refs/heads/master@{#23101}
> > >
> > > TBR=phoglund@webrtc.org,alessiob@webrtc.org,aleloi@webrtc.org,ivoc@webrtc.org
> > >
> > > Change-Id: Ic311c4b7d79094e959d3a2c4a53c398f34c954e2
> > > No-Presubmit: true
> > > No-Tree-Checks: true
> > > No-Try: true
> > > Bug: webrtc:9076
> > > Reviewed-on: https://webrtc-review.googlesource.com/74200
> > > Reviewed-by: Sam Zackrisson <saza@webrtc.org>
> > > Commit-Queue: Sam Zackrisson <saza@webrtc.org>
> > > Cr-Commit-Position: refs/heads/master@{#23103}
> >
> > TBR=phoglund@webrtc.org,saza@webrtc.org,alessiob@webrtc.org,aleloi@webrtc.org,ivoc@webrtc.org
> >
> > Change-Id: I0c7f8e0f59be926322d05b1da1d4d19c0777dab2
> > No-Presubmit: true
> > No-Tree-Checks: true
> > No-Try: true
> > Bug: webrtc:9076
> > Reviewed-on: https://webrtc-review.googlesource.com/74460
> > Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
> > Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
> > Cr-Commit-Position: refs/heads/master@{#23113}
>
> TBR=phoglund@webrtc.org,saza@webrtc.org,alessiob@webrtc.org,aleloi@webrtc.org,ivoc@webrtc.org
>
> Change-Id: I3985a6d38df1d4438a50d031bc9f6cf41eb83121
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: webrtc:9076
> Reviewed-on: https://webrtc-review.googlesource.com/74560
> Reviewed-by: Sam Zackrisson <saza@webrtc.org>
> Commit-Queue: Sam Zackrisson <saza@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#23117}
TBR=phoglund@webrtc.org,saza@webrtc.org,alessiob@webrtc.org,aleloi@webrtc.org,ivoc@webrtc.org
# Not skipping CQ checks because original CL landed > 1 day ago.
Bug: webrtc:9076
Change-Id: I4d81786837017d4daf0dbb1218306795b977ade5
Reviewed-on: https://webrtc-review.googlesource.com/74760
Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23138}
2018-05-07 09:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-02 06:58:45 +00:00
|
|
|
PitchTestData::PitchTestData() {
|
2020-12-07 17:02:22 +01:00
|
|
|
FloatFileReader<float> reader(
|
|
|
|
|
/*filename=*/ResourcePath(
|
|
|
|
|
"audio_processing/agc2/rnn_vad/pitch_search_int", "dat"));
|
|
|
|
|
reader.ReadChunk(pitch_buffer_24k_);
|
|
|
|
|
reader.ReadChunk(square_energies_24k_);
|
|
|
|
|
reader.ReadChunk(auto_correlation_12k_);
|
2020-11-11 12:54:39 +01:00
|
|
|
// Reverse the order of the squared energy values.
|
|
|
|
|
// Required after the WebRTC CL 191703 which switched to forward computation.
|
2020-12-07 17:02:22 +01:00
|
|
|
std::reverse(square_energies_24k_.begin(), square_energies_24k_.end());
|
2019-04-02 06:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PitchTestData::~PitchTestData() = default;
|
|
|
|
|
|
2018-04-27 16:44:11 +02:00
|
|
|
} // namespace rnn_vad
|
2018-04-19 17:56:55 +02:00
|
|
|
} // namespace webrtc
|