-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_utils.py
More file actions
63 lines (51 loc) · 2.09 KB
/
audio_utils.py
File metadata and controls
63 lines (51 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import librosa
import numpy as np
import soundfile as sf
from scipy.io import wavfile
import sys
import sounddevice as sd
from logger import logger
from retrying import retry
def format_folder_name(folder_name):
folder_name = folder_name.strip(). \
replace("?", "").replace("*", "").replace("<", ""). \
replace(",", " ").replace(".", "").replace(";", ""). \
replace(":", "").replace(">", "").replace("|", "").replace("\"", "")
return folder_name
def resample_audio(audio_path, sr=16000):
y, sr = librosa.load(audio_path, sr=sr)
sf.write(audio_path, y, sr)
# def play_audio(audio_file, fs_multi=1):
# # 音量的放大倍数
# logger.info("Playing audio file: {}".format(audio_file))
# sps, audio = wavfile.read(audio_file)
# sd.play(audio, sps*fs_multi, blocking=True) # Samples per second
def play_audio(audio_file, fs_multi=1.0, gain=1.5, gain_db=None):
"""
播放音频文件,可调整采样率和增益。
参数:
audio_file (str): 音频文件路径。
fs_multi (float, 可选): 采样率倍数,默认1.0。
gain (float, 可选): 增益倍数,默认1.0。
gain_db (float, 可选): 增益 dB 值,默认None。
"""
try:
sps, audio = wavfile.read(audio_file)
if np.issubdtype(audio.dtype, np.integer):
x = audio.astype(np.float32) / np.iinfo(audio.dtype).max
else:
x = audio.astype(np.float32)
except ValueError:
# wavfile 只支持 RIFF WAV,FLAC 等格式交给 soundfile 处理
x, sps = sf.read(audio_file, dtype="float32")
# 增益:优先用 dB
if gain_db is not None:
gain = 10 ** (gain_db / 20.0)
y = np.clip(x * float(gain), -1.0, 1.0) # 简单限幅避免削顶
sd.play(y, int(sps * fs_multi), blocking=True)
@retry(stop_max_attempt_number=3, wait_fixed=1000)
def write_audio(audio_file, audio):
wavfile.write(audio_file, audio[0], audio[1])
if __name__=="__main__":
audio_file = "X:\BaiduNetdiskDownload\\0001_What's_your_name.wav"
play_audio(audio_file)