-
Notifications
You must be signed in to change notification settings - Fork 0
/
fe8Voice.py
58 lines (50 loc) · 1.53 KB
/
fe8Voice.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
import sys
import string
import base64
import urllib.request
import urllib.response
import urllib.error
import urllib.parse
import json
import wave
import pyaudio
#音声合成する言葉を入力
n = input(">>>")
#URL
tts_url ="http://rospeex.nict.go.jp/nauth_json/jsServices/VoiceTraSS"
#音声合成
tts_command = { 'method':'speak',
'params':['1.1',
{'language':'ja','text':n,'voiceType':"*",'audioType':"audio/x-wav"}]}
obj_command = json.dumps(tts_command) # string to json object
obj_command = obj_command.encode('utf-8')
req = urllib.request.Request(tts_url, obj_command)
response = urllib.request.urlopen(req)
received = response.read().decode('utf-8') # conv bytes to str by decode()
# extract wav file
obj_received = json.loads(received)
tmp = obj_received['result']['audio'] # extract result->audio
speech = base64.decodestring(tmp.encode('utf-8'))
#.waveで出力
f = open ("out.wav",'wb')
f.write(speech)
f.close
#音声合成の音声データを再生
input_filename = 'out.wav'
buffer_size = 4096
wav_file = wave.open ( input_filename , 'rb' )
p = pyaudio.PyAudio ()
stream = p.open (
format = p.get_format_from_width ( wav_file . getsampwidth ()) ,
channels = wav_file.getnchannels () ,
rate = wav_file.getframerate () ,
output = True
)
remain = wav_file.getnframes ()
while remain > 0:
buf = wav_file.readframes ( min ( buffer_size , remain ))
stream.write ( buf )
remain -= buffer_size
stream.close ()
p.terminate ()
wav_file.close ()