File size: 671 Bytes
6624537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import librosa
import numpy as np
import matplotlib.pyplot as plt

def extract_melody(audio_file):
    # 加载音频文件
    y, sr = librosa.load(audio_file)
    
    # 提取旋律(和声成分)
    y_harmonic, y_percussive = librosa.effects.harmonic(y)
    
    # 计算音高特征(Chroma)
    chroma = librosa.feature.chroma_cqt(y=y_harmonic, sr=sr)
    
    # 显示音高特征
    plt.figure(figsize=(10, 4))
    librosa.display.specshow(chroma, y_axis='chroma', x_axis='time')
    plt.colorbar()
    plt.title('Chromagram')
    plt.tight_layout()
    plt.show()

# 替换为你的音频文件路径
audio_file = 'your_song.mp3'
extract_melody(audio_file)