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