Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 1 | # SPDX-FileCopyrightText: Copyright 2021, 2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 15 | """ |
| 16 | This script can be used with the noise reduction use case to save |
| 17 | the dumped noise reduced audio to a wav file. |
| 18 | |
| 19 | Example use: |
| 20 | python rnnoise_dump_extractor.py --dump_file output.bin --output_dir ./denoised_wavs/ |
| 21 | """ |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 22 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 23 | import argparse |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 24 | import struct |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 25 | import typing |
| 26 | from os import path |
| 27 | |
| 28 | import numpy as np |
| 29 | import soundfile as sf |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 30 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 31 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 32 | def extract( |
| 33 | dump_file: typing.IO, |
| 34 | output_dir: str, |
| 35 | export_npy: bool |
| 36 | ): |
| 37 | """ |
| 38 | Extract audio file from RNNoise output dump |
| 39 | |
| 40 | @param dump_file: Audio dump file location |
| 41 | @param output_dir: Output direction |
| 42 | @param export_npy: Whether to export the audio as .npy |
| 43 | """ |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 44 | while True: |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 45 | filename_length = struct.unpack("i", dump_file.read(4))[0] |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 46 | |
| 47 | if filename_length == -1: |
| 48 | return |
| 49 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 50 | filename = struct \ |
| 51 | .unpack(f"{filename_length}s", dump_file.read(filename_length))[0] \ |
| 52 | .decode('ascii') |
| 53 | |
| 54 | audio_clip_length = struct.unpack("I", dump_file.read(4))[0] |
| 55 | output_file_name = path.join(output_dir, f"denoised_{filename}") |
| 56 | audio_clip = dump_file.read(audio_clip_length) |
| 57 | |
| 58 | with sf.SoundFile(output_file_name, 'w', channels=1, samplerate=48000, subtype="PCM_16", |
| 59 | endian="LITTLE") as wav_file: |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 60 | wav_file.buffer_write(audio_clip, dtype='int16') |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 61 | print(f"{output_file_name} written to disk") |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 62 | |
| 63 | if export_npy: |
| 64 | output_file_name += ".npy" |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 65 | pack_format = f"{int(audio_clip_length / 2)}h" |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 66 | npdata = np.array(struct.unpack(pack_format, audio_clip)).astype(np.int16) |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 67 | np.save(output_file_name, npdata) |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 68 | print(f"{output_file_name} written to disk") |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 69 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 70 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 71 | def main(args): |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 72 | """ |
| 73 | Run RNNoise audio dump extraction |
| 74 | @param args: Parsed args |
| 75 | """ |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 76 | extract(args.dump_file, args.output_dir, args.export_npy) |
| 77 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 78 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 79 | parser = argparse.ArgumentParser() |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 80 | |
| 81 | parser.add_argument( |
| 82 | "--dump_file", |
| 83 | type=argparse.FileType('rb'), |
| 84 | help="Dump file with audio files to extract.", |
| 85 | required=True |
| 86 | ) |
| 87 | |
| 88 | parser.add_argument( |
| 89 | "--output_dir", |
| 90 | help="Output directory, Warning: Duplicated file names will be overwritten.", |
| 91 | required=True |
| 92 | ) |
| 93 | |
| 94 | parser.add_argument( |
| 95 | "--export_npy", |
| 96 | help="Export the audio buffer in NumPy format", |
| 97 | action="store_true" |
| 98 | ) |
| 99 | |
| 100 | parsed_args = parser.parse_args() |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 101 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 102 | if __name__ == "__main__": |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 103 | main(parsed_args) |