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 microphone 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.client.cros.chameleon import chameleon_audio_helper
15from autotest_lib.server.cros.audio import audio_test
16
17
18class audio_AudioBasicInternalMicrophone(audio_test.AudioTest):
19    """Server side internal microphone audio test.
20
21    This test talks to a Chameleon board and a Cros device to verify
22    internal mic audio function of the Cros device.
23
24    """
25    version = 1
26    DELAY_BEFORE_RECORD_SECONDS = 0.5
27    RECORD_SECONDS = 9
28    DELAY_AFTER_BINDING = 0.5
29
30    def run_once(self):
31        """Runs Basic Audio Microphone test."""
32        if not audio_test_utils.has_internal_microphone(self.host):
33            return
34
35        # The small volume is to avoid distortion when played on Chameleon.
36        golden_file = audio_test_data.GenerateAudioTestData(
37                path=os.path.join(self.bindir, 'fix_1330_16.raw'),
38                duration_secs=10,
39                frequencies=[1330, 1330],
40                volume_scale=0.1)
41
42        source = self.widget_factory.create_widget(
43                chameleon_audio_ids.ChameleonIds.LINEOUT)
44        sink = self.widget_factory.create_widget(
45                chameleon_audio_ids.PeripheralIds.SPEAKER)
46        binder = self.widget_factory.create_binder(source, sink)
47
48        recorder = self.widget_factory.create_widget(
49                chameleon_audio_ids.CrosIds.INTERNAL_MIC)
50
51        with chameleon_audio_helper.bind_widgets(binder):
52            # Checks the node selected by cras is correct.
53            time.sleep(self.DELAY_AFTER_BINDING)
54
55            audio_test_utils.dump_cros_audio_logs(
56                    self.host, self.facade, self.resultsdir, 'after_binding')
57
58            # Selects and checks the node selected by cras is correct.
59            audio_test_utils.check_and_set_chrome_active_node_types(
60                    self.facade, None,
61                    audio_test_utils.get_internal_mic_node(self.host))
62
63            logging.info('Setting playback data on Chameleon')
64            source.set_playback_data(golden_file)
65
66            # Starts playing, waits for some time, and then starts recording.
67            # This is to avoid artifact caused by chameleon codec initialization
68            # in the beginning of playback.
69            logging.info('Start playing %s from Chameleon', golden_file.path)
70            source.start_playback()
71
72            time.sleep(self.DELAY_BEFORE_RECORD_SECONDS)
73            logging.info('Start recording from Cros device.')
74            recorder.start_recording()
75
76            time.sleep(self.RECORD_SECONDS)
77
78            recorder.stop_recording()
79            logging.info('Stopped recording from Cros device.')
80
81            audio_test_utils.dump_cros_audio_logs(
82                    self.host, self.facade, self.resultsdir, 'after_recording')
83
84            recorder.read_recorded_binary()
85            logging.info('Read recorded binary from Cros device.')
86
87        recorded_file = os.path.join(self.resultsdir, "recorded.raw")
88        logging.info('Saving recorded data to %s', recorded_file)
89        recorder.save_file(recorded_file)
90
91        # Removes the beginning of recorded data. This is to avoid artifact
92        # caused by Cros device codec initialization in the beginning of
93        # recording.
94        recorder.remove_head(1.0)
95
96        # Removes noise by a lowpass filter.
97        recorder.lowpass_filter(1500)
98        recorded_file = os.path.join(self.resultsdir, "recorded_filtered.raw")
99        logging.info('Saving filtered data to %s', recorded_file)
100        recorder.save_file(recorded_file)
101
102        # Cros device only records one channel data. Here we set the channel map
103        # of the recorder to compare the recorded data with left channel of the
104        # test data. This is fine as left and right channel of test data are
105        # identical.
106        recorder.channel_map = [0]
107
108        # Compares data by frequency. Audio signal from Chameleon Line-Out to
109        # speaker and finally recorded on Cros device using internal microphone
110        # has gone through analog processing and through the air.
111        # This suffers from codec artifacts and noise on the path.
112        # Comparing data by frequency is more robust than comparing them by
113        # correlation, which is suitable for fully-digital audio path like USB
114        # and HDMI.
115        audio_test_utils.check_recorded_frequency(
116                golden_file, recorder, second_peak_ratio=0.2)
117