def timeStretchAudio(inputAudio, outputAudio, outputDuration, writeOutput=1): originalWav = Sndfile(inputAudio, 'r') x = originalWav.read_frames(originalWav.nframes) fs = originalWav.samplerate nChannel = originalWav.channels print fs if nChannel >1: x = x[0] w = np.hamming(801) N = 2048 t = -90 minSineDur = .005 maxnSines = 150 freqDevOffset = 20 freqDevSlope = 0.02 Ns = 512 H = Ns/4 tfreq, tmag, tphase = SM.sineModelAnal(x, fs, w, N, H, t, maxnSines, minSineDur, freqDevOffset, freqDevSlope) inputDur = float(len(tfreq)*H/fs) #timeScale = np.array([0.1,0.1, inputDur, inputDur*2]) timeScale = np.array([0,0, .4,outputDuration]) ytfreq, ytmag = trans.sineTimeScaling(tfreq, tmag, timeScale) y = SM.sineModelSynth(ytfreq, ytmag, np.array([]), Ns, H, fs) if writeOutput ==1: outputWav = Sndfile(outputAudio, 'w', originalWav.format, originalWav.channels, originalWav.samplerate) outputWav.write_frames(y) outputWav.close() else: return y, fs, nChannel
def transformation_synthesis(inputFile, fs, hfreq, hmag, freqScaling = np.array([0, 2.0, 1, .3]), freqStretching = np.array([0, 1, 1, 1.5]), timbrePreservation = 1, timeScaling = np.array([0, .0, .671, .671, 1.978, 1.978+1.0])): # transform the analysis values returned by the analysis function and synthesize the sound # inputFile: name of input file # fs: sampling rate of input file # tfreq, tmag: sinusoidal frequencies and magnitudes # freqScaling: frequency scaling factors, in time-value pairs # freqStretchig: frequency stretching factors, in time-value pairs # timbrePreservation: 1 preserves original timbre, 0 it does not # timeScaling: time scaling factors, in time-value pairs # size of fft used in synthesis Ns = 512 # hop size (has to be 1/4 of Ns) H = 128 # frequency scaling of the harmonics yhfreq, yhmag = HT.harmonicFreqScaling(hfreq, hmag, freqScaling, freqStretching, timbrePreservation, fs) # time scale the sound yhfreq, yhmag = ST.sineTimeScaling(yhfreq, yhmag, timeScaling) # synthesis y = SM.sineModelSynth(yhfreq, yhmag, np.array([]), Ns, H, fs) # write output sound outputFile = 'output_sounds/' + os.path.basename(inputFile)[:-4] + '_harmonicModelTransformation.wav' UF.wavwrite(y,fs, outputFile) # --------- plotting -------------------- # create figure to plot plt.figure(figsize=(12, 6)) # frequency range to plot maxplotfreq = 15000.0 plt.subplot(2,1,1) # plot the transformed sinusoidal frequencies tracks = yhfreq*np.less(yhfreq, maxplotfreq) tracks[tracks<=0] = np.nan numFrames = int(tracks[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) plt.plot(frmTime, tracks, color='k') plt.title('transformed harmonic tracks') plt.autoscale(tight=True) # plot the output sound plt.subplot(2,1,2) plt.plot(np.arange(y.size)/float(fs), y) plt.axis([0, y.size/float(fs), min(y), max(y)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('output sound: y') plt.tight_layout() plt.show()
def transformation_synthesis(inputFile, fs, tfreq, tmag, freqScaling = np.array([0, 2.0, 1, .3]), timeScaling = np.array([0, .0, .671, .671, 1.978, 1.978+1.0])): """ Transform the analysis values returned by the analysis function and synthesize the sound inputFile: name of input file; fs: sampling rate of input file tfreq, tmag: sinusoidal frequencies and magnitudes freqScaling: frequency scaling factors, in time-value pairs timeScaling: time scaling factors, in time-value pairs """ # size of fft used in synthesis Ns = 512 # hop size (has to be 1/4 of Ns) H = 128 # frequency scaling of the sinusoidal tracks ytfreq = ST.sineFreqScaling(tfreq, freqScaling) # time scale the sinusoidal tracks ytfreq, ytmag = ST.sineTimeScaling(ytfreq, tmag, timeScaling) # synthesis y = SM.sineModelSynth(ytfreq, ytmag, np.array([]), Ns, H, fs) # write output sound outputFile = 'output_sounds/' + os.path.basename(inputFile)[:-4] + '_sineModelTransformation.wav' UF.wavwrite(y,fs, outputFile) # create figure to plot plt.figure(figsize=(12, 6)) # frequency range to plot maxplotfreq = 15000.0 # plot the transformed sinusoidal frequencies if (ytfreq.shape[1] > 0): plt.subplot(2,1,1) tracks = np.copy(ytfreq) tracks = tracks*np.less(tracks, maxplotfreq) tracks[tracks<=0] = np.nan numFrames = int(tracks[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) plt.plot(frmTime, tracks) plt.title('transformed sinusoidal tracks') plt.autoscale(tight=True) # plot the output sound plt.subplot(2,1,2) plt.plot(np.arange(y.size)/float(fs), y) plt.axis([0, y.size/float(fs), min(y), max(y)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('output sound: y') plt.tight_layout() plt.show()
def transformation_synthesis(inputFile, fs, tfreq, tmag, freqScaling=np.array([0, 2.0, 1, .3]), timeScaling=np.array( [0, .0, .671, .671, 1.978, 1.978 + 1.0])): """ Transform the analysis values returned by the analysis function and synthesize the sound inputFile: name of input file; fs: sampling rate of input file tfreq, tmag: sinusoidal frequencies and magnitudes freqScaling: frequency scaling factors, in time-value pairs timeScaling: time scaling factors, in time-value pairs """ # size of fft used in synthesis Ns = 512 # hop size (has to be 1/4 of Ns) H = 128 # frequency scaling of the sinusoidal tracks ytfreq = ST.sineFreqScaling(tfreq, freqScaling) # time scale the sinusoidal tracks ytfreq, ytmag = ST.sineTimeScaling(ytfreq, tmag, timeScaling) # synthesis y = SM.sineModelSynth(ytfreq, ytmag, np.array([]), Ns, H, fs) # write output sound outputFile = 'output_sounds/' + os.path.basename( inputFile)[:-4] + '_sineModelTransformation.wav' UF.wavwrite(y, fs, outputFile) # create figure to plot plt.figure(figsize=(12, 6)) # frequency range to plot maxplotfreq = 15000.0 # plot the transformed sinusoidal frequencies if (ytfreq.shape[1] > 0): plt.subplot(2, 1, 1) tracks = np.copy(ytfreq) tracks = tracks * np.less(tracks, maxplotfreq) tracks[tracks <= 0] = np.nan numFrames = int(tracks[:, 0].size) frmTime = H * np.arange(numFrames) / float(fs) plt.plot(frmTime, tracks) plt.title('transformed sinusoidal tracks') plt.autoscale(tight=True) # plot the output sound plt.subplot(2, 1, 2) plt.plot(np.arange(y.size) / float(fs), y) plt.axis([0, y.size / float(fs), min(y), max(y)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('output sound: y') plt.tight_layout() plt.show(block=False)
(fs, x) = UF.wavread('../../../sounds/mridangam.wav') w = np.hamming(801) N = 2048 t = -90 minSineDur = .005 maxnSines = 150 freqDevOffset = 20 freqDevSlope = 0.02 Ns = 512 H = Ns/4 mX, pX = STFT.stftAnal(x, w, N, H) tfreq, tmag, tphase = SM.sineModelAnal(x, fs, w, N, H, t, maxnSines, minSineDur, freqDevOffset, freqDevSlope) timeScale = np.array([.01, .0, .03, .03, .335, .4, .355, .42, .671, .8, .691, .82, .858, 1.2, .878, 1.22, 1.185, 1.6, 1.205, 1.62, 1.497, 2.0, 1.517, 2.02, 1.686, 2.4, 1.706, 2.42, 1.978, 2.8]) ytfreq, ytmag = SMT.sineTimeScaling(tfreq, tmag, timeScale) y = SM.sineModelSynth(ytfreq, ytmag, np.array([]), Ns, H, fs) mY, pY = STFT.stftAnal(y, w, N, H) plt.figure(1, figsize=(12, 9)) maxplotfreq = 4000.0 plt.subplot(4,1,1) plt.plot(np.arange(x.size)/float(fs), x, 'b') plt.axis([0,x.size/float(fs),min(x),max(x)]) plt.title('x (mridangam.wav)') plt.subplot(4,1,2) numFrames = int(tfreq[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) tracks = tfreq*np.less(tfreq, maxplotfreq) tracks[tracks<=0] = np.nan
(fs, x) = UF.wavread('../../../sounds/mridangam.wav') w = np.hamming(801) N = 2048 t = -90 minSineDur = .005 maxnSines = 150 freqDevOffset = 20 freqDevSlope = 0.02 Ns = 512 H = Ns/4 mX, pX = STFT.stftAnal(x, fs, w, N, H) tfreq, tmag, tphase = SM.sineModelAnal(x, fs, w, N, H, t, maxnSines, minSineDur, freqDevOffset, freqDevSlope) timeScale = np.array([.01, .0, .03, .03, .335, .4, .355, .42, .671, .8, .691, .82, .858, 1.2, .878, 1.22, 1.185, 1.6, 1.205, 1.62, 1.497, 2.0, 1.517, 2.02, 1.686, 2.4, 1.706, 2.42, 1.978, 2.8]) ytfreq, ytmag = SMT.sineTimeScaling(tfreq, tmag, timeScale) y = SM.sineModelSynth(ytfreq, ytmag, np.array([]), Ns, H, fs) mY, pY = STFT.stftAnal(y, fs, w, N, H) plt.figure(1, figsize=(12, 9)) maxplotfreq = 4000.0 plt.subplot(4,1,1) plt.plot(np.arange(x.size)/float(fs), x, 'b') plt.axis([0,x.size/float(fs),min(x),max(x)]) plt.title('x (mridangam.wav)') plt.subplot(4,1,2) numFrames = int(tfreq[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) tracks = tfreq*np.less(tfreq, maxplotfreq) tracks[tracks<=0] = np.nan
yhfreq[l,ind_valid] = yhfreq[l,ind_valid] * freqScaling[l] return yhfreq, yhmag if __name__ == '__main__': (fs, x) = UF.wavread(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../sounds/soprano-E4.wav')) w = np.blackman(801) N = 1024 t = -90 nH = 100 minf0 = 250 maxf0 = 400 f0et = 8 minSineDur = .1 harmDevSlope = 0.01 Ns = 512 H = Ns/4 hfreq, hmag, hphase = HM.harmonicModelAnal(x, fs, w, N, H, t, nH, minf0, maxf0, f0et, harmDevSlope, minSineDur) freqScaling = np.array([0, 3, 1, .5]) freqStretching = np.array([]) timbrePreservation = 1 hfreqt, hmagt = harmonicFreqScaling(hfreq, hmag, freqScaling, freqStretching, timbrePreservation, fs) timeScaling = np.array([0, 0, 1, .5, 2, 4]) hfreqt, hmagt = ST.sineTimeScaling(hfreq, hmag, timeScaling) yh = SM.sineModelSynth(hfreqt, hmagt, np.array([]), Ns, H, fs) UF.play(yh, fs)