Record Audio in a Jupyter Notebook

Harry Coultas Blum
2 min readJul 15, 2020

--

When working with audio in pytorch, especially with speech, I’ve found it useful to test models with live audio recorded from your mic. I’m going to show you how to do that in this tutorial.

  1. First we need to setup things up a bit
sudo apt install ffmpeg
pip install torchaudio ipywebrtc notebook
jupyter nbextension enable --py widgetsnbextension

2. Now in a fresh notebook you need to import the modules that we are going to need

from ipywebrtc import AudioRecorder, CameraStream
import torchaudio
from IPython.display import Audio

3. Then we want to actually record the audio by creating a AudioRecorder. This will prompt you for permissions from you browser and you maybe need to refresh the page.

camera = CameraStream(constraints={'audio': True,'video':False})
recorder = AudioRecorder(stream=camera)
recorder

You can use the circle icon to record

4. We then need to save and convert the data into a format that torchaudio can read

with open('recording.webm', 'wb') as f:
f.write(recorder.audio.value)
!ffmpeg -i recording.webm -ac 1 -f wav file.wav -y -hide_banner -loglevel panic
sig, sr = torchaudio.load("file.wav")
print(sig.shape)
Audio(data=sig, rate=sr)

We convert with ffmpeg from webm to ffmpeg, load the signal with torchaudio and prove that it is the same by putting the data into a Audio Widget and playing it back.

I hope this is useful to you.

Here is a complete notebook file to play with

--

--