https://github.com/tensorflow/magenta/blob/master/magenta/models/nsynth/wavenet/fastgen.py -- Code is from the magenta nsynth work. Loads a batch of data from wav files. Requires util file from nsynth.
def load_batch(files, sample_length=64000):
"""Load a batch of data from either .wav or .npy files.
Args:
files: A list of filepaths to .wav or .npy files
sample_length: Maximum sample length
Returns:
batch_data: A padded array of audio or embeddings [batch, length, (dims)]
"""
batch_data = []
max_length = 0
is_npy = (os.path.splitext(files[0])[1] == ".npy")
# Load the data
for f in files:
if is_npy:
data = np.load(f)
batch_data.append(data)
else:
data = utils.load_audio(f, sample_length, sr=16000)
batch_data.append(data)
if data.shape[0] > max_length:
max_length = data.shape[0]
# Add padding
for i, data in enumerate(batch_data):
if data.shape[0] < max_length:
if is_npy:
padded = np.zeros([max_length, +data.shape[1]])
padded[:data.shape[0], :] = data
else:
padded = np.zeros([max_length])
padded[:data.shape[0]] = data
batch_data[i] = padded
# Return arrays
batch_data = np.array(batch_data)
return batch_data