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