OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 // | |
12 // Command line tool for speech intelligibility enhancement. Provides for | |
13 // running and testing intelligibility_enhancer as an independent process. | |
14 // Use --help for options. | |
15 // | |
16 | |
17 #include <sys/stat.h> | |
18 | |
19 #include "gflags/gflags.h" | 11 #include "gflags/gflags.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "webrtc/base/criticalsection.h" | 13 #include "webrtc/base/criticalsection.h" |
| 14 #include "webrtc/common_audio/channel_buffer.h" |
22 #include "webrtc/common_audio/include/audio_util.h" | 15 #include "webrtc/common_audio/include/audio_util.h" |
23 #include "webrtc/common_audio/wav_file.h" | 16 #include "webrtc/common_audio/wav_file.h" |
24 #include "webrtc/modules/audio_processing/audio_buffer.h" | 17 #include "webrtc/modules/audio_processing/audio_buffer.h" |
25 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc
er.h" | 18 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhanc
er.h" |
26 #include "webrtc/modules/audio_processing/noise_suppression_impl.h" | 19 #include "webrtc/modules/audio_processing/noise_suppression_impl.h" |
27 | 20 |
28 using std::complex; | 21 using std::complex; |
29 | 22 |
30 namespace webrtc { | 23 namespace webrtc { |
31 namespace { | 24 namespace { |
32 | 25 |
33 DEFINE_string(clear_file, "speech.wav", "Input file with clear speech."); | 26 DEFINE_string(clear_file, "speech.wav", "Input file with clear speech."); |
34 DEFINE_string(noise_file, "noise.wav", "Input file with noise data."); | 27 DEFINE_string(noise_file, "noise.wav", "Input file with noise data."); |
35 DEFINE_string(out_file, "proc_enhanced.wav", "Enhanced output file."); | 28 DEFINE_string(out_file, "proc_enhanced.wav", "Enhanced output file."); |
36 | 29 |
37 // void function for gtest | 30 // void function for gtest |
38 void void_main(int argc, char* argv[]) { | 31 void void_main(int argc, char* argv[]) { |
39 google::SetUsageMessage( | 32 google::SetUsageMessage( |
40 "\n\nInput files must be little-endian 16-bit signed raw PCM.\n"); | 33 "\n\nInput files must be little-endian 16-bit signed raw PCM.\n"); |
41 google::ParseCommandLineFlags(&argc, &argv, true); | 34 google::ParseCommandLineFlags(&argc, &argv, true); |
42 | 35 |
43 // Load settings and wav input. | |
44 struct stat in_stat, noise_stat; | |
45 ASSERT_EQ(stat(FLAGS_clear_file.c_str(), &in_stat), 0) | |
46 << "Empty speech file."; | |
47 ASSERT_EQ(stat(FLAGS_noise_file.c_str(), &noise_stat), 0) | |
48 << "Empty noise file."; | |
49 | |
50 const size_t samples = std::min(in_stat.st_size, noise_stat.st_size) / 2; | |
51 | |
52 WavReader in_file(FLAGS_clear_file); | 36 WavReader in_file(FLAGS_clear_file); |
53 std::vector<float> in_fpcm(samples); | |
54 in_file.ReadSamples(samples, &in_fpcm[0]); | |
55 FloatS16ToFloat(&in_fpcm[0], samples, &in_fpcm[0]); | |
56 | |
57 WavReader noise_file(FLAGS_noise_file); | 37 WavReader noise_file(FLAGS_noise_file); |
58 std::vector<float> noise_fpcm(samples); | 38 WavWriter out_file(FLAGS_out_file, in_file.sample_rate(), |
59 noise_file.ReadSamples(samples, &noise_fpcm[0]); | 39 in_file.num_channels()); |
60 FloatS16ToFloat(&noise_fpcm[0], samples, &noise_fpcm[0]); | |
61 | |
62 // Run intelligibility enhancement. | |
63 IntelligibilityEnhancer enh(in_file.sample_rate(), in_file.num_channels()); | 40 IntelligibilityEnhancer enh(in_file.sample_rate(), in_file.num_channels()); |
64 rtc::CriticalSection crit; | 41 rtc::CriticalSection crit; |
65 NoiseSuppressionImpl ns(&crit); | 42 NoiseSuppressionImpl ns(&crit); |
66 ns.Initialize(noise_file.num_channels(), noise_file.sample_rate()); | 43 ns.Initialize(noise_file.num_channels(), noise_file.sample_rate()); |
67 ns.Enable(true); | 44 ns.Enable(true); |
68 | 45 const size_t in_samples = noise_file.sample_rate() / 100; |
69 // Mirror real time APM chunk size. Duplicates chunk_length_ in | 46 const size_t noise_samples = noise_file.sample_rate() / 100; |
70 // IntelligibilityEnhancer. | 47 std::vector<float> in(in_samples * in_file.num_channels()); |
71 size_t fragment_size = in_file.sample_rate() / 100; | 48 std::vector<float> noise(noise_samples * noise_file.num_channels()); |
72 AudioBuffer capture_audio(fragment_size, noise_file.num_channels(), | 49 ChannelBuffer<float> in_buf(in_samples, in_file.num_channels()); |
73 fragment_size, noise_file.num_channels(), | 50 ChannelBuffer<float> noise_buf(noise_samples, noise_file.num_channels()); |
74 fragment_size); | 51 AudioBuffer capture_audio(noise_samples, noise_file.num_channels(), |
75 StreamConfig stream_config(in_file.sample_rate(), noise_file.num_channels()); | 52 noise_samples, noise_file.num_channels(), |
76 | 53 noise_samples); |
77 // Slice the input into smaller chunks, as the APM would do, and feed them | 54 StreamConfig stream_config(noise_file.sample_rate(), |
78 // through the enhancer. | 55 noise_file.num_channels()); |
79 float* clear_cursor = &in_fpcm[0]; | 56 while (in_file.ReadSamples(in.size(), in.data()) == in.size() && |
80 float* noise_cursor = &noise_fpcm[0]; | 57 noise_file.ReadSamples(noise.size(), noise.data()) == noise.size()) { |
81 | 58 FloatS16ToFloat(in.data(), in.size(), in.data()); |
82 for (size_t i = 0; i < samples; i += fragment_size) { | 59 FloatS16ToFloat(noise.data(), noise.size(), noise.data()); |
83 capture_audio.CopyFrom(&noise_cursor, stream_config); | 60 Deinterleave(in.data(), in_buf.num_frames(), in_buf.num_channels(), |
| 61 in_buf.channels()); |
| 62 Deinterleave(noise.data(), noise_buf.num_frames(), noise_buf.num_channels(), |
| 63 noise_buf.channels()); |
| 64 capture_audio.CopyFrom(noise_buf.channels(), stream_config); |
84 ns.AnalyzeCaptureAudio(&capture_audio); | 65 ns.AnalyzeCaptureAudio(&capture_audio); |
85 ns.ProcessCaptureAudio(&capture_audio); | 66 ns.ProcessCaptureAudio(&capture_audio); |
86 enh.SetCaptureNoiseEstimate(ns.NoiseEstimate()); | 67 enh.SetCaptureNoiseEstimate(ns.NoiseEstimate()); |
87 enh.ProcessRenderAudio(&clear_cursor, in_file.sample_rate(), | 68 enh.ProcessRenderAudio(in_buf.channels(), in_file.sample_rate(), |
88 in_file.num_channels()); | 69 in_file.num_channels()); |
89 clear_cursor += fragment_size; | 70 Interleave(in_buf.channels(), in_buf.num_frames(), in_buf.num_channels(), |
90 noise_cursor += fragment_size; | 71 in.data()); |
| 72 FloatToFloatS16(in.data(), in.size(), in.data()); |
| 73 out_file.WriteSamples(in.data(), in.size()); |
91 } | 74 } |
92 | |
93 FloatToFloatS16(&in_fpcm[0], samples, &in_fpcm[0]); | |
94 | |
95 WavWriter out_file(FLAGS_out_file, | |
96 in_file.sample_rate(), | |
97 in_file.num_channels()); | |
98 out_file.WriteSamples(&in_fpcm[0], samples); | |
99 } | 75 } |
100 | 76 |
101 } // namespace | 77 } // namespace |
102 } // namespace webrtc | 78 } // namespace webrtc |
103 | 79 |
104 int main(int argc, char* argv[]) { | 80 int main(int argc, char* argv[]) { |
105 webrtc::void_main(argc, argv); | 81 webrtc::void_main(argc, argv); |
106 return 0; | 82 return 0; |
107 } | 83 } |
OLD | NEW |