• 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.cros.audio import audio_test_data
12from autotest_lib.client.cros.chameleon import audio_test_utils
13from autotest_lib.client.cros.chameleon import chameleon_audio_helper
14from autotest_lib.client.cros.chameleon import chameleon_audio_ids
15from autotest_lib.server.cros.audio import audio_test
16from autotest_lib.server.cros.multimedia import remote_facade_factory
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, cfm_speaker=False):
31
32        if not cfm_speaker and 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 = remote_facade_factory.RemoteFacadeFactory(
39                host, results_dir=self.resultsdir)
40
41        chameleon_board.setup_and_reset(self.outputdir)
42
43        widget_factory = chameleon_audio_helper.AudioWidgetFactory(
44                factory, host)
45
46        source = widget_factory.create_widget(
47            chameleon_audio_ids.CrosIds.SPEAKER)
48
49        recorder = widget_factory.create_widget(
50            chameleon_audio_ids.ChameleonIds.MIC)
51
52        audio_facade = factory.create_audio_facade()
53
54        audio_test_utils.dump_cros_audio_logs(
55                host, audio_facade, self.resultsdir, 'start')
56
57        # Checks the node selected by cras is correct.
58        if not cfm_speaker:
59            audio_test_utils.check_audio_nodes(audio_facade,
60                    (['INTERNAL_SPEAKER'], None))
61        else:
62            audio_test_utils.check_audio_nodes(audio_facade, (['USB'], None))
63
64        audio_facade.set_selected_output_volume(80)
65
66        logging.info('Setting playback data on Cros device')
67        source.set_playback_data(golden_file)
68
69        # Starts playing, waits for some time, and then starts recording.
70        # This is to avoid artifact caused by codec initialization.
71        logging.info('Start playing %s on Cros device',
72                     golden_file.path)
73        source.start_playback()
74
75        time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
76        logging.info('Start recording from Chameleon.')
77        recorder.start_recording()
78
79        time.sleep(self.RECORD_SECONDS)
80
81        recorder.stop_recording()
82        logging.info('Stopped recording from Chameleon.')
83
84        audio_test_utils.dump_cros_audio_logs(
85                host, audio_facade, self.resultsdir, 'after_recording')
86
87        recorder.read_recorded_binary()
88        logging.info('Read recorded binary from Chameleon.')
89
90        recorded_file = os.path.join(self.resultsdir, "recorded.raw")
91        logging.info('Saving recorded data to %s', recorded_file)
92        recorder.save_file(recorded_file)
93
94        # Removes the beginning of recorded data. This is to avoid artifact
95        # caused by Chameleon codec initialization in the beginning of
96        # recording.
97        recorder.remove_head(0.5)
98
99        # Removes noise by a lowpass filter.
100        recorder.lowpass_filter(1000)
101        recorded_file = os.path.join(self.resultsdir, "recorded_filtered.raw")
102        logging.info('Saving filtered data to %s', recorded_file)
103        recorder.save_file(recorded_file)
104
105        # Compares data by frequency. Audio signal recorded by microphone has
106        # gone through analog processing and through the air.
107        # This suffers from codec artifacts and noise on the path.
108        # Comparing data by frequency is more robust than comparing by
109        # correlation, which is suitable for fully-digital audio path like USB
110        # and HDMI.
111        audio_test_utils.check_recorded_frequency(golden_file, recorder,
112                                                  second_peak_ratio=0.1,
113                                                  ignore_frequencies=[50, 60])
114