app.py
Browse files- add audio-play +26 -0
add audio-play
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import librosa
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import librosa.display
|
5 |
+
|
6 |
+
def extract_melody(audio_file):
|
7 |
+
# 加载音频文件
|
8 |
+
y, sr = librosa.load(audio_file)
|
9 |
+
|
10 |
+
# 提取旋律(和声成分)
|
11 |
+
y_harmonic = librosa.effects.harmonic(y) # 注意这里的变化
|
12 |
+
|
13 |
+
# 计算音高特征(Chroma)
|
14 |
+
chroma = librosa.feature.chroma_cqt(y=y_harmonic, sr=sr)
|
15 |
+
|
16 |
+
# 显示音高特征
|
17 |
+
plt.figure(figsize=(10, 4))
|
18 |
+
librosa.display.specshow(chroma, y_axis='chroma', x_axis='time')
|
19 |
+
plt.colorbar()
|
20 |
+
plt.title('Chromagram')
|
21 |
+
plt.tight_layout()
|
22 |
+
plt.show()
|
23 |
+
|
24 |
+
# 替换为你的音频文件路径
|
25 |
+
audio_file = 'your_song.mp3'
|
26 |
+
extract_melody(audio_file)
|