mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-06-02 00:57:41 +08:00
[voice] add text to voice
This commit is contained in:
@@ -4,23 +4,47 @@ google voice service
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import speech_recognition
|
||||
import time
|
||||
import speech_recognition
|
||||
import pyttsx3
|
||||
from common.log import logger
|
||||
from voice.voice import Voice
|
||||
|
||||
|
||||
class GoogleVoice(Voice):
|
||||
tmpFilePath = './tmp/'
|
||||
recognizer = speech_recognition.Recognizer()
|
||||
engine = pyttsx3.init()
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
# 语速
|
||||
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)
|
||||
|
||||
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)
|
||||
subprocess.call('ffmpeg -i ' + voice_file +
|
||||
' -acodec pcm_s16le -ac 1 -ar 16000 ' + new_file, shell=True)
|
||||
with speech_recognition.AudioFile(new_file) as source:
|
||||
audio = self.recognizer.record(source)
|
||||
try:
|
||||
return self.recognizer.recognize_google(audio, language='zh-CN')
|
||||
text = self.recognizer.recognize_google(audio, language='zh-CN')
|
||||
logger.info(
|
||||
'[Google] voiceToText text={} voice file name={}'.format(text, voice_file))
|
||||
return text
|
||||
except speech_recognition.UnknownValueError:
|
||||
return "抱歉,我听不懂。"
|
||||
except speech_recognition.RequestError as e:
|
||||
return "抱歉,无法连接到 Google 语音识别服务;{0}".format(e)
|
||||
|
||||
def textToVoice(self, text):
|
||||
textFile = self.tmpFilePath + '语音回复_' + str(int(time.time())) + '.mp3'
|
||||
self.engine.save_to_file(text, textFile)
|
||||
self.engine.runAndWait()
|
||||
logger.info(
|
||||
'[Google] textToVoice text={} voice file name={}'.format(text, textFile))
|
||||
return textFile
|
||||
|
||||
@@ -8,3 +8,9 @@ class Voice(object):
|
||||
Send voice to voice service and get text
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def textToVoice(self, text):
|
||||
"""
|
||||
Send text to voice service and get voice
|
||||
"""
|
||||
raise NotImplementedError
|
||||
Reference in New Issue
Block a user