[autotest] sox_utils: Handle format conversion from raw to non-raw

Add an argument use_dst_header to convert_format to support format
conversion from raw to non-raw.
When converting to non-raw format, we should not pass '-t raw -e signed'
to sox.
The file format will be determined by the extension of destination path.

Let audio_test_data use this argument when the destination format is not
raw. This will be used to convert test data from raw file to wav file,
which can be played by Play Music app in ARC.

BUG=chromium:644036
TEST=not used yet.

Change-Id: I6198fe20b451f6c91a47beb8dd8d6619758a4f0e
Reviewed-on: https://chromium-review.googlesource.com/381535
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/sox_utils.py b/client/cros/audio/sox_utils.py
index ca88f09..4b86292 100644
--- a/client/cros/audio/sox_utils.py
+++ b/client/cros/audio/sox_utils.py
@@ -10,12 +10,33 @@
 SOX_PATH = 'sox'
 
 def _raw_format_args(channels, bits, rate):
+    """Gets raw format args used in sox.
+
+    @param channels: Number of channels.
+    @param bits: Bit length for a sample.
+    @param rate: Sampling rate.
+
+    @returns: A list of args.
+
+    """
     args = ['-t', 'raw', '-e', 'signed']
-    args += ['-c', str(channels)]
-    args += ['-b', str(bits)]
-    args += ['-r', str(rate)]
+    args += _format_args(channels, bits, rate)
     return args
 
+
+def _format_args(channels, bits, rate):
+    """Gets format args used in sox.
+
+    @param channels: Number of channels.
+    @param bits: Bit length for a sample.
+    @param rate: Sampling rate.
+
+    @returns: A list of args.
+
+    """
+    return ['-c', str(channels), '-b', str(bits), '-r', str(rate)]
+
+
 def generate_sine_tone_cmd(
         filename, channels=2, bits=16, rate=48000, duration=None, frequence=440,
         gain=None):
@@ -207,7 +228,7 @@
 
 def convert_format(path_src, channels_src, bits_src, rate_src,
                    path_dst, channels_dst, bits_dst, rate_dst,
-                   volume_scale, use_src_header=False):
+                   volume_scale, use_src_header=False, use_dst_header=False):
     """Converts a raw file to a new format.
 
     @param path_src: The path to the source file.
@@ -224,15 +245,23 @@
     @param use_src_header: True to use header from source file and skip
                            specifying channel, sample format, and rate for
                            source. False otherwise.
+    @param use_dst_header: True to use header for dst file. False to treat
+                           dst file as a raw file.
 
     """
     sox_cmd = [SOX_PATH]
+
     if not use_src_header:
         sox_cmd += _raw_format_args(channels_src, bits_src, rate_src)
     sox_cmd += ['-v', '%f' % volume_scale]
     sox_cmd += [path_src]
-    sox_cmd += _raw_format_args(channels_dst, bits_dst, rate_dst)
+
+    if not use_dst_header:
+        sox_cmd += _raw_format_args(channels_dst, bits_dst, rate_dst)
+    else:
+        sox_cmd += _format_args(channels_dst, bits_dst, rate_dst)
     sox_cmd += [path_dst]
+
     cmd_utils.execute(sox_cmd)