Index: webrtc/modules/audio_processing/intelligibility/intelligibility_proc.cc |
diff --git a/webrtc/modules/audio_processing/intelligibility/intelligibility_proc.cc b/webrtc/modules/audio_processing/intelligibility/intelligibility_proc.cc |
index b0ea2dfee0315b19aa779ac3521e70d52ee5ff7c..9f7d84e701af6b0a9f303866ae999e6dbec8c613 100644 |
--- a/webrtc/modules/audio_processing/intelligibility/intelligibility_proc.cc |
+++ b/webrtc/modules/audio_processing/intelligibility/intelligibility_proc.cc |
@@ -8,180 +8,138 @@ |
* be found in the AUTHORS file in the root of the source tree. |
*/ |
-#include <arpa/inet.h> |
-#include <fcntl.h> |
+// |
+// Command line tool for speech intelligibility enhancement. Provides for |
+// running and testing intelligibility_enhancer as an independent process. |
+// Use --help for options. |
+// |
+ |
#include <stdint.h> |
-#include <stdio.h> |
#include <stdlib.h> |
-#include <sys/mman.h> |
+#include <string> |
#include <sys/stat.h> |
#include <sys/types.h> |
-#include <unistd.h> |
- |
-#include <fenv.h> |
-#include <limits> |
- |
-#include <complex> |
#include "gflags/gflags.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
#include "webrtc/base/checks.h" |
#include "webrtc/common_audio/real_fourier.h" |
+#include "webrtc/common_audio/wav_file.h" |
#include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h" |
#include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h" |
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
-#include "webrtc/system_wrappers/interface/scoped_ptr.h" |
- |
-const int16_t* in_ipcm; |
-int16_t* out_ipcm; |
-const int16_t* noise_ipcm; |
+#include "webrtc/test/testsupport/fileutils.h" |
-float* in_fpcm; |
-float* out_fpcm; |
-float* noise_fpcm; |
-float* noise_cursor; |
-float* clear_cursor; |
+using std::complex; |
-int samples; |
-int fragment_size; |
+namespace webrtc { |
-using std::complex; |
using webrtc::RealFourier; |
using webrtc::IntelligibilityEnhancer; |
-DEFINE_int32(clear_type, webrtc::intelligibility::VarianceArray::kStepInfinite, |
+DEFINE_int32(clear_type, |
+ webrtc::intelligibility::VarianceArray::kStepInfinite, |
"Variance algorithm for clear data."); |
-DEFINE_double(clear_alpha, 0.9, |
- "Variance decay factor for clear data."); |
-DEFINE_int32(clear_window, 475, |
+DEFINE_double(clear_alpha, 0.9, "Variance decay factor for clear data."); |
+DEFINE_int32(clear_window, |
+ 475, |
"Window size for windowed variance for clear data."); |
-DEFINE_int32(sample_rate, 16000, |
+DEFINE_int32(sample_rate, |
+ 16000, |
"Audio sample rate used in the input and output files."); |
-DEFINE_int32(ana_rate, 800, |
+DEFINE_int32(ana_rate, |
+ 800, |
"Analysis rate; gains recalculated every N blocks."); |
-DEFINE_int32(var_rate, 2, |
- "Variance clear rate; history is forgotten every N gain recalculations."); |
+DEFINE_int32( |
+ var_rate, |
+ 2, |
+ "Variance clear rate; history is forgotten every N gain recalculations."); |
DEFINE_double(gain_limit, 1000.0, "Maximum gain change in one block."); |
-DEFINE_bool(repeat, false, "Repeat input file ad nauseam."); |
- |
-DEFINE_string(clear_file, "speech.pcm", "Input file with clear speech."); |
-DEFINE_string(noise_file, "noise.pcm", "Input file with noise data."); |
-DEFINE_string(out_file, "proc_enhanced.pcm", "Enhanced output. Use '-' to " |
- "pipe through aplay internally."); |
- |
-// Write an Sun AU-formatted audio chunk into file descriptor |fd|. Can be used |
-// to pipe the audio stream directly into aplay. |
-void writeau(int fd) { |
- uint32_t thing; |
- |
- write(fd, ".snd", 4); |
- thing = htonl(24); |
- write(fd, &thing, sizeof(thing)); |
- thing = htonl(0xffffffff); |
- write(fd, &thing, sizeof(thing)); |
- thing = htonl(3); |
- write(fd, &thing, sizeof(thing)); |
- thing = htonl(FLAGS_sample_rate); |
- write(fd, &thing, sizeof(thing)); |
- thing = htonl(1); |
- write(fd, &thing, sizeof(thing)); |
- |
- for (int i = 0; i < samples; ++i) { |
- out_ipcm[i] = htons(out_ipcm[i]); |
- } |
- write(fd, out_ipcm, sizeof(*out_ipcm) * samples); |
-} |
- |
-int main(int argc, char* argv[]) { |
- google::SetUsageMessage("\n\nVariance algorithm types are:\n" |
- " 0 - infinite/normal,\n" |
- " 1 - exponentially decaying,\n" |
- " 2 - rolling window.\n" |
- "\nInput files must be little-endian 16-bit signed raw PCM.\n"); |
+DEFINE_string(clear_file, "speech.wav", "Input file with clear speech."); |
+DEFINE_string(noise_file, "noise.wav", "Input file with noise data."); |
+DEFINE_string(out_file, |
+ "proc_enhanced.wav", |
+ "Enhanced output. Use '-' to " |
+ "play through aplay immediately."); |
+ |
+// Constant IntelligibilityEnhancer constructor parameters. |
+const int kErbResolution = 2; |
+const int kNumChannels = 1; |
+ |
+// void function for gtest |
+void void_main(int argc, char* argv[]) { |
+ google::SetUsageMessage( |
+ "\n\nVariance algorithm types are:\n" |
+ " 0 - infinite/normal,\n" |
+ " 1 - exponentially decaying,\n" |
+ " 2 - rolling window.\n" |
+ "\nInput files must be little-endian 16-bit signed raw PCM.\n"); |
google::ParseCommandLineFlags(&argc, &argv, true); |
- const char* in_name = FLAGS_clear_file.c_str(); |
- const char* out_name = FLAGS_out_file.c_str(); |
- const char* noise_name = FLAGS_noise_file.c_str(); |
+ size_t samples; // Number of samples in input PCM file |
+ size_t fragment_size; // Number of samples to process at a time |
+ // to simulate APM stream processing |
+ |
+ // Load settings and wav input. |
+ |
+ fragment_size = FLAGS_sample_rate / 100; // Mirror real time APM chunk size. |
+ // Duplicates chunk_length_ in |
+ // IntelligibilityEnhancer. |
+ |
struct stat in_stat, noise_stat; |
- int in_fd, out_fd, noise_fd; |
- FILE* aplay_file = nullptr; |
+ ASSERT_EQ(stat(FLAGS_clear_file.c_str(), &in_stat), 0) |
+ << "Empty speech file."; |
+ ASSERT_EQ(stat(FLAGS_noise_file.c_str(), &noise_stat), 0) |
+ << "Empty noise file."; |
- fragment_size = FLAGS_sample_rate / 100; |
+ samples = std::min(in_stat.st_size, noise_stat.st_size) / 2; |
- stat(in_name, &in_stat); |
- stat(noise_name, &noise_stat); |
- samples = in_stat.st_size / sizeof(*in_ipcm); |
+ WavReader in_file(FLAGS_clear_file); |
+ std::vector<float> in_fpcm(samples); |
+ in_file.ReadSamples(samples, &in_fpcm[0]); |
- in_fd = open(in_name, O_RDONLY); |
- if (!strcmp(out_name, "-")) { |
- aplay_file = popen("aplay -t au", "w"); |
- out_fd = fileno(aplay_file); |
- } else { |
- out_fd = open(out_name, O_WRONLY | O_CREAT | O_TRUNC, |
- S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
- } |
- noise_fd = open(noise_name, O_RDONLY); |
- |
- in_ipcm = static_cast<int16_t*>(mmap(nullptr, in_stat.st_size, PROT_READ, |
- MAP_PRIVATE, in_fd, 0)); |
- noise_ipcm = static_cast<int16_t*>(mmap(nullptr, noise_stat.st_size, |
- PROT_READ, MAP_PRIVATE, noise_fd, 0)); |
- out_ipcm = new int16_t[samples]; |
- out_fpcm = new float[samples]; |
- in_fpcm = new float[samples]; |
- noise_fpcm = new float[samples]; |
- |
- for (int i = 0; i < samples; ++i) { |
- noise_fpcm[i] = noise_ipcm[i % (noise_stat.st_size / sizeof(*noise_ipcm))]; |
- } |
+ WavReader noise_file(FLAGS_noise_file); |
+ std::vector<float> noise_fpcm(samples); |
+ noise_file.ReadSamples(samples, &noise_fpcm[0]); |
- //feenableexcept(FE_INVALID | FE_OVERFLOW); |
- IntelligibilityEnhancer enh(2, |
- FLAGS_sample_rate, 1, |
- FLAGS_clear_type, |
- static_cast<float>(FLAGS_clear_alpha), |
- FLAGS_clear_window, |
- FLAGS_ana_rate, |
- FLAGS_var_rate, |
- FLAGS_gain_limit); |
+ // Run intelligibility enhancement. |
- // Slice the input into smaller chunks, as the APM would do, and feed them |
- // into the enhancer. Repeat indefinitely if FLAGS_repeat is set. |
- do { |
- noise_cursor = noise_fpcm; |
- clear_cursor = in_fpcm; |
- for (int i = 0; i < samples; ++i) { |
- in_fpcm[i] = in_ipcm[i]; |
- } |
+ IntelligibilityEnhancer enh( |
+ kErbResolution, FLAGS_sample_rate, kNumChannels, FLAGS_clear_type, |
+ static_cast<float>(FLAGS_clear_alpha), FLAGS_clear_window, FLAGS_ana_rate, |
+ FLAGS_var_rate, FLAGS_gain_limit); |
- for (int i = 0; i < samples; i += fragment_size) { |
- enh.ProcessCaptureAudio(&noise_cursor); |
- enh.ProcessRenderAudio(&clear_cursor); |
- clear_cursor += fragment_size; |
- noise_cursor += fragment_size; |
- } |
+ // Slice the input into smaller chunks, as the APM would do, and feed them |
+ // through the enhancer. |
+ float* clear_cursor = &in_fpcm[0]; |
+ float* noise_cursor = &noise_fpcm[0]; |
+ |
+ for (size_t i = 0; i < samples; i += fragment_size) { |
+ enh.ProcessCaptureAudio(&noise_cursor); |
+ enh.ProcessRenderAudio(&clear_cursor); |
+ clear_cursor += fragment_size; |
+ noise_cursor += fragment_size; |
+ } |
- for (int i = 0; i < samples; ++i) { |
- out_ipcm[i] = static_cast<float>(in_fpcm[i]); |
+ if (FLAGS_out_file.compare("-") == 0) { |
+ const std::string temp_out_filename = |
+ test::TempFilename(test::WorkingDir(), "temp_wav_file"); |
+ { |
+ WavWriter out_file(temp_out_filename, FLAGS_sample_rate, kNumChannels); |
+ out_file.WriteSamples(&in_fpcm[0], samples); |
} |
- if (!strcmp(out_name, "-")) { |
- writeau(out_fd); |
- } else { |
- write(out_fd, out_ipcm, samples * sizeof(*out_ipcm)); |
- } |
- } while (FLAGS_repeat); |
- |
- munmap(const_cast<int16_t*>(noise_ipcm), noise_stat.st_size); |
- munmap(const_cast<int16_t*>(in_ipcm), in_stat.st_size); |
- close(noise_fd); |
- if (aplay_file) { |
- pclose(aplay_file); |
+ system(("aplay " + temp_out_filename).c_str()); |
+ system(("rm " + temp_out_filename).c_str()); |
} else { |
- close(out_fd); |
+ WavWriter out_file(FLAGS_out_file, FLAGS_sample_rate, kNumChannels); |
+ out_file.WriteSamples(&in_fpcm[0], samples); |
} |
- close(in_fd); |
+} |
+} // namespace webrtc |
+ |
+int main(int argc, char* argv[]) { |
+ webrtc::void_main(argc, argv); |
return 0; |
} |
- |