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