Skip to content

Instantly share code, notes, and snippets.

@dehaenw
Created July 2, 2021 17:20
Show Gist options
  • Save dehaenw/59a5fd5f29b87dc5bd27afa788991993 to your computer and use it in GitHub Desktop.
Save dehaenw/59a5fd5f29b87dc5bd27afa788991993 to your computer and use it in GitHub Desktop.
from scipy.io import wavfile
import scipy.io
from scipy.fft import fft, ifft
import numpy as np
"""
chord.wav should be a short sound, equivalent to carrier in vocoder
impulse.wav should be a quite sparse transient heavy sound, equivalent to modulator in vocoder
play with tsmear and asmear to tweak between vocoder and convolution like sound
"""
Tsmear = 1000
Asmear = 100
polar2z = lambda r,θ:r*np.exp(1j*θ)
z2polar = lambda z:(np.abs(z),np.angle(z))
samplerate,data = wavfile.read("chord.wav")
Xl = data[:,0]
Xr = data[:,1]
samplerate,phasedata = wavfile.read("impulse.wav")
X2l = phasedata[:,0]
X2r = phasedata[:,1]
X2l = np.convolve(X2l,np.random.rand(Tsmear)-0.5,mode="same") #time smear template
X2r = np.convolve(X2r,np.random.rand(Tsmear)-0.5,mode="same") #time smear template
Xl = np.resize(Xl,len(X2l))
Xr = np.resize(Xr,len(X2r))
convAl = fft(Xl)
convPl = fft(X2l)
convAr = fft(Xr)
convPr = fft(X2r)
YAl = z2polar(convAl)
YPl = z2polar(convPl)
YAr = z2polar(convAr)
YPr = z2polar(convPr)
Amaskl = np.convolve(YAl[0],np.hamming(Asmear),mode="same") #blur amplitude of pad file and multiply w/amplitude of tmplate
Amaskr = np.convolve(YAr[0],np.hamming(Asmear),mode="same") #blur amplitude of pad file and multiply w/amplitude of tmplate
Yl = polar2z(YPl[0]*Amaskl,YPl[1])
Yr = polar2z(YPr[0]*Amaskl,YPr[1])
Xl = ifft(Yl)
Xr = ifft(Yr)
Xl = np.real(Xl)
Xr = np.real(Xr)
Xl = Xl/np.max(np.abs([Xl,Xr]))
Xr = Xr/np.max(np.abs([Xl,Xr]))
wavfile.write("output.wav", samplerate, np.array([Xl,Xr]).T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment