Skip to content

Instantly share code, notes, and snippets.

@fedden
Last active November 19, 2017 23:45
Show Gist options
  • Save fedden/5b6e7dedff9e1fdded22226b5a3027fa to your computer and use it in GitHub Desktop.
Save fedden/5b6e7dedff9e1fdded22226b5a3027fa to your computer and use it in GitHub Desktop.
from magenta.models.nsynth import utils
from magenta.models.nsynth.wavenet import fastgen
def wavenet_encode(file_path):
# Load the model weights.
checkpoint_path = './wavenet-ckpt/model.ckpt-200000'
# Load and downsample the audio.
neural_sample_rate = 16000
audio = utils.load_audio(file_path,
sample_length=400000,
sr=neural_sample_rate)
# Pass the audio through the first half of the autoencoder,
# to get a list of latent variables that describe the sound.
# Note that it would be quicker to pass a batch of audio
# to fastgen.
encoding = fastgen.encode(audio, checkpoint_path, len(audio))
# Reshape to a single sound.
return encoding.reshape((-1, 16))
# An array of n * 16 frames.
wavenet_z_data = wavenet_encode(file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment