1# Lint as: python2, python3
2# Copyright 2015 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
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_ids
14from autotest_lib.server.cros.audio import audio_test
15
16
17class audio_AudioBasicInternalSpeaker(audio_test.AudioTest):
18    """Server side internal speaker audio test.
19
20    This test talks to a Chameleon board and a Cros device to verify
21    internal speaker audio function of the Cros device.
22
23    """
24    version = 1
25    DELAY_BEFORE_RECORD_SECONDS = 0.5
26    RECORD_SECONDS = 8
27
28    def run_once(self):
29        """Runs Basic Audio Speaker test."""
30        if not audio_test_utils.has_internal_speaker(self.host):
31            return
32
33        golden_file = audio_test_data.GenerateAudioTestData(
34                        path=os.path.join(self.bindir, 'fix_440_16_0.5.raw'),
35                        duration_secs=10,
36                        frequencies=[440, 440],
37                        volume_scale=0.5)
38
39        source = self.widget_factory.create_widget(
40                chameleon_audio_ids.CrosIds.SPEAKER)
41
42        recorder = self.widget_factory.create_widget(
43                chameleon_audio_ids.ChameleonIds.MIC)
44
45        audio_test_utils.dump_cros_audio_logs(self.host, self.facade,
46                                              self.resultsdir, 'start')
47
48        # Selects and checks the node selected by cras is correct.
49        audio_test_utils.check_and_set_chrome_active_node_types(
50                self.facade, 'INTERNAL_SPEAKER', None)
51
52        self.facade.set_selected_output_volume(80)
53
54        logging.info('Setting playback data on Cros device')
55        source.set_playback_data(golden_file)
56
57        # Starts playing, waits for some time, and then starts recording.
58        # This is to avoid artifact caused by codec initialization.
59        logging.info('Start playing %s on Cros device', golden_file.path)
60        source.start_playback()
61
62        time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
63        logging.info('Start recording from Chameleon.')
64        recorder.start_recording()
65
66        time.sleep(self.RECORD_SECONDS)
67        self.facade.check_audio_stream_at_selected_device()
68        recorder.stop_recording()
69        logging.info('Stopped recording from Chameleon.')
70
71        audio_test_utils.dump_cros_audio_logs(
72                self.host, self.facade, self.resultsdir, 'after_recording')
73
74        recorder.read_recorded_binary()
75        logging.info('Read recorded binary from Chameleon.')
76
77        recorded_file = os.path.join(self.resultsdir, "recorded.raw")
78        logging.info('Saving recorded data to %s', recorded_file)
79        recorder.save_file(recorded_file)
80
81        # Removes the beginning of recorded data. This is to avoid artifact
82        # caused by Chameleon codec initialization in the beginning of
83        # recording.
84        recorder.remove_head(0.5)
85
86        # Removes noise by a lowpass filter.
87        recorder.lowpass_filter(1000)
88        recorded_file = os.path.join(self.resultsdir, "recorded_filtered.raw")
89        logging.info('Saving filtered data to %s', recorded_file)
90        recorder.save_file(recorded_file)
91
92        # Compares data by frequency. Audio signal recorded by microphone has
93        # gone through analog processing and through the air.
94        # This suffers from codec artifacts and noise on the path.
95        # Comparing data by frequency is more robust than comparing by
96        # correlation, which is suitable for fully-digital audio path like USB
97        # and HDMI.
98        audio_test_utils.check_recorded_frequency(
99                golden_file,
100                recorder,
101                second_peak_ratio=0.1,
102                ignore_frequencies=[50, 60])
103