blob: 53ed0190491663394992b4c34aa7ffbb689eed24 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001#!env/bin/python3
2
3# Copyright (c) 2021 Arm Limited. All rights reserved.
4# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17"""
18Utility script to convert an audio clip into eval platform desired spec.
19"""
20import soundfile as sf
21
22from argparse import ArgumentParser
23from os import path
24
25from gen_utils import AudioUtils
26
27parser = ArgumentParser()
28parser.add_argument("--audio_path", help="Audio file path", required=True)
29parser.add_argument("--output_dir", help="Output directory", required=True)
30parser.add_argument("--sampling_rate", type=int, help="target sampling rate.", default=16000)
31parser.add_argument("--mono", type=bool, help="convert signal to mono.", default=True)
32parser.add_argument("--offset", type=float, help="start reading after this time (in seconds).", default=0)
33parser.add_argument("--duration", type=float, help="only load up to this much audio (in seconds).", default=0)
34parser.add_argument("--res_type", type=AudioUtils.res_data_type, help=f"Resample type: {AudioUtils.res_type_list()}.", default='kaiser_best')
35parser.add_argument("--min_samples", type=int, help="Minimum sample number.", default=16000)
36parser.add_argument("-v", "--verbosity", action="store_true")
37args = parser.parse_args()
38
39def main(args):
40 audio_data, samplerate = AudioUtils.load_resample_audio_clip(args.audio_path,
41 args.sampling_rate,
42 args.mono, args.offset,
43 args.duration, args.res_type,
44 args.min_samples)
45 sf.write(path.join(args.output_dir, path.basename(args.audio_path)), audio_data, samplerate)
46
47if __name__ == '__main__':
48 main(args)