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 speaker 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_AudioBasicInternalSpeaker(audio_test.AudioTest):
21    """Server side internal speaker audio test.
22
23    This test talks to a Chameleon board and a Cros device to verify
24    internal speaker audio function of the Cros device.
25
26    """
27    version = 1
28    DELAY_BEFORE_RECORD_SECONDS = 0.5
29    RECORD_SECONDS = 8
30
31    def run_once(self, host):
32
33        if not audio_test_utils.has_internal_speaker(host):
34            return
35
36        golden_file = audio_test_data.SIMPLE_FREQUENCY_SPEAKER_TEST_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.CrosIds.SPEAKER)
49
50        recorder = widget_factory.create_widget(
51            chameleon_audio_ids.ChameleonIds.MIC)
52
53        audio_facade = factory.create_audio_facade()
54
55        audio_test_utils.dump_cros_audio_logs(
56                host, audio_facade, self.resultsdir, 'start')
57
58        # Checks the node selected by cras is correct.
59        output_nodes, _ = audio_facade.get_selected_node_types()
60        if output_nodes != ['INTERNAL_SPEAKER']:
61            raise error.TestFail(
62                    '%s rather than internal speaker is selected on Cros '
63                    'device' % output_nodes)
64
65        audio_facade.set_selected_output_volume(80)
66
67        logging.info('Setting playback data on Cros device')
68        source.set_playback_data(golden_file)
69
70        # Starts playing, waits for some time, and then starts recording.
71        # This is to avoid artifact caused by codec initialization.
72        logging.info('Start playing %s on Cros device',
73                     golden_file.path)
74        source.start_playback()
75
76        time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
77        logging.info('Start recording from Chameleon.')
78        recorder.start_recording()
79
80        time.sleep(self.RECORD_SECONDS)
81
82        recorder.stop_recording()
83        logging.info('Stopped recording from Chameleon.')
84
85        audio_test_utils.dump_cros_audio_logs(
86                host, audio_facade, self.resultsdir, 'after_recording')
87
88        recorder.read_recorded_binary()
89        logging.info('Read recorded binary from Chameleon.')
90
91        recorded_file = os.path.join(self.resultsdir, "recorded.raw")
92        logging.info('Saving recorded data to %s', recorded_file)
93        recorder.save_file(recorded_file)
94
95        # Removes the beginning of recorded data. This is to avoid artifact
96        # caused by Chameleon codec initialization in the beginning of
97        # recording.
98        recorder.remove_head(0.5)
99
100        # Removes noise by a lowpass filter.
101        recorder.lowpass_filter(1000)
102        recorded_file = os.path.join(self.resultsdir, "recorded_filtered.raw")
103        logging.info('Saving filtered data to %s', recorded_file)
104        recorder.save_file(recorded_file)
105
106        # Compares data by frequency. Audio signal recorded by microphone has
107        # gone through analog processing and through the air.
108        # This suffers from codec artifacts and noise on the path.
109        # Comparing data by frequency is more robust than comparing by
110        # correlation, which is suitable for fully-digital audio path like USB
111        # and HDMI.
112        audio_test_utils.check_recorded_frequency(golden_file, recorder,
113                                                  second_peak_ratio=0.1,
114                                                  ignore_frequencies=[50, 60])
115