-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathplay.py
35 lines (32 loc) · 988 Bytes
/
play.py
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
import sounddevice as sd
import numpy as np
import wave
from config import config
import os
from loguru import logger
flag=0
def play(filename, volume=config.get("general_volume"), samplerate=16000):
global flag
if flag == 1:
return
flag=1
_, ext = os.path.splitext(filename)
# 如果文件是WAV格式
if ext.lower() == ".wav":
with wave.open(filename, 'rb') as wf:
samples = np.frombuffer(wf.readframes(wf.getnframes()), dtype=np.int16)
# 如果文件是raw PCM格式
elif ext.lower() == ".raw":
samples = np.fromfile(filename, dtype=np.int16)
samplerate=24000
else:
raise ValueError("Unsupported file type!")
# 调整音量
samples = (samples * volume).astype(np.int16)
# 播放音频
try:
sd.play(samples, samplerate=samplerate)
sd.wait()
except Exception as e:
logger.warning(f"Error occurred while playing {filename}: {e}")
flag=0