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