From dfb2e460b43eed88188af0791486a948ec061405 Mon Sep 17 00:00:00 2001 From: lanvent Date: Sat, 1 Apr 2023 21:58:55 +0800 Subject: [PATCH] fix: voice length bug in wechaty --- channel/wechat/wechaty_channel.py | 2 +- voice/audio_convert.py | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/channel/wechat/wechaty_channel.py b/channel/wechat/wechaty_channel.py index 2b2c044..82c4f01 100644 --- a/channel/wechat/wechaty_channel.py +++ b/channel/wechat/wechaty_channel.py @@ -72,7 +72,7 @@ class WechatyChannel(ChatChannel): voiceLength = None file_path = reply.content sil_file = os.path.splitext(file_path)[0] + '.sil' - voiceLength = any_to_sil(file_path, sil_file) + voiceLength = int(any_to_sil(file_path, sil_file)) # 发送语音 t = int(time.time()) msg = FileBox.from_file(sil_file, name=str(t) + '.sil') diff --git a/voice/audio_convert.py b/voice/audio_convert.py index b6f9d5a..5528099 100644 --- a/voice/audio_convert.py +++ b/voice/audio_convert.py @@ -15,7 +15,7 @@ def find_closest_sil_supports(sample_rate): for rate in sil_supports: diff = abs(rate - sample_rate) if diff < mindiff: - closest = sample_rate + closest = rate mindiff = diff return closest @@ -68,8 +68,9 @@ def pcm_to_sil(pcm_path, silk_path): """ audio = AudioSegment.from_wav(pcm_path) wav_data = audio.raw_data + rate = find_closest_sil_supports(audio.frame_rate) silk_data = pysilk.encode( - wav_data, data_rate=audio.frame_rate, sample_rate=find_closest_sil_supports(audio.frame_rate)) + wav_data, data_rate=rate, sample_rate=rate) with open(silk_path, "wb") as f: f.write(silk_data) return audio.duration_seconds * 1000 @@ -82,7 +83,8 @@ def mp3_to_sil(mp3_path, silk_path): """ audio = AudioSegment.from_mp3(mp3_path) wav_data = audio.raw_data - silk_data = pysilk.encode(wav_data, data_rate=audio.frame_rate, sample_rate=find_closest_sil_supports(audio.frame_rate)) + rate = find_closest_sil_supports(audio.frame_rate) + silk_data = pysilk.encode(wav_data, data_rate=rate, sample_rate=rate) # Save the silk file with open(silk_path, "wb") as f: f.write(silk_data)