blob: fb0b572777f1800ff81338048813bcabb54cd750 [file] [log] [blame]
Owen Linca365f82013-11-08 16:52:28 +08001# Copyright (c) 2013 The Chromium 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
Owen Lin7ab45a22013-11-19 17:26:33 +08005import logging
6import re
Owen Lin7ab45a22013-11-19 17:26:33 +08007
8from autotest_lib.client.cros.audio import cmd_utils
9
Cheng-Yi Chiang17a25272014-11-28 19:05:13 +080010SOX_PATH = 'sox'
Owen Linca365f82013-11-08 16:52:28 +080011
Owen Lin7ab45a22013-11-19 17:26:33 +080012def _raw_format_args(channels, bits, rate):
Cheng-Yi Chiang547d61e2016-09-07 00:45:03 +080013 """Gets raw format args used in sox.
14
15 @param channels: Number of channels.
16 @param bits: Bit length for a sample.
17 @param rate: Sampling rate.
18
19 @returns: A list of args.
20
21 """
Owen Lin7ab45a22013-11-19 17:26:33 +080022 args = ['-t', 'raw', '-e', 'signed']
Cheng-Yi Chiang547d61e2016-09-07 00:45:03 +080023 args += _format_args(channels, bits, rate)
Owen Lin7ab45a22013-11-19 17:26:33 +080024 return args
25
Cheng-Yi Chiang547d61e2016-09-07 00:45:03 +080026
27def _format_args(channels, bits, rate):
28 """Gets format args used in sox.
29
30 @param channels: Number of channels.
31 @param bits: Bit length for a sample.
32 @param rate: Sampling rate.
33
34 @returns: A list of args.
35
36 """
37 return ['-c', str(channels), '-b', str(bits), '-r', str(rate)]
38
39
Owen Linca365f82013-11-08 16:52:28 +080040def generate_sine_tone_cmd(
Cheng-Yi Chiange098f5c2016-11-28 14:31:58 +080041 filename, channels=2, bits=16, rate=48000, duration=None, frequencies=440,
42 gain=None, raw=True):
Owen Linca365f82013-11-08 16:52:28 +080043 """Gets a command to generate sine tones at specified ferquencies.
44
Owen Lin7ab45a22013-11-19 17:26:33 +080045 @param filename: The name of the file to store the sine wave in.
46 @param channels: The number of channels.
47 @param bits: The number of bits of each sample.
48 @param rate: The sampling rate.
49 @param duration: The length of the generated sine tone (in seconds).
Cheng-Yi Chiange098f5c2016-11-28 14:31:58 +080050 @param frequencies: The frequencies of the sine wave. Pass a number or a
51 list to specify frequency for each channel.
Owen Lin7ab45a22013-11-19 17:26:33 +080052 @param gain: The gain (in db).
Cheng-Yi Chiange098f5c2016-11-28 14:31:58 +080053 @param raw: True to use raw data format. False to use what filename specifies.
54
Owen Linca365f82013-11-08 16:52:28 +080055 """
Owen Lin7ab45a22013-11-19 17:26:33 +080056 args = [SOX_PATH, '-n']
Cheng-Yi Chiange098f5c2016-11-28 14:31:58 +080057 if raw:
58 args += _raw_format_args(channels, bits, rate)
59 else:
60 args += _format_args(channels, bits, rate)
Owen Linca365f82013-11-08 16:52:28 +080061 args.append(filename)
62 args.append('synth')
63 if duration is not None:
64 args.append(str(duration))
Cheng-Yi Chiange098f5c2016-11-28 14:31:58 +080065 if not isinstance(frequencies, list):
66 frequencies = [frequencies]
67 for freq in frequencies:
68 args += ['sine', str(freq)]
Owen Linca365f82013-11-08 16:52:28 +080069 if gain is not None:
70 args += ['gain', str(gain)]
71 return args
Owen Lin7ab45a22013-11-19 17:26:33 +080072
73
74def noise_profile(*arg, **karg):
75 """A helper function to execute the noise_profile_cmd."""
76 return cmd_utils.execute(noise_profile_cmd(*arg, **karg))
77
78
Owen Lin2013e462013-12-05 17:54:42 +080079def noise_profile_cmd(input, output, channels=1, bits=16, rate=48000):
Owen Lin7ab45a22013-11-19 17:26:33 +080080 """Gets the noise profile of the input audio.
81
82 @param input: The input audio.
83 @param output: The file where the output profile will be stored in.
84 @param channels: The number of channels.
85 @param bits: The number of bits of each sample.
86 @param rate: The sampling rate.
87 """
88 args = [SOX_PATH]
89 args += _raw_format_args(channels, bits, rate)
90 args += [input, '-n', 'noiseprof', output]
91 return args
92
93
94def noise_reduce(*args, **kargs):
95 """A helper function to execute the noise_reduce_cmd."""
96 return cmd_utils.execute(noise_reduce_cmd(*args, **kargs))
97
98
99def noise_reduce_cmd(
Owen Lin2013e462013-12-05 17:54:42 +0800100 input, output, noise_profile, channels=1, bits=16, rate=48000):
Owen Lin7ab45a22013-11-19 17:26:33 +0800101 """Reduce noise in the input audio by the given noise profile.
102
103 @param input: The input audio file.
104 @param ouput: The output file in which the noise reduced audio is stored.
105 @param noise_profile: The noise profile.
106 @param channels: The number of channels.
107 @param bits: The number of bits of each sample.
108 @param rate: The sampling rate.
109 """
110 args = [SOX_PATH]
111 format_args = _raw_format_args(channels, bits, rate)
112 args += format_args
113 args.append(input)
114 # Uses the same format for output.
115 args += format_args
116 args.append(output)
117 args.append('noisered')
118 args.append(noise_profile)
119 return args
120
121
122def extract_channel_cmd(
Owen Lin2013e462013-12-05 17:54:42 +0800123 input, output, channel_index, channels=2, bits=16, rate=48000):
Owen Lin7ab45a22013-11-19 17:26:33 +0800124 """Extract the specified channel data from the given input audio file.
125
126 @param input: The input audio file.
127 @param output: The output file to which the extracted channel is stored
128 @param channel_index: The index of the channel to be extracted.
129 Note: 1 for the first channel.
130 @param channels: The number of channels.
131 @param bits: The number of bits of each sample.
132 @param rate: The sampling rate.
133 """
134 args = [SOX_PATH]
135 args += _raw_format_args(channels, bits, rate)
136 args.append(input)
137 args += ['-t', 'raw', output]
138 args += ['remix', str(channel_index)]
139 return args
140
141
Owen Lin2013e462013-12-05 17:54:42 +0800142def stat_cmd(input, channels=1, bits=16, rate=44100):
Owen Lin7ab45a22013-11-19 17:26:33 +0800143 """Get statistical information about the input audio data.
144
145 The statistics will be output to standard error.
146
147 @param input: The input audio file.
148 @param channels: The number of channels.
149 @param bits: The number of bits of each sample.
150 @param rate: The sampling rate.
151 """
152 args = [SOX_PATH]
153 args += _raw_format_args(channels, bits, rate)
154 args += [input, '-n', 'stat']
155 return args
156
157
158def get_stat(*args, **kargs):
159 """A helper function to execute the stat_cmd.
160
161 It returns the statistical information (in text) read from the standard
162 error.
163 """
Owen Linad6610a2013-12-13 11:20:48 +0800164 p = cmd_utils.popen(stat_cmd(*args, **kargs), stderr=cmd_utils.PIPE)
Owen Lin7ab45a22013-11-19 17:26:33 +0800165
166 #The output is read from the stderr instead of stdout
167 stat_output = p.stderr.read()
168 cmd_utils.wait_and_check_returncode(p)
169 return parse_stat_output(stat_output)
170
171
172_SOX_STAT_ATTR_MAP = {
173 'Samples read': ('sameple_count', int),
174 'Length (seconds)': ('length', float),
175 'RMS amplitude': ('rms', float),
176 'Rough frequency': ('rough_frequency', float)}
177
178_RE_STAT_LINE = re.compile('(.*):(.*)')
179
180class _SOX_STAT:
181 def __str__(self):
182 return str(vars(self))
183
184
185def _remove_redundant_spaces(value):
186 return ' '.join(value.split()).strip()
187
188
189def parse_stat_output(stat_output):
190 """A helper function to parses the stat_cmd's output to get a python object
191 for easy access to the statistics.
192
193 It returns a python object with the following attributes:
194 .sample_count: The number of the audio samples.
195 .length: The length of the audio (in seconds).
196 .rms: The RMS value of the audio.
197 .rough_frequency: The rough frequency of the audio (in Hz).
198
199 @param stat_output: The statistics ouput to be parsed.
200 """
201 stat = _SOX_STAT()
202
203 for line in stat_output.splitlines():
204 match = _RE_STAT_LINE.match(line)
205 if not match:
206 continue
207 key, value = (_remove_redundant_spaces(x) for x in match.groups())
208 attr, convfun = _SOX_STAT_ATTR_MAP.get(key, (None, None))
209 if attr:
210 setattr(stat, attr, convfun(value))
211
212 if not all(hasattr(stat, x[0]) for x in _SOX_STAT_ATTR_MAP.values()):
213 logging.error('stat_output: %s', stat_output)
214 raise RuntimeError('missing entries: ' + str(stat))
215
216 return stat
Cheng-Yi Chiangbeafe572015-01-12 17:23:24 +0800217
218
Cheng-Yi Chiang13ac4682015-11-24 15:22:36 +0800219def convert_raw_file(path_src, channels_src, bits_src, rate_src,
220 path_dst):
221 """Converts a raw file to a new format.
222
223 @param path_src: The path to the source file.
224 @param channels_src: The channel number of the source file.
225 @param bits_src: The size of sample in bits of the source file.
226 @param rate_src: The sampling rate of the source file.
227 @param path_dst: The path to the destination file. The file name determines
228 the new file format.
229
230 """
231 sox_cmd = [SOX_PATH]
232 sox_cmd += _raw_format_args(channels_src, bits_src, rate_src)
233 sox_cmd += [path_src]
234 sox_cmd += [path_dst]
235 cmd_utils.execute(sox_cmd)
236
237
Cheng-Yi Chiangbeafe572015-01-12 17:23:24 +0800238def convert_format(path_src, channels_src, bits_src, rate_src,
239 path_dst, channels_dst, bits_dst, rate_dst,
Cheng-Yi Chiang547d61e2016-09-07 00:45:03 +0800240 volume_scale, use_src_header=False, use_dst_header=False):
Cheng-Yi Chiangbeafe572015-01-12 17:23:24 +0800241 """Converts a raw file to a new format.
242
243 @param path_src: The path to the source file.
244 @param channels_src: The channel number of the source file.
245 @param bits_src: The size of sample in bits of the source file.
246 @param rate_src: The sampling rate of the source file.
247 @param path_dst: The path to the destination file.
248 @param channels_dst: The channel number of the destination file.
249 @param bits_dst: The size of sample in bits of the destination file.
250 @param rate_dst: The sampling rate of the destination file.
251 @param volume_scale: A float for volume scale used in sox command.
252 E.g. 1.0 is the same. 0.5 to scale volume by
253 half. -1.0 to invert the data.
Cheng-Yi Chiangcad095c2016-08-11 17:43:02 +0800254 @param use_src_header: True to use header from source file and skip
255 specifying channel, sample format, and rate for
256 source. False otherwise.
Cheng-Yi Chiang547d61e2016-09-07 00:45:03 +0800257 @param use_dst_header: True to use header for dst file. False to treat
258 dst file as a raw file.
Cheng-Yi Chiangbeafe572015-01-12 17:23:24 +0800259
260 """
261 sox_cmd = [SOX_PATH]
Cheng-Yi Chiang547d61e2016-09-07 00:45:03 +0800262
Cheng-Yi Chiangcad095c2016-08-11 17:43:02 +0800263 if not use_src_header:
264 sox_cmd += _raw_format_args(channels_src, bits_src, rate_src)
Cheng-Yi Chiangbeafe572015-01-12 17:23:24 +0800265 sox_cmd += ['-v', '%f' % volume_scale]
266 sox_cmd += [path_src]
Cheng-Yi Chiang547d61e2016-09-07 00:45:03 +0800267
268 if not use_dst_header:
269 sox_cmd += _raw_format_args(channels_dst, bits_dst, rate_dst)
270 else:
271 sox_cmd += _format_args(channels_dst, bits_dst, rate_dst)
Cheng-Yi Chiangbeafe572015-01-12 17:23:24 +0800272 sox_cmd += [path_dst]
Cheng-Yi Chiang547d61e2016-09-07 00:45:03 +0800273
Cheng-Yi Chiangbeafe572015-01-12 17:23:24 +0800274 cmd_utils.execute(sox_cmd)
Cheng-Yi Chiang2ac8d9a2015-01-12 17:32:02 +0800275
276
277def lowpass_filter(path_src, channels_src, bits_src, rate_src,
278 path_dst, frequency):
279 """Passes a raw file to a lowpass filter.
280
281 @param path_src: The path to the source file.
282 @param channels_src: The channel number of the source file.
283 @param bits_src: The size of sample in bits of the source file.
284 @param rate_src: The sampling rate of the source file.
285 @param path_dst: The path to the destination file.
286 @param frequency: A float for frequency used in sox command. The 3dB
287 frequency of the lowpass filter. Checks manual of sox
288 command for detail.
289
290 """
291 sox_cmd = [SOX_PATH]
292 sox_cmd += _raw_format_args(channels_src, bits_src, rate_src)
293 sox_cmd += [path_src]
294 sox_cmd += _raw_format_args(channels_src, bits_src, rate_src)
295 sox_cmd += [path_dst]
296 sox_cmd += ['lowpass', '-2', str(frequency)]
297 cmd_utils.execute(sox_cmd)