alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | #!env/bin/python3 |
| 2 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 3 | # SPDX-FileCopyrightText: Copyright 2021, 2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | """ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 20 | from argparse import ArgumentParser |
| 21 | from os import path |
| 22 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 23 | import soundfile as sf |
| 24 | |
| 25 | from gen_utils import GenUtils |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 26 | |
| 27 | parser = ArgumentParser() |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 28 | |
| 29 | # pylint: disable=duplicate-code |
| 30 | parser.add_argument( |
| 31 | "--audio_path", |
| 32 | help="Audio file path", |
| 33 | required=True |
| 34 | ) |
| 35 | |
| 36 | parser.add_argument( |
| 37 | "--output_dir", |
| 38 | help="Output directory", |
| 39 | required=True |
| 40 | ) |
| 41 | |
| 42 | parser.add_argument( |
| 43 | "--sampling_rate", |
| 44 | type=int, |
| 45 | help="target sampling rate.", |
| 46 | default=16000 |
| 47 | ) |
| 48 | |
| 49 | parser.add_argument( |
| 50 | "--mono", |
| 51 | type=bool, |
| 52 | help="convert signal to mono.", |
| 53 | default=True |
| 54 | ) |
| 55 | |
| 56 | parser.add_argument( |
| 57 | "--offset", |
| 58 | type=float, |
| 59 | help="start reading after this time (in seconds).", |
| 60 | default=0 |
| 61 | ) |
| 62 | |
| 63 | parser.add_argument( |
| 64 | "--duration", |
| 65 | type=float, |
| 66 | help="only load up to this much audio (in seconds).", |
| 67 | default=0 |
| 68 | ) |
| 69 | |
| 70 | parser.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 | |
| 77 | parser.add_argument( |
| 78 | "--min_samples", |
| 79 | type=int, |
| 80 | help="Minimum sample number.", |
| 81 | default=16000 |
| 82 | ) |
| 83 | |
| 84 | parser.add_argument( |
| 85 | "-v", |
| 86 | "--verbosity", |
| 87 | action="store_true" |
| 88 | ) |
| 89 | # pylint: enable=duplicate-code |
| 90 | |
| 91 | parsed_args = parser.parse_args() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 92 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 93 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 94 | def main(args): |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 95 | """ |
| 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 | ) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 112 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 113 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 114 | if __name__ == '__main__': |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 115 | main(parsed_args) |