通过免费 Python 库进行实时音频和音乐分析
Aubio 是一款开源 Python 音频处理库,软件开发者用于音高检测、节拍跟踪、MFCC 提取和实时音频处理。
Aubio 是什么?
现代音频应用——如音乐制作工具、音频分析平台和实时声音处理系统——需要高效的方式来分析声音信号。为此,一个流行的开源解决方案是 Aubio,这是一款轻量但功能强大的库,旨在从音频信号中提取有意义的信息。Aubio 是一个开源库,旨在提取音频信号中的注释和音乐特征。它分析音频流并识别诸如音符起始、音高、节拍和速度等事件。它主要使用 C 语言编写,以实现高性能,并可在 Linux、macOS 和 Windows 上运行。
该库非常易于使用,并支持实时音频处理。它可以检测音乐事件,例如鼓击的出现、音符的频率以及曲目的节奏或速度。它既可以离线(音频文件)运行,也可以在实时音频流中工作,适用于音频编辑器、数字乐器和声音分析工具。凭借对音高检测、节拍跟踪、起始点检测、MFCC 提取和实时信号处理的支持,为开发者提供构建高级音频应用所需的工具。
Aubio 入门指南
安装 Aubio 最简单的方法是使用 Python 包索引(PyPI)。请使用以下命令进行完整安装。
使用 Aubio 安装
pip install aubio 从 GitHub 安装 Aubio
go get -u github.com/aubio/aubio.git 使用 Python 进行起始点检测
Aubio 库最有用的功能之一是起始检测,它能够识别音乐音符或声音事件何时开始。起始检测能够找到新声音或音乐音符开始的精确时刻。在音乐中,"onset" 指的是音符的起音——鼓被敲击、吉他被拨弦或人声开始一个音节的瞬间。Aubio 包含多种起始检测算法,以适用于不同的使用场景。下面的示例演示了如何使用 Python 代码在音频文件中检测 Onsets。
如何通过 Python 库检测音频文件中的起始点?
import aubio
import numpy as np
# Configuration
filename = "audio_sample.wav"
win_s = 512 # FFT window size
hop_s = 256 # Hop size (frames between analysis windows)
# Open the audio source
src = aubio.source(filename, hop_size=hop_s)
samplerate = src.samplerate
# Create the onset detector (using 'complex' method)
onset = aubio.onset("complex", win_s, hop_s, samplerate)
# Set detection threshold (0.0 to 1.0 — lower = more sensitive)
onset.set_threshold(0.3)
onsets = []
# Process audio frame by frame
while True:
samples, read = src() # Read one hop of audio
if onset(samples): # Returns True when onset detected
print(f"Onset at: {onset.get_last_s():.3f} seconds")
onsets.append(onset.get_last_s())
if read < hop_s: # End of file
break
print(f"\nTotal onsets detected: {len(onsets)}")
音高检测支持
音高检测是开源 Aubio 库的另一项重要功能。音高检测能够识别声音的基频 (F0)——本质上是"what musical note is this?"。它是任何调音器、旋律提取器或声乐分析工具的核心。Aubio 支持多种音高检测算法,如 yin、yinfft、mcomb、fcomb 等。下面是一个简单示例,演示如何在 Python 应用中进行实时 Pitch 检测。
如何使用 Python 实现实时音高检测?
import aubio
import numpy as np
samplerate = 44100
win_s = 1024
hop_s = 512
pitch_o = aubio.pitch("yin", win_s, hop_s, samplerate)
pitch_o.set_unit("Hz")
pitch_o.set_silence(-40)
samples = np.random.rand(hop_s).astype(np.float32)
pitch = pitch_o(samples)
print("Detected pitch:", pitch)
使用 Python 进行节奏跟踪和节拍检测
开源的 Aubio 库可以在 Python 应用程序中分析节奏模式并检测音乐中的节拍和速度。Aubio 能够监听音频并随之敲击——检测以 BPM(每分钟节拍数)为单位的速度,并精准定位各个节拍的时间戳。这对于从 DJ 软件到音乐同步的各种场景都极为宝贵。该库通过分析音频信号中的频谱变化和节奏模式来确定节拍位置。
使用 Python 读取和写入音频文件
Python 库 Aubio 通过其 source 和 sink 对象封装音频文件的 I/O,支持多种格式(WAV、FLAC、OGG、通过 libav 或 ffmpeg 的 MP3),并自动处理采样率转换。下面是一个简单示例,展示软件开发者如何使用 Python 命令进行音频文件的读取、处理和写入。
如何使用 Python 读取、处理和写入音频文件?
import aubio
import numpy as np
input_file = "input.wav"
output_file = "output.wav"
target_samplerate = 22050 # Downsample to 22.05 kHz
hop_s = 512
# Open source — resamples automatically to target samplerate
src = aubio.source(input_file, samplerate=target_samplerate, hop_size=hop_s)
actual_sr = src.samplerate
num_channels = src.channels
# Open sink for writing
sink = aubio.sink(output_file, actual_sr, channels=1)
while True:
samples, read = src()
# Write mono (sink expects 1D array for mono output)
if num_channels > 1:
mono = samples.reshape(num_channels, -1).mean(axis=0)
else:
mono = samples
sink(mono[:read], read)
if read < hop_s:
break
del sink # Flushes and closes file
print(f"Written {output_file} at {actual_sr} Hz")