1""" 2/* Copyright (c) 2023 Amazon 3 Written by Jan Buethe */ 4/* 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions 7 are met: 8 9 - Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 12 - Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*/ 28""" 29 30import argparse 31 32import torch 33 34from scipy.io import wavfile 35 36 37from models import model_dict 38from utils.silk_features import load_inference_data 39from utils import endoscopy 40 41debug = False 42if debug: 43 args = type('dummy', (object,), 44 { 45 'input' : 'testitems/all_0_orig.se', 46 'checkpoint' : 'testout/checkpoints/checkpoint_epoch_5.pth', 47 'output' : 'out.wav', 48 })() 49else: 50 parser = argparse.ArgumentParser() 51 52 parser.add_argument('input', type=str, help='path to folder with features and signals') 53 parser.add_argument('checkpoint', type=str, help='checkpoint file') 54 parser.add_argument('output', type=str, help='output file') 55 parser.add_argument('--debug', action='store_true', help='enables debug output') 56 57 58 args = parser.parse_args() 59 60 61torch.set_num_threads(2) 62 63input_folder = args.input 64checkpoint_file = args.checkpoint 65 66 67output_file = args.output 68if not output_file.endswith('.wav'): 69 output_file += '.wav' 70 71checkpoint = torch.load(checkpoint_file, map_location="cpu") 72 73# check model 74if not 'name' in checkpoint['setup']['model']: 75 print(f'warning: did not find model name entry in setup, using pitchpostfilter per default') 76 model_name = 'pitchpostfilter' 77else: 78 model_name = checkpoint['setup']['model']['name'] 79 80model = model_dict[model_name](*checkpoint['setup']['model']['args'], **checkpoint['setup']['model']['kwargs']) 81 82model.load_state_dict(checkpoint['state_dict']) 83 84# generate model input 85setup = checkpoint['setup'] 86signal, features, periods, numbits = load_inference_data(input_folder, **setup['data']) 87 88if args.debug: 89 endoscopy.init() 90 91output = model.process(signal, features, periods, numbits, debug=args.debug) 92 93wavfile.write(output_file, 16000, output.cpu().numpy()) 94 95if args.debug: 96 endoscopy.close() 97