rustom/Audio_plot_Streamlit
0
1import streamlit as st2import librosa3import librosa.display4import matplotlib.pyplot as plt5import numpy as np6 7 8def LIBROSA_LOAD(data):9 y,sr = librosa.load(data,sr = None)10 11 return y,sr12 13def TWO_PLOT_TIME_DOMAIN(signal1,signal2):14 y_1,sr = LIBROSA_LOAD(signal1)15 y_2,sr = LIBROSA_LOAD(signal2)16 17 fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)18 librosa.display.waveshow(y_1, sr=sr, ax=ax[0])19 ax[0].set(title='Time domain plot of {}'.format(signal1))20 ax[0].label_outer()21 22 librosa.display.waveshow(y_2, sr=sr, ax=ax[1])23 ax[1].set(title='Time domain plot of {}'.format(signal2))24 plt.plot()25 26 # Render the Matplotlib figure in Streamlit27 st.pyplot(fig)28 29 30def DIFF_TWO_PLOT_TIME_DOMAIN(y_1,sr_1,y_2,sr_2):31 #y_1,sr = LIBROSA_LOAD(signal1)32 #y_2,sr = LIBROSA_LOAD(signal2)33 34 fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)35 librosa.display.waveshow(y_1, sr=sr_1, ax=ax[0])36 ax[0].set(title='Time domain plot of Expert')37 ax[0].label_outer()38 39 librosa.display.waveshow(y_2, sr=sr_2, ax=ax[1])40 ax[1].set(title='Time domain plot of Learner')41 plt.plot()42 43 # Render the Matplotlib figure in Streamlit44 st.pyplot(fig)45 46def TWO_PLOT_MEL_SPECTROGRAM(signal1,signal2):47 # Load the first audio file and compute its mel spectrogram48 y1,sr1 = LIBROSA_LOAD(signal1)49 y2,sr2 = LIBROSA_LOAD(signal2)50 51 #y1, sr1 = librosa.load(signal1)52 S1 = librosa.feature.melspectrogram(y = y1, sr=sr1)53 54 # Load the second audio file and compute its mel spectrogram55 #y2, sr2 = librosa.load(signal2)56 S2 = librosa.feature.melspectrogram(y = y2, sr=sr2)57 58 # Create a figure with two subplots59 fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(8, 10))60 61 # Plot the first mel spectrogram on the first subplot62 img1 = librosa.display.specshow(librosa.power_to_db(S1, ref=np.max), y_axis='mel', x_axis='time', ax=ax1, cmap='viridis')63 ax1.set(title='Mel Spectrogram of {}'.format(signal1))64 #ax1.set_title('Mel spectrogram of audio file 1')65 fig.colorbar(img1, ax=ax1, format='%+2.0f dB')66 67 # Plot the second mel spectrogram on the second subplot68 img2 = librosa.display.specshow(librosa.power_to_db(S2, ref=np.max), y_axis='mel', x_axis='time', ax=ax2, cmap='viridis')69 #ax2.set_title('Mel spectrogram of audio file 2')70 ax2.set(title='Mel Spectrogram of {}'.format(signal2))71 fig.colorbar(img2, ax=ax2, format='%+2.0f dB')72 73 # Adjust the layout of the subplots74 plt.tight_layout()75 76 # Show the plot77 #plt.show()78 st.pyplot(fig)79 80 81 82def DIFF_TWO_PLOT_MEL_SPECTROGRAM(y1, sr1, y2, sr2):83 # Load the first audio file and compute its mel spectrogram84 # y1,sr1 = LIBROSA_LOAD(signal1)85 # y2,sr2 = LIBROSA_LOAD(signal2)86 87 #y1, sr1 = librosa.load(signal1)88 S1 = librosa.feature.melspectrogram(y = y1, sr=sr1)89 90 # Load the second audio file and compute its mel spectrogram91 #y2, sr2 = librosa.load(signal2)92 S2 = librosa.feature.melspectrogram(y = y2, sr=sr2)93 94 # Create a figure with two subplots95 fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(8, 10))96 97 # Plot the first mel spectrogram on the first subplot98 img1 = librosa.display.specshow(librosa.power_to_db(S1, ref=np.max), y_axis='mel', x_axis='time', ax=ax1, cmap='viridis')99 ax1.set(title='Mel Spectrogram of Expert')100 #ax1.set_title('Mel spectrogram of audio file 1')101 fig.colorbar(img1, ax=ax1, format='%+2.0f dB')102 103 # Plot the second mel spectrogram on the second subplot104 img2 = librosa.display.specshow(librosa.power_to_db(S2, ref=np.max), y_axis='mel', x_axis='time', ax=ax2, cmap='viridis')105 #ax2.set_title('Mel spectrogram of audio file 2')106 ax2.set(title='Mel Spectrogram of Learner')107 fig.colorbar(img2, ax=ax2, format='%+2.0f dB')108 109 # Adjust the layout of the subplots110 plt.tight_layout()111 112 # Show the plot113 #plt.show()114 st.pyplot(fig)115 116 117st.set_page_config(118 page_title="Hello Carnatic Music",119 page_icon="๐",120)121 122st.write("# Drop your files here! ๐")123 124 125 126# Streamlit app127#st.title('Audio Signal Visualization')128 129# File upload130uploaded_file_T = st.file_uploader('Upload an audio file lvl Expert', type=['mp3'])131 132uploaded_file_S = st.file_uploader('Upload an audio file lvl learner', type=['mp3'])133 134# signal_T = uploaded_file_T135 136# signal_S = uploaded_file_S137 138 139 140# Assuming you have the audio data in `y` and the sample rate in `sr`141 142#output_path_T = 'data/output_teacher.wav' # Specify the output file path143#output_path_S = 'data/output_student.wav'144# Convert and save the audio file145#librosa.output.write_wav(output_path, y, sr)146 147 148if uploaded_file_T is not None:149 if uploaded_file_S is not None:150 y_1, sr_1 = LIBROSA_LOAD(uploaded_file_T)151 y_2, sr_2 = LIBROSA_LOAD(uploaded_file_S)152 #librosa.output.write_wav(output_path_T, y_1, sr_1)153 #librosa.output.write_wav(output_path_S, y_2, sr_2)154 #st.write(y_1,y_2)155 #st.write("Done")156 st.title('Audio Signal Visualization Time Domain')157 # TWO_PLOT_T(uploaded_file_T,uploaded_file_S)158 DIFF_TWO_PLOT_TIME_DOMAIN(y_1,sr_1,y_2,sr_2)159 160 st.title('Audio Signal Visualization Frequency Domain')161 DIFF_TWO_PLOT_MEL_SPECTROGRAM(y_1,sr_1,y_2,sr_2)162 163 164 165 166 167 168 # st.title('Audio Signal Visualization Time Domain')169 # TWO_PLOT_T(uploaded_file_T,uploaded_file_S)170 171 172 173 # st.title('Audio Signal Visualization Frequency Domain')174 # TWO_PLOT_MEL_SPECTROGRAM(uploaded_file_T,uploaded_file_S)175 176 177 178 179 180 181# if uploaded_file_T is not None:182# if uploaded_file_S is not None:183# st.title('Audio Signal Visualization Time Domain')184 #TWO_PLOT_T(uploaded_file_T,uploaded_file_S)185 186 #st.title('Audio Signal Visualization Frequency Domain')187 #TWO_PLOT_MEL_SPECTROGRAM(uploaded_file_T,uploaded_file_S)188 189 190 #TWO_PLOT_MEL_SPECTROGRAM(uploaded_file_T,uploaded_file_S)191 192 193# if signal_T is not None:194# if signal_S is not None:195# st.title('Audio Signal Visualization Frequency Domain')196# #TWO_PLOT_TIME_DOMAIN(uploaded_file_T,uploaded_file_S)197# TWO_PLOT_MEL_SPECTROGRAM(signal_T,signal_S)198 199 200# if uploaded_file_T is not None:201# if uploaded_file_S is not None:202# TWO_PLOT_MEL_SPECTROGRAM(uploaded_file_T,uploaded_file_S)203 204 205 206# for i,j in zip(uploaded_file_T,uploaded_file_S):207# if i is not None and j is not None:208# TWO_PLOT_TIME_DOMAIN(i,j)209 210# if uploaded_file_T is not None:211# audio_data, sr = librosa.load(uploaded_file_T, sr=None)212# plot_audio_signal(audio_data, sr)213 214 215# if uploaded_file_S is not None:216# audio_data, sr = librosa.load(uploaded_file_S, sr=None)217# plot_audio_signal(audio_data, sr)218 219 220 221 222 223 224 225 226 