Skip to content

Commit

Permalink
fix: Chroma橫軸換成時間(#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keycatowo committed Aug 9, 2023
1 parent 2a30913 commit 51100cc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pages/4-Chord_Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@
# STFT Chroma
with tab1:
chroma, _, _, _, duration = compute_chromagram(y_sub, sr)
fig4_1, ax4_1 = plot_chord(chroma, "STFT Chroma")
fig4_1, ax4_1 = plot_chord(chroma, "STFT Chroma", shift_time=shift_time)
st.pyplot(fig4_1)

with tab2:
_, chord_max = chord_recognition_template(chroma, norm_sim='max')
fig4_2, ax4_2 = plot_chord(chord_max, "Chord Recognition Result", cmap="crest", include_minor=True)
fig4_2, ax4_2 = plot_chord(chord_max, "Chord Recognition Result", cmap="crest", include_minor=True, shift_time=shift_time)
st.pyplot(fig4_2)
sec_per_frame = duration/chroma.shape[1]
chord_results_df = pd.DataFrame({
Expand Down
51 changes: 48 additions & 3 deletions src/chord_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,28 @@ def chord_table(chord_max):
return chord_results


def plot_chord(chroma, title="", figsize=(12, 6), cmap="coolwarm", include_minor=False):
def plot_chord(chroma, title="", figsize=(12, 6), cmap="coolwarm", include_minor=False, shift_time=0.0):
import seaborn as sns
chroma_labels = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
if include_minor:
chroma_labels += ['Cm', 'C#m', 'Dm', 'D#m', 'Em', 'Fm', 'F#m', 'Gm', 'G#m', 'Am', 'A#m', 'Bm']

# 計算時間標記
times = librosa.times_like(
chroma,
sr=22050,
n_fft=4096,
hop_length=2048
)
duration = times[-1]
target_values = np.arange(5, duration, 5)
x_ticks_loc = []
x_ticks_label = []
for target in target_values:
cloest_index = np.argmin(np.abs(times-target))
x_ticks_loc.append(cloest_index)
x_ticks_label.append(int(target)+shift_time)

fig, ax = plt.subplots(figsize=figsize)

sns.heatmap(chroma, ax=ax, cmap=cmap, linewidths=0.01, linecolor=(1, 1, 1, 0.1))
Expand All @@ -251,12 +267,17 @@ def plot_chord(chroma, title="", figsize=(12, 6), cmap="coolwarm", include_minor
ax.set_ylabel("Chord")
ax.set_xlabel('Time (frame)')
ax.set_title(title)

ax.set_xticks(
x_ticks_loc,
x_ticks_label,
rotation=0
)
return fig, ax

def plot_user_chord(
df,
ax = None
ax = None,
shift_time=0.0
):

import seaborn as sns
Expand Down Expand Up @@ -289,6 +310,30 @@ def plot_user_chord(
ax.set_xlabel('Time (frame)')
ax.set_title('User Chord Recognition Result')

# 計算時間標記
times = librosa.times_like(
chroma,
sr=22050,
n_fft=4096,
hop_length=2048
)
duration = times[-1]
target_values = np.arange(5, duration, 5)
x_ticks_loc = []
x_ticks_label = []
for target in target_values:
cloest_index = np.argmin(np.abs(times-target))
x_ticks_loc.append(cloest_index)
x_ticks_label.append(int(target)+shift_time)
# 繪製時間標記
ax.set_xticks(
x_ticks_loc,
x_ticks_label,
rotation=0
)



return fig, ax


Expand Down

0 comments on commit 51100cc

Please sign in to comment.