mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 13:50:40 +01:00

This is a reland ofa66395e72f
Original change's description: > Reland "Add core multi-channel pipeline in AEC3 This CL adds basic the basic pipeline to support multi-channel processing in AEC3." > > This is a reland off3a197e553
> > Original change's description: > > Add core multi-channel pipeline in AEC3 > > This CL adds basic the basic pipeline to support multi-channel > > processing in AEC3. > > > > Apart from that, it removes the 8 kHz processing support in several > > places of the AEC3 code. > > > > Bug: webrtc:10913 > > Change-Id: If5b75fa325ed0071deea94a7546cb4a7adf22137 > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/150332 > > Commit-Queue: Per Åhgren <peah@webrtc.org> > > Reviewed-by: Sam Zackrisson <saza@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#29017} > > Bug: webrtc:10913 > Change-Id: Ifc4b13bd994cfd22dca8f8755fa5700617cc379d > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/151124 > Reviewed-by: Sam Zackrisson <saza@webrtc.org> > Commit-Queue: Per Åhgren <peah@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#29034} Bug: webrtc:10913 Change-Id: Id8da5666df8c86f290c73ad5dc9958199f1a7ebe Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/151127 Commit-Queue: Sam Zackrisson <saza@webrtc.org> Reviewed-by: Sam Zackrisson <saza@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29042}
152 lines
5.2 KiB
C++
152 lines
5.2 KiB
C++
/*
|
|
* Copyright (c) 2017 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/aec3/render_signal_analyzer.h"
|
|
|
|
#include <math.h>
|
|
|
|
#include <array>
|
|
#include <cmath>
|
|
#include <vector>
|
|
|
|
#include "api/array_view.h"
|
|
#include "modules/audio_processing/aec3/aec3_common.h"
|
|
#include "modules/audio_processing/aec3/aec3_fft.h"
|
|
#include "modules/audio_processing/aec3/fft_data.h"
|
|
#include "modules/audio_processing/aec3/render_delay_buffer.h"
|
|
#include "modules/audio_processing/test/echo_canceller_test_tools.h"
|
|
#include "rtc_base/random.h"
|
|
#include "test/gtest.h"
|
|
|
|
namespace webrtc {
|
|
namespace {
|
|
|
|
constexpr float kPi = 3.141592f;
|
|
|
|
void ProduceSinusoid(int sample_rate_hz,
|
|
float sinusoidal_frequency_hz,
|
|
size_t* sample_counter,
|
|
std::vector<std::vector<std::vector<float>>>* x) {
|
|
// Produce a sinusoid of the specified frequency.
|
|
for (size_t k = *sample_counter, j = 0; k < (*sample_counter + kBlockSize);
|
|
++k, ++j) {
|
|
for (size_t channel = 0; channel < (*x)[0].size(); ++channel) {
|
|
(*x)[0][channel][j] =
|
|
32767.f *
|
|
std::sin(2.f * kPi * sinusoidal_frequency_hz * k / sample_rate_hz);
|
|
}
|
|
}
|
|
*sample_counter = *sample_counter + kBlockSize;
|
|
|
|
for (size_t band = 1; band < x->size(); ++band) {
|
|
for (size_t channel = 0; channel < (*x)[band].size(); ++channel) {
|
|
std::fill((*x)[band][channel].begin(), (*x)[band][channel].end(), 0.f);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
|
|
// Verifies that the check for non-null output parameter works.
|
|
TEST(RenderSignalAnalyzer, NullMaskOutput) {
|
|
RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
|
|
EXPECT_DEATH(analyzer.MaskRegionsAroundNarrowBands(nullptr), "");
|
|
}
|
|
|
|
#endif
|
|
|
|
// Verify that no narrow bands are detected in a Gaussian noise signal.
|
|
TEST(RenderSignalAnalyzer, NoFalseDetectionOfNarrowBands) {
|
|
RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
|
|
Random random_generator(42U);
|
|
std::vector<std::vector<std::vector<float>>> x(
|
|
3,
|
|
std::vector<std::vector<float>>(1, std::vector<float>(kBlockSize, 0.f)));
|
|
std::array<float, kBlockSize> x_old;
|
|
std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
|
|
RenderDelayBuffer::Create(EchoCanceller3Config(), 48000, 1));
|
|
std::array<float, kFftLengthBy2Plus1> mask;
|
|
x_old.fill(0.f);
|
|
|
|
for (size_t k = 0; k < 100; ++k) {
|
|
RandomizeSampleVector(&random_generator, x[0][0]);
|
|
|
|
render_delay_buffer->Insert(x);
|
|
if (k == 0) {
|
|
render_delay_buffer->Reset();
|
|
}
|
|
render_delay_buffer->PrepareCaptureProcessing();
|
|
|
|
analyzer.Update(*render_delay_buffer->GetRenderBuffer(),
|
|
absl::optional<size_t>(0));
|
|
}
|
|
|
|
mask.fill(1.f);
|
|
analyzer.MaskRegionsAroundNarrowBands(&mask);
|
|
EXPECT_TRUE(
|
|
std::all_of(mask.begin(), mask.end(), [](float a) { return a == 1.f; }));
|
|
EXPECT_FALSE(analyzer.PoorSignalExcitation());
|
|
}
|
|
|
|
// Verify that a sinusiod signal is detected as narrow bands.
|
|
TEST(RenderSignalAnalyzer, NarrowBandDetection) {
|
|
RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
|
|
Random random_generator(42U);
|
|
constexpr size_t kNumChannels = 1;
|
|
constexpr int kSampleRateHz = 48000;
|
|
constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
|
|
std::vector<std::vector<std::vector<float>>> x(
|
|
kNumBands, std::vector<std::vector<float>>(
|
|
kNumChannels, std::vector<float>(kBlockSize, 0.f)));
|
|
std::array<float, kBlockSize> x_old;
|
|
Aec3Fft fft;
|
|
EchoCanceller3Config config;
|
|
std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
|
|
RenderDelayBuffer::Create(config, kSampleRateHz, kNumChannels));
|
|
|
|
std::array<float, kFftLengthBy2Plus1> mask;
|
|
x_old.fill(0.f);
|
|
constexpr int kSinusFrequencyBin = 32;
|
|
|
|
auto generate_sinusoid_test = [&](bool known_delay) {
|
|
size_t sample_counter = 0;
|
|
for (size_t k = 0; k < 100; ++k) {
|
|
ProduceSinusoid(16000, 16000 / 2 * kSinusFrequencyBin / kFftLengthBy2,
|
|
&sample_counter, &x);
|
|
|
|
render_delay_buffer->Insert(x);
|
|
if (k == 0) {
|
|
render_delay_buffer->Reset();
|
|
}
|
|
render_delay_buffer->PrepareCaptureProcessing();
|
|
|
|
analyzer.Update(*render_delay_buffer->GetRenderBuffer(),
|
|
known_delay ? absl::optional<size_t>(0) : absl::nullopt);
|
|
}
|
|
};
|
|
|
|
generate_sinusoid_test(true);
|
|
mask.fill(1.f);
|
|
analyzer.MaskRegionsAroundNarrowBands(&mask);
|
|
for (int k = 0; k < static_cast<int>(mask.size()); ++k) {
|
|
EXPECT_EQ(abs(k - kSinusFrequencyBin) <= 2 ? 0.f : 1.f, mask[k]);
|
|
}
|
|
EXPECT_TRUE(analyzer.PoorSignalExcitation());
|
|
|
|
// Verify that no bands are detected as narrow when the delay is unknown.
|
|
generate_sinusoid_test(false);
|
|
mask.fill(1.f);
|
|
analyzer.MaskRegionsAroundNarrowBands(&mask);
|
|
std::for_each(mask.begin(), mask.end(), [](float a) { EXPECT_EQ(1.f, a); });
|
|
EXPECT_FALSE(analyzer.PoorSignalExcitation());
|
|
}
|
|
|
|
} // namespace webrtc
|