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 os
6import tempfile
7
8from autotest_lib.client.bin import test
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.cros.audio import alsa_utils, cras_utils
11
12DURATION = 3
13TOLERANT_RATIO = 0.1
14
15class audio_Microphone(test.test):
16    version = 1
17
18
19    def check_recorded_filesize(
20            self, filesize, duration, channels, rate, bits=16):
21        expected = duration * channels * (bits / 8) * rate
22        if abs(float(filesize) / expected - 1) > TOLERANT_RATIO:
23            raise error.TestFail('File size not correct: %d' % filesize)
24
25
26    def verify_alsa_capture(self, channels, rate, bits=16):
27        recorded_file = tempfile.NamedTemporaryFile()
28        alsa_utils.record(
29                recorded_file.name, duration=DURATION, channels=channels,
30                bits=bits, rate=rate)
31        self.check_recorded_filesize(
32                os.path.getsize(recorded_file.name),
33                DURATION, channels, rate, bits)
34
35
36    def verify_cras_capture(self, channels, rate):
37        recorded_file = tempfile.NamedTemporaryFile()
38        cras_utils.capture(
39                recorded_file.name, duration=DURATION, channels=channels,
40                rate=rate)
41        self.check_recorded_filesize(
42                os.path.getsize(recorded_file.name),
43                DURATION, channels, rate)
44
45
46    def run_once(self):
47        # Mono and stereo capturing should work fine @ 44.1KHz and 48KHz.
48        # Verify recording using ALSA utils.
49        self.verify_alsa_capture(1, 44100)
50        self.verify_alsa_capture(1, 48000)
51        self.verify_alsa_capture(2, 48000)
52        self.verify_alsa_capture(2, 44100)
53        # Verify recording of CRAS.
54        self.verify_cras_capture(1, 44100)
55        self.verify_cras_capture(1, 48000)
56        self.verify_cras_capture(2, 48000)
57        self.verify_cras_capture(2, 44100)
58