audiocraft icon indicating copy to clipboard operation
audiocraft copied to clipboard

download the Tensor object at the end of the generation

Open pramatias opened this issue 1 year ago • 2 comments

When the program runs on collab, i was wondering if there is an easy way to download the wav file.

The usual:

from google.colab import files files.download(res)

doesn't work on Tensor objects. I tried to convert it to wav or mp3 to no avail. I tried modifying utils/notebook.py display_audio function and i couldn't figure it out.

Does anyone knows how to take the samples and samples_rate and create the wav (or mp3) file? Is Torchaudio the right library for that purpose?

pramatias avatar Jun 09 '23 23:06 pramatias

Try this:

import soundfile as sf
from google.colab import files
import numpy as np
sf.write('audio.wav', np.squeeze(np.array(res.cpu()).T), 32000)
files.download('audio.wav')

NaJeongMo avatar Jun 16 '23 15:06 NaJeongMo

Thank you very much @NaJeongMo , it works!

pramatias avatar Jun 16 '23 17:06 pramatias

As an interesting sidenote, i was not aware of the library soundfile, as i don't program in python. I filed the issue after googling and experimenting with code found on stackoverflow and other sites, and not going anywhere. Only today it dawned me, to ask ChatGpt the same question, and it came up with soundfile in one second.

Anyway, i created a copy of the audiocraft notebook, which features automatically file download, and it can be found here

https://colab.research.google.com/drive/1eJbjeOLoK3XIGM9rWmJFZxH1zdX0Fi7A?usp=sharing

pramatias avatar Jun 18 '23 01:06 pramatias