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 an audio quality test over headphone 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_MediaBasicVerification(audio_test.AudioTest):
20    """Server side audio quality test over 3.5 headphones.
21
22    This test talks to a Chameleon board and a Cros device to verify
23    headphone audio quality of the Cros device.
24
25    """
26    version = 1
27    DELAY_BEFORE_RECORD_SECONDS = 0.5
28    RECORD_SECONDS = 10
29    DELAY_AFTER_BINDING = 0.5
30
31    def run_once(self, host, audio_test_file):
32        chameleon_board = host.chameleon
33        factory = self.create_remote_facade_factory(host)
34        chameleon_board.reset()
35
36        widget_factory = chameleon_audio_helper.AudioWidgetFactory(
37                factory, host)
38
39        source = widget_factory.create_widget(
40            chameleon_audio_ids.CrosIds.HEADPHONE)
41        recorder = widget_factory.create_widget(
42            chameleon_audio_ids.ChameleonIds.LINEIN)
43        binder = widget_factory.create_binder(source, recorder)
44
45        with chameleon_audio_helper.bind_widgets(binder):
46            # Checks the node selected by cras is correct.
47            time.sleep(self.DELAY_AFTER_BINDING)
48            audio_facade = factory.create_audio_facade()
49
50            audio_test_utils.dump_cros_audio_logs(
51                    host, audio_facade, self.resultsdir, 'after_binding')
52
53            output_nodes, _ = audio_facade.get_selected_node_types()
54            if output_nodes != ['HEADPHONE']:
55                raise error.TestFail(
56                        '%s rather than headphone is selected on Cros '
57                        'device' % output_nodes)
58
59            # Starts playing, waits for some time, and then starts recording.
60            # This is to avoid artifact caused by codec initialization.
61            logging.info('Start playing %s on Cros device',
62                         audio_test_file)
63            browser_facade = factory.create_browser_facade()
64            tab_descriptor = browser_facade.new_tab(audio_test_file)
65
66            time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
67            logging.info('Start recording from Chameleon.')
68            recorder.start_recording()
69
70            time.sleep(self.RECORD_SECONDS)
71
72            recorder.stop_recording()
73            logging.info('Stopped recording from Chameleon.')
74            browser_facade.close_tab(tab_descriptor)
75
76            audio_test_utils.dump_cros_audio_logs(
77                    host, audio_facade, self.resultsdir, 'after_recording')
78
79            recorder.read_recorded_binary()
80            logging.info('Read recorded binary from Chameleon.')
81
82        recorded_file = os.path.join(self.resultsdir, 'recorded.raw')
83        logging.info('Saving recorded data to %s', recorded_file)
84        recorder.save_file(recorded_file)
85
86        # Removes the beginning of recorded data. This is to avoid artifact
87        # caused by Chameleon codec initialization in the beginning of
88        # recording.
89        recorder.remove_head(0.5)
90
91        # Removes noise by a lowpass filter.
92        recorder.lowpass_filter(4000)
93        recorded_file = os.path.join(self.resultsdir, 'recorded_filtered.raw')
94        logging.info('Saving filtered data to %s', recorded_file)
95        recorder.save_file(recorded_file)
96
97        # Compares data by frequency. Headphone audio signal has gone through
98        # analog processing. This suffers from codec artifacts and noise on the
99        # path. Comparing data by frequency is more robust than comparing by
100        # correlation, which is suitable for fully-digital audio path like USB
101        # and HDMI.
102        audio_test_utils.check_recorded_frequency(
103                audio_test_data.MEDIA_HEADPHONE_TEST_FILE,
104                recorder, check_anomaly=True)
105