1# Copyright 2016 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
5import logging
6import tempfile
7import time
8
9import common
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.common_lib import site_utils
12from autotest_lib.client.common_lib.feedback import client
13from autotest_lib.server import test
14from autotest_lib.server.brillo import audio_utils
15
16
17# Number of channels to generate.
18_DEFAULT_NUM_CHANNELS = 2
19# Sine wave sample rate (44.1kHz).
20_DEFAULT_SAMPLE_RATE = 44100
21# Sine wave default sample format is signed 16-bit PCM (two bytes).
22_DEFAULT_SAMPLE_WIDTH = 2
23# Default sine wave frequency.
24_DEFAULT_SINE_FREQUENCY = 440
25# Default duration of the sine wave in seconds.
26_DEFAULT_DURATION_SECS = 10
27
28class brillo_DecodingAudioTest(test.test):
29    """Verify that basic audio playback works."""
30    version = 1
31
32
33    def run_once(self, host, fb_client, file_format,
34                 duration_secs=_DEFAULT_DURATION_SECS):
35        """Runs the test.
36
37        @param host: A host object representing the DUT.
38        @param fb_client: A feedback client implementation.
39        @param file_format: A string represeting the format to the audio
40                            encoding to use.
41        @param duration_secs: Duration to play file for.
42        """
43        self.temp_dir = tempfile.mkdtemp(dir=fb_client.tmp_dir)
44        with fb_client.initialize(self, host):
45            logging.info('Testing silent playback to get silence threshold')
46            fb_query = fb_client.new_query(client.QUERY_AUDIO_PLAYBACK_SILENT)
47            fb_query.prepare()
48            time.sleep(duration_secs)
49            fb_query.validate()
50
51            logging.info('Generate mp3 file')
52            local_filename, dut_play_file = audio_utils.generate_sine_file(
53                    host, _DEFAULT_NUM_CHANNELS, _DEFAULT_SAMPLE_RATE,
54                    _DEFAULT_SAMPLE_WIDTH, _DEFAULT_DURATION_SECS,
55                    _DEFAULT_SINE_FREQUENCY, self.temp_dir, file_format)
56
57            fb_query = fb_client.new_query(client.QUERY_AUDIO_PLAYBACK_AUDIBLE)
58
59            fb_query.prepare(sample_width=_DEFAULT_SAMPLE_WIDTH,
60                             sample_rate=_DEFAULT_SAMPLE_RATE,
61                             duration_secs=duration_secs,
62                             num_channels=_DEFAULT_NUM_CHANNELS)
63
64            playback_cmd = 'slesTest_playFdPath %s 0' % dut_play_file
65            logging.info('Testing decode playback')
66            host.run(playback_cmd)
67            fb_query.validate(audio_file=local_filename)
68