Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_processing / test / process_test.cc
index 05f4b77..76a916d 100644 (file)
@@ -59,7 +59,7 @@ void usage() {
   "when -ir or -i is used, the specified files will be processed directly in\n"
   "a simulation mode. Otherwise the full set of legacy test files is expected\n"
   "to be present in the working directory. OUT_FILE should be specified\n"
-  "without extension to support both int and float output.\n\n");
+  "without extension to support both raw and wav output.\n\n");
   printf("Options\n");
   printf("General configuration (only used for the simulation mode):\n");
   printf("  -fs SAMPLE_RATE_HZ\n");
@@ -112,6 +112,7 @@ void usage() {
   printf("  --perf             Measure performance.\n");
   printf("  --quiet            Suppress text output.\n");
   printf("  --no_progress      Suppress progress.\n");
+  printf("  --raw_output       Raw output instead of WAV file.\n");
   printf("  --debug_file FILE  Dump a debug recording.\n");
 }
 
@@ -167,6 +168,7 @@ void void_main(int argc, char* argv[]) {
   bool perf_testing = false;
   bool verbose = true;
   bool progress = true;
+  bool raw_output = false;
   int extra_delay_ms = 0;
   int override_delay_ms = 0;
 
@@ -427,6 +429,9 @@ void void_main(int argc, char* argv[]) {
     } else if (strcmp(argv[i], "--no_progress") == 0) {
       progress = false;
 
+    } else if (strcmp(argv[i], "--raw_output") == 0) {
+      raw_output = true;
+
     } else if (strcmp(argv[i], "--debug_file") == 0) {
       i++;
       ASSERT_LT(i, argc) << "Specify filename after --debug_file";
@@ -464,8 +469,6 @@ void void_main(int argc, char* argv[]) {
   if (out_filename.size() == 0) {
     out_filename = out_path + "out";
   }
-  std::string out_float_filename = out_filename + ".float";
-  out_filename += ".pcm";
 
   if (!vad_out_filename) {
     vad_out_filename = vad_file_default.c_str();
@@ -486,6 +489,9 @@ void void_main(int argc, char* argv[]) {
   FILE* aecm_echo_path_in_file = NULL;
   FILE* aecm_echo_path_out_file = NULL;
 
+  scoped_ptr<WavWriter> output_wav_file;
+  scoped_ptr<RawFile> output_raw_file;
+
   if (pb_filename) {
     pb_file = OpenFile(pb_filename, "rb");
   } else {
@@ -605,13 +611,14 @@ void void_main(int argc, char* argv[]) {
 
         samples_per_channel = msg.sample_rate() / 100;
         far_frame.sample_rate_hz_ = msg.sample_rate();
-        far_frame.samples_per_channel_ = samples_per_channel;
+        far_frame.samples_per_channel_ = reverse_sample_rate / 100;
         far_frame.num_channels_ = msg.num_reverse_channels();
         near_frame.sample_rate_hz_ = msg.sample_rate();
         near_frame.samples_per_channel_ = samples_per_channel;
         near_frame.num_channels_ = msg.num_input_channels();
-        reverse_cb.reset(new ChannelBuffer<float>(samples_per_channel,
-                                                  msg.num_reverse_channels()));
+        reverse_cb.reset(new ChannelBuffer<float>(
+            far_frame.samples_per_channel_,
+            msg.num_reverse_channels()));
         primary_cb.reset(new ChannelBuffer<float>(samples_per_channel,
                                                   msg.num_input_channels()));
 
@@ -627,6 +634,14 @@ void void_main(int argc, char* argv[]) {
           printf("  Reverse channels: %d\n", msg.num_reverse_channels());
         }
 
+        if (!raw_output) {
+          // The WAV file needs to be reset every time, because it cant change
+          // it's sample rate or number of channels.
+          output_wav_file.reset(new WavWriter(out_filename + ".wav",
+                                              output_sample_rate,
+                                              msg.num_output_channels()));
+        }
+
       } else if (event_msg.type() == Event::REVERSE_STREAM) {
         ASSERT_TRUE(event_msg.has_reverse_stream());
         ReverseStream msg = event_msg.reverse_stream();
@@ -634,7 +649,7 @@ void void_main(int argc, char* argv[]) {
 
         ASSERT_TRUE(msg.has_data() ^ (msg.channel_size() > 0));
         if (msg.has_data()) {
-          ASSERT_EQ(sizeof(int16_t) * samples_per_channel *
+          ASSERT_EQ(sizeof(int16_t) * far_frame.samples_per_channel_ *
               far_frame.num_channels_, msg.data().size());
           memcpy(far_frame.data_, msg.data().data(), msg.data().size());
         } else {
@@ -686,14 +701,16 @@ void void_main(int argc, char* argv[]) {
           memcpy(near_frame.data_,
                  msg.input_data().data(),
                  msg.input_data().size());
+          near_read_bytes += msg.input_data().size();
         } else {
           for (int i = 0; i < msg.input_channel_size(); ++i) {
             primary_cb->CopyFrom(msg.input_channel(i).data(), i);
+            near_read_bytes += msg.input_channel(i).size();
           }
         }
 
-        near_read_bytes += msg.input_data().size();
         if (progress && primary_count % 100 == 0) {
+          near_read_bytes = std::min(near_read_bytes, near_size_bytes);
           printf("%.0f%% complete\r",
               (near_read_bytes * 100.0) / near_size_bytes);
           fflush(stdout);
@@ -769,19 +786,24 @@ void void_main(int argc, char* argv[]) {
           }
         }
 
-        size_t num_samples = samples_per_channel * apm->num_output_channels();
+        const size_t samples_per_channel = output_sample_rate / 100;
         if (msg.has_input_data()) {
-          static FILE* out_file = OpenFile(out_filename, "wb");
-          ASSERT_EQ(num_samples, fwrite(near_frame.data_,
-                                        sizeof(*near_frame.data_),
-                                        num_samples,
-                                        out_file));
+          if (raw_output && !output_raw_file) {
+            output_raw_file.reset(new RawFile(out_filename + ".pcm"));
+          }
+          WriteIntData(near_frame.data_,
+                       apm->num_output_channels() * samples_per_channel,
+                       output_wav_file.get(),
+                       output_raw_file.get());
         } else {
-          static FILE* out_float_file = OpenFile(out_float_filename, "wb");
-          ASSERT_EQ(num_samples, fwrite(primary_cb->data(),
-                                        sizeof(*primary_cb->data()),
-                                        num_samples,
-                                        out_float_file));
+          if (raw_output && !output_raw_file) {
+            output_raw_file.reset(new RawFile(out_filename + ".float"));
+          }
+          WriteFloatData(primary_cb->channels(),
+                         samples_per_channel,
+                         apm->num_output_channels(),
+                         output_wav_file.get(),
+                         output_raw_file.get());
         }
       }
     }
@@ -851,6 +873,14 @@ void void_main(int argc, char* argv[]) {
         near_frame.sample_rate_hz_ = sample_rate_hz;
         near_frame.samples_per_channel_ = samples_per_channel;
 
+        if (!raw_output) {
+          // The WAV file needs to be reset every time, because it can't change
+          // it's sample rate or number of channels.
+          output_wav_file.reset(new WavWriter(out_filename + ".wav",
+                                              sample_rate_hz,
+                                              num_capture_output_channels));
+        }
+
         if (verbose) {
           printf("Init at frame: %d (primary), %d (reverse)\n",
               primary_count, reverse_count);
@@ -995,12 +1025,18 @@ void void_main(int argc, char* argv[]) {
           }
         }
 
-        size = samples_per_channel * near_frame.num_channels_;
-        static FILE* out_file = OpenFile(out_filename, "wb");
-        ASSERT_EQ(size, fwrite(near_frame.data_,
-                               sizeof(int16_t),
-                               size,
-                               out_file));
+        if (raw_output && !output_raw_file) {
+          output_raw_file.reset(new RawFile(out_filename + ".pcm"));
+        }
+        if (!raw_output && !output_wav_file) {
+          output_wav_file.reset(new WavWriter(out_filename + ".wav",
+                                              sample_rate_hz,
+                                              num_capture_output_channels));
+        }
+        WriteIntData(near_frame.data_,
+                     size,
+                     output_wav_file.get(),
+                     output_raw_file.get());
       }
       else {
         FAIL() << "Event " << event << " is unrecognized";