import argparse
import sys
import h5py
import numpy as np
import lpcnet
from ulaw import ulaw2lin, lin2ulaw
parser = argparse.ArgumentParser()
parser.add_argument('model-file', type=str, help='model weight h5 file')
parser.add_argument('--lpc-gamma', type=float, help='LPC weighting factor. WARNING: giving an inconsistent value here will severely degrade performance', default=1)
args = parser.parse_args()
filename = args.model_file
with h5py.File(filename, "r") as f:
units = min(f['model_weights']['gru_a']['gru_a']['recurrent_kernel:0'].shape)
units2 = min(f['model_weights']['gru_b']['gru_b']['recurrent_kernel:0'].shape)
cond_size = min(f['model_weights']['feature_dense1']['feature_dense1']['kernel:0'].shape)
e2e = 'rc2lpc' in f['model_weights']
model, enc, dec = lpcnet.new_lpcnet_model(training = False, rnn_units1=units, rnn_units2=units2, flag_e2e = e2e, cond_size=cond_size, batch_size=1)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
feature_file = sys.argv[2]
out_file = sys.argv[3]
frame_size = model.frame_size
nb_features = 36
nb_used_features = model.nb_used_features
features = np.fromfile(feature_file, dtype='float32')
features = np.resize(features, (-1, nb_features))
nb_frames = 1
feature_chunk_size = features.shape[0]
pcm_chunk_size = frame_size*feature_chunk_size
features = np.reshape(features, (nb_frames, feature_chunk_size, nb_features))
periods = (.1 + 50*features[:,:,18:19]+100).astype('int16')
model.load_weights(filename);
order = 16
pcm = np.zeros((nb_frames*pcm_chunk_size, ))
fexc = np.zeros((1, 1, 3), dtype='int16')+128
state1 = np.zeros((1, model.rnn_units1), dtype='float32')
state2 = np.zeros((1, model.rnn_units2), dtype='float32')
mem = 0
coef = 0.85
lpc_weights = np.array([args.lpc_gamma ** (i + 1) for i in range(16)])
fout = open(out_file, 'wb')
skip = order + 1
for c in range(0, nb_frames):
if not e2e:
cfeat = enc.predict([features[c:c+1, :, :nb_used_features], periods[c:c+1, :, :]])
else:
cfeat,lpcs = enc.predict([features[c:c+1, :, :nb_used_features], periods[c:c+1, :, :]])
for fr in range(0, feature_chunk_size):
f = c*feature_chunk_size + fr
if not e2e:
a = features[c, fr, nb_features-order:] * lpc_weights
else:
a = lpcs[c,fr]
for i in range(skip, frame_size):
pred = -sum(a*pcm[f*frame_size + i - 1:f*frame_size + i - order-1:-1])
fexc[0, 0, 1] = lin2ulaw(pred)
p, state1, state2 = dec.predict([fexc, cfeat[:, fr:fr+1, :], state1, state2])
p *= np.power(p, np.maximum(0, 1.5*features[c, fr, 19] - .5))
p = p/(1e-18 + np.sum(p))
p = np.maximum(p-0.002, 0).astype('float64')
p = p/(1e-8 + np.sum(p))
fexc[0, 0, 2] = np.argmax(np.random.multinomial(1, p[0,0,:], 1))
pcm[f*frame_size + i] = pred + ulaw2lin(fexc[0, 0, 2])
fexc[0, 0, 0] = lin2ulaw(pcm[f*frame_size + i])
mem = coef*mem + pcm[f*frame_size + i]
np.array([np.round(mem)], dtype='int16').tofile(fout)
skip = 0