ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 11 | #include "webrtc/common_audio/channel_buffer.h" |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 12 | #include "webrtc/common_audio/include/audio_util.h" |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 13 | #include "webrtc/common_audio/wav_file.h" |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 14 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 15 | #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h" |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 16 | #include "webrtc/modules/audio_processing/noise_suppression_impl.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 17 | #include "webrtc/rtc_base/criticalsection.h" |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 18 | #include "webrtc/rtc_base/flags.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 19 | |
| 20 | using std::complex; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
pkasting | b297c5a | 2015-07-22 15:17:22 -0700 | [diff] [blame] | 23 | namespace { |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 24 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 25 | DEFINE_string(clear_file, "speech.wav", "Input file with clear speech."); |
| 26 | DEFINE_string(noise_file, "noise.wav", "Input file with noise data."); |
Alejandro Luebs | 4458d09 | 2016-02-18 19:16:08 -0800 | [diff] [blame] | 27 | DEFINE_string(out_file, "proc_enhanced.wav", "Enhanced output file."); |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 28 | DEFINE_bool(help, false, "Print this message."); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 29 | |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 30 | int int_main(int argc, char* argv[]) { |
| 31 | if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) { |
| 32 | return 1; |
| 33 | } |
| 34 | if (FLAG_help) { |
| 35 | rtc::FlagList::Print(nullptr, false); |
| 36 | return 0; |
| 37 | } |
| 38 | if (argc != 1) { |
| 39 | printf("\n\nInput files must be little-endian 16-bit signed raw PCM.\n"); |
| 40 | return 0; |
| 41 | } |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 42 | |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 43 | WavReader in_file(FLAG_clear_file); |
| 44 | WavReader noise_file(FLAG_noise_file); |
| 45 | WavWriter out_file(FLAG_out_file, in_file.sample_rate(), |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 46 | in_file.num_channels()); |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 47 | rtc::CriticalSection crit; |
| 48 | NoiseSuppressionImpl ns(&crit); |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 49 | IntelligibilityEnhancer enh(in_file.sample_rate(), in_file.num_channels(), 1u, |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 50 | NoiseSuppressionImpl::num_noise_bins()); |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 51 | ns.Initialize(noise_file.num_channels(), noise_file.sample_rate()); |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 52 | ns.Enable(true); |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 53 | const size_t in_samples = noise_file.sample_rate() / 100; |
| 54 | const size_t noise_samples = noise_file.sample_rate() / 100; |
| 55 | std::vector<float> in(in_samples * in_file.num_channels()); |
| 56 | std::vector<float> noise(noise_samples * noise_file.num_channels()); |
| 57 | ChannelBuffer<float> in_buf(in_samples, in_file.num_channels()); |
| 58 | ChannelBuffer<float> noise_buf(noise_samples, noise_file.num_channels()); |
| 59 | AudioBuffer capture_audio(noise_samples, noise_file.num_channels(), |
| 60 | noise_samples, noise_file.num_channels(), |
| 61 | noise_samples); |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 62 | AudioBuffer render_audio(in_samples, in_file.num_channels(), in_samples, |
| 63 | in_file.num_channels(), in_samples); |
| 64 | StreamConfig noise_config(noise_file.sample_rate(), |
| 65 | noise_file.num_channels()); |
| 66 | StreamConfig in_config(in_file.sample_rate(), in_file.num_channels()); |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 67 | while (in_file.ReadSamples(in.size(), in.data()) == in.size() && |
| 68 | noise_file.ReadSamples(noise.size(), noise.data()) == noise.size()) { |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 69 | FloatS16ToFloat(noise.data(), noise.size(), noise.data()); |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 70 | FloatS16ToFloat(in.data(), in.size(), in.data()); |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 71 | Deinterleave(in.data(), in_buf.num_frames(), in_buf.num_channels(), |
| 72 | in_buf.channels()); |
| 73 | Deinterleave(noise.data(), noise_buf.num_frames(), noise_buf.num_channels(), |
| 74 | noise_buf.channels()); |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 75 | capture_audio.CopyFrom(noise_buf.channels(), noise_config); |
| 76 | render_audio.CopyFrom(in_buf.channels(), in_config); |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 77 | ns.AnalyzeCaptureAudio(&capture_audio); |
| 78 | ns.ProcessCaptureAudio(&capture_audio); |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 79 | enh.SetCaptureNoiseEstimate(ns.NoiseEstimate(), 1); |
| 80 | enh.ProcessRenderAudio(&render_audio); |
| 81 | render_audio.CopyTo(in_config, in_buf.channels()); |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 82 | Interleave(in_buf.channels(), in_buf.num_frames(), in_buf.num_channels(), |
| 83 | in.data()); |
Alejandro Luebs | ef00925 | 2016-09-20 14:51:56 -0700 | [diff] [blame] | 84 | FloatToFloatS16(in.data(), in.size(), in.data()); |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 85 | out_file.WriteSamples(in.data(), in.size()); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 86 | } |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 87 | |
| 88 | return 0; |
ekm | b7553df | 2015-06-16 18:57:32 -0700 | [diff] [blame] | 89 | } |
aluebs | c555b99 | 2015-06-16 20:26:16 -0700 | [diff] [blame] | 90 | |
pkasting | b297c5a | 2015-07-22 15:17:22 -0700 | [diff] [blame] | 91 | } // namespace |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 92 | } // namespace webrtc |
| 93 | |
| 94 | int main(int argc, char* argv[]) { |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 95 | return webrtc::int_main(argc, argv); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 96 | } |