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