音频特征提取——librosa工具包使用
作者:桂。
时间:2017-05-06 11:20:47
链接:http://www.cnblogs.com/xingshansi/p/6816308.html

前言
本文主要记录librosa工具包的使用,librosa在音频、乐音信号的分析中经常用到,是python的一个工具包,这里主要记录它的相关内容以及安装步骤,用的是python3.5以及win8.1环境。
一、MIR简介
音乐信息检索(Music information retrieval,MIR)主要翻译自wikipedia.
MIR是从音乐中检索信息的跨学科科学,该领域需要心理学、乐理学、信号处理、机器学习等学科的背景知识。
目前MIR的商业应用主要包括:
- 推荐系统
目前音乐推荐的应用很多,但很少是基于MIR技术实现的,现在主流技术是通过人工标记或者用户的评论以及收听历史等简介数据进行分类判断,进而实现推荐,但事实上不同音乐本身的相似性是很多的- 轨道分离及乐器识别
实现音乐的轨道分离,以及从音乐中识别出是何种乐器在演奏- 自动录音
根据音乐自动转换成MIDI文件或者乐谱- 音乐分类
根据音乐的产地、艺术家身份、音乐节奏等特征,借助机器学习方法进行音乐分类- 自动生成音乐
利用数据库训练模式,让机器自主创造音乐MIR领域涉及到的知识包括:
- 语料库:没有音乐库,利用机器学习挖掘历史出统计规律,是不够现实的
- 特征提取:例如常见的MFCC,是音色的一种度量,另外和弦、和声、节奏等音乐的特性,都需要合适的特征来进行表征
- 统计学习方法以及机器学习的相关知识
MIR用到的相关工具包可以参考isMIR主页。
二、Librosa功能简介
librosa对于MIR来讲就是特征提取的工具,当然一般音频分析也可以借用librosa。
A-主要功能
更多细节可以参考其主页。
- 音频处理

load:读取文件,可以是wav、mp3等格式;resample:重采样;get_duration:计算音频时长;autocorrelate:自相关函数;zero crossings:过零率;
- 频谱特性

stft:短时傅里叶变换;istft:逆短时傅里叶变换;ifgram:瞬时频率;cqt:音乐中常用的CQT算法(constant-Q transform);hybrid cqt:混合CQT变换;fmt:快速梅林变换;interp harmonics:主要计算时频信号中谐波的能量;salience:谐波显示功能;phase vocoder:相位声码;magphase:相位幅值
- 幅度

就是一些数值不同度量的转化。
- 时频转化

这个比较直观,就不啰嗦了。
- Pitch and tuning(音调和曲调?清楚的麻烦说一下二者具体区别)

- Dynamic Time Warping
就是DWT,动态时间规整。

以上只是一部分,其他的功能还有很多:

例如常用的MFCC提取就是Feature extraction中的一个函数而已。
B-常用功能
比如读取一个音频信号:
import librosa
# 1. Get the file path to the included audio example
filepath = 'C:\\Users\\Nobleding\\Documents\\FileRecv\\'
filename =filepath+'bluesky.wav'
# 2. Load the audio as a waveform `y`
# Store the sampling rate as `sr`
y, sr = librosa.load(filename,sr=None)
load默认的采样率是22050,如果需要读取原始采样率,需要.load(filename,sr=None)而不是load(filename)
例如读取一段音频,判断节奏,并画出时频特性:
# Beat tracking example
#from __future__ import print_function
import librosa
import matplotlib.pyplot as plt
import librosa.display # 1. Get the file path to the included audio example
# Sonify detected beat events
y, sr = librosa.load(librosa.util.example_audio_file())
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
y_beats = librosa.clicks(frames=beats, sr=sr) # Or generate a signal of the same length as y
y_beats = librosa.clicks(frames=beats, sr=sr, length=len(y)) # Or use timing instead of frame indices
times = librosa.frames_to_time(beats, sr=sr)
y_beat_times = librosa.clicks(times=times, sr=sr) # Or with a click frequency of 880Hz and a 500ms sample
y_beat_times880 = librosa.clicks(times=times, sr=sr,
click_freq=880, click_duration=0.5) # Display click waveform next to the spectrogram
plt.figure()
S = librosa.feature.melspectrogram(y=y, sr=sr)
ax = plt.subplot(2,1,2)
librosa.display.specshow(librosa.power_to_db(S, ref=np.max),
x_axis='time', y_axis='mel')
plt.subplot(2,1,1, sharex=ax)
librosa.display.waveplot(y_beat_times, sr=sr, label='Beat clicks')
plt.legend()
plt.xlim(15, 30)
plt.tight_layout()

关于可视化多说两句,librosa.display模块并不默认包含在librosa中,所以开头两句都要有:
import librosa
import librosa.display
例如这个时候想显示语谱图:
import librosa
import matplotlib.pyplot as plt
import numpy as np
import librosa.display # 1. Get the file path to the included audio example
filepath = 'C:\\Users\\Nobleding\\Documents\\FileRecv\\'
filename =filepath+'bluesky1.wav'
# 2. Load the audio as a waveform `y`
# Store the sampling rate as `sr`
y, sr = librosa.load(filename,sr=None) plt.figure(figsize=(12, 8))
D = librosa.amplitude_to_db(librosa.stft(y), ref=np.max)
plt.subplot(4, 2, 1)
librosa.display.specshow(D, y_axis='linear')
plt.colorbar(format='%+2.0f dB')
plt.title('Linear-frequency power spectrogram')

例如想观察CQT变换:
CQT = librosa.amplitude_to_db(librosa.cqt(y, sr=16000), ref=np.max)
plt.subplot(4, 2, 3)
librosa.display.specshow(CQT, y_axis='cqt_note')
plt.colorbar(format='%+2.0f dB')
plt.title('Constant-Q power spectrogram (note)')
其他以此类推。

MFCC提取:
import librosa
import librosa.display # 1. Get the file path to the included audio example
# Sonify detected beat events
y, sr = librosa.load(librosa.util.example_audio_file())
librosa.feature.mfcc(y=y, sr=sr)
librosa在youtube上有简要的教程。
三、librosa的安装
libsora对应的链接点击这里。安装报错两个:
关于microsoft visual c++ 14.0 :

解决思路是:
- Download Microsoft Visual C++ Build Tools 2015
- Install this, making sure in the install options to select the “Windows SDK” appropriate for your version of Windows. Windows 7 systems should use Windows 8.1 SDK.
找到visual C++下载页面,点击这里:

安装完成后,安装resampy。
关于resampy(同样依赖microsoft visual c++ 14.0):

resampy是采样率转化工具,github关于resampy的安装包点击这里。
cd到对应文件夹,我放在了\pkgs\lib文件夹内,输入:
pip install resampy
可以看到resampy已经成功安装:

进一步安装librosa,同样放在\pkgs\lib文件夹内,cd到对应目录,输入:
pip install librosa
即可完成librosa的安装。

参考:
音频特征提取——librosa工具包使用的更多相关文章
- 音频特征提取——pyAudioAnalysis工具包
作者:桂. 时间:2017-05-04 18:31:09 链接:http://www.cnblogs.com/xingshansi/p/6806637.html 前言 语音识别等应用离不开音频特征的 ...
- python特征提取——pyAudioAnalysis工具包
作者:桂. 时间:2017-05-04 18:31:09 链接:http://www.cnblogs.com/xingshansi/p/6806637.html 前言 语音识别等应用离不开音频特征的 ...
- librosa音频特征提取,python librosa库在centos上依赖llvm的问题?
win10下安装使用: https://blog.csdn.net/qq_39516859/article/details/80679718 https://blog.csdn.net/qq_3951 ...
- pyAudioAnalysis-audioFeatureExtraction 错误纠正
1. TypeError: mfccInitFilterBanks() takes 2 positional arguments but 7 were given The issue In the f ...
- 学习笔记TF046:TensoFlow开发环境,Mac、Ubuntu/Linux、Windows,CPU版本、GPU版本
下载TensorFlow https://github.com/tensorflow/tensorflow/tree/v1.1.0 .Tags选择版本,下载解压. pip安装.pip,Python包管 ...
- 顶级Python库
绝不能错过的24个顶级Python库 Python有以下三个特点: · 易用性和灵活性 · 全行业高接受度:Python无疑是业界最流行的数据科学语言 · 用于数据科学的Python库的数量优势 事实 ...
- 一文总结数据科学家常用的Python库(下)
用于建模的Python库 我们已经到达了本文最受期待的部分 - 构建模型!这就是我们大多数人首先进入数据科学领域的原因,不是吗? 让我们通过这三个Python库探索模型构建. Scikit-learn ...
- 总结数据科学家常用的Python库
概述 这篇文章中,我们挑选了24个用于数据科学的Python库. 这些库有着不同的数据科学功能,例如数据收集,数据清理,数据探索,建模等,接下来我们会分类介绍. 您觉得我们还应该包含哪些Python库 ...
- Wavenet运行
作者:桂. 时间:2017-05-10 19:17:32 链接:http://www.cnblogs.com/xingshansi/p/6832219.html 一.环境 python3.5 Win ...
随机推荐
- Spring报错——Scope 'session' is not active for the current thread
在对程序进行了一些修改后,运行发现spring报了这个错误,这是由于我设置了一个@Scope("session")导致的,现记录下解决方法. 解决方法: 将Scope设置为scop ...
- (转)关闭iptables和SELinux
1. 关闭SELinux setenforce 0 #临时关闭 编辑/etc/selinux/config,找到SELINUX 行修改成为:SELINUX=disabled: #永久关闭, ...
- 不使用回调函数的ajax请求实现(async和await简化回调函数嵌套)
在常规的服务器端程序设计中, 比如说爬虫程序, 发送http请求的过程会使整个执行过程阻塞,直到http请求响应完成代码才会继续执行, 以php为例子 $url = "http://www. ...
- Plupload上传插件简单整理
Plupload Plupload是有TinyMCE的开发者开发的,为您的内容管理系统或是类似上传程序提供一个高度可用的上传插件.Plupload 目前分为一个核心API 和一个jQuery上传队列部 ...
- python 之tornado 入门
#!/usr/bin/env python # -*- coding:utf-8 -*- # --------------------------------------- # email : gen ...
- js正则表达式匹配字符串与优化过程
前言 有时候需要实现对js源文件中的url字符串做拦截预处理,或者前端js语法高亮,或者需要对动态加载的关键源码做混淆保护,在某些步骤实现之前,有一个步骤是需要提炼出所有的合法字符串. 目标:检测源文 ...
- JS中遍历语法的比较
for循环 JavaScript 提供多种遍历语法.最原始的写法就是for循环.(假设myArray是数组,下面同理) let arr = [1,2,3,4,5]; for (var index = ...
- python修行:练习购物车
product_list = [ ('Iphone',5800), ('Mac Pro',9800), ('Bike',800), ('Watch',10600), ('Coffee',31), (' ...
- Android M以上运行时权限(Google官方出品)
转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6690152.html 网上运行时权限的例子.Demo无计其数,但是和Google官方出品的比起来,都显得很 ...
- Struts2中there is no action mapped for acion name (/XXXXX)
这里的问题出在配置struts.xml中,去掉配置中 namespace="/"属性 即可解决.不同的调用action的方式对namespace="/"属性有的 ...