def transformation_synthesis( inputFile, fs, hfreq, hmag, mYst, freqScaling=np.array([0, 1.2, 2.01, 1.2, 2.679, .7, 3.146, .7]), freqStretching=np.array([0, 1, 2.01, 1, 2.679, 1.5, 3.146, 1.5]), timbrePreservation=1, timeScaling=np.array([0, 0, 2.138, 2.138 - 1.0, 3.146, 3.146])): """ transform the analysis values returned by the analysis function and synthesize the sound inputFile: name of input file fs: sampling rate of input file hfreq, hmag: harmonic frequencies and magnitudes mYst: stochastic residual freqScaling: frequency scaling factors, in time-value pairs (value of 1 no scaling) freqStretching: frequency stretching factors, in time-value pairs (value of 1 no stretching) 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 hfreqt, hmagt = HT.harmonicFreqScaling(hfreq, hmag, freqScaling, freqStretching, timbrePreservation, fs) # time scaling the sound yhfreq, yhmag, ystocEnv = HPST.hpsTimeScale(hfreqt, hmagt, mYst, timeScaling) # synthesis from the trasformed hps representation y, yh, yst = HPS.hpsModelSynth(yhfreq, yhmag, np.array([]), ystocEnv, Ns, H, fs) # write output sound outputFile = 'output_sounds/' + os.path.basename( inputFile)[:-4] + '_hpsModelTransformation.wav' UF.wavwrite(y, fs, outputFile) # create figure to plot plt.figure(figsize=(12, 6)) # frequency range to plot maxplotfreq = 15000.0 # plot spectrogram of transformed stochastic compoment plt.subplot(2, 1, 1) numFrames = int(ystocEnv[:, 0].size) sizeEnv = int(ystocEnv[0, :].size) frmTime = H * np.arange(numFrames) / float(fs) binFreq = (.5 * fs) * np.arange(sizeEnv * maxplotfreq / (.5 * fs)) / sizeEnv plt.pcolormesh( frmTime, binFreq, np.transpose(ystocEnv[:, :int(sizeEnv * maxplotfreq / (.5 * fs)) + 1])) plt.autoscale(tight=True) # plot transformed harmonic on top of stochastic spectrogram if (yhfreq.shape[1] > 0): harms = yhfreq * np.less(yhfreq, maxplotfreq) harms[harms == 0] = np.nan numFrames = int(harms[:, 0].size) frmTime = H * np.arange(numFrames) / float(fs) plt.plot(frmTime, harms, color='k', ms=3, alpha=1) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.autoscale(tight=True) plt.title('harmonics + stochastic spectrogram') # 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, hfreq, hmag, mYst, freqScaling = np.array([0, 1.2, 2.01, 1.2, 2.679, .7, 3.146, .7]), freqStretching = np.array([0, 1, 2.01, 1, 2.679, 1.5, 3.146, 1.5]), timbrePreservation = 1, timeScaling = np.array([0, 0, 2.138, 2.138-1.0, 3.146, 3.146])): """ transform the analysis values returned by the analysis function and synthesize the sound inputFile: name of input file fs: sampling rate of input file hfreq, hmag: harmonic frequencies and magnitudes mYst: stochastic residual freqScaling: frequency scaling factors, in time-value pairs (value of 1 no scaling) freqStretching: frequency stretching factors, in time-value pairs (value of 1 no stretching) 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 hfreqt, hmagt = HT.harmonicFreqScaling(hfreq, hmag, freqScaling, freqStretching, timbrePreservation, fs) # time scaling the sound yhfreq, yhmag, ystocEnv = HPST.hpsTimeScale(hfreqt, hmagt, mYst, timeScaling) # synthesis from the trasformed hps representation y, yh, yst = HPS.hpsModelSynth(yhfreq, yhmag, np.array([]), ystocEnv, Ns, H, fs) # write output sound outputFile = 'output_sounds/' + os.path.basename(inputFile)[:-4] + '_hpsModelTransformation.wav' UF.wavwrite(y,fs, outputFile) # create figure to plot plt.figure(figsize=(12, 6)) # frequency range to plot maxplotfreq = 15000.0 # plot spectrogram of transformed stochastic compoment plt.subplot(2,1,1) numFrames = int(ystocEnv[:,0].size) sizeEnv = int(ystocEnv[0,:].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = (.5*fs)*np.arange(sizeEnv*maxplotfreq/(.5*fs))/sizeEnv plt.pcolormesh(frmTime, binFreq, np.transpose(ystocEnv[:,:sizeEnv*maxplotfreq/(.5*fs)+1])) plt.autoscale(tight=True) # plot transformed harmonic on top of stochastic spectrogram if (yhfreq.shape[1] > 0): harms = yhfreq*np.less(yhfreq,maxplotfreq) harms[harms==0] = np.nan numFrames = int(harms[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) plt.plot(frmTime, harms, color='k', ms=3, alpha=1) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.autoscale(tight=True) plt.title('harmonics + stochastic spectrogram') # 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_stereo(inputFile, fs, hfreq, hmag, mYst, freqScaling = np.array([0, 1.2, 2.01, 1.2, 2.679, .7, 3.146, .7]), freqStretching = np.array([0, 1, 2.01, 1, 2.679, 1.5, 3.146, 1.5]), timbrePreservation = 1, timeScaling = np.array([0, 0, 2.138, 2.138-1.0, 3.146, 3.146]), inputSound=[]): """ transform the analysis values returned by the analysis function and synthesize the sound inputFile: name of input file fs: sampling rate of input file hfreq, hmag: harmonic frequencies and magnitudes mYst: stochastic residual freqScaling: tuple (for L and R channels) of arrays of frequency scaling factors, in time-value pairs (value of 1 no scaling) freqStretching: tuple (for L and R channels) of arrays of frequency stretching factors, in time-value pairs (value of 1 no stretching) timbrePreservation: tuple (for L and R channels) of arrays of 1 preserves original timbre, 0 it does not timeScaling: tuple (for L and R channels) of arrays of time scaling factors, in time-value pairs inputSound: original sound, used for auto-attenuation of the output sound """ # size of fft used in synthesis Ns = 512 # hop size (has to be 1/4 of Ns) H = 128 # frequency scaling of the harmonics hfreqtLeft , hmagtLeft = HT.harmonicFreqScaling(hfreq, hmag, freqScaling[0], freqStretching[0], timbrePreservation[0], fs) hfreqtRight, hmagtRight = HT.harmonicFreqScaling(hfreq, hmag, freqScaling[1], freqStretching[1], timbrePreservation[1], fs) # time scaling the sound yhfreqLeft , yhmagLeft , ystocEnvLeft = HPST.hpsTimeScale(hfreqtLeft , hmagtLeft , mYst, timeScaling[0]) yhfreqRight, yhmagRight, ystocEnvRight = HPST.hpsTimeScale(hfreqtRight, hmagtRight, mYst, timeScaling[1]) # synthesis from the trasformed hps representation yLeft, yhLeft, ystLeft = HPS.hpsModelSynth(yhfreqLeft , yhmagLeft , np.array([]), ystocEnvLeft , Ns, H, fs) yRight, yhRight, ystRight = HPS.hpsModelSynth(yhfreqRight, yhmagRight, np.array([]), ystocEnvRight, Ns, H, fs) # write output sound outputFile = 'output_sounds/' + os.path.basename(inputFile)[:-4] + '_hpsModelTransformation.wav' UF.wavwriteStereo(yLeft, yRight, fs, outputFile, inputSound) # create figure to plot plt.figure(figsize=(12, 12)) # frequency range to plot maxplotfreq = 15000.0 def plotTransformedSignals(channelName,subplot1,subplot2,yhfreq,ystocEnv,y): # plot spectrogram of transformed stochastic compoment plt.subplot(subplot1) numFrames = int(ystocEnv[:,0].size) sizeEnv = int(ystocEnv[0,:].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = (.5*fs)*np.arange(sizeEnv*maxplotfreq/(.5*fs))/sizeEnv plt.pcolormesh(frmTime, binFreq, np.transpose(ystocEnv[:,:sizeEnv*maxplotfreq/(.5*fs)+1])) plt.autoscale(tight=True) # plot transformed harmonic on top of stochastic spectrogram if (yhfreq.shape[1] > 0): harms = yhfreq*np.less(yhfreq,maxplotfreq) harms[harms==0] = np.nan numFrames = int(harms[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) plt.plot(frmTime, harms, color='k', ms=3, alpha=1) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.autoscale(tight=True) plt.title('harmonics + stochastic spectrogram (' + channelName + ')') # plot the output sound plt.subplot(subplot2) 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 (' + channelName + ')') plotTransformedSignals('Left' , 411, 413, yhfreqLeft, ystocEnvLeft, yLeft) plotTransformedSignals('Right', 412, 414, yhfreqRight, ystocEnvRight, yRight) plt.tight_layout() plt.show()
3.524, 3.609, 3.609, 4.573, 4.573, 4.7, 4.7, 5.8 - 0.4, 5.8 - 0.4, 5.902 - 0.4, 5.902 - 0.4, 8.483, 8.483, ] ) yhfreq, yhmag, ystocEnv = HPST.hpsTimeScale(hfreqt, hmagt, mYst, timeScaling) y, yh, yst = HPS.hpsModelSynth(yhfreq, yhmag, np.array([]), ystocEnv, Ns, H, fs) mY, pY = STFT.stftAnal(y, fs, w, N, H) UF.wavwrite(y, fs, "cello-phrase-weird-transformation.wav") # plot the spectrogram of the transformed sound plt.figure(10, figsize=(16, 4.5)) maxplotfreq = 5000.0 numFrames = int(mY[:, 0].size) frmTime = H * np.arange(numFrames) / float(fs) binFreq = fs * np.arange(N * maxplotfreq / fs) / N plt.pcolormesh(frmTime, binFreq, np.transpose(mY[:, : N * maxplotfreq / fs + 1])) plt.xlabel("time (sec)") plt.ylabel("frequency (Hz)") plt.title("spectrogram of transformed sound") plt.autoscale(tight=True)
minf0 = 350 maxf0 = 700 f0et = 5 harmDevSlope = 0.01 stocf = 0.1 Ns = 512 H = 128 (fs, x) = UF.wavread(inputFile) w = get_window(window, M) hfreq, hmag, hphase, mYst = HPS.hpsModelAnal(x, fs, w, N, H, t, nH, minf0, maxf0, f0et, harmDevSlope, minSineDur, Ns, stocf) timeScaling = np.array([0, 0, 2.138, 2.138 - 1.5, 3.146, 3.146]) yhfreq, yhmag, ystocEnv = HPST.hpsTimeScale(hfreq, hmag, mYst, timeScaling) y, yh, yst = HPS.hpsModelSynth(yhfreq, yhmag, np.array([]), ystocEnv, Ns, H, fs) UF.wavwrite(y, fs, 'hps-transformation.wav') plt.figure(figsize=(12, 9)) maxplotfreq = 14900.0 # plot the input sound plt.subplot(4, 1, 1) plt.plot(np.arange(x.size) / float(fs), x) plt.axis([0, x.size / float(fs), min(x), max(x)]) plt.title('x (sax-phrase-short.wav')