-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtts.py
99 lines (75 loc) · 2.75 KB
/
tts.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-
import time
from ctypes import *
from io import BytesIO
import wave
import platform
import logging
import os
logging.basicConfig(level=logging.DEBUG)
BASEPATH=os.path.split(os.path.realpath(__file__))[0]
def play(filename):
import pygame
pygame.mixer.init(frequency=16000)
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
def saveWave(raw_data,_tmpFile = 'test.wav'):
f = wave.open(_tmpFile,'w')
f.setparams((1, 2, 16000, 262720, 'NONE', 'not compressed'))
f.writeframesraw(raw_data)
f.close()
return _tmpFile
def text_to_speech(src_text="这不仅仅是一个测试",file_name = None):
plat = platform.architecture()
if plat[0] == '32bit':
cur = cdll.LoadLibrary(BASEPATH + '/x86/libmsc.so')
else:
cur = cdll.LoadLibrary(BASEPATH + '/x64/libmsc.so')
MSPLogin = cur.MSPLogin
QTTSSessionBegin = cur.QTTSSessionBegin
QTTSTextPut = cur.QTTSTextPut
QTTSAudioGet = cur.QTTSAudioGet
QTTSAudioGet.restype = c_void_p
QTTSSessionEnd = cur.QTTSSessionEnd
ret_c = c_int(0)
ret = 0
ret = MSPLogin(None,None,'appid = 539feff8, work_dir = .')
if ret != 0:
logging.error("MSPLogin failed, error code: " + ret);
return
session_begin_params="voice_name = xiaoyan, text_encoding = utf8, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2"
sessionID = QTTSSessionBegin(session_begin_params, byref(ret_c));
if ret_c.value != 0 :
logging.error("QTTSSessionBegin failed, error code: " + ret_c.value);
return
ret = QTTSTextPut(sessionID, src_text, len(src_text),None)
if ret != 0:
logging.error("QTTSTextPut failed, error code: " + ret);
QTTSSessionEnd(sessionID, "TextPutError");
return
logging.info("正在合成 [%s]..." %(src_text))
audio_len = c_uint(0)
synth_status = c_int(0)
f = BytesIO()
while True:
p = QTTSAudioGet(sessionID, byref(audio_len), byref(synth_status), byref(ret_c));
if ret_c.value != 0:
logging.error("QTTSAudioGet failed, error code: " + ret_c);
QTTSSessionEnd(sessionID, "AudioGetError");
break
if p != None:
buf = (c_char * audio_len.value).from_address(p)
f.write(buf)
if synth_status.value == 2 :
saveWave(f.getvalue(),file_name)
break
logging.debug(".")
time.sleep(1)
logging.info('合成完成!')
ret = QTTSSessionEnd(sessionID, "Normal");
if ret != 0:
logging.error("QTTSTextPut failed, error code: " + ret);
if __name__ == '__main__':
text_to_speech('科大讯飞还是不错的','xx.wav')