blob: 6286b5ab6460ef910c8046491ad15e88731013f3 [file] [log] [blame]
andrew@webrtc.org08df9b22014-12-16 20:57:15 +00001/*
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#include <stdio.h>
12
13#include "gflags/gflags.h"
14#include "webrtc/base/checks.h"
15#include "webrtc/common_audio/wav_file.h"
16#include "webrtc/modules/audio_processing/channel_buffer.h"
17#include "webrtc/modules/audio_processing/include/audio_processing.h"
18#include "webrtc/modules/audio_processing/test/test_utils.h"
19#include "webrtc/system_wrappers/interface/scoped_ptr.h"
20
21DEFINE_string(dump, "", "The name of the debug dump file to read from.");
22DEFINE_string(c, "", "The name of the capture input file to read from.");
23DEFINE_string(o, "out.wav", "Name of the capture output file to write to.");
24DEFINE_int32(o_channels, 0, "Number of output channels. Defaults to input.");
25DEFINE_int32(o_sample_rate, 0, "Output sample rate in Hz. Defaults to input.");
26
27DEFINE_bool(aec, false, "Enable echo cancellation.");
28DEFINE_bool(agc, false, "Enable automatic gain control.");
29DEFINE_bool(hpf, false, "Enable high-pass filtering.");
30DEFINE_bool(ns, false, "Enable noise suppression.");
31DEFINE_bool(ts, false, "Enable transient suppression.");
32DEFINE_bool(all, false, "Enable all components.");
33
34DEFINE_int32(ns_level, -1, "Noise suppression level [0 - 3].");
35
36static const int kChunksPerSecond = 100;
37static const char kUsage[] =
38 "Command-line tool to run audio processing on WAV files. Accepts either\n"
39 "an input capture WAV file or protobuf debug dump and writes to an output\n"
40 "WAV file.\n"
41 "\n"
42 "All components are disabled by default. If any bi-directional components\n"
43 "are enabled, only debug dump files are permitted.";
44
45namespace webrtc {
46
47int main(int argc, char* argv[]) {
48 {
49 const std::string program_name = argv[0];
50 const std::string usage = kUsage;
51 google::SetUsageMessage(usage);
52 }
53 google::ParseCommandLineFlags(&argc, &argv, true);
54
55 if (!((FLAGS_c == "") ^ (FLAGS_dump == ""))) {
56 fprintf(stderr,
57 "An input file must be specified with either -c or -dump.\n");
58 return 1;
59 }
60 if (FLAGS_dump != "") {
61 fprintf(stderr, "FIXME: the -dump option is not yet implemented.\n");
62 return 1;
63 }
64
65 WavReader c_file(FLAGS_c);
66 // If the output format is uninitialized, use the input format.
67 int o_channels = FLAGS_o_channels;
68 if (!o_channels)
69 o_channels = c_file.num_channels();
70 int o_sample_rate = FLAGS_o_sample_rate;
71 if (!o_sample_rate)
72 o_sample_rate = c_file.sample_rate();
73 WavWriter o_file(FLAGS_o, o_sample_rate, o_channels);
74
75 printf("Input file: %s\nChannels: %d, Sample rate: %d Hz\n\n",
76 FLAGS_c.c_str(), c_file.num_channels(), c_file.sample_rate());
77 printf("Output file: %s\nChannels: %d, Sample rate: %d Hz\n\n",
78 FLAGS_o.c_str(), o_file.num_channels(), o_file.sample_rate());
79
80 Config config;
81 config.Set<ExperimentalNs>(new ExperimentalNs(FLAGS_ts || FLAGS_all));
82 scoped_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
83 if (FLAGS_dump != "") {
84 CHECK_EQ(kNoErr, ap->echo_cancellation()->Enable(FLAGS_aec || FLAGS_all));
85 } else if (FLAGS_aec) {
86 fprintf(stderr, "-aec requires a -dump file.\n");
87 return -1;
88 }
89 CHECK_EQ(kNoErr, ap->gain_control()->Enable(FLAGS_agc || FLAGS_all));
90 CHECK_EQ(kNoErr, ap->gain_control()->set_mode(GainControl::kFixedDigital));
91 CHECK_EQ(kNoErr, ap->high_pass_filter()->Enable(FLAGS_hpf || FLAGS_all));
92 CHECK_EQ(kNoErr, ap->noise_suppression()->Enable(FLAGS_ns || FLAGS_all));
93 if (FLAGS_ns_level != -1)
94 CHECK_EQ(kNoErr, ap->noise_suppression()->set_level(
95 static_cast<NoiseSuppression::Level>(FLAGS_ns_level)));
96
97 ChannelBuffer<float> c_buf(c_file.sample_rate() / kChunksPerSecond,
98 c_file.num_channels());
99 ChannelBuffer<float> o_buf(o_file.sample_rate() / kChunksPerSecond,
100 o_file.num_channels());
101
102 const size_t c_length = static_cast<size_t>(c_buf.length());
103 scoped_ptr<float[]> c_interleaved(new float[c_length]);
104 scoped_ptr<float[]> o_interleaved(new float[o_buf.length()]);
105 while (c_file.ReadSamples(c_length, c_interleaved.get()) == c_length) {
106 FloatS16ToFloat(c_interleaved.get(), c_length, c_interleaved.get());
107 Deinterleave(c_interleaved.get(), c_buf.samples_per_channel(),
108 c_buf.num_channels(), c_buf.channels());
109
110 CHECK_EQ(kNoErr,
111 ap->ProcessStream(c_buf.channels(),
112 c_buf.samples_per_channel(),
113 c_file.sample_rate(),
114 LayoutFromChannels(c_buf.num_channels()),
115 o_file.sample_rate(),
116 LayoutFromChannels(o_buf.num_channels()),
117 o_buf.channels()));
118
119 Interleave(o_buf.channels(), o_buf.samples_per_channel(),
120 o_buf.num_channels(), o_interleaved.get());
121 FloatToFloatS16(o_interleaved.get(), o_buf.length(), o_interleaved.get());
122 o_file.WriteSamples(o_interleaved.get(), o_buf.length());
123 }
124
125 return 0;
126}
127
128} // namespace webrtc
129
130int main(int argc, char* argv[]) {
131 return webrtc::main(argc, argv);
132}