import streamlit as st import pyttsx3 from scipy.io import wavfile from scipy import signal from scipy.spatial import distance import plotly.graph_objs as go import numpy as np from scipy.interpolate import interp2d def similarity(file1, file2): fs1, data1 = wavfile.read(file1) fs2, data2 = wavfile.read(file2) f1, t1, Sxx1 = signal.spectrogram(data1, fs=fs1) f2, t2, Sxx2 = signal.spectrogram(data2, fs=fs2) interp1 = interp2d(t1, f1, Sxx1) interp2 = interp2d(t2, f2, Sxx2) tmin = max(t1.min(), t2.min()) tmax = min(t1.max(), t2.max()) fmin = max(f1.min(), f2.min()) fmax = min(f1.max(), f2.max()) tnew = np.linspace(tmin, tmax, num=max(len(t1), len(t2))) fnew = np.linspace(fmin, fmax, num=max(len(f1), len(f2))) Sxx1new = interp1(tnew, fnew) Sxx2new = interp2(tnew, fnew) d = distance.euclidean(Sxx1new.flatten(), Sxx2new.flatten()) max_d = max(Sxx1new.max(), Sxx2new.max()) * len(Sxx1new.flatten()) return 100 * (1 - d / max_d) st.title('Text to Sound Comparing') texto1 = st.text_input('First text','hi') texto2 = st.text_input('Second text','five') intercambiar = st.selectbox('Interchange texts', [False, True]) if intercambiar: texto1, texto2 = texto2, texto1 if texto1=="": texto1="hi" if texto2=="": texto2="five" if 1: engine = pyttsx3.init() engine.setProperty('voice', 'english') engine.save_to_file(texto1, 'texto1.wav') engine.save_to_file(texto2, 'texto2.wav') engine.runAndWait() fs1, data1 = wavfile.read('texto1.wav') fs2, data2 = wavfile.read('texto2.wav') inicio1 = st.slider('Start 1: '+str(texto1), 0, len(data1), 0) inicio2 = st.slider('Start 2: '+str(texto2), 0, len(data2), 0) f1, t1, Sxx1 = signal.spectrogram(data1[inicio1:], fs=fs1) f2, t2, Sxx2 = signal.spectrogram(data2[inicio2:], fs=fs2) fig1 = go.Figure() fig1.add_trace(go.Scatter(y=data1[inicio1:], mode='lines', name='Text 1: '+str(texto1))) fig1.add_trace(go.Scatter(y=data2[inicio2:], mode='lines', name='Text 2: '+str(texto2))) st.header('Waves') st.plotly_chart(fig1) fig2 = go.Figure() fig2.add_trace(go.Heatmap(x=t1, y=f1, z=Sxx1, name='Spectrogram 1', xaxis='x2', yaxis='y2')) st.header('Spectrogram '+str(texto1)) st.plotly_chart(fig2) fig3 = go.Figure() fig3.add_trace(go.Heatmap(x=t2, y=f2, z=Sxx2, name='Spectrogram 2', xaxis='x3', yaxis='y3')) st.header('Spectrogram '+str(texto2)) st.plotly_chart(fig3) #sim = similarity('texto1.wav', 'texto2.wav') #print(f'Similarity: {sim:.2f}%') #st.header('Similarity: '+str(sim)) N = len(data1[inicio1:]) T = 1.0 / fs1 yf = np.fft.fft(data1[inicio1:]) xf = np.linspace(0.0, 1.0/(2.0*T), N//2) fig4 = go.Figure() fig4.add_trace(go.Scatter(x=xf, y=2.0/N * np.abs(yf[0:N//2]), mode='lines', name='Text 1: '+str(texto1))) st.header('Fourier '+str(texto1)) st.plotly_chart(fig4) N = len(data2[inicio2:]) T = 1.0 / fs2 yf = np.fft.fft(data2[inicio2:]) xf = np.linspace(0.0, 1.0/(2.0*T), N//2) fig5 = go.Figure() fig5.add_trace(go.Scatter(x=xf, y=2.0/N * np.abs(yf[0:N//2]), mode='lines', name='Text 2: '+str(texto2))) st.header('Fourier '+str(texto2)) st.plotly_chart(fig5) N = len(data1[inicio1:]) T = 1.0 / fs1 yf = np.fft.fft(data1[inicio1:]) xf = np.linspace(0.0, 1.0/(2.0*T), N//2) fig6 = go.Figure() fig6.add_trace(go.Scatter(x=xf, y=2.0/N * np.abs(yf[0:N//2]), mode='lines', name='Text 1: '+str(texto1))) N = len(data2[inicio2:]) T = 1.0 / fs2 yf = np.fft.fft(data2[inicio2:]) xf = np.linspace(0.0, 1.0/(2.0*T), N//2) fig6.add_trace(go.Scatter(x=xf, y=2.0/N * np.abs(yf[0:N//2]), mode='lines', name='Text 2: '+str(texto2))) st.header('Fourier Mix') st.plotly_chart(fig6)