librosa.onset.onset_detect 函数介绍

1 音符起始点检测介绍

音符起始点检测(onset detection)是音乐信号处理中非常重要的一个算法。节拍和速度(tempo)的检测都会基于音符起始点的检测。

音符起始点就是其字面意思,表示琴键🎹按下的那个时刻。

2 函数介绍

librosa.onset.onset_detect 函数

librosa.onset.onset_detect(*, 
y: Optional[ndarray] = None, 
sr: float = 22050, 
onset_envelope: Optional[ndarray] = None, 
hop_length: int = 512, 
backtrack: bool = False, 
energy: Optional[ndarray] = None, 
units: str = 'frames', 
normalize: bool = True,
**kwargs: Any)→ ndarray

参数介绍:
y:一维音频样本
sr:音频采样率
hop_length:样本分割间隔
units:{‘frames’, ‘samples’, ‘time’} 默认为 ‘frames’
backtrack:是否回溯,backtrack默认为 False,这样会造成,找出起始位置并不是能量开始增加的位置,也即不是真正的起始位置。backtrack为向前追溯找到能量开始增加的点,即真正的起始点

返回:
起始位置序列,默认返回 frame 索引

例子:

import librosa

# 加载内置音频
y, sr = librosa.load(librosa.ex('trumpet'))
# 返回起始点时间点
librosa.onset.onset_detect(y=y, sr=sr, units='time')

Out>>>array([0.06965986, 0.23219955, 0.39473923, 0.60371882, 0.74303855,
       0.92879819, 1.04489796, 1.11455782, 1.41641723, 1.67183673,
       1.88081633, 2.04335601, 2.20589569, 2.36843537, 2.55419501,
       3.0185941 ])
o_env = librosa.onset.onset_strength(y=y, sr=sr)
times = librosa.times_like(o_env, sr=sr)
onset_frames = librosa.onset.onset_detect(onset_envelope=o_env, sr=sr)
import matplotlib.pyplot as plt
import numpy as np
# 短时傅里叶变换
D = np.abs(librosa.stft(y))
fig, ax = plt.subplots(nrows=2, sharex=True)
librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max),
                         x_axis='time', y_axis='log', ax=ax[0])
ax[0].set(title='Power spectrogram')
ax[0].label_outer()
ax[1].plot(times, o_env, label='Onset strength')
# 起始点位置作为竖线画出
ax[1].vlines(times[onset_frames], 0, o_env.max(), color='r', alpha=0.9,
           linestyle='--', label='Onsets')
ax[1].legend()

在这里插入图片描述

参考:

结合Python代码介绍音符起始点检测 (onset detection)
librosa.onset.onset_detect

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值