mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-19 21:38:18 +08:00
feat: new voice class pytts
This commit is contained in:
@@ -5,7 +5,6 @@ google voice service
|
||||
|
||||
import time
|
||||
import speech_recognition
|
||||
import pyttsx3
|
||||
from gtts import gTTS
|
||||
from bridge.reply import Reply, ReplyType
|
||||
from common.log import logger
|
||||
@@ -15,21 +14,11 @@ from voice.voice import Voice
|
||||
|
||||
class GoogleVoice(Voice):
|
||||
recognizer = speech_recognition.Recognizer()
|
||||
engine = pyttsx3.init()
|
||||
|
||||
def __init__(self):
|
||||
# 语速
|
||||
self.engine.setProperty('rate', 125)
|
||||
# 音量
|
||||
self.engine.setProperty('volume', 1.0)
|
||||
# 0为男声,1为女声
|
||||
voices = self.engine.getProperty('voices')
|
||||
self.engine.setProperty('voice', voices[1].id)
|
||||
pass
|
||||
|
||||
def voiceToText(self, voice_file):
|
||||
# new_file = voice_file.replace('.mp3', '.wav')
|
||||
# subprocess.call('ffmpeg -i ' + voice_file +
|
||||
# ' -acodec pcm_s16le -ac 1 -ar 16000 ' + new_file, shell=True)
|
||||
with speech_recognition.AudioFile(voice_file) as source:
|
||||
audio = self.recognizer.record(source)
|
||||
try:
|
||||
@@ -46,8 +35,6 @@ class GoogleVoice(Voice):
|
||||
def textToVoice(self, text):
|
||||
try:
|
||||
mp3File = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
|
||||
# self.engine.save_to_file(text, textFile)
|
||||
# self.engine.runAndWait()
|
||||
tts = gTTS(text=text, lang='zh')
|
||||
tts.save(mp3File)
|
||||
logger.info(
|
||||
|
||||
@@ -28,6 +28,3 @@ class OpenaiVoice(Voice):
|
||||
reply = Reply(ReplyType.ERROR, str(e))
|
||||
finally:
|
||||
return reply
|
||||
|
||||
def textToVoice(self, text):
|
||||
pass
|
||||
|
||||
37
voice/pytts/pytts_voice.py
Normal file
37
voice/pytts/pytts_voice.py
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
"""
|
||||
pytts voice service (offline)
|
||||
"""
|
||||
|
||||
import time
|
||||
import pyttsx3
|
||||
from bridge.reply import Reply, ReplyType
|
||||
from common.log import logger
|
||||
from common.tmp_dir import TmpDir
|
||||
from voice.voice import Voice
|
||||
|
||||
|
||||
class PyttsVoice(Voice):
|
||||
engine = pyttsx3.init()
|
||||
|
||||
def __init__(self):
|
||||
# 语速
|
||||
self.engine.setProperty('rate', 125)
|
||||
# 音量
|
||||
self.engine.setProperty('volume', 1.0)
|
||||
for voice in self.engine.getProperty('voices'):
|
||||
if "Chinese" in voice.name:
|
||||
self.engine.setProperty('voice', voice.id)
|
||||
|
||||
def textToVoice(self, text):
|
||||
try:
|
||||
mp3File = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
|
||||
self.engine.save_to_file(text, mp3File)
|
||||
self.engine.runAndWait()
|
||||
logger.info(
|
||||
'[Pytts] textToVoice text={} voice file name={}'.format(text, mp3File))
|
||||
reply = Reply(ReplyType.VOICE, mp3File)
|
||||
except Exception as e:
|
||||
reply = Reply(ReplyType.ERROR, str(e))
|
||||
finally:
|
||||
return reply
|
||||
@@ -17,4 +17,7 @@ def create_voice(voice_type):
|
||||
elif voice_type == 'openai':
|
||||
from voice.openai.openai_voice import OpenaiVoice
|
||||
return OpenaiVoice()
|
||||
elif voice_type == 'pytts':
|
||||
from voice.pytts.pytts_voice import PyttsVoice
|
||||
return PyttsVoice()
|
||||
raise RuntimeError
|
||||
|
||||
Reference in New Issue
Block a user