[autotest] check_quality: Dump spectral and quality measurement results

Let user specify an output file path to dump spectral and quality
measurement result in json format.

BUG=chromium:714066
TEST=run check_quality argument with --output-file argument.

Change-Id: If700aceb78a39c951a9b9586f217435ba8a87558
Reviewed-on: https://chromium-review.googlesource.com/532502
Commit-Ready: Cheng-Yi Chiang <cychiang@chromium.org>
Tested-by: Cheng-Yi Chiang <cychiang@chromium.org>
Reviewed-by: Hsu Wei-Cheng <mojahsu@chromium.org>
diff --git a/client/cros/audio/check_quality.py b/client/cros/audio/check_quality.py
index da8972e..6093cdc 100755
--- a/client/cros/audio/check_quality.py
+++ b/client/cros/audio/check_quality.py
@@ -7,6 +7,7 @@
 """Command line tool to analyze wave file and detect artifacts."""
 
 import argparse
+import json
 import logging
 import math
 import numpy
@@ -58,6 +59,8 @@
                         type=float, default=5000,
                         help='Frequency threshold in Hz to be ignored for '
                              'high frequency. Default is 5KHz')
+    parser.add_argument('--output-file', metavar='OUTPUT_FILE', type=str,
+                        help='Output file to dump analysis result in JSON format')
     parser.add_argument('-b', '--bit-width', metavar='BIT_WIDTH', type=int,
                         default=32,
                         help='For raw file. Bit width of a sample. '
@@ -227,6 +230,7 @@
         self._raw_data = raw_data
         self._rate = rate
         self._spectrals = []
+        self._quality_result = []
 
 
     def do_spectral_analysis(self, ignore_high_freq, check_quality=False):
@@ -270,8 +274,9 @@
                         signal=normalized_signal,
                         rate=self._rate,
                         dominant_frequency=spectral[0][0])
-                logging.info('Channel %d quality:\n%s', channel_idx,
-                             pprint.pformat(quality))
+                logging.debug('Channel %d quality:\n%s', channel_idx,
+                              pprint.pformat(quality))
+                self._quality_result.append(quality)
 
             self._spectrals.append(spectral)
 
@@ -308,6 +313,22 @@
                         'Failed at channel %d: %f is too far away from %f' % (
                                 idx, dominant_freq, expected_freq))
 
+
+    def dump(self, output_file):
+        """Dumps the result into a file in json format.
+
+        @param output_file: A file path to dump spectral and quality
+                            measurement result of each channel.
+
+        """
+        dump_dict = {
+            'spectrals': self._spectrals,
+            'quality_result': self._quality_result
+        }
+        with open(output_file, 'w') as f:
+            json.dump(dump_dict, f)
+
+
 class CheckQualityError(Exception):
     """Error in check_quality main function."""
     pass
@@ -361,6 +382,8 @@
     checker.do_spectral_analysis(ignore_high_freq=args.ignore_high_freq,
                                  check_quality=(not args.spectral_only))
 
+    if args.output_file:
+        checker.dump(args.output_file)
 
     if args.freqs:
         checker.check_freqs(args.freqs, args.freq_threshold)