mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-19 13:28:11 +08:00
fix: voice length bug in wechaty
This commit is contained in:
@@ -72,7 +72,7 @@ class WechatyChannel(ChatChannel):
|
|||||||
voiceLength = None
|
voiceLength = None
|
||||||
file_path = reply.content
|
file_path = reply.content
|
||||||
sil_file = os.path.splitext(file_path)[0] + '.sil'
|
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())
|
t = int(time.time())
|
||||||
msg = FileBox.from_file(sil_file, name=str(t) + '.sil')
|
msg = FileBox.from_file(sil_file, name=str(t) + '.sil')
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ def find_closest_sil_supports(sample_rate):
|
|||||||
for rate in sil_supports:
|
for rate in sil_supports:
|
||||||
diff = abs(rate - sample_rate)
|
diff = abs(rate - sample_rate)
|
||||||
if diff < mindiff:
|
if diff < mindiff:
|
||||||
closest = sample_rate
|
closest = rate
|
||||||
mindiff = diff
|
mindiff = diff
|
||||||
return closest
|
return closest
|
||||||
|
|
||||||
@@ -68,8 +68,9 @@ def pcm_to_sil(pcm_path, silk_path):
|
|||||||
"""
|
"""
|
||||||
audio = AudioSegment.from_wav(pcm_path)
|
audio = AudioSegment.from_wav(pcm_path)
|
||||||
wav_data = audio.raw_data
|
wav_data = audio.raw_data
|
||||||
|
rate = find_closest_sil_supports(audio.frame_rate)
|
||||||
silk_data = pysilk.encode(
|
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:
|
with open(silk_path, "wb") as f:
|
||||||
f.write(silk_data)
|
f.write(silk_data)
|
||||||
return audio.duration_seconds * 1000
|
return audio.duration_seconds * 1000
|
||||||
@@ -82,7 +83,8 @@ def mp3_to_sil(mp3_path, silk_path):
|
|||||||
"""
|
"""
|
||||||
audio = AudioSegment.from_mp3(mp3_path)
|
audio = AudioSegment.from_mp3(mp3_path)
|
||||||
wav_data = audio.raw_data
|
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
|
# Save the silk file
|
||||||
with open(silk_path, "wb") as f:
|
with open(silk_path, "wb") as f:
|
||||||
f.write(silk_data)
|
f.write(silk_data)
|
||||||
|
|||||||
Reference in New Issue
Block a user