Path: blob/master/site/en-snapshot/hub/tutorials/spice.ipynb
25118 views
Copyright 2020 The TensorFlow Hub Authors.
Licensed under the Apache License, Version 2.0 (the "License");
Pitch Detection with SPICE
This colab will show you how to use the SPICE model downloaded from TensorFlow Hub.
The audio input file
Now the hardest part: Record your singing! 😃
We provide four methods to obtain an audio file:
Record audio directly in colab
Upload from your computer
Use a file saved on Google Drive
Download the file from the web
Choose one of the four methods below.
Preparing the audio data
Now we have the audio, let's convert it to the expected format and then listen to it!
The SPICE model needs as input an audio file at a sampling rate of 16kHz and with only one channel (mono).
To help you with this part, we created a function (convert_audio_for_model
) to convert any wav file you have to the model's expected format:
First thing, let's take a look at the waveform of our singing.
A more informative visualization is the spectrogram, which shows frequencies present over time.
Here, we use a logarithmic frequency scale, to make the singing more clearly visible.
We need one last conversion here. The audio samples are in int16 format. They need to be normalized to floats between -1 and 1.
Executing the Model
Now is the easy part, let's load the model with TensorFlow Hub, and feed the audio to it. SPICE will give us two outputs: pitch and uncertainty
TensorFlow Hub is a library for the publication, discovery, and consumption of reusable parts of machine learning models. It makes easy to use machine learning to solve your challenges.
To load the model you just need the Hub module and the URL pointing to the model:
Note: An interesting detail here is that all the model urls from Hub can be used for download and also to read the documentation, so if you point your browser to that link you can read documentation on how to use the model and learn more about how it was trained.
With the model loaded, data prepared, we need 3 lines to get the result:
Let's make the results easier to understand by removing all pitch estimates with low confidence (confidence < 0.9) and plot the remaining ones.
The pitch values returned by SPICE are in the range from 0 to 1. Let's convert them to absolute pitch values in Hz.
Now, let's see how good the prediction is: We will overlay the predicted pitches over the original spectrogram. To make the pitch predictions more visible, we changed the spectrogram to black and white.
Converting to musical notes
Now that we have the pitch values, let's convert them to notes! This is part is challenging by itself. We have to take into account two things:
the rests (when there's no singing)
the size of each note (offsets)
1: Adding zeros to the output to indicate when there's no singing
2: Adding note offsets
When a person sings freely, the melody may have an offset to the absolute pitch values that notes can represent. Hence, to convert predictions to notes, one needs to correct for this possible offset. This is what the following code computes.
We can now use some heuristics to try and estimate the most likely sequence of notes that were sung. The ideal offset computed above is one ingredient - but we also need to know the speed (how many predictions make, say, an eighth?), and the time offset to start quantizing. To keep it simple, we'll just try different speeds and time offsets and measure the quantization error, using in the end the values that minimize this error.
Now let's write the quantized notes as sheet music score!
To do it we will use two libraries: music21 and Open Sheet Music Display
Note: for simplicity, we assume here that all notes have the same duration (a half note).
Let's convert the music notes to a MIDI file and listen to it.
To create this file, we can use the stream we created before.
To listen to it on colab, we need to convert it back to wav. An easy way of doing that is using Timidity.
And finally, listen the audio, created from notes, created via MIDI from the predicted pitches, inferred by the model!