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
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_AudioBasicExternalMicrophone(audio_test.AudioTest):
20    """Server side external microphone audio test.
21
22    This test talks to a Chameleon board and a Cros device to verify
23    external mic 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
31    def run_once(self, host, check_quality=False):
32        """Running basic headphone audio tests.
33
34        @param host: device under test host
35        @param check_quality: flag to check audio quality.
36
37        """
38        golden_file = audio_test_data.SIMPLE_FREQUENCY_TEST_FILE
39
40        chameleon_board = host.chameleon
41        factory = self.create_remote_facade_factory(host)
42
43        chameleon_board.reset()
44
45        widget_factory = chameleon_audio_helper.AudioWidgetFactory(
46                factory, host)
47
48        source = widget_factory.create_widget(
49            chameleon_audio_ids.ChameleonIds.LINEOUT)
50        recorder = widget_factory.create_widget(
51            chameleon_audio_ids.CrosIds.EXTERNAL_MIC)
52        binder = widget_factory.create_binder(source, recorder)
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 != ['MIC']:
64                raise error.TestFail(
65                        '%s rather than external 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.5)
101
102        recorded_file = os.path.join(self.resultsdir, "recorded_clipped.raw")
103        logging.info('Saving clipped data to %s', recorded_file)
104        recorder.save_file(recorded_file)
105
106        # Compares data by frequency. Audio signal from Chameleon Line-Out to
107        # Cros device external microphone has gone through analog processing.
108        # This suffers from codec artifacts and noise on the path.
109        # Comparing data by frequency is more robust than comparing them by
110        # correlation, which is suitable for fully-digital audio path like USB
111        # and HDMI.
112        audio_test_utils.check_recorded_frequency(
113                golden_file, recorder, check_anomaly=check_quality)
114