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 36from time import time 37 38 39from models import model_dict 40from utils.lpcnet_features import load_lpcnet_features 41from utils import endoscopy 42 43debug = False 44if debug: 45 args = type('dummy', (object,), 46 { 47 'input' : 'testitems/all_0_orig.se', 48 'checkpoint' : 'testout/checkpoints/checkpoint_epoch_5.pth', 49 'output' : 'out.wav', 50 })() 51else: 52 parser = argparse.ArgumentParser() 53 54 parser.add_argument('input', type=str, help='path to input features') 55 parser.add_argument('checkpoint', type=str, help='checkpoint file') 56 parser.add_argument('output', type=str, help='output file') 57 parser.add_argument('--debug', action='store_true', help='enables debug output') 58 59 60 args = parser.parse_args() 61 62 63torch.set_num_threads(2) 64 65input_folder = args.input 66checkpoint_file = args.checkpoint 67 68 69output_file = args.output 70if not output_file.endswith('.wav'): 71 output_file += '.wav' 72 73checkpoint = torch.load(checkpoint_file, map_location="cpu") 74 75# check model 76if not 'name' in checkpoint['setup']['model']: 77 print(f'warning: did not find model name entry in setup, using pitchpostfilter per default') 78 model_name = 'pitchpostfilter' 79else: 80 model_name = checkpoint['setup']['model']['name'] 81 82model = model_dict[model_name](*checkpoint['setup']['model']['args'], **checkpoint['setup']['model']['kwargs']) 83 84model.load_state_dict(checkpoint['state_dict']) 85 86# generate model input 87setup = checkpoint['setup'] 88testdata = load_lpcnet_features(input_folder) 89features = testdata['features'] 90periods = testdata['periods'] 91 92if args.debug: 93 endoscopy.init() 94 95start = time() 96output = model.process(features, periods, debug=args.debug) 97elapsed = time() - start 98print(f"[timing] inference took {elapsed * 1000} ms") 99 100wavfile.write(output_file, 16000, output.cpu().numpy()) 101 102if args.debug: 103 endoscopy.close() 104