From 867e2d875bb63754b02175f6b63347017802f085 Mon Sep 17 00:00:00 2001 From: Arun Raghavan Date: Tue, 24 Dec 2024 12:04:52 -0500 Subject: [PATCH] Add a trivial example to run AEC offline Just allows for some sanity testing for now, will improve for configurability and add some sample data in the future. --- examples/meson.build | 8 +++++ examples/run-offline.cpp | 67 ++++++++++++++++++++++++++++++++++++++++ meson.build | 2 ++ 3 files changed, 77 insertions(+) create mode 100644 examples/meson.build create mode 100644 examples/run-offline.cpp diff --git a/examples/meson.build b/examples/meson.build new file mode 100644 index 0000000..f8beaa9 --- /dev/null +++ b/examples/meson.build @@ -0,0 +1,8 @@ +top_incdir = include_directories('..') + +executable('run-offline', + 'run-offline.cpp', + install: false, + include_directories: top_incdir, + dependencies: [audio_processing_dep, absl_dep] +) diff --git a/examples/run-offline.cpp b/examples/run-offline.cpp new file mode 100644 index 0000000..3a8e15d --- /dev/null +++ b/examples/run-offline.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024 Asymptotic Inc. All Rights Reserved. + * Author: Arun Raghavan + * + * 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 "api/scoped_refptr.h" +#include +#include +#include + +#include + +#define DEFAULT_BLOCK_MS 10 +#define DEFAULT_RATE 32000 +#define DEFAULT_CHANNELS 1 + +int main(int argc, char **argv) { + if (argc != 4) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return EXIT_FAILURE; + } + + std::ifstream play_file(argv[1], std::ios::binary); + std::ifstream rec_file(argv[2], std::ios::binary); + std::ofstream aec_file(argv[3], std::ios::binary); + + rtc::scoped_refptr apm = webrtc::AudioProcessingBuilder().Create(); + + webrtc::AudioProcessing::Config config; + config.echo_canceller.enabled = true; + config.echo_canceller.mobile_mode = false; + config.gain_controller1.enabled = true; + config.gain_controller1.mode = webrtc::AudioProcessing::Config::GainController1::kAdaptiveAnalog; + + config.gain_controller2.enabled = true; + + config.high_pass_filter.enabled = true; + + apm->ApplyConfig(config); + + webrtc::StreamConfig stream_config(DEFAULT_RATE, DEFAULT_CHANNELS); + + while (!play_file.eof() && !rec_file.eof()) { + int16_t play_frame[DEFAULT_RATE * DEFAULT_BLOCK_MS / 1000 * DEFAULT_CHANNELS]; + int16_t rec_frame[DEFAULT_RATE * DEFAULT_BLOCK_MS / 1000 * DEFAULT_CHANNELS]; + + play_file.read(reinterpret_cast(play_frame), sizeof(play_frame)); + rec_file.read(reinterpret_cast(rec_frame), sizeof(rec_frame)); + + apm->ProcessReverseStream(play_frame, stream_config, stream_config, play_frame); + apm->ProcessStream(rec_frame, stream_config, stream_config, rec_frame); + + aec_file.write(reinterpret_cast(rec_frame), sizeof(rec_frame)); + } + + play_file.close(); + rec_file.close(); + aec_file.close(); + + return EXIT_SUCCESS; +} diff --git a/meson.build b/meson.build index 72e749f..72279c7 100644 --- a/meson.build +++ b/meson.build @@ -218,3 +218,5 @@ audio_coding_dep = declare_dependency( ) meson.override_dependency(ac_project_name, audio_coding_dep) + +subdir('examples')