Skip to content

Commit de2d58e

Browse files
committed
fix: improve WAV to Tencent Silk conversion by handling sample width and resampling
1 parent 1621218 commit de2d58e

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

astrbot/core/utils/tencent_record_helper.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,18 @@ async def wav_to_tencent_silk(wav_path: str, output_path: str) -> float:
7474
with wave.open(wav_path, "rb") as wav:
7575
rate = wav.getframerate()
7676
channels = wav.getnchannels()
77+
sampwidth = wav.getsampwidth()
7778
pcm_data = wav.readframes(wav.getnframes())
7879

79-
# downmix to mono and resample to 24 kHz -- no external dependency.
80+
# Downmix to mono, resample to 24 kHz if needed, and convert to 16-bit PCM
81+
# (pysilk only accepts 16-bit linear PCM)
8082
if channels == 2:
81-
pcm_data = audioop.tomono(pcm_data, 2, 0.5, 0.5)
83+
pcm_data = audioop.tomono(pcm_data, sampwidth, 0.5, 0.5)
8284
if rate not in _PYSILK_SUPPORTED_RATES:
83-
pcm_data, _ = audioop.ratecv(pcm_data, 2, 1, rate, 24000, None)
85+
pcm_data, _ = audioop.ratecv(pcm_data, sampwidth, 1, rate, 24000, None)
8486
rate = 24000
87+
if sampwidth != 2:
88+
pcm_data = audioop.lin2lin(pcm_data, sampwidth, 2)
8589

8690
input_io = BytesIO(pcm_data)
8791
output_io = BytesIO()

0 commit comments

Comments
 (0)