diff --git a/home.py b/home.py index 1432f3f..ec7b4a2 100644 --- a/home.py +++ b/home.py @@ -11,6 +11,7 @@ st.session_state.first_run = True st.session_state["use_segment"] = False st.session_state["use_plotly"] = False + st.session_state["file_name"] = "" st.session_state["0-file"] = { } diff --git a/pages/1-Basic_Analysis.py b/pages/1-Basic_Analysis.py index 11aa638..8fc0468 100644 --- a/pages/1-Basic_Analysis.py +++ b/pages/1-Basic_Analysis.py @@ -6,7 +6,7 @@ import numpy as np import librosa import pandas as pd -from src.st_helper import convert_df, show_readme, get_shift +from src.st_helper import convert_df, get_shift, update_sessions from src.basic_info import plot_waveform, signal_RMS_analysis, plot_spectrogram @@ -23,6 +23,7 @@ st.audio(file, format="audio/ogg") st.subheader("File information") st.write(f"File name: `{file.name}`", ) + st.session_state["file_name"] = file.name st.write(f"File type: `{file.type}`") st.write(f"File size: `{file.size}`") @@ -35,8 +36,13 @@ start_time = 0 end_time = duration -#%% 片段模式 +#%% 更新session +update_sessions() + +#%% if file is not None: + + # 片段模式 use_segment = st.sidebar.checkbox("使用片段模式", value=st.session_state["use_segment"], key="segment") st.session_state["use_segment"] = use_segment @@ -71,6 +77,7 @@ st.write(f"Selected segment: `{start_time}` ~ `{end_time}`, duration: `{end_time-start_time}`") st.audio(y_sub, format="audio/ogg", sample_rate=sr) + #%% 功能分頁 diff --git a/pages/2-Pitch_Analysis.py b/pages/2-Pitch_Analysis.py index ccee444..89a25d1 100644 --- a/pages/2-Pitch_Analysis.py +++ b/pages/2-Pitch_Analysis.py @@ -7,7 +7,7 @@ import librosa import pandas as pd import seaborn as sns -from src.st_helper import convert_df, show_readme, get_shift +from src.st_helper import convert_df, get_shift, update_sessions from src.pitch_estimation import ( plot_mel_spectrogram, plot_constant_q_transform, @@ -29,6 +29,7 @@ st.audio(file, format="audio/ogg") st.subheader("File information") st.write(f"File name: `{file.name}`", ) + st.session_state["file_name"] = file.name st.write(f"File type: `{file.type}`") st.write(f"File size: `{file.size}`") @@ -40,6 +41,9 @@ y_all = y start_time = 0 end_time = duration + +#%% 更新session +update_sessions() #%% 片段模式 if file is not None: diff --git a/pages/3-Time_Analysis.py b/pages/3-Time_Analysis.py index 5346bf6..dc0c7e9 100644 --- a/pages/3-Time_Analysis.py +++ b/pages/3-Time_Analysis.py @@ -7,7 +7,7 @@ import librosa import pandas as pd from src.beat_track import onsets_detection, plot_onset_strength, beat_analysis, predominant_local_pulse, static_tempo_estimation, plot_tempogram, onset_click_plot, beat_plot, plot_bpm -from src.st_helper import convert_df, show_readme, get_shift +from src.st_helper import convert_df, get_shift, update_sessions import numpy as np st.title('Time Analysis') @@ -23,6 +23,7 @@ st.audio(file, format="audio/ogg") st.subheader("File information") st.write(f"File name: `{file.name}`", ) + st.session_state["file_name"] = file.name st.write(f"File type: `{file.type}`") st.write(f"File size: `{file.size}`") @@ -34,7 +35,9 @@ y_all = y start_time = 0 end_time = duration - + +#%% 更新session +update_sessions() #%% 片段模式 if file is not None: use_segment = st.sidebar.checkbox("使用片段模式", value=st.session_state["use_segment"], key="segment") diff --git a/pages/4-Chord_Analysis.py b/pages/4-Chord_Analysis.py index 23e7535..27fa06d 100644 --- a/pages/4-Chord_Analysis.py +++ b/pages/4-Chord_Analysis.py @@ -7,7 +7,7 @@ import librosa import pandas as pd import seaborn as sns -from src.st_helper import convert_df, show_readme, get_shift +from src.st_helper import convert_df, get_shift, update_sessions from src.chord_recognition import ( plot_chord_recognition, plot_binary_template_chord_recognition, @@ -31,6 +31,7 @@ st.audio(file, format="audio/ogg") st.subheader("File information") st.write(f"File name: `{file.name}`", ) + st.session_state["file_name"] = file.name st.write(f"File type: `{file.type}`") st.write(f"File size: `{file.size}`") @@ -43,6 +44,9 @@ start_time = 0 end_time = duration +#%% 更新session +update_sessions() + #%% 片段模式 if file is not None: use_segment = st.sidebar.checkbox("使用片段模式", value=st.session_state["use_segment"], key="segment") diff --git a/pages/5-Structure_Analysis.py b/pages/5-Structure_Analysis.py index 03f575e..fc0d9d1 100644 --- a/pages/5-Structure_Analysis.py +++ b/pages/5-Structure_Analysis.py @@ -6,7 +6,7 @@ import numpy as np import librosa import pandas as pd -from src.st_helper import convert_df, show_readme +from src.st_helper import convert_df, get_shift, update_sessions from src.structure_analysis import ( plot_self_similarity ) @@ -24,6 +24,7 @@ st.audio(file, format="audio/ogg") st.subheader("File information") st.write(f"File name: `{file.name}`", ) + st.session_state["file_name"] = file.name st.write(f"File type: `{file.type}`") st.write(f"File size: `{file.size}`") @@ -36,6 +37,9 @@ start_time = 0 end_time = duration +#%% 更新session +update_sessions() + #%% 片段模式 if file is not None: use_segment = st.sidebar.checkbox("使用片段模式", value=st.session_state["use_segment"], key="segment") diff --git a/pages/6-Timbre_Analysis.py b/pages/6-Timbre_Analysis.py index 711b01b..5f957fe 100644 --- a/pages/6-Timbre_Analysis.py +++ b/pages/6-Timbre_Analysis.py @@ -6,7 +6,7 @@ import numpy as np import librosa import pandas as pd -from src.st_helper import convert_df, show_readme, get_shift +from src.st_helper import convert_df, get_shift, update_sessions from src.timbre_analysis import ( spectral_centroid_analysis, rolloff_frequency_analysis, @@ -27,6 +27,7 @@ st.audio(file, format="audio/ogg") st.subheader("File information") st.write(f"File name: `{file.name}`", ) + st.session_state["file_name"] = file.name st.write(f"File type: `{file.type}`") st.write(f"File size: `{file.size}`") @@ -39,6 +40,9 @@ start_time = 0 end_time = duration +#%% 更新session +update_sessions() + #%% 片段模式 if file is not None: use_segment = st.sidebar.checkbox("使用片段模式", value=st.session_state["use_segment"], key="segment") diff --git a/pages/999-dev.py b/pages/999-dev.py index 824ab15..6156609 100644 --- a/pages/999-dev.py +++ b/pages/999-dev.py @@ -19,3 +19,23 @@ st.session_state["use_plotly"] = st.checkbox("Use plotly", value=st.session_state["use_plotly"]) +st.session_state["debug"] = st.checkbox("Debug", value=st.session_state["debug"]) + + + +with st.expander("Session state"): + st.write(type(st.session_state)) + st.write(st.session_state) + session_state = dict(st.session_state) + st.write(type(session_state)) + st.write(session_state) + + +import pickle + + + + +# from src.st_helper import download_upload_settings + +# download_upload_settings() \ No newline at end of file diff --git a/src/st_helper.py b/src/st_helper.py index 8ef6b99..52f0bbb 100644 --- a/src/st_helper.py +++ b/src/st_helper.py @@ -1,4 +1,5 @@ import streamlit as st +import pickle @st.cache_data def convert_df(df): @@ -30,4 +31,29 @@ def get_shift(start_time, end_time): shift_array = np.round(shift_array, 1) return start_time, shift_array - \ No newline at end of file + + +def update_sessions(): + """ + Update the session state. + Download/Upload button + """ + if st.session_state["file_name"]: + + with st.expander("Config Download/Upload"): + + col1, col2 = st.columns([2, 6]) + + col1.download_button(label="Download Current Settings", + data=pickle.dumps(dict(st.session_state)), + file_name=st.session_state["file_name"]+"_config.pkl", + help="Click to Download Current Settings") + + session_file = col2.file_uploader(label="Upload Previous Settings", + help="Upload a previously saved settings file" + ) + + if session_file is not None: + new_session = pickle.loads(session_file.read()) + st.write(new_session) + st.session_state.update(new_session)