Librosa音频处理(四)

8 篇文章 4 订阅
6 篇文章 9 订阅

使用 Librosa 对音色可视化

specshow.py

import librosa 
import matplotlib.pyplot as plt
import numpy as np
import librosa.display
import os

class specshowplot:
    def __init__(self):
        self.plotlist = []

    def addplot(self, path, index):
        self.plotlist.insert(index, path)

    def show(self):
        for index, path in  enumerate(self.plotlist):
            self._show(path, index)
        plt.show()
    
    def _show(self, path, index):
        y,sr = librosa.load(path)
        count = len(self.plotlist)
        plt.subplot(count, 2, index*2+1)

        plt.title(os.path.basename(path))
        plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.4, hspace=0.5)
        librosa.display.waveplot(y)

        s = librosa.stft(y)
        s[abs(s)<s.max()/5] = 0
        y = librosa.istft(s)
        tone = librosa.tone(y,sr,len(y))
        d = librosa.feature.melspectrogram( y=tone )
        d = librosa.power_to_db(d,ref=np.max)
        s2 = plt.subplot(count, 2, index*2+2)
        librosa.display.specshow(d,x_axis='time', y_axis='mel')


if __name__ == '__main__':
    outputpath = "/Users/birenjianmo/Desktop/learn/librosa/cover_nihao.wav"
    path = "/Users/birenjianmo/Desktop/learn/librosa/你好我是小猪佩奇.mp3"
    sp = specshowplot()
    sp.addplot(path, 0)
    sp.addplot(outputpath, 1)
    sp.show()
    

备注:让matplotlib支持中文  https://www.jianshu.com/p/1fc84794a2d1 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值