Hsinyu Chao | 4b8300e | 2011-11-15 13:07:32 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright (c) 2012 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 | |
| 6 | import logging |
| 7 | import os |
Hsinyu Chao | f80337a | 2012-04-07 18:02:29 +0800 | [diff] [blame] | 8 | import re |
Hsinyu Chao | 2a7e2f2 | 2012-04-18 16:46:43 +0800 | [diff] [blame^] | 9 | import tempfile |
| 10 | import threading |
Hsinyu Chao | 4b8300e | 2011-11-15 13:07:32 -0800 | [diff] [blame] | 11 | |
| 12 | from autotest_lib.client.bin import utils |
| 13 | from autotest_lib.client.common_lib import error |
| 14 | |
| 15 | LD_LIBRARY_PATH = 'LD_LIBRARY_PATH' |
| 16 | |
Hsinyu Chao | f80337a | 2012-04-07 18:02:29 +0800 | [diff] [blame] | 17 | _DEFAULT_NUM_CHANNELS = 2 |
| 18 | _DEFAULT_INPUT_DEVICE = 'default' |
Hsinyu Chao | 2a7e2f2 | 2012-04-18 16:46:43 +0800 | [diff] [blame^] | 19 | _DEFAULT_RECORD_DURATION = 10 |
Hsinyu Chao | f80337a | 2012-04-07 18:02:29 +0800 | [diff] [blame] | 20 | _DEFAULT_SOX_FORMAT = '-t raw -b 16 -e signed -r 48000 -L' |
| 21 | |
| 22 | _SOX_RMS_AMPLITUDE_RE = re.compile('RMS\s+amplitude:\s+(.+)') |
| 23 | _SOX_FORMAT = '-t raw -b 16 -e signed -r 48000 -L' |
| 24 | |
Hsinyu Chao | 2a7e2f2 | 2012-04-18 16:46:43 +0800 | [diff] [blame^] | 25 | |
| 26 | class RecordSampleThread(threading.Thread): |
| 27 | '''Wraps the execution of arecord in a thread.''' |
| 28 | def __init__(self, audio, recordfile): |
| 29 | threading.Thread.__init__(self) |
| 30 | self._audio = audio |
| 31 | self._recordfile = recordfile |
| 32 | |
| 33 | def run(self): |
| 34 | self._audio.record_sample(self._recordfile) |
| 35 | |
| 36 | |
Hsinyu Chao | 4b8300e | 2011-11-15 13:07:32 -0800 | [diff] [blame] | 37 | class AudioHelper(object): |
| 38 | ''' |
| 39 | A helper class contains audio related utility functions. |
| 40 | ''' |
Hsinyu Chao | f80337a | 2012-04-07 18:02:29 +0800 | [diff] [blame] | 41 | def __init__(self, test, sox_format=_DEFAULT_SOX_FORMAT, |
Hsinyu Chao | 2a7e2f2 | 2012-04-18 16:46:43 +0800 | [diff] [blame^] | 42 | input_device=_DEFAULT_INPUT_DEVICE, |
| 43 | record_duration=_DEFAULT_RECORD_DURATION, |
| 44 | num_channels = _DEFAULT_NUM_CHANNELS): |
Hsinyu Chao | 4b8300e | 2011-11-15 13:07:32 -0800 | [diff] [blame] | 45 | self._test = test |
Hsinyu Chao | f80337a | 2012-04-07 18:02:29 +0800 | [diff] [blame] | 46 | self._sox_format = sox_format |
| 47 | self._input_device = input_device |
Hsinyu Chao | 2a7e2f2 | 2012-04-18 16:46:43 +0800 | [diff] [blame^] | 48 | self._record_duration = record_duration |
| 49 | self._num_channels = num_channels |
Hsinyu Chao | 4b8300e | 2011-11-15 13:07:32 -0800 | [diff] [blame] | 50 | |
| 51 | def setup_deps(self, deps): |
| 52 | ''' |
| 53 | Sets up audio related dependencies. |
| 54 | ''' |
| 55 | for dep in deps: |
| 56 | if dep == 'test_tones': |
| 57 | dep_dir = os.path.join(self._test.autodir, 'deps', dep) |
| 58 | self._test.job.install_pkg(dep, 'dep', dep_dir) |
| 59 | self.test_tones_path = os.path.join(dep_dir, 'src', dep) |
| 60 | elif dep == 'audioloop': |
| 61 | dep_dir = os.path.join(self._test.autodir, 'deps', dep) |
| 62 | self._test.job.install_pkg(dep, 'dep', dep_dir) |
| 63 | self.audioloop_path = os.path.join(dep_dir, 'src', |
| 64 | 'looptest') |
| 65 | elif dep == 'sox': |
| 66 | dep_dir = os.path.join(self._test.autodir, 'deps', dep) |
| 67 | self._test.job.install_pkg(dep, 'dep', dep_dir) |
| 68 | self.sox_path = os.path.join(dep_dir, 'bin', dep) |
| 69 | self.sox_lib_path = os.path.join(dep_dir, 'lib') |
| 70 | if os.environ.has_key(LD_LIBRARY_PATH): |
| 71 | paths = os.environ[LD_LIBRARY_PATH].split(':') |
| 72 | if not self.sox_lib_path in paths: |
| 73 | paths.append(self.sox_lib_path) |
| 74 | os.environ[LD_LIBRARY_PATH] = ':'.join(paths) |
| 75 | else: |
| 76 | os.environ[LD_LIBRARY_PATH] = self.sox_lib_path |
| 77 | |
| 78 | def cleanup_deps(self, deps): |
| 79 | ''' |
| 80 | Cleans up environments which has been setup for dependencies. |
| 81 | ''' |
| 82 | for dep in deps: |
| 83 | if dep == 'sox': |
| 84 | if (os.environ.has_key(LD_LIBRARY_PATH) |
| 85 | and hasattr(self, 'sox_lib_path')): |
| 86 | paths = filter(lambda x: x != self.sox_lib_path, |
| 87 | os.environ[LD_LIBRARY_PATH].split(':')) |
| 88 | os.environ[LD_LIBRARY_PATH] = ':'.join(paths) |
| 89 | |
| 90 | def set_mixer_controls(self, mixer_settings={}, card='0'): |
| 91 | ''' |
| 92 | Sets all mixer controls listed in the mixer settings on card. |
| 93 | ''' |
| 94 | logging.info('Setting mixer control values on %s' % card) |
| 95 | for item in mixer_settings: |
| 96 | logging.info('Setting %s to %s on card %s' % |
| 97 | (item['name'], item['value'], card)) |
| 98 | cmd = 'amixer -c %s cset name=%s %s' |
| 99 | cmd = cmd % (card, item['name'], item['value']) |
| 100 | try: |
| 101 | utils.system(cmd) |
| 102 | except error.CmdError: |
| 103 | # A card is allowed not to support all the controls, so don't |
| 104 | # fail the test here if we get an error. |
| 105 | logging.info('amixer command failed: %s' % cmd) |
Hsinyu Chao | f80337a | 2012-04-07 18:02:29 +0800 | [diff] [blame] | 106 | |
| 107 | def get_audio_rms(self, infile, channel): |
| 108 | sox_mixer_cmd = self.get_sox_mixer_cmd(infile, channel) |
| 109 | stat_cmd = '%s -c 1 %s - -n stat 2>&1' % (self.sox_path, |
| 110 | self._sox_format) |
| 111 | sox_cmd = '%s | %s' % (sox_mixer_cmd, stat_cmd) |
| 112 | sox_output = utils.system_output(sox_cmd, retain_output=True) |
| 113 | for rms_line in sox_output.split('\n'): |
| 114 | m = _SOX_RMS_AMPLITUDE_RE.match(rms_line) |
| 115 | if m is not None: |
| 116 | return float(m.group(1)) |
| 117 | |
| 118 | def get_sox_mixer_cmd(self, infile, channel): |
| 119 | # Build up a pan value string for the sox command. |
| 120 | if channel == 0: |
| 121 | pan_values = '1' |
| 122 | else: |
| 123 | pan_values = '0' |
| 124 | for pan_index in range(1, self._num_channels): |
| 125 | if channel == pan_index: |
| 126 | pan_values = '%s%s' % (pan_values, ',1') |
| 127 | else: |
| 128 | pan_values = '%s%s' % (pan_values, ',0') |
| 129 | |
| 130 | return '%s -c 2 %s %s -c 1 %s - mixer %s' % (self.sox_path, |
| 131 | self._sox_format, infile, self._sox_format, pan_values) |
| 132 | |
| 133 | def noise_reduce_file(self, in_file, noise_file, out_file): |
Hsinyu Chao | 2a7e2f2 | 2012-04-18 16:46:43 +0800 | [diff] [blame^] | 134 | '''Runs the sox command to noise-reduce in_file using |
Hsinyu Chao | f80337a | 2012-04-07 18:02:29 +0800 | [diff] [blame] | 135 | the noise profile from noise_file. |
| 136 | |
| 137 | Args: |
| 138 | in_file: The file to noise reduce. |
| 139 | noise_file: The file containing the noise profile. |
| 140 | This can be created by recording silence. |
| 141 | out_file: The file contains the noise reduced sound. |
| 142 | |
| 143 | Returns: |
| 144 | The name of the file containing the noise-reduced data. |
Hsinyu Chao | 2a7e2f2 | 2012-04-18 16:46:43 +0800 | [diff] [blame^] | 145 | ''' |
Hsinyu Chao | f80337a | 2012-04-07 18:02:29 +0800 | [diff] [blame] | 146 | prof_cmd = '%s -c 2 %s %s -n noiseprof' % (self.sox_path, |
| 147 | _SOX_FORMAT, noise_file) |
| 148 | reduce_cmd = ('%s -c 2 %s %s -c 2 %s %s noisered' % |
| 149 | (self.sox_path, _SOX_FORMAT, in_file, _SOX_FORMAT, out_file)) |
| 150 | utils.system('%s | %s' % (prof_cmd, reduce_cmd)) |
| 151 | |
Hsinyu Chao | 2a7e2f2 | 2012-04-18 16:46:43 +0800 | [diff] [blame^] | 152 | def record_sample(self, tmpfile): |
| 153 | '''Records a sample from the default input device. |
Hsinyu Chao | f80337a | 2012-04-07 18:02:29 +0800 | [diff] [blame] | 154 | |
| 155 | Args: |
| 156 | duration: How long to record in seconds. |
| 157 | tmpfile: The file to record to. |
Hsinyu Chao | 2a7e2f2 | 2012-04-18 16:46:43 +0800 | [diff] [blame^] | 158 | ''' |
Hsinyu Chao | f80337a | 2012-04-07 18:02:29 +0800 | [diff] [blame] | 159 | cmd_rec = 'arecord -D %s -d %f -f dat %s' % (self._input_device, |
Hsinyu Chao | 2a7e2f2 | 2012-04-18 16:46:43 +0800 | [diff] [blame^] | 160 | self._record_duration, tmpfile) |
| 161 | logging.info('Command %s recording now (%fs)' % (cmd_rec, |
| 162 | self._record_duration)) |
Hsinyu Chao | f80337a | 2012-04-07 18:02:29 +0800 | [diff] [blame] | 163 | utils.system(cmd_rec) |
Hsinyu Chao | 2a7e2f2 | 2012-04-18 16:46:43 +0800 | [diff] [blame^] | 164 | |
| 165 | def loopback_test_channels(self, noise_file, loopback_callback, |
| 166 | check_recorded_callback): |
| 167 | '''Tests loopback on all channels. |
| 168 | |
| 169 | Args: |
| 170 | noise_file: The file contains the pre-recorded noise. |
| 171 | loopback_callback: The callback to do the loopback for one channel. |
| 172 | check_recorded_callback: The callback function to check the |
| 173 | calculated RMS value. |
| 174 | ''' |
| 175 | for channel in xrange(self._num_channels): |
| 176 | # Temp file for the final noise-reduced file. |
| 177 | with tempfile.NamedTemporaryFile(mode='w+t') as reduced_file: |
| 178 | # Temp file that records before noise reduction. |
| 179 | with tempfile.NamedTemporaryFile(mode='w+t') as tmpfile: |
| 180 | record_thread = RecordSampleThread(self, tmpfile.name) |
| 181 | record_thread.start() |
| 182 | loopback_callback(channel) |
| 183 | record_thread.join() |
| 184 | |
| 185 | self.noise_reduce_file(tmpfile.name, noise_file.name, |
| 186 | reduced_file.name) |
| 187 | |
| 188 | rms_val = self.get_audio_rms(reduced_file.name, channel) |
| 189 | check_recorded_callback(rms_val) |