Chromium Code Reviews| 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..e18a811abba43870a4d51d704ab2f4fe2af6c30e 100644 |
| --- a/webrtc/modules/audio_processing/intelligibility/intelligibility_proc.cc |
| +++ b/webrtc/modules/audio_processing/intelligibility/intelligibility_proc.cc |
| @@ -8,180 +8,167 @@ |
| * 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" |
| +// PCM data simulating streams |
| const int16_t* in_ipcm; |
|
Andrew MacDonald
2015/06/18 03:38:15
I don't think any of these from here down to line
ekm
2015/06/18 18:50:27
Done.
|
| -int16_t* out_ipcm; |
| -const int16_t* noise_ipcm; |
| - |
| float* in_fpcm; |
| float* out_fpcm; |
| float* noise_fpcm; |
| + |
| +// Current locations in streams |
| float* noise_cursor; |
| float* clear_cursor; |
| -int samples; |
| -int fragment_size; |
| +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 |
| using std::complex; |
| + |
| +namespace webrtc { |
| + |
| 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; |
| + |
| +const std::string kTmpOutName = "tmp_out_file.wav"; |
|
Andrew MacDonald
2015/06/18 03:38:15
Prefer not to use non-POD types as static globals.
ekm
2015/06/18 18:50:26
Done.
|
| + |
| +// 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(); |
| struct stat in_stat, noise_stat; |
| - int in_fd, out_fd, noise_fd; |
| - FILE* aplay_file = nullptr; |
| + std::string out_file_name; |
| + std::string in_file_name = in_name; |
| + std::string noise_file_name = noise_name; |
|
Andrew MacDonald
2015/06/18 03:38:15
You have three variables referring to each of the
ekm
2015/06/18 18:50:26
Done.
|
| - fragment_size = FLAGS_sample_rate / 100; |
| + // Load settings and wav input. |
| + |
| + fragment_size = FLAGS_sample_rate / 100; // Mirror real time APM chunk size. |
| + // Duplicates chunk_length_ in |
| + // IntelligibilityEnhancer. |
| + |
| + ASSERT_EQ(stat(in_name, &in_stat), 0) << "Empty speech input."; |
|
Andrew MacDonald
2015/06/18 03:38:15
Not sure these are really needed. How often will y
ekm
2015/06/18 18:50:27
Done.
|
| + ASSERT_EQ(stat(noise_name, &noise_stat), 0) << "Empty noise input."; |
| - stat(in_name, &in_stat); |
| - stat(noise_name, &noise_stat); |
| samples = in_stat.st_size / sizeof(*in_ipcm); |
| - in_fd = open(in_name, O_RDONLY); |
| if (!strcmp(out_name, "-")) { |
| - aplay_file = popen("aplay -t au", "w"); |
| - out_fd = fileno(aplay_file); |
| + out_file_name = kTmpOutName; |
| } else { |
| - out_fd = open(out_name, O_WRONLY | O_CREAT | O_TRUNC, |
| - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| + out_file_name = out_name; |
| } |
| - 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]; |
| + |
| + WavReader in_file(in_file_name); |
| in_fpcm = new float[samples]; |
|
Andrew MacDonald
2015/06/18 03:38:15
vector or at least scoped_ptr. Doesn't matter if w
ekm
2015/06/18 18:50:26
Done.
|
| - noise_fpcm = new float[samples]; |
| + const size_t samples_read = in_file.ReadSamples(samples, in_fpcm); |
| - for (int i = 0; i < samples; ++i) { |
| - noise_fpcm[i] = noise_ipcm[i % (noise_stat.st_size / sizeof(*noise_ipcm))]; |
| + WavReader noise_file(noise_file_name); |
| + noise_fpcm = new float[samples]; |
|
Andrew MacDonald
2015/06/18 03:38:15
vector
ekm
2015/06/18 18:50:27
Done.
|
| + const size_t noise_samples = noise_file.ReadSamples(samples_read, noise_fpcm); |
| + for (size_t i = noise_samples; i < samples_read; ++i) { |
| + noise_fpcm[i] = noise_fpcm[i % noise_samples]; |
|
Andrew MacDonald
2015/06/18 03:38:15
This is a bit weird. You're restarting the noise f
ekm
2015/06/18 18:50:27
For iid noise it's fine, but I see with real world
|
| } |
| - //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. |
| + |
| + 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); |
| // Slice the input into smaller chunks, as the APM would do, and feed them |
| - // into the enhancer. Repeat indefinitely if FLAGS_repeat is set. |
| + // through 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]; |
| - } |
| + noise_cursor = noise_fpcm; |
| - for (int i = 0; i < samples; i += fragment_size) { |
| + for (size_t i = 0; i < samples; i += fragment_size) { |
|
Andrew MacDonald
2015/06/18 03:38:15
I think it's more typical to read directly from th
ekm
2015/06/18 18:50:26
Acknowledged.
|
| 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]); |
| + { |
| + WavWriter out_file(out_file_name, FLAGS_sample_rate, kNumChannels); |
|
Andrew MacDonald
2015/06/18 03:38:15
Indent inside scopes.
ekm
2015/06/18 18:50:26
Done.
|
| + out_file.WriteSamples(in_fpcm, samples); |
| } |
| + |
| if (!strcmp(out_name, "-")) { |
| - writeau(out_fd); |
| - } else { |
| - write(out_fd, out_ipcm, samples * sizeof(*out_ipcm)); |
| + system(("aplay " + out_file_name).c_str()); |
| + system(("rm " + out_file_name).c_str()); |
| } |
| + |
| } while (FLAGS_repeat); |
|
Andrew MacDonald
2015/06/18 03:38:15
This probably made sense with the piping directly
ekm
2015/06/18 18:50:26
Done. Removed. I don't think it's worth it.
|
| - 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); |
| - } else { |
| - close(out_fd); |
| - } |
| - close(in_fd); |
| +} |
| +} // namespace webrtc |
| + |
| +int main(int argc, char* argv[]) { |
| + webrtc::void_main(argc, argv); |
| return 0; |
| } |
| - |