blob: ff33bfb5a1943c484c4f5b30f817e52e6da6a020 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001#!env/bin/python3
2
Richard Burtonf32a86a2022-11-15 11:46:11 +00003# SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
alexander3c798932021-03-26 21:42:19 +00004# 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
Richard Burton17069622022-03-17 10:54:26 +000039
alexander3c798932021-03-26 21:42:19 +000040def main(args):
41 audio_data, samplerate = AudioUtils.load_resample_audio_clip(args.audio_path,
42 args.sampling_rate,
43 args.mono, args.offset,
44 args.duration, args.res_type,
45 args.min_samples)
46 sf.write(path.join(args.output_dir, path.basename(args.audio_path)), audio_data, samplerate)
47
Richard Burton17069622022-03-17 10:54:26 +000048
alexander3c798932021-03-26 21:42:19 +000049if __name__ == '__main__':
50 main(args)