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

前言
本文旨在介绍librosa工具包的使用方法。该工具包常用于对音频和音乐信号进行分析,并属于Python编程语言的一个第三方库。本文将详细讨论其功能特点并提供安装指南。基于Python 3.5和Windows 10-64位系统平台进行开发。
一、MIR简介
音乐信息检索(MIR)主要源自wikipedia上的条目[https://en.wikipedia.org/wiki/Music_information_retrieval].
MIR是一种跨学科的信息检索科学,在音乐领域需要心理学、乐理学、信号处理以及机器学习等多个领域的知识背景。
当前MIR在商业应用中的主要应用领域包括:
- 基于MIR技术的音乐推荐系统
目前音乐推荐系统中基于MIR技术的应用相对较少;大多数主流推荐系统仍依赖于人工标记、用户评论以及收听历史等数据进行分类判断;然而不同音乐作品本身的相似性非常高- 轨道分离及乐器识别功能
MIR能够实现对音乐信号的轨道分离以及乐器识别功能- 自动录音功能
根据输入的音乐文件可以自动生成对应的 MIDI 文件或乐谱文件- 音乐分类功能
通过机器学习算法可以依据歌曲的产地、艺术家信息以及节奏特点进行分类判断- 音乐生成功能
利用训练的数据模型使机器具备一定的自主创作能力
MIR领域涉及的知识点主要包括:
- 语料库:缺乏专业音乐数据库的情况下,单纯通过机器学习挖掘历史数据并提取统计规律并非切实可行。
- 特征提取:例如常用的MFCC特征作为音色特征的一种量化指标,在描述音乐元素时还应考虑乐段中的和弦、和声 progression以及节奏等元素。
- 统计学习方法与机器学习理论基础:这些是构建MIR系统所需的核心知识体系。
MIR涉及的相关工具包可获取于isMIR主页
二、Librosa功能简介
librosa是一种广泛应用于音乐信息检索(MIR)领域的特征提取工具;同时它还可以借助于该框架进行一般的音频分析。
A-主要功能
更多细节可以参考其主页。
- 音频处理

load 获取文件;resample 重采样;get_duration 计算音频时长;autocorrelate 自相关函数;zero crossings 过零率
- 频谱特性

stft:短时傅里叶转换;istft:逆短时傅里叶转换;ifgram:瞬态频率图;cqt:常数Q变换算法,在音乐领域广泛应用;hybrid cqt:混合常数Q变换算法;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()

对于可视化的内容可以进一步说明, 该模块并非预置在主库中, 因此, 在代码开始处需要明确这两项内容:
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的安装
对应链接为点击这里。在安装过程中出现错误提示两次
关于microsoft visual c++ 14.0 :

解决思路是:
- 安装Microsoft Visual C++ Build Tools 2015
- 安装此软件时,请确保在安装选项中选择与您的Windows版本兼容的Windows SDK。建议使用Windows 8.1 SDK以兼容您的系统版本。
访问visual C++的下载页面或网站,并单击此处。

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

Resampy是一种用于采样率转换的工具;其官方安装包可通过GitHub平台访问此处点击。
cd到对应文件夹,我放在了\pkgs\lib文件夹内,输入:
pip install resampy
可以看到resampy已经成功安装:

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

参考:
