Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 1 | # Copyright (c) 2021 Arm Limited. All rights reserved. |
| 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 soundfile as sf |
| 24 | import numpy as np |
| 25 | |
| 26 | import argparse |
| 27 | from os import path |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 28 | import struct |
| 29 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 30 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 31 | def extract(fp, output_dir, export_npy): |
| 32 | while True: |
| 33 | filename_length = struct.unpack("i", fp.read(4))[0] |
| 34 | |
| 35 | if filename_length == -1: |
| 36 | return |
| 37 | |
| 38 | filename = struct.unpack("{}s".format(filename_length), fp.read(filename_length))[0].decode('ascii') |
| 39 | audio_clip_length = struct.unpack("I", fp.read(4))[0] |
| 40 | output_file_name = path.join(output_dir, "denoised_{}".format(filename)) |
| 41 | audio_clip = fp.read(audio_clip_length) |
| 42 | |
| 43 | with sf.SoundFile(output_file_name, 'w', channels=1, samplerate=48000, subtype="PCM_16", endian="LITTLE") as wav_file: |
| 44 | wav_file.buffer_write(audio_clip, dtype='int16') |
| 45 | print("{} written to disk".format(output_file_name)) |
| 46 | |
| 47 | if export_npy: |
| 48 | output_file_name += ".npy" |
| 49 | pack_format = "{}h".format(int(audio_clip_length/2)) |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 50 | npdata = np.array(struct.unpack(pack_format, audio_clip)).astype(np.int16) |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 51 | np.save(output_file_name, npdata) |
| 52 | print("{} written to disk".format(output_file_name)) |
| 53 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 54 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 55 | def main(args): |
| 56 | extract(args.dump_file, args.output_dir, args.export_npy) |
| 57 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 58 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 59 | parser = argparse.ArgumentParser() |
| 60 | parser.add_argument("--dump_file", type=argparse.FileType('rb'), help="Dump file with audio files to extract.", required=True) |
| 61 | parser.add_argument("--output_dir", help="Output directory, Warning: Duplicated file names will be overwritten.", required=True) |
| 62 | parser.add_argument("--export_npy", help="Export the audio buffer in NumPy format", action="store_true") |
| 63 | args = parser.parse_args() |
| 64 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 65 | if __name__ == "__main__": |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 66 | main(args) |