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
17
18
19class audio_AudioBasicInternalMicrophone(audio_test.AudioTest):
20    """Server side internal microphone audio test.
21
22    This test talks to a Chameleon board and a Cros device to verify
23    internal mic audio function of the Cros device.
24
25    """
26    version = 1
27    DELAY_BEFORE_RECORD_SECONDS = 0.5
28    RECORD_SECONDS = 9
29    DELAY_AFTER_BINDING = 0.5
30
31    def run_once(self, host):
32        if not audio_test_utils.has_internal_microphone(host):
33            return
34
35        golden_file = audio_test_data.SIMPLE_FREQUENCY_TEST_FILE
36
37        chameleon_board = host.chameleon
38        factory = self.create_remote_facade_factory(host)
39
40        chameleon_board.reset()
41
42        widget_factory = chameleon_audio_helper.AudioWidgetFactory(
43                factory, host)
44
45        source = widget_factory.create_widget(
46            chameleon_audio_ids.ChameleonIds.LINEOUT)
47        sink = widget_factory.create_widget(
48            chameleon_audio_ids.PeripheralIds.SPEAKER)
49        binder = widget_factory.create_binder(source, sink)
50
51        recorder = widget_factory.create_widget(
52            chameleon_audio_ids.CrosIds.INTERNAL_MIC)
53
54        with chameleon_audio_helper.bind_widgets(binder):
55            # Checks the node selected by cras is correct.
56            time.sleep(self.DELAY_AFTER_BINDING)
57            audio_facade = factory.create_audio_facade()
58
59            audio_test_utils.dump_cros_audio_logs(
60                    host, audio_facade, self.resultsdir, 'after_binding')
61
62            _, input_nodes = audio_facade.get_selected_node_types()
63            if input_nodes != ['INTERNAL_MIC']:
64                raise error.TestFail(
65                        '%s rather than internal mic is selected on Cros '
66                        'device' % input_nodes)
67
68            logging.info('Setting playback data on Chameleon')
69            source.set_playback_data(golden_file)
70
71            # Starts playing, waits for some time, and then starts recording.
72            # This is to avoid artifact caused by chameleon codec initialization
73            # in the beginning of playback.
74            logging.info('Start playing %s from Chameleon',
75                         golden_file.path)
76            source.start_playback()
77
78            time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
79            logging.info('Start recording from Cros device.')
80            recorder.start_recording()
81
82            time.sleep(self.RECORD_SECONDS)
83
84            recorder.stop_recording()
85            logging.info('Stopped recording from Cros device.')
86
87            audio_test_utils.dump_cros_audio_logs(
88                    host, audio_facade, self.resultsdir, 'after_recording')
89
90            recorder.read_recorded_binary()
91            logging.info('Read recorded binary from Cros device.')
92
93        recorded_file = os.path.join(self.resultsdir, "recorded.raw")
94        logging.info('Saving recorded data to %s', recorded_file)
95        recorder.save_file(recorded_file)
96
97        # Removes the beginning of recorded data. This is to avoid artifact
98        # caused by Cros device codec initialization in the beginning of
99        # recording.
100        recorder.remove_head(1.0)
101
102        # Removes noise by a lowpass filter.
103        recorder.lowpass_filter(1500)
104        recorded_file = os.path.join(self.resultsdir, "recorded_filtered.raw")
105        logging.info('Saving filtered data to %s', recorded_file)
106        recorder.save_file(recorded_file)
107
108        # Cros device only records one channel data. Here we set the channel map
109        # of the recorder to compare the recorded data with left channel of the
110        # test data. This is fine as left and right channel of test data are
111        # identical.
112        recorder.channel_map = [0]
113
114        # Compares data by frequency. Audio signal from Chameleon Line-Out to
115        # speaker and finally recorded on Cros device using internal microphone
116        # has gone through analog processing and through the air.
117        # This suffers from codec artifacts and noise on the path.
118        # Comparing data by frequency is more robust than comparing them by
119        # correlation, which is suitable for fully-digital audio path like USB
120        # and HDMI.
121        audio_test_utils.check_recorded_frequency(golden_file, recorder,
122                                                  second_peak_ratio=0.2)
123