1# Lint as: python2, python3 2# Copyright 2018 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 hotwording test using the Chameleon board.""" 6 7import logging 8import os 9import time 10import threading 11 12from autotest_lib.client.common_lib import error 13from autotest_lib.client.cros.audio import audio_test_data 14from autotest_lib.client.cros.chameleon import audio_test_utils 15from autotest_lib.client.cros.chameleon import chameleon_audio_helper 16from autotest_lib.client.cros.chameleon import chameleon_audio_ids 17from autotest_lib.server.cros.audio import audio_test 18 19 20class audio_AudioBasicHotwording(audio_test.AudioTest): 21 """Server side hotwording test. 22 23 This test talks to a Chameleon board and a Cros device to verify 24 hotwording function of the Cros device. 25 26 """ 27 version = 1 28 DELAY_AFTER_BINDING_SECS = 0.5 29 DELAY_AFTER_START_LISTENING_SECS = 1 30 DELAY_AFTER_SUSPEND_SECS = 5 31 32 RECORD_SECONDS = 5 33 SUSPEND_SECONDS = 20 34 RESUME_TIMEOUT_SECS = 60 35 36 def run_once(self, suspend=False): 37 """Runs Basic Audio Hotwording test. 38 39 @param suspend: True for suspend the device before playing hotword. 40 False for not suspend. 41 42 """ 43 if (not audio_test_utils.has_hotwording(self.host)): 44 raise error.TestNAError( 45 'No hotwording device for the DUT.' 46 ' Confirm swarming bot dimension and control file' 47 ' dependency for hot-wording is matching.' 48 ' For new boards, please update the BOARDS_WITH_HOTWORDING.' 49 ) 50 51 hotword_file = audio_test_data.HOTWORD_TEST_FILE 52 golden_file = audio_test_data.GenerateAudioTestData( 53 path=os.path.join(self.bindir, 'fix_1330_16.raw'), 54 duration_secs=10, 55 frequencies=[1330, 1330], 56 volume_scale=0.1) 57 58 source = self.widget_factory.create_widget( 59 chameleon_audio_ids.ChameleonIds.LINEOUT) 60 sink = self.widget_factory.create_widget( 61 chameleon_audio_ids.PeripheralIds.SPEAKER) 62 binder = self.widget_factory.create_binder(source, sink) 63 64 listener = self.widget_factory.create_widget( 65 chameleon_audio_ids.CrosIds.HOTWORDING) 66 67 with chameleon_audio_helper.bind_widgets(binder): 68 time.sleep(self.DELAY_AFTER_BINDING_SECS) 69 70 audio_test_utils.dump_cros_audio_logs( 71 self.host, self.facade, self.resultsdir, 'after_binding') 72 73 logging.info('Start listening from Cros device.') 74 listener.start_listening() 75 time.sleep(self.DELAY_AFTER_START_LISTENING_SECS) 76 77 audio_test_utils.dump_cros_audio_logs(self.host, self.facade, 78 self.resultsdir, 79 'after_start_listening') 80 if suspend: 81 82 def suspend_host(): 83 """Call the host method suspend.""" 84 logging.info('Suspend the DUT for %d secs', 85 self.SUSPEND_SECONDS) 86 self.host.suspend( 87 suspend_time=self.SUSPEND_SECONDS, 88 allow_early_resume=True) 89 90 # Folk a thread to suspend the host 91 boot_id = self.host.get_boot_id() 92 thread = threading.Thread(target=suspend_host) 93 thread.start() 94 suspend_start_time = time.time() 95 time.sleep(self.DELAY_AFTER_SUSPEND_SECS) 96 97 # Starts playing hotword and golden file. 98 # Sleep for 5s for DUT to detect and record the sounds 99 logging.info('Setting hotword playback data on Chameleon') 100 remote_hotword_file_path = source.set_playback_data(hotword_file) 101 102 logging.info('Setting golden playback data on Chameleon') 103 remote_golden_file_path = source.set_playback_data(golden_file) 104 105 logging.info('Start playing %s from Chameleon', hotword_file.path) 106 source.start_playback_with_path(remote_hotword_file_path) 107 time.sleep(hotword_file.duration_secs) 108 109 logging.info('Start playing %s from Chameleon', golden_file.path) 110 source.start_playback_with_path(remote_golden_file_path) 111 112 time.sleep(self.RECORD_SECONDS) 113 114 # If the DUT suspended, the server will reconnect to DUT 115 if suspend: 116 self.host.test_wait_for_resume(boot_id, 117 self.RESUME_TIMEOUT_SECS) 118 real_suspend_time = time.time() - suspend_start_time 119 logging.info('Suspend for %f time.', real_suspend_time) 120 121 # Check the if real suspend time is less than SUSPEND_SECOND 122 if real_suspend_time < self.SUSPEND_SECONDS: 123 logging.info('Real suspend time is less than ' 124 'SUSPEND_SECONDS. Hotwording succeeded.') 125 else: 126 logging.error( 127 'Real suspend time is larger than or equal to' 128 'SUSPEND_SECONDS. Hostwording failed.') 129 130 listener.stop_listening() 131 logging.info('Stopped listening from Cros device.') 132 133 audio_test_utils.dump_cros_audio_logs( 134 self.host, self.facade, self.resultsdir, 'after_listening') 135 136 listener.read_recorded_binary() 137 logging.info('Read recorded binary from Cros device.') 138 139 recorded_file = os.path.join(self.resultsdir, "recorded.raw") 140 logging.info('Saving recorded data to %s', recorded_file) 141 listener.save_file(recorded_file) 142 143 # Removes the first 5s of recorded data, which included hotword and 144 # the data before hotword. 145 listener.remove_head(5.0) 146 short_recorded_file = os.path.join(self.resultsdir, 147 "short_recorded.raw") 148 listener.save_file(short_recorded_file) 149 150 # Cros device only records one channel data. Here we set the channel map 151 # of the recorder to compare the recorded data with left channel of the 152 # test data. This is fine as left and right channel of test data are 153 # identical. 154 listener.channel_map = [0] 155 156 # Compares data by frequency. Audio signal from Chameleon Line-Out to 157 # speaker and finally recorded on Cros device using hotwording chip 158 # has gone through analog processing and through the air. 159 # This suffers from codec artifacts and noise on the path. 160 # Comparing data by frequency is more robust than comparing them by 161 # correlation, which is suitable for fully-digital audio path like USB 162 # and HDMI. 163 audio_test_utils.check_recorded_frequency( 164 golden_file, listener, second_peak_ratio=0.2) 165