blob: 92279e6b01f10c933cc5fb2912d4fbf0897a7dde [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
7
8from autotest_lib.client.bin import utils
9from autotest_lib.client.cros.audio import audio_helper
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
15def playback(*args, **kargs):
16 """A helper function to execute the playback_cmd."""
17 audio_helper.execute(playback_cmd(*args, **kargs))
18
19def capture(*args, **kargs):
20 """A helper function to execute the capture_cmd."""
21 audio_helper.execute(capture_cmd(*args, **kargs))
22
23def 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
41def 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
57def 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)]
64 audio_helper.execute(args)
65
66def 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)]
74 audio_helper.execute(args)
75
76def 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)]
82 audio_helper.execute(args)
83
84def dump_server_info():
85 """Gets the CRAS's server information."""
86 return utils.system_output('%s --dump_server_info' % _CRAS_TEST_CLIENT)
87
88def 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())