blob: 715b9229e2caa447f55a0ea05b8f0f2a9e21ea15 [file] [log] [blame]
Richard Burtonf32a86a2022-11-15 11:46:11 +00001# SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
Richard Burton00553462021-11-10 16:27:14 +00002# 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 Burton00553462021-11-10 16:27:14 +000015"""
16This script can be used with the noise reduction use case to save
17the dumped noise reduced audio to a wav file.
18
19Example use:
20python rnnoise_dump_extractor.py --dump_file output.bin --output_dir ./denoised_wavs/
21"""
Richard Burton17069622022-03-17 10:54:26 +000022
Richard Burton00553462021-11-10 16:27:14 +000023import soundfile as sf
24import numpy as np
25
26import argparse
27from os import path
Richard Burton00553462021-11-10 16:27:14 +000028import struct
29
Richard Burton17069622022-03-17 10:54:26 +000030
Richard Burton00553462021-11-10 16:27:14 +000031def 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 Burton17069622022-03-17 10:54:26 +000050 npdata = np.array(struct.unpack(pack_format, audio_clip)).astype(np.int16)
Richard Burton00553462021-11-10 16:27:14 +000051 np.save(output_file_name, npdata)
52 print("{} written to disk".format(output_file_name))
53
Richard Burton17069622022-03-17 10:54:26 +000054
Richard Burton00553462021-11-10 16:27:14 +000055def main(args):
56 extract(args.dump_file, args.output_dir, args.export_npy)
57
Richard Burton17069622022-03-17 10:54:26 +000058
Richard Burton00553462021-11-10 16:27:14 +000059parser = argparse.ArgumentParser()
60parser.add_argument("--dump_file", type=argparse.FileType('rb'), help="Dump file with audio files to extract.", required=True)
61parser.add_argument("--output_dir", help="Output directory, Warning: Duplicated file names will be overwritten.", required=True)
62parser.add_argument("--export_npy", help="Export the audio buffer in NumPy format", action="store_true")
63args = parser.parse_args()
64
Richard Burton17069622022-03-17 10:54:26 +000065if __name__ == "__main__":
Richard Burton00553462021-11-10 16:27:14 +000066 main(args)