Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the audio file from IPython?

How to save the audio file using ipd.display?

Just a minimal sample here

from IPython.display import Audio 
from IPython.core.display import display
import librosa
audio_data, sample_rate = librosa.load('/Users/0_hey.wav', sr=None)
ipd.Audio(audio_data, rate=sample_rate)

Then it plays a sound, but I don't know how to save the audio file.

I know that we can download it by clicking the right bottom. enter image description here But I hope it can also be written in codes.

like image 909
Megan Avatar asked Jan 22 '26 03:01

Megan


1 Answers

The display.Audio is a widget, a user interface component that provides Audio controls. After the docs:

When this object is returned by an input cell or passed to the display function, it will result in Audio controls being displayed in the frontend (only works in the notebook).

audio_data is a numpy array and you can use any library that works with such data structures. PySoundFile is a solid choice, also recommended by librosa, library that you are using.

import soundfile as sf

# Write out audio as 24bit flac, change accordingly
sf.write('audio.flac', audio_data, sample_rate, format='flac', subtype='PCM_24')
like image 191
Lukasz Tracewski Avatar answered Jan 24 '26 19:01

Lukasz Tracewski