Search code examples
raudiospectrogram

How to convert a seewave spectrogram into a wav file


I would like to manipulate a seewave audio spectrogram and then convert it back to a .wav file. A quick example

library(tuneR)
library(seewave)

data(tico)
#generate spectrogram with phase information
spec_tico=spectro(tico,plot=FALSE,complex=TRUE,norm=FALSE,dB=NULL)
#manipulate spectrogram
spec_tico_new=dostuff(spec_tico)
#convert back into Wave object - but there is no function spectr2Wave!
tico_new=spectr2Wave(spec_tico_new,...)

I haven't been able to find anything close to spectr2Wave in the seewave documentation.

Do you guys know a way how to convert it back without digging into the wav file specs and doing it manually? Thank you!


Solution

  • Turned out to be relatively simple! The important keyword that I was missing is "short-time Fourier transformation" - that is what seewave::spectro basically does. After googling for "inverse short-time Fourier transformation" the seewave function istft showed up.

    library(tuneR)
    library(seewave)
    
    data(tico)
    #generate spectrogram with phase information
    spec_tico=spectro(tico,plot=FALSE,complex=TRUE,norm=FALSE,dB=NULL,ovlp=50)
    #convert back into Wave object
    tico_new=istft(spec_tico$amp,[email protected],ovlp=50,wl=512,output = "Wave")
    

    Now enjoy the sound of Zonotrichia capensis(*)

    #play on Windows
    play(tico_new)
    #play on Linux with vlc (or any other player ...)
    play(tico_new,player="cvlc")
    #on Linux you have to kill the two vlc processES afterwards!
    

    (*) that's the bird that you can hear if you execute the play command. :)