一、音符检测的基本原理
本文基于 OpenHarmony 开源系统提供了一种音符检测的原理方法,结合多首音乐,运用了 python 和 C++ 两种编程环境实现了预期的检出效果。旨在为振动马达(vibrator)提供音乐节奏感的触觉效果,代码所在目录 .\base\sensors\sensor\vibration_convert。 先从 python 实现说起,Librosa 关于音符检测主要用到了两个函数,一个是 onset_strength(),负责生成包含音符产生的频率突变的包络线,如蓝色线条所示。另一个是 onset_detect(),主要运用峰点检测找到每个音符的位置,如黄色线条所示。
图 1 音符检测包络图 包含有用的频率突变的包络线是音符检测的核心所在。傅里叶变换能够得到全部信号采样的频谱图,即每个频率的能量贡献,如图 2 所示。但是每个时刻频谱图却得不到,于是将全部采样分割成若干固定长度的窗口,每个窗口应用傅里叶变化,从而得到这一窗口的频率分布,水平轴为时间,纵轴为频率,颜色代表能量大小如图 3 所示。
图 2 整体频率分布图
图 3 时频图
每种乐器在音符产生时,前后时间片段的频率将会发生明显变化,如图 4 所示。于是将时频图相邻列做差分,将明显看到变化的频率。为了便于分析,只取正值,具有相同的效果,所以负值填零。一个时刻变化的频率有多个,如何取舍,有三种方法,平均数、中位数和联合,目前常用到的是中位数和平均数。至此,将得到任意时刻发生明显频率变化的单一能量,如图 1 蓝色线条所示。
图 4 时频图相邻列差分前后变化
二、音符检测的准确性
目前采用频谱光通量(相邻列差分)方法检测是业界公认且较为准确的方法,音符检出率仅为 70% 多。不准确的原因可能有乐器多且差异较大,信号衰减对性能的影响,颤音影响,峰点检测时不同参数的影响,这些主要是针对音乐的研究。
三、音符检测的程序流程
3.1 程序实现
音符检测功能核心就是频谱图和梅尔滤波器,频谱图的核心就是短时傅里叶变换,C++ 代码片段如下,参考链接 https://github.com/kooBH/STFT/blob/main/cpp/STFT.h
void STFT::stft(short*in,int length,double**out){
int i,j;
/*** Shfit & Copy***/
for (j = 0; j < channels; j++) {
for (i = 0; i < ol; i++) {
buf[j][i] = buf[j][i + shift_size];
}
}
// EOF
if(length!=shift_size*channels){
length = length/channels;
for (i = 0; i < length; i++) {
for (j = 0; j < channels; j++)
buf[j][i + ol]
= (double)(in[i * channels+ j]);
}
for (i = length; i < shift_size; i++) {
for (j = 0; j < channels; j++)
buf[j][i + ol] = 0;
}
//continue
}else{
for (i = 0; i < shift_size; i++) {
for (j = 0; j < channels; j++){
buf[j][i + ol]
= (double)(in[i * channels+ j]);
}
}
}
/*** Copy input -> hann_input buffer ***/
for (i = 0; i < channels; i++)
memcpy(out[i], buf[i], sizeof(double) * frame_size);
// scaling for precision
if(opt_scale)
for (i = 0; i < channels; i++)
for (j = 0; j < frame_size; j++)
out[i][j] /= MATLAB_scale;
/*** Window ***/
hw->Process(out, channels);
/*** FFT ***/
fft->FFT(out);
}
void STFT::stft(short*in,int length,double**out){
int i,j;
/*** Shfit & Copy***/
for (j = 0; j < channels; j++) {
for (i = 0; i < ol; i++) {
buf[j][i] = buf[j][i + shift_size];
}
}
// EOF
if(length!=shift_size*channels){
length = length/channels;
for (i = 0; i < length; i++) {
for (j = 0; j < channels; j++)
buf[j][i + ol]
= (double)(in[i * channels+ j]);
}
for (i = length; i < shift_size; i++) {
for (j = 0; j < channels; j++)
buf[j][i + ol] = 0;
}
//continue
}else{
for (i = 0; i < shift_size; i++) {
for (j = 0; j < channels; j++){
buf[j][i + ol]
= (double)(in[i * channels+ j]);
}
}
}
/*** Copy input -> hann_input buffer ***/
for (i = 0; i < channels; i++)
memcpy(out[i], buf[i], sizeof(double) * frame_size);
// scaling for precision
if(opt_scale)
for (i = 0; i < channels; i++)
for (j = 0; j < frame_size; j++)
out[i][j] /= MATLAB_scale;
/*** Window ***/
hw->Process(out, channels);
/*** FFT ***/
fft->FFT(out);
}
Mel 滤波器构造代码如下:
if fmax is None:
fmax = float(sr) / 2
# Initialize the weights
n_mels = int(n_mels)
weights = np.zeros((n_mels, int(1 + n_fft // 2)), dtype=dtype)
# Center freqs of each FFT bin
fftfreqs = fft_frequencies(sr=sr, n_fft=n_fft)
# 'Center freqs' of mel bands - uniformly spaced between limits
mel_f = mel_frequencies(n_mels + 2, fmin=fmin, fmax=fmax, htk=htk)
fdiff = np.diff(mel_f)
ramps = np.subtract.outer(mel_f, fftfreqs)
for i in range(n_mels):
# lower and upper slopes for all bins
lower = -ramps[i] / fdiff[i]
upper = ramps[i + 2] / fdiff[i + 1]
# .. then intersect them with each other and zero
weights[i] = np.maximum(0, np.minimum(lower, upper))
if norm == "slaney":
# Slaney-style mel is scaled to be approx constant energy per channel
enorm = 2.0 / (mel_f[2 : n_mels + 2] - mel_f[:n_mels])
weights *= enorm[:, np.newaxis]
else:
weights = util.normalize(weights, norm=norm, axis=-1)
# Only check weights if f_mel[0] is positive
if not np.all((mel_f[:-2] == 0) | (weights.max(axis=1) > 0)):
# This means we have an empty channel somewhere
warnings.warn(
"Empty filters detected in mel frequency basis. "
"Some channels will produce empty responses. "
"Try increasing your sampling rate (and fmax) or "
"reducing n_mels.",
stacklevel=2,
)
return weights