Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import logging |
| 6 | import re |
| 7 | |
| 8 | from autotest_lib.client.bin import utils |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame^] | 9 | from autotest_lib.client.cros.audio import cmd_utils |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 10 | |
| 11 | _CRAS_TEST_CLIENT = '/usr/bin/cras_test_client' |
| 12 | _RE_SELECTED_OUTPUT_NODE = re.compile('Selected Output Node: (.*)') |
| 13 | _RE_SELECTED_INPUT_NODE = re.compile('Selected Input Node: (.*)') |
| 14 | |
| 15 | def playback(*args, **kargs): |
| 16 | """A helper function to execute the playback_cmd.""" |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame^] | 17 | cmd_utils.execute(playback_cmd(*args, **kargs)) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 18 | |
| 19 | def capture(*args, **kargs): |
| 20 | """A helper function to execute the capture_cmd.""" |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame^] | 21 | cmd_utils.execute(capture_cmd(*args, **kargs)) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 22 | |
| 23 | def playback_cmd(playback_file, buffer_frames=None, duration=None, rate=44100): |
| 24 | """Gets a command to playback a file with given settings. |
| 25 | |
| 26 | @param playback_file: the name of the file to play. '-' indicates to |
| 27 | playback raw audio from the stdin. |
| 28 | @param buffer_frames: total number of frames to buffer. |
| 29 | @param duration: seconds to playback |
| 30 | @param rate: the sampling rate |
| 31 | """ |
| 32 | args = [_CRAS_TEST_CLIENT] |
| 33 | args += ['--playback_file', playback_file] |
| 34 | if buffer_frames is not None: |
| 35 | args += ['--buffer_frames', str(buffer_frames)] |
| 36 | if duration is not None: |
| 37 | args += ['--duration', str(duration)] |
| 38 | args += ['--rate', str(rate)] |
| 39 | return args |
| 40 | |
| 41 | def capture_cmd(capture_file, buffer_frames=None, duration=10, rate=44100): |
| 42 | """Gets a command to capture the audio into the file with given settings. |
| 43 | |
| 44 | @param capture_file: the name of file the audio to be stored in. |
| 45 | @param buffer_frames: total number of frames to bufffer. |
| 46 | @param duration: seconds to record. |
| 47 | @param rate: the sampling rate. |
| 48 | """ |
| 49 | args = [_CRAS_TEST_CLIENT] |
| 50 | args += ['--capture_file', capture_file] |
| 51 | if buffer_frames is not None: |
| 52 | args += ['--buffer_frames', str(buffer_frames)] |
| 53 | args += ['--duration', str(duration)] |
| 54 | args += ['--rate', str(rate)] |
| 55 | return args |
| 56 | |
| 57 | def set_system_volume(volume): |
| 58 | """Set the system volume. |
| 59 | |
| 60 | @param volume: the system output vlume to be set(0 - 100). |
| 61 | """ |
| 62 | args = [_CRAS_TEST_CLIENT] |
| 63 | args += ['--volume', str(volume)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame^] | 64 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 65 | |
| 66 | def set_node_volume(node_id, volume): |
| 67 | """Set the volume of the given output node. |
| 68 | |
| 69 | @param node_id: the id of the output node to be set the volume. |
| 70 | @param volume: the volume to be set(0-100). |
| 71 | """ |
| 72 | args = [_CRAS_TEST_CLIENT] |
| 73 | args += ['--set_node_volume', '%s:%d' % (node_id, volume)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame^] | 74 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 75 | |
| 76 | def set_capture_gain(gain): |
| 77 | """Set the system capture gain. |
| 78 | @param gain the capture gain in db*100 (100 = 1dB) |
| 79 | """ |
| 80 | args = [_CRAS_TEST_CLIENT] |
| 81 | args += ['--capture_gain', str(gain)] |
Owen Lin | 9d19b27 | 2013-11-28 12:13:24 +0800 | [diff] [blame^] | 82 | cmd_utils.execute(args) |
Owen Lin | ca365f8 | 2013-11-08 16:52:28 +0800 | [diff] [blame] | 83 | |
| 84 | def dump_server_info(): |
| 85 | """Gets the CRAS's server information.""" |
| 86 | return utils.system_output('%s --dump_server_info' % _CRAS_TEST_CLIENT) |
| 87 | |
| 88 | def get_selected_nodes(): |
| 89 | """Returns the pair of active output node and input node.""" |
| 90 | server_info = dump_server_info() |
| 91 | output_match = _RE_SELECTED_OUTPUT_NODE.search(server_info) |
| 92 | input_match = _RE_SELECTED_INPUT_NODE.search(server_info) |
| 93 | if not output_match or not input_match: |
| 94 | logging.error(server_info) |
| 95 | raise RuntimeError('No match for the pattern') |
| 96 | |
| 97 | return (output_match.group(1).strip(), input_match.group(1).strip()) |