alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | #!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 | """ |
| 18 | Utility script to convert an audio clip into eval platform desired spec. |
| 19 | """ |
| 20 | import soundfile as sf |
| 21 | |
| 22 | from argparse import ArgumentParser |
| 23 | from os import path |
| 24 | |
| 25 | from gen_utils import AudioUtils |
| 26 | |
| 27 | parser = ArgumentParser() |
| 28 | parser.add_argument("--audio_path", help="Audio file path", required=True) |
| 29 | parser.add_argument("--output_dir", help="Output directory", required=True) |
| 30 | parser.add_argument("--sampling_rate", type=int, help="target sampling rate.", default=16000) |
| 31 | parser.add_argument("--mono", type=bool, help="convert signal to mono.", default=True) |
| 32 | parser.add_argument("--offset", type=float, help="start reading after this time (in seconds).", default=0) |
| 33 | parser.add_argument("--duration", type=float, help="only load up to this much audio (in seconds).", default=0) |
| 34 | parser.add_argument("--res_type", type=AudioUtils.res_data_type, help=f"Resample type: {AudioUtils.res_type_list()}.", default='kaiser_best') |
| 35 | parser.add_argument("--min_samples", type=int, help="Minimum sample number.", default=16000) |
| 36 | parser.add_argument("-v", "--verbosity", action="store_true") |
| 37 | args = parser.parse_args() |
| 38 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 39 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 40 | def 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 Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 48 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 49 | if __name__ == '__main__': |
| 50 | main(args) |