-
Notifications
You must be signed in to change notification settings - Fork 97
/
jamspeak.py
57 lines (53 loc) · 1.67 KB
/
jamspeak.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/9/2 22:52
# @Author : Fandes
# @FileName: jamspeak.py
# @Software: PyCharm
import os
import random
import sys
import time
import requests
try:
import pyttsx3
if sys.platform == "win32":
import pyttsx3.drivers
import pyttsx3.drivers.sapi5
speaker_engine = pyttsx3.init()
except:
e = sys.exc_info()
if "libespeak.so" in e[1].args[0]:
try:
os.system("sudo apt-get install espeak -y")
except Exception as ex:
print("linux下驱动未安装,请尝试使用`sudo apt-get install espeak`命令进行安装",ex)
print(e, __file__)
speaker_engine = None
class Speaker():
def speak(self, text="none", lan="en"):
try:
self.stop()
speaker_engine.say(text)
# speaker_engine.startLoop(False)
speaker_engine.runAndWait()
except:
e = sys.exc_info()
if "libespeak.so" in e[1].args[0]:
try:
os.system("sudo apt-get install espeak -y")
except Exception as ex:
print("linux下驱动未安装,请尝试使用`sudo apt-get install espeak`命令进行安装",ex)
print(e, __file__)
def stop(self):
if speaker_engine is not None and speaker_engine.isBusy():
speaker_engine.stop()
def is_alphabet(self, uchar):
"""判断一个unicode是否是英文字母"""
if (u'\u0041' <= uchar <= u'\u005a') or (u'\u0061' <= uchar <= u'\u007a'):
return True
else:
return False
if __name__ == '__main__':
s = Speaker()
s.speak()