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 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 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 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 17 | #include <stdint.h> |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 19 | #include <string> |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 22 | |
| 23 | #include "gflags/gflags.h" |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 25 | #include "webrtc/base/checks.h" |
| 26 | #include "webrtc/common_audio/real_fourier.h" |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 27 | #include "webrtc/common_audio/wav_file.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 28 | #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h" |
| 29 | #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h" |
| 30 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 31 | #include "webrtc/test/testsupport/fileutils.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 32 | |
| 33 | using std::complex; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 34 | |
| 35 | namespace webrtc { |
| 36 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 37 | using webrtc::RealFourier; |
| 38 | using webrtc::IntelligibilityEnhancer; |
| 39 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 40 | DEFINE_int32(clear_type, |
| 41 | webrtc::intelligibility::VarianceArray::kStepInfinite, |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 42 | "Variance algorithm for clear data."); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 43 | DEFINE_double(clear_alpha, 0.9, "Variance decay factor for clear data."); |
| 44 | DEFINE_int32(clear_window, |
| 45 | 475, |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 46 | "Window size for windowed variance for clear data."); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 47 | DEFINE_int32(sample_rate, |
| 48 | 16000, |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 49 | "Audio sample rate used in the input and output files."); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 50 | DEFINE_int32(ana_rate, |
| 51 | 800, |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 52 | "Analysis rate; gains recalculated every N blocks."); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 53 | DEFINE_int32( |
| 54 | var_rate, |
| 55 | 2, |
| 56 | "Variance clear rate; history is forgotten every N gain recalculations."); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 57 | DEFINE_double(gain_limit, 1000.0, "Maximum gain change in one block."); |
| 58 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 59 | DEFINE_string(clear_file, "speech.wav", "Input file with clear speech."); |
| 60 | DEFINE_string(noise_file, "noise.wav", "Input file with noise data."); |
| 61 | DEFINE_string(out_file, |
| 62 | "proc_enhanced.wav", |
| 63 | "Enhanced output. Use '-' to " |
| 64 | "play through aplay immediately."); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 65 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 66 | // Constant IntelligibilityEnhancer constructor parameters. |
| 67 | const int kErbResolution = 2; |
| 68 | const int kNumChannels = 1; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 69 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 70 | // void function for gtest |
| 71 | void void_main(int argc, char* argv[]) { |
| 72 | google::SetUsageMessage( |
| 73 | "\n\nVariance algorithm types are:\n" |
| 74 | " 0 - infinite/normal,\n" |
| 75 | " 1 - exponentially decaying,\n" |
| 76 | " 2 - rolling window.\n" |
| 77 | "\nInput files must be little-endian 16-bit signed raw PCM.\n"); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 78 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 79 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 80 | size_t samples; // Number of samples in input PCM file |
| 81 | size_t fragment_size; // Number of samples to process at a time |
| 82 | // to simulate APM stream processing |
| 83 | |
| 84 | // Load settings and wav input. |
| 85 | |
| 86 | fragment_size = FLAGS_sample_rate / 100; // Mirror real time APM chunk size. |
| 87 | // Duplicates chunk_length_ in |
| 88 | // IntelligibilityEnhancer. |
| 89 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 90 | struct stat in_stat, noise_stat; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 91 | ASSERT_EQ(stat(FLAGS_clear_file.c_str(), &in_stat), 0) |
| 92 | << "Empty speech file."; |
| 93 | ASSERT_EQ(stat(FLAGS_noise_file.c_str(), &noise_stat), 0) |
| 94 | << "Empty noise file."; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 95 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 96 | samples = std::min(in_stat.st_size, noise_stat.st_size) / 2; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 97 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 98 | WavReader in_file(FLAGS_clear_file); |
| 99 | std::vector<float> in_fpcm(samples); |
| 100 | in_file.ReadSamples(samples, &in_fpcm[0]); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 101 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 102 | WavReader noise_file(FLAGS_noise_file); |
| 103 | std::vector<float> noise_fpcm(samples); |
| 104 | noise_file.ReadSamples(samples, &noise_fpcm[0]); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 105 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 106 | // Run intelligibility enhancement. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 107 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 108 | IntelligibilityEnhancer enh( |
| 109 | kErbResolution, FLAGS_sample_rate, kNumChannels, FLAGS_clear_type, |
| 110 | static_cast<float>(FLAGS_clear_alpha), FLAGS_clear_window, FLAGS_ana_rate, |
| 111 | FLAGS_var_rate, FLAGS_gain_limit); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 112 | |
| 113 | // Slice the input into smaller chunks, as the APM would do, and feed them |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 114 | // through the enhancer. |
| 115 | float* clear_cursor = &in_fpcm[0]; |
| 116 | float* noise_cursor = &noise_fpcm[0]; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 117 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 118 | for (size_t i = 0; i < samples; i += fragment_size) { |
| 119 | enh.ProcessCaptureAudio(&noise_cursor); |
| 120 | enh.ProcessRenderAudio(&clear_cursor); |
| 121 | clear_cursor += fragment_size; |
| 122 | noise_cursor += fragment_size; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 123 | } |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 124 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 125 | if (FLAGS_out_file.compare("-") == 0) { |
| 126 | const std::string temp_out_filename = |
| 127 | test::TempFilename(test::WorkingDir(), "temp_wav_file"); |
| 128 | { |
| 129 | WavWriter out_file(temp_out_filename, FLAGS_sample_rate, kNumChannels); |
| 130 | out_file.WriteSamples(&in_fpcm[0], samples); |
| 131 | } |
| 132 | system(("aplay " + temp_out_filename).c_str()); |
| 133 | system(("rm " + temp_out_filename).c_str()); |
| 134 | } else { |
| 135 | WavWriter out_file(FLAGS_out_file, FLAGS_sample_rate, kNumChannels); |
| 136 | out_file.WriteSamples(&in_fpcm[0], samples); |
| 137 | } |
ekm | b7553df | 2015-06-16 18:57:32 -0700 | [diff] [blame] | 138 | } |
aluebs | c555b99 | 2015-06-16 20:26:16 -0700 | [diff] [blame] | 139 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame^] | 140 | } // namespace webrtc |
| 141 | |
| 142 | int main(int argc, char* argv[]) { |
| 143 | webrtc::void_main(argc, argv); |
| 144 | return 0; |
| 145 | } |