mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-21 06:07:13 +08:00
fix: get correct audio format in pytts
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import shutil
|
||||
import wave
|
||||
import pysilk
|
||||
from pydub import AudioSegment
|
||||
|
||||
|
||||
def get_pcm_from_wav(wav_path):
|
||||
"""
|
||||
从 wav 文件中读取 pcm
|
||||
@@ -13,6 +13,30 @@ def get_pcm_from_wav(wav_path):
|
||||
wav = wave.open(wav_path, "rb")
|
||||
return wav.readframes(wav.getnframes())
|
||||
|
||||
def any_to_wav(any_path, wav_path):
|
||||
"""
|
||||
把任意格式转成wav文件
|
||||
"""
|
||||
if any_path.endswith('.wav'):
|
||||
shutil.copy2(any_path, wav_path)
|
||||
return
|
||||
if any_path.endswith('.sil') or any_path.endswith('.silk') or any_path.endswith('.slk'):
|
||||
return sil_to_wav(any_path, wav_path)
|
||||
audio = AudioSegment.from_file(any_path)
|
||||
audio.export(wav_path, format="wav")
|
||||
|
||||
def any_to_sil(any_path, sil_path):
|
||||
"""
|
||||
把任意格式转成sil文件
|
||||
"""
|
||||
if any_path.endswith('.sil') or any_path.endswith('.silk') or any_path.endswith('.slk'):
|
||||
shutil.copy2(any_path, sil_path)
|
||||
return 10000
|
||||
if any_path.endswith('.wav'):
|
||||
return pcm_to_sil(any_path, sil_path)
|
||||
if any_path.endswith('.mp3'):
|
||||
return mp3_to_sil(any_path, sil_path)
|
||||
raise NotImplementedError("Not support file type: {}".format(any_path))
|
||||
|
||||
def mp3_to_wav(mp3_path, wav_path):
|
||||
"""
|
||||
@@ -21,18 +45,7 @@ def mp3_to_wav(mp3_path, wav_path):
|
||||
audio = AudioSegment.from_mp3(mp3_path)
|
||||
audio.export(wav_path, format="wav")
|
||||
|
||||
def any_to_wav(any_path, wav_path):
|
||||
"""
|
||||
把任意格式转成wav文件
|
||||
"""
|
||||
if any_path.endswith('.wav'):
|
||||
return
|
||||
if any_path.endswith('.sil') or any_path.endswith('.silk') or any_path.endswith('.slk'):
|
||||
return sil_to_wav(any_path, wav_path)
|
||||
audio = AudioSegment.from_file(any_path)
|
||||
audio.export(wav_path, format="wav")
|
||||
|
||||
def pcm_to_silk(pcm_path, silk_path):
|
||||
def pcm_to_sil(pcm_path, silk_path):
|
||||
"""
|
||||
wav 文件转成 silk
|
||||
return 声音长度,毫秒
|
||||
@@ -60,7 +73,6 @@ def mp3_to_sil(mp3_path, silk_path):
|
||||
f.write(silk_data)
|
||||
return audio.duration_seconds * 1000
|
||||
|
||||
|
||||
def sil_to_wav(silk_path, wav_path, rate: int = 24000):
|
||||
"""
|
||||
silk 文件转 wav
|
||||
|
||||
@@ -56,7 +56,7 @@ class AzureVoice(Voice):
|
||||
return reply
|
||||
|
||||
def textToVoice(self, text):
|
||||
fileName = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
|
||||
fileName = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.wav'
|
||||
audio_config = speechsdk.AudioConfig(filename=fileName)
|
||||
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=self.speech_config, audio_config=audio_config)
|
||||
result = speech_synthesizer.speak_text(text)
|
||||
|
||||
@@ -25,12 +25,12 @@ class PyttsVoice(Voice):
|
||||
|
||||
def textToVoice(self, text):
|
||||
try:
|
||||
mp3File = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
|
||||
self.engine.save_to_file(text, mp3File)
|
||||
wavFile = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.wav'
|
||||
self.engine.save_to_file(text, wavFile)
|
||||
self.engine.runAndWait()
|
||||
logger.info(
|
||||
'[Pytts] textToVoice text={} voice file name={}'.format(text, mp3File))
|
||||
reply = Reply(ReplyType.VOICE, mp3File)
|
||||
'[Pytts] textToVoice text={} voice file name={}'.format(text, wavFile))
|
||||
reply = Reply(ReplyType.VOICE, wavFile)
|
||||
except Exception as e:
|
||||
reply = Reply(ReplyType.ERROR, str(e))
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user