Skip to content

PigeonDan1/gpurir

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gpuRIR Audio Augmentation Tool

基于 gpuRIR 的语音混响和噪声增强工具,用于语音数据增强和鲁棒性测试。

Python 3.8+ License

功能特性

  • 单文件处理:快速为单个音频文件添加混响和噪声
  • 📁 批量处理:支持整个文件夹的音频批量增强
  • 🎛️ 参数可控:房间大小、混响时间、信噪比等参数可调
  • 🎯 可复现性:通过随机种子保证结果可复现
  • 🚀 GPU 加速:利用 CUDA 快速生成房间脉冲响应
  • 📊 白噪声:无需外部噪声文件,使用高斯白噪声

快速开始

1. 一键安装

# 克隆项目
git clone <your-repo-url>
cd gpurir

# 运行安装脚本
./install.sh

安装脚本会自动:

  • 检查并配置 CUDA 环境
  • 创建 Conda 环境 (gpurir)
  • 安装所有 Python 依赖
  • 安装 gpuRIR 库
  • 验证安装

2. 激活环境

source activate_gpurir.sh

或手动激活:

conda activate gpurir
export PATH=/usr/local/cuda/bin:$PATH

3. 处理音频

单文件处理:

python scripts/add_reverb.py -i input.wav -o output.wav

批量处理:

python scripts/batch_process.py -i input_dir/ -o output_dir/

系统要求

  • 操作系统:Linux (Ubuntu 18.04+ 推荐)
  • GPU:NVIDIA GPU + 驱动程序
  • CUDA:CUDA 10.0+ (推荐 11.4+)
  • Python:3.8+
  • Conda:Anaconda 或 Miniconda

详细使用说明

命令行工具

单文件处理 (add_reverb.py)

python scripts/add_reverb.py -i <input> -o <output> [options]

参数说明:

参数 说明 默认值 范围/建议
-i, --input 输入音频文件路径 必填 .wav, .flac
-o, --output 输出音频文件路径 必填 任意路径
--t60 混响时间 (秒) 0.5 0.1 - 2.0
--room 房间尺寸 (长 宽 高) 5 4 3 米为单位
--snr 信噪比 (dB) 15 -10 - 50,越高噪声越小
--seed 随机种子 42 任意整数
-v, --verbose 显示详细信息 False -

使用示例:

# 默认参数处理
python scripts/add_reverb.py -i input.wav -o output.wav

# 强混响 + 低噪声
python scripts/add_reverb.py -i input.wav -o output.wav --t60 0.8 --snr 20

# 小房间 + 高噪声
python scripts/add_reverb.py -i input.wav -o output.wav --room 3 3 2.5 --snr 5

# 指定随机种子(保证可复现)
python scripts/add_reverb.py -i input.wav -o output.wav --seed 123 -v

批量处理 (batch_process.py)

python scripts/batch_process.py -i <input_dir> -o <output_dir> [options]

参数说明:

add_reverb.py 类似,额外支持:

参数 说明 默认值
-i, --input-dir 输入文件夹 必填
-o, --output-dir 输出文件夹 必填
--ext 处理的文件扩展名 .wav .flac

使用示例:

# 批量处理整个文件夹
python scripts/batch_process.py -i ./raw_audio/ -o ./augmented/

# 只处理 .wav 文件
python scripts/batch_process.py -i ./raw/ -o ./out/ --ext .wav

# 强混响设置
python scripts/batch_process.py -i ./in/ -o ./out/ --t60 0.9 --snr 10

Python API

基础用法

from src.augmentor import AudioAugmentor

# 创建增强器(默认参数)
augmentor = AudioAugmentor()

# 处理音频
info = augmentor.process("input.wav", "output.wav")
print(info)

自定义参数

from src.augmentor import AudioAugmentor

# 自定义增强器
augmentor = AudioAugmentor(
    room_sz=(10, 8, 3.5),  # 大房间(会议室)
    t60=0.8,                # 强混响
    snr_db=20,              # 低噪声
    seed=42                 # 随机种子
)

# 处理
info = augmentor.process("input.wav", "output.wav")

便捷函数

from src.augmentor import process_audio

# 一行代码处理
process_audio(
    input_path="input.wav",
    output_path="output.wav",
    room_sz=(4, 3, 2.5),
    t60=0.3,
    snr_db=10,
    seed=42
)

完整示例

import sys
sys.path.insert(0, 'src')

from augmentor import AudioAugmentor

# 1. 创建增强器
augmentor = AudioAugmentor(
    room_sz=(5.0, 4.0, 3.0),  # 普通房间
    t60=0.5,                   # 中等混响
    snr_db=15,                 # 中等噪声
    seed=42
)

# 2. 处理多个文件
files = ["audio1.wav", "audio2.wav", "audio3.wav"]
for i, f in enumerate(files):
    output = f"output_{i}.wav"
    info = augmentor.process(f, output)
    print(f"处理完成: {output}, T60={info['t60']}s")

参数调节指南

房间尺寸 (--room)

模拟不同场景:

场景 尺寸 (米) 示例
小房间 3×3×2.5 卧室、小办公室
普通房间 5×4×3 普通办公室
大房间 10×8×3.5 会议室、教室
大厅 20×15×5 礼堂、体育馆

混响时间 (--t60)

T60 表示声音衰减 60dB 所需时间:

T60 值 环境描述
0.1-0.3s 消声室、吸音好的房间
0.3-0.5s 普通办公室、客厅
0.5-0.8s 会议室、教室
0.8-1.2s 教堂、音乐厅
>1.2s 体育馆、大礼堂

信噪比 (--snr)

SNR 越高,噪声越小:

SNR (dB) 噪声程度 适用场景
5-10 重噪声 嘈杂街道、工地
10-15 中等噪声 办公室、咖啡厅
15-20 轻噪声 安静室内
20-30 几乎无噪声 录音棚

项目结构

gpurir/
├── README.md              # 本文件
├── requirements.txt       # Python 依赖
├── install.sh            # 一键安装脚本
├── activate_gpurir.sh    # 环境激活脚本(安装后生成)
├── scripts/
│   ├── add_reverb.py     # 单文件处理工具
│   └── batch_process.py  # 批量处理工具
├── src/
│   ├── __init__.py
│   └── augmentor.py      # 核心功能模块
├── examples/
│   └── demo.py           # 使用示例
└── tests/                # 测试文件(待添加)

常见问题

Q1: 安装时提示 "No CMAKE_CUDA_COMPILER could be found"

解决:CUDA 未正确安装或配置

# 检查 CUDA
nvcc --version

# 如果没有输出,手动设置环境变量
export PATH=/usr/local/cuda/bin:$PATH
export CUDACXX=/usr/local/cuda/bin/nvcc

# 重新运行安装
./install.sh

Q2: 运行时报错 "No module named 'gpuRIR'"

解决:环境未激活

# 激活环境
source activate_gpurir.sh

# 或手动激活
conda activate gpurir

Q3: GPU 内存不足 (OOM)

解决:减小 RIR 长度或分批处理

# 减小 T60 值(减少 RIR 长度)
augmentor = AudioAugmentor(t60=0.3)  # 默认 0.5

# 批量处理时减少并发(如果有)

Q4: 如何处理外部噪声文件?

当前版本使用白噪声。如需使用外部噪声,可以修改 src/augmentor.py 中的 _add_noise 方法:

def _add_noise(self, x, noise_file=None):
    if noise_file:
        noise, _ = sf.read(noise_file, dtype="float32")
        # ... 裁剪/循环噪声以匹配长度
    else:
        noise = self.rng.standard_normal(len(x)).astype(np.float32)
    # ... 后续处理

Q5: 如何调整声源和麦克风位置?

当前版本随机放置。如需固定位置,修改 src/augmentor.py

# 在 __init__ 中添加位置参数
self.pos_mic = pos_mic  # [x, y, z]
self.pos_src = pos_src  # [x, y, z]

# 在 _generate_rir 中使用固定位置
pos_mic = self.pos_mic
pos_src = self.pos_src

性能对比

在处理 10 秒音频(16kHz 采样率)时:

设备 处理时间 说明
CPU (Intel i7) ~30s 纯 CPU 计算
GPU (RTX 3090) ~0.5s 使用本工具
GPU (RTX 2080) ~1s 使用本工具

约 30-60 倍加速!

相关项目

引用

如果本项目对你的研究有帮助,请引用:

@article{diaz2020gpurir,
  title={gpuRIR: A python library for room impulse response simulation with GPU acceleration},
  author={Diaz-Guerra, David and Miguel, Alfonso and Beltran, Jose R},
  journal={Multimedia Tools and Applications},
  year={2020}
}

许可证

本项目采用 MIT 许可证。gpuRIR 库采用 AGPL-3.0 许可证。

更新日志

v1.0.0 (2024-01-30)

  • ✨ 初始版本发布
  • 📦 支持单文件和批量处理
  • 📝 完整的文档和示例
  • 🚀 一键安装脚本

联系与支持

如有问题或建议,欢迎提交 Issue 或 Pull Request。


Happy Audio Augmenting! 🎵

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors