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 internal microphone 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_helper 15from autotest_lib.client.cros.chameleon import chameleon_audio_ids 16from autotest_lib.server.cros.audio import audio_test 17from autotest_lib.server.cros.multimedia import remote_facade_factory 18 19 20class audio_AudioBasicInternalMicrophone(audio_test.AudioTest): 21 """Server side internal microphone audio test. 22 23 This test talks to a Chameleon board and a Cros device to verify 24 internal mic audio function of the Cros device. 25 26 """ 27 version = 1 28 DELAY_BEFORE_RECORD_SECONDS = 0.5 29 RECORD_SECONDS = 9 30 DELAY_AFTER_BINDING = 0.5 31 32 def run_once(self, host): 33 if not audio_test_utils.has_internal_microphone(host): 34 return 35 36 golden_file = audio_test_data.SIMPLE_FREQUENCY_TEST_1330_FILE 37 38 chameleon_board = host.chameleon 39 factory = remote_facade_factory.RemoteFacadeFactory( 40 host, results_dir=self.resultsdir) 41 42 chameleon_board.setup_and_reset(self.outputdir) 43 44 widget_factory = chameleon_audio_helper.AudioWidgetFactory( 45 factory, host) 46 47 source = widget_factory.create_widget( 48 chameleon_audio_ids.ChameleonIds.LINEOUT) 49 sink = widget_factory.create_widget( 50 chameleon_audio_ids.PeripheralIds.SPEAKER) 51 binder = widget_factory.create_binder(source, sink) 52 53 recorder = widget_factory.create_widget( 54 chameleon_audio_ids.CrosIds.INTERNAL_MIC) 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 != ['INTERNAL_MIC']: 66 raise error.TestFail( 67 '%s rather than internal 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.0) 103 104 # Removes noise by a lowpass filter. 105 recorder.lowpass_filter(1500) 106 recorded_file = os.path.join(self.resultsdir, "recorded_filtered.raw") 107 logging.info('Saving filtered data to %s', recorded_file) 108 recorder.save_file(recorded_file) 109 110 # Cros device only records one channel data. Here we set the channel map 111 # of the recorder to compare the recorded data with left channel of the 112 # test data. This is fine as left and right channel of test data are 113 # identical. 114 recorder.channel_map = [0] 115 116 # Compares data by frequency. Audio signal from Chameleon Line-Out to 117 # speaker and finally recorded on Cros device using internal microphone 118 # has gone through analog processing and through the air. 119 # This suffers from codec artifacts and noise on the path. 120 # Comparing data by frequency is more robust than comparing them by 121 # correlation, which is suitable for fully-digital audio path like USB 122 # and HDMI. 123 audio_test_utils.check_recorded_frequency(golden_file, recorder, 124 second_peak_ratio=0.2) 125