• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2010 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 os
7import tempfile
8
9from autotest_lib.client.bin import test, utils
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.cros.audio import audio_spec
12from autotest_lib.client.cros.audio import alsa_utils
13from autotest_lib.client.cros.audio import cras_utils
14
15DURATION = 3
16TOLERANT_RATIO = 0.1
17
18class audio_Microphone(test.test):
19    version = 1
20
21
22    def check_recorded_filesize(
23            self, filesize, duration, channels, rate, bits=16):
24        expected = duration * channels * (bits / 8) * rate
25        if abs(float(filesize) / expected - 1) > TOLERANT_RATIO:
26            raise error.TestFail('File size not correct: %d' % filesize)
27
28
29    def verify_alsa_capture(self, channels, rate, device, bits=16):
30        recorded_file = tempfile.NamedTemporaryFile()
31        alsa_utils.record(
32                recorded_file.name, duration=DURATION, channels=channels,
33                bits=bits, rate=rate, device=device)
34        self.check_recorded_filesize(
35                os.path.getsize(recorded_file.name),
36                DURATION, channels, rate, bits)
37
38
39    def verify_cras_capture(self, channels, rate):
40        recorded_file = tempfile.NamedTemporaryFile()
41        cras_utils.capture(
42                recorded_file.name, duration=DURATION, channels=channels,
43                rate=rate)
44        self.check_recorded_filesize(
45                os.path.getsize(recorded_file.name),
46                DURATION, channels, rate)
47
48
49    def run_once(self):
50        cras_device_name = cras_utils.get_selected_input_device_name()
51        logging.debug("Selected input device name=%s", cras_device_name)
52
53        if cras_device_name is None:
54            board_type = utils.get_board_type()
55            if not audio_spec.has_internal_microphone(board_type):
56                logging.debug("No internal mic. Skipping the test.")
57                return
58            raise error.TestFail("Fail to get selected input device.")
59
60        # Mono and stereo capturing should work fine @ 44.1KHz and 48KHz.
61
62        # Verify recording using ALSA utils.
63        alsa_device_name = alsa_utils.convert_device_name(cras_device_name)
64        channels = alsa_utils.get_record_device_supported_channels(
65                alsa_device_name)
66        if channels is None:
67            raise error.TestFail("Fail to get supported channels for %s",
68                                alsa_device_name)
69
70        for c in channels:
71            self.verify_alsa_capture(c, 44100, alsa_device_name)
72            self.verify_alsa_capture(c, 48000, alsa_device_name)
73
74        # Verify recording of CRAS.
75        self.verify_cras_capture(1, 44100)
76        self.verify_cras_capture(1, 48000)
77        self.verify_cras_capture(2, 48000)
78        self.verify_cras_capture(2, 44100)
79