blob: 74422de944b13e2a90a6be43b95b430923fbc6a4 [file] [log] [blame]
Owen Linca365f82013-11-08 16:52:28 +08001# 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
5import logging
6import re
Owen Lin0d65e8a2013-11-28 14:29:54 +08007import subprocess
Owen Linca365f82013-11-08 16:52:28 +08008
9from autotest_lib.client.bin import utils
Owen Lin9d19b272013-11-28 12:13:24 +080010from autotest_lib.client.cros.audio import cmd_utils
Owen Linca365f82013-11-08 16:52:28 +080011
12_CRAS_TEST_CLIENT = '/usr/bin/cras_test_client'
13_RE_SELECTED_OUTPUT_NODE = re.compile('Selected Output Node: (.*)')
14_RE_SELECTED_INPUT_NODE = re.compile('Selected Input Node: (.*)')
15
16def playback(*args, **kargs):
17 """A helper function to execute the playback_cmd."""
Owen Lin9d19b272013-11-28 12:13:24 +080018 cmd_utils.execute(playback_cmd(*args, **kargs))
Owen Linca365f82013-11-08 16:52:28 +080019
20def capture(*args, **kargs):
21 """A helper function to execute the capture_cmd."""
Owen Lin9d19b272013-11-28 12:13:24 +080022 cmd_utils.execute(capture_cmd(*args, **kargs))
Owen Linca365f82013-11-08 16:52:28 +080023
24def playback_cmd(playback_file, buffer_frames=None, duration=None, rate=44100):
25 """Gets a command to playback a file with given settings.
26
27 @param playback_file: the name of the file to play. '-' indicates to
28 playback raw audio from the stdin.
29 @param buffer_frames: total number of frames to buffer.
30 @param duration: seconds to playback
31 @param rate: the sampling rate
32 """
33 args = [_CRAS_TEST_CLIENT]
34 args += ['--playback_file', playback_file]
35 if buffer_frames is not None:
36 args += ['--buffer_frames', str(buffer_frames)]
37 if duration is not None:
38 args += ['--duration', str(duration)]
39 args += ['--rate', str(rate)]
40 return args
41
42def capture_cmd(capture_file, buffer_frames=None, duration=10, rate=44100):
43 """Gets a command to capture the audio into the file with given settings.
44
45 @param capture_file: the name of file the audio to be stored in.
46 @param buffer_frames: total number of frames to bufffer.
47 @param duration: seconds to record.
48 @param rate: the sampling rate.
49 """
50 args = [_CRAS_TEST_CLIENT]
51 args += ['--capture_file', capture_file]
52 if buffer_frames is not None:
53 args += ['--buffer_frames', str(buffer_frames)]
54 args += ['--duration', str(duration)]
55 args += ['--rate', str(rate)]
56 return args
57
58def set_system_volume(volume):
59 """Set the system volume.
60
61 @param volume: the system output vlume to be set(0 - 100).
62 """
63 args = [_CRAS_TEST_CLIENT]
64 args += ['--volume', str(volume)]
Owen Lin9d19b272013-11-28 12:13:24 +080065 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +080066
67def set_node_volume(node_id, volume):
68 """Set the volume of the given output node.
69
70 @param node_id: the id of the output node to be set the volume.
71 @param volume: the volume to be set(0-100).
72 """
73 args = [_CRAS_TEST_CLIENT]
74 args += ['--set_node_volume', '%s:%d' % (node_id, volume)]
Owen Lin9d19b272013-11-28 12:13:24 +080075 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +080076
77def set_capture_gain(gain):
78 """Set the system capture gain.
79 @param gain the capture gain in db*100 (100 = 1dB)
80 """
81 args = [_CRAS_TEST_CLIENT]
82 args += ['--capture_gain', str(gain)]
Owen Lin9d19b272013-11-28 12:13:24 +080083 cmd_utils.execute(args)
Owen Linca365f82013-11-08 16:52:28 +080084
85def dump_server_info():
86 """Gets the CRAS's server information."""
Owen Lin0d65e8a2013-11-28 14:29:54 +080087 args = [_CRAS_TEST_CLIENT, '--dump_server_info']
88 return cmd_utils.execute(args, stdout=subprocess.PIPE)
Owen Linca365f82013-11-08 16:52:28 +080089
90def get_selected_nodes():
91 """Returns the pair of active output node and input node."""
92 server_info = dump_server_info()
93 output_match = _RE_SELECTED_OUTPUT_NODE.search(server_info)
94 input_match = _RE_SELECTED_INPUT_NODE.search(server_info)
95 if not output_match or not input_match:
96 logging.error(server_info)
97 raise RuntimeError('No match for the pattern')
98
99 return (output_match.group(1).strip(), input_match.group(1).strip())