|
|
|
|
|
|
|
|
""" |
|
|
Created on Tue Jun 23 16:23:15 2020 |
|
|
|
|
|
@author: nils |
|
|
""" |
|
|
|
|
|
import soundfile as sf |
|
|
import numpy as np |
|
|
import tensorflow as tf |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
block_len = 512 |
|
|
block_shift = 128 |
|
|
|
|
|
model = tf.saved_model.load('./pretrained_model/dtln_saved_model') |
|
|
infer = model.signatures["serving_default"] |
|
|
|
|
|
audio,fs = sf.read('path_to_your_favorite_audio.wav') |
|
|
|
|
|
if fs != 16000: |
|
|
raise ValueError('This model only supports 16k sampling rate.') |
|
|
|
|
|
out_file = np.zeros((len(audio))) |
|
|
|
|
|
in_buffer = np.zeros((block_len)) |
|
|
out_buffer = np.zeros((block_len)) |
|
|
|
|
|
num_blocks = (audio.shape[0] - (block_len-block_shift)) // block_shift |
|
|
|
|
|
for idx in range(num_blocks): |
|
|
|
|
|
in_buffer[:-block_shift] = in_buffer[block_shift:] |
|
|
in_buffer[-block_shift:] = audio[idx*block_shift:(idx*block_shift)+block_shift] |
|
|
|
|
|
in_block = np.expand_dims(in_buffer, axis=0).astype('float32') |
|
|
|
|
|
out_block= infer(tf.constant(in_block))['conv1d_1'] |
|
|
|
|
|
out_buffer[:-block_shift] = out_buffer[block_shift:] |
|
|
out_buffer[-block_shift:] = np.zeros((block_shift)) |
|
|
out_buffer += np.squeeze(out_block) |
|
|
|
|
|
out_file[idx*block_shift:(idx*block_shift)+block_shift] = out_buffer[:block_shift] |
|
|
|
|
|
|
|
|
|
|
|
sf.write('out.wav', out_file, fs) |
|
|
|
|
|
print('Processing finished.') |
|
|
|