test_server: Add utils to record from DUT headphone

Add two handy functions to connect audio path and copy the file from
Chameleon to local.

Example usage:

>>>ConnectCrosToLineIn()
>>>p.StartCapturingAudio(6)

(do someting on DUT to play audio to headphone)

>>>s = p.StopCapturingAudio(6)
>>>GetAndConvertRecordedFile(s[0])

Then the recorded raw file and wav file will be in current directory.

BUG=None
TEST=Follow above example.

Change-Id: I080618b2b43a349e8b0534b58eb57c6a187b3673
Reviewed-on: https://chromium-review.googlesource.com/412602
Commit-Ready: Cheng-Yi Chiang <cychiang@chromium.org>
Tested-by: Cheng-Yi Chiang <cychiang@chromium.org>
Reviewed-by: Cheng-Yi Chiang <cychiang@chromium.org>
diff --git a/utils/test_server b/utils/test_server
index 2a4857f..c775112 100755
--- a/utils/test_server
+++ b/utils/test_server
@@ -8,8 +8,10 @@
 import argparse
 import code
 import logging
+import os
 import readline
 import rlcompleter
+import subprocess
 import xmlrpclib
 
 
@@ -43,11 +45,13 @@
       hdmi_port, hdmi_port)
 
 
-def StartInteractiveShell(p):
+def StartInteractiveShell(p, options):
   """Starts an interactive shell.
 
   Args:
     p: The xmlrpclib.ServerProxy to chameleond.
+    options: The namespace from argparse.
+
   """
   vars = globals()
   vars.update(locals())
@@ -73,6 +77,40 @@
   return parser.parse_args()
 
 
+def GetAndConvertRecordedFile(remote_path):
+  """Gets recorded file and converts it into a wav file.
+
+  A helper function to get recorded file from Chameleon host and do
+  file format conversion from 32 bit, 48000 rate, 8 channel raw file
+  to 2 channel wav file.
+
+  E.g.
+  >>> p.StartCapturingAudio(6)
+  >>> s = p.StopCapturingAudio(6)
+  >>> GetAndConvertRecordedFile(s[0])
+
+  The recorded raw file and converted wav file will be in current
+  directory.
+
+  Args:
+    remote_path: The file to copy from Chameleon host.
+
+  """
+  basename = os.path.basename(remote_path)
+  # options is already in the namespace.
+  subprocess.check_call(
+      ['scp', 'root@%s:%s' % (options.host, remote_path), basename])
+  subprocess.check_call(
+      ['sox', '-b', '32', '-r', '48000', '-c', '8', '-e', 'signed',
+       basename, '-c', '2', basename + '.wav'])
+
+
+def ConnectCrosToLineIn():
+  """Connects a audio bus path from Cros headphone to Chameleon LineIn."""
+  p.AudioBoardConnect(1, 'Cros device headphone')
+  p.AudioBoardConnect(1, 'Chameleon FPGA line-in')
+
+
 def Main():
   """The Main program."""
   logging.basicConfig(
@@ -85,7 +123,7 @@
   logging.info('Connected to %s with MAC address %s',
                address, proxy.GetMacAddress())
   ShowMessages(proxy)
-  StartInteractiveShell(proxy)
+  StartInteractiveShell(proxy, options)
 
 
 if __name__ == '__main__':