Home | History | Annotate | Download | only in audio_AudioBasicExternalMicrophone
      1 # Copyright 2015 The Chromium OS 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 """This is a server side external microphone test using the Chameleon board."""
      6 
      7 import logging
      8 import os
      9 import time
     10 
     11 from autotest_lib.client.common_lib import error
     12 from autotest_lib.client.cros.audio import audio_test_data
     13 from autotest_lib.client.cros.chameleon import audio_test_utils
     14 from autotest_lib.client.cros.chameleon import chameleon_audio_ids
     15 from autotest_lib.client.cros.chameleon import chameleon_audio_helper
     16 from autotest_lib.server.cros.audio import audio_test
     17 from autotest_lib.server.cros.multimedia import remote_facade_factory
     18 
     19 
     20 class audio_AudioBasicExternalMicrophone(audio_test.AudioTest):
     21     """Server side external microphone audio test.
     22 
     23     This test talks to a Chameleon board and a Cros device to verify
     24     external mic audio function of the Cros device.
     25 
     26     """
     27     version = 1
     28     DELAY_BEFORE_RECORD_SECONDS = 0.5
     29     RECORD_SECONDS = 5
     30     DELAY_AFTER_BINDING = 0.5
     31 
     32     def run_once(self, host, check_quality=False):
     33         """Running basic headphone audio tests.
     34 
     35         @param host: device under test host
     36         @param check_quality: flag to check audio quality.
     37 
     38         """
     39         golden_file = audio_test_data.SIMPLE_FREQUENCY_TEST_FILE
     40 
     41         chameleon_board = host.chameleon
     42         factory = remote_facade_factory.RemoteFacadeFactory(
     43                 host, results_dir=self.resultsdir)
     44 
     45         chameleon_board.setup_and_reset(self.outputdir)
     46 
     47         widget_factory = chameleon_audio_helper.AudioWidgetFactory(
     48                 factory, host)
     49 
     50         source = widget_factory.create_widget(
     51             chameleon_audio_ids.ChameleonIds.LINEOUT)
     52         recorder = widget_factory.create_widget(
     53             chameleon_audio_ids.CrosIds.EXTERNAL_MIC)
     54         binder = widget_factory.create_binder(source, recorder)
     55 
     56         with chameleon_audio_helper.bind_widgets(binder):
     57             # Checks the node selected by cras is correct.
     58             time.sleep(self.DELAY_AFTER_BINDING)
     59             audio_facade = factory.create_audio_facade()
     60 
     61             audio_test_utils.dump_cros_audio_logs(
     62                     host, audio_facade, self.resultsdir, 'after_binding')
     63 
     64             _, input_nodes = audio_facade.get_selected_node_types()
     65             if input_nodes != ['MIC']:
     66                 raise error.TestFail(
     67                         '%s rather than external mic is selected on Cros '
     68                         'device' % input_nodes)
     69 
     70             logging.info('Setting playback data on Chameleon')
     71             source.set_playback_data(golden_file)
     72 
     73             # Starts playing, waits for some time, and then starts recording.
     74             # This is to avoid artifact caused by chameleon codec initialization
     75             # in the beginning of playback.
     76             logging.info('Start playing %s from Chameleon',
     77                          golden_file.path)
     78             source.start_playback()
     79 
     80             time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
     81             logging.info('Start recording from Cros device.')
     82             recorder.start_recording()
     83 
     84             time.sleep(self.RECORD_SECONDS)
     85 
     86             recorder.stop_recording()
     87             logging.info('Stopped recording from Cros device.')
     88 
     89             audio_test_utils.dump_cros_audio_logs(
     90                     host, audio_facade, self.resultsdir, 'after_recording')
     91 
     92             recorder.read_recorded_binary()
     93             logging.info('Read recorded binary from Cros device.')
     94 
     95         recorded_file = os.path.join(self.resultsdir, "recorded.raw")
     96         logging.info('Saving recorded data to %s', recorded_file)
     97         recorder.save_file(recorded_file)
     98 
     99         # Removes the beginning of recorded data. This is to avoid artifact
    100         # caused by Cros device codec initialization in the beginning of
    101         # recording.
    102         recorder.remove_head(1.5)
    103 
    104         recorded_file = os.path.join(self.resultsdir, "recorded_clipped.raw")
    105         logging.info('Saving clipped data to %s', recorded_file)
    106         recorder.save_file(recorded_file)
    107 
    108         # Compares data by frequency. Audio signal from Chameleon Line-Out to
    109         # Cros device external microphone has gone through analog processing.
    110         # This suffers from codec artifacts and noise on the path.
    111         # Comparing data by frequency is more robust than comparing them by
    112         # correlation, which is suitable for fully-digital audio path like USB
    113         # and HDMI.
    114         audio_test_utils.check_recorded_frequency(
    115                 golden_file, recorder, check_artifacts=check_quality,
    116                 ignore_frequencies=[50, 60])
    117