1# Lint as: python2, python3 2# Copyright 2014 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""This is a server side headphone audio test using the Chameleon board.""" 6 7import logging 8import os 9import time 10 11from autotest_lib.client.common_lib import error 12from autotest_lib.client.cros.audio import audio_test_data 13from autotest_lib.client.cros.chameleon import audio_test_utils 14from autotest_lib.client.cros.chameleon import chameleon_audio_ids 15from autotest_lib.client.cros.chameleon import chameleon_audio_helper 16from autotest_lib.server.cros.audio import audio_test 17 18 19class audio_AudioBasicHeadphone(audio_test.AudioTest): 20 """Server side headphone audio test. 21 22 This test talks to a Chameleon board and a Cros device to verify 23 headphone audio function of the Cros device. 24 25 """ 26 version = 1 27 DELAY_BEFORE_RECORD_SECONDS = 0.5 28 RECORD_SECONDS = 5 29 DELAY_AFTER_BINDING = 0.5 30 SILENCE_WAIT = 5 31 32 def run_once(self, check_quality=False): 33 """Running basic headphone audio tests. 34 35 @param check_quality: flag to check audio quality. 36 """ 37 if not audio_test_utils.has_audio_jack(self.host): 38 raise error.TestNAError( 39 'No audio jack for the DUT.' 40 ' Confirm swarming bot dimension and control file' 41 ' dependency for audio jack is matching.' 42 ' For new boards, has_audio_jack might need to be updated.' 43 ) 44 45 golden_file = audio_test_data.GenerateAudioTestData( 46 path=os.path.join(self.bindir, 'fix_1k_440_16.wav'), 47 duration_secs=6, 48 frequencies=[1000, 440]) 49 50 source = self.widget_factory.create_widget( 51 chameleon_audio_ids.CrosIds.HEADPHONE) 52 recorder = self.widget_factory.create_widget( 53 chameleon_audio_ids.ChameleonIds.LINEIN) 54 binder = self.widget_factory.create_binder(source, recorder) 55 56 with chameleon_audio_helper.bind_widgets(binder): 57 time.sleep(self.DELAY_AFTER_BINDING) 58 59 audio_test_utils.dump_cros_audio_logs( 60 self.host, self.facade, self.resultsdir, 'after_binding') 61 62 # Selects and checks the node selected by cras is correct. 63 audio_test_utils.check_and_set_chrome_active_node_types( 64 self.facade, 65 audio_test_utils.get_headphone_node(self.host), None) 66 67 logging.info('Setting playback data on Cros device') 68 source.set_playback_data(golden_file) 69 70 # Starts playing, waits for some time, and then starts recording. 71 # This is to avoid artifact caused by codec initialization. 72 logging.info('Start playing %s on Cros device', golden_file.path) 73 source.start_playback() 74 75 time.sleep(self.DELAY_BEFORE_RECORD_SECONDS) 76 logging.info('Start recording from Chameleon.') 77 recorder.start_recording() 78 79 time.sleep(self.RECORD_SECONDS) 80 81 if check_quality: 82 # Add a silence while recording to detect audio 83 # spikes after playback. 84 time.sleep(self.SILENCE_WAIT) 85 86 recorder.stop_recording() 87 logging.info('Stopped recording from Chameleon.') 88 89 audio_test_utils.dump_cros_audio_logs( 90 self.host, self.facade, self.resultsdir, 'after_recording') 91 92 recorder.read_recorded_binary() 93 logging.info('Read recorded binary from Chameleon.') 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 Chameleon codec initialization in the beginning of 101 # recording. 102 recorder.remove_head(0.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. Headphone audio signal has gone through 109 # analog processing. This suffers from codec artifacts and noise on the 110 # path. Comparing data by frequency is more robust than comparing by 111 # correlation, which is suitable for fully-digital audio path like USB 112 # and HDMI. 113 audio_test_utils.check_recorded_frequency( 114 golden_file, recorder, check_artifacts=check_quality) 115