blob: 4d7318c7771194a4c37143b4c677b7ac37d61542 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001#!env/bin/python3
2
Alex Tawsedaba3cf2023-09-29 15:55:38 +01003# SPDX-FileCopyrightText: Copyright 2021, 2023 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"""
alexander3c798932021-03-26 21:42:19 +000020from argparse import ArgumentParser
21from os import path
22
Alex Tawsedaba3cf2023-09-29 15:55:38 +010023import soundfile as sf
24
25from gen_utils import GenUtils
alexander3c798932021-03-26 21:42:19 +000026
27parser = ArgumentParser()
Alex Tawsedaba3cf2023-09-29 15:55:38 +010028
29# pylint: disable=duplicate-code
30parser.add_argument(
31 "--audio_path",
32 help="Audio file path",
33 required=True
34)
35
36parser.add_argument(
37 "--output_dir",
38 help="Output directory",
39 required=True
40)
41
42parser.add_argument(
43 "--sampling_rate",
44 type=int,
45 help="target sampling rate.",
46 default=16000
47)
48
49parser.add_argument(
50 "--mono",
51 type=bool,
52 help="convert signal to mono.",
53 default=True
54)
55
56parser.add_argument(
57 "--offset",
58 type=float,
59 help="start reading after this time (in seconds).",
60 default=0
61)
62
63parser.add_argument(
64 "--duration",
65 type=float,
66 help="only load up to this much audio (in seconds).",
67 default=0
68)
69
70parser.add_argument(
71 "--res_type",
72 type=GenUtils.res_data_type,
73 help=f"Resample type: {GenUtils.res_type_list()}.",
74 default='kaiser_best'
75)
76
77parser.add_argument(
78 "--min_samples",
79 type=int,
80 help="Minimum sample number.",
81 default=16000
82)
83
84parser.add_argument(
85 "-v",
86 "--verbosity",
87 action="store_true"
88)
89# pylint: enable=duplicate-code
90
91parsed_args = parser.parse_args()
alexander3c798932021-03-26 21:42:19 +000092
Richard Burton17069622022-03-17 10:54:26 +000093
alexander3c798932021-03-26 21:42:19 +000094def main(args):
Alex Tawsedaba3cf2023-09-29 15:55:38 +010095 """
96 Generate the new audio file
97 @param args: Parsed args
98 """
99 audio_sample = GenUtils.read_audio_file(
100 args.audio_path, args.offset, args.duration
101 )
102
103 resampled_audio = GenUtils.resample_audio_clip(
104 audio_sample, args.sampling_rate, args.mono, args.res_type, args.min_samples
105 )
106
107 sf.write(
108 path.join(args.output_dir, path.basename(args.audio_path)),
109 resampled_audio.data,
110 resampled_audio.sample_rate
111 )
alexander3c798932021-03-26 21:42:19 +0000112
Richard Burton17069622022-03-17 10:54:26 +0000113
alexander3c798932021-03-26 21:42:19 +0000114if __name__ == '__main__':
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100115 main(parsed_args)