import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
filepath = 'MWdVM0VuQU9NTDQ91557756385902.wav'
# y音频时间序列 sr:音频的采样率
y, sr = librosa.load(filepath, sr=None) # 文件路径 sr:采样频率 mono:是否将信号转换为单声道 offset:1.2 浮点型(秒为单位)
# print(y.shape)
print(type(y))
"""
y :音频时间序列 frame_length :帧长 hop_length :帧移 center:bool,如果为True,则通过填充y的边缘来使帧居中。
"""
# librosa.feature.zero_crossing_rate(y, frame_length=2048, hop_length=512, center=True)
# 创建自定义图像 plt中绘制一张图片
plt.figure(figsize=(10, 10))
# 调整边距和子图的间距
plt.subplots_adjust(wspace=1, hspace=0.5)# wspace 为子图之间的空间保留的宽度,平均轴宽的一部分,,,,# 为子图之间的空间保留的高度,平均轴高度的一部分
# 创建单个子图
plt.subplot(311)
# 计算出每一帧音频信号的频谱质心
cent = librosa.feature.spectral_centroid(y=y, sr=sr)
plt.plot(cent[0])
# x轴名
plt.xlabel('sample')
# y轴名
plt.ylabel('frequency')
# 创建单个子图
plt.subplot(312)
# 显示频谱图 音色谱
chroma = librosa.feature.chroma_cqt(y=y, sr=sr, n_chroma=12)
librosa.display.specshow(chroma, y_axis='chroma', x_axis='time')
plt.xlabel('note')
plt.ylabel('beat')
plt.subplot(313)
# 绘制音频。
librosa.display.waveplot(y, sr=sr)
plt.xlabel('second')
plt.ylabel('amplitude')
#
plt.show()