-
Notifications
You must be signed in to change notification settings - Fork 0
/
pronunciation.py
160 lines (122 loc) · 3.98 KB
/
pronunciation.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pygame
import pyaudio
import wave
#import openai
#import os
import json
#import requests
#from datetime import datetime as dt
#import time
#import pyttsx3
import asyncio
import websockets
import wave
def record_audio(file_name, current_question):
"""
Record audio file from PC microphone
Until Space key not pressed
:param file_name: name of audio file
:return: Escape key pressed
"""
chunk = 1024
sample_format = pyaudio.paInt16
channels = 1
fs = 16000
p = pyaudio.PyAudio()
print('Recording')
escape = False
stream = p.open(format=sample_format,
channels=channels,
rate=fs,
frames_per_buffer=chunk,
input=True)
frames = []
recording = True
while recording:
data = stream.read(chunk)
frames.append(data)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_ESCAPE:
print('Finished recording')
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(file_name, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
wf.writeframes(b''.join(frames))
wf.close()
recording = False
if event.key == pygame.K_ESCAPE:
escape = True
break
print('Finished recording')
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(file_name, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
wf.writeframes(b''.join(frames))
wf.close()
return escape
def accept_feature_extractor(phrases, accept):
if len(accept) > 1 and accept['text'] != '':
accept_text = str(accept['text'])
"""conf_score = []
for result_rec in accept['result']:
print(
'#',
result_rec['conf'],
result_rec['start'],
result_rec['end'],
result_rec['word']
)
conf_score.append(float(result_rec['conf']))"""
#conf_mid = str(sum(conf_score)/len(conf_score))
#print('=== middle confidence:', conf_mid, '\n')
phrases.append(accept_text)
async def stt(uri, file_name):
async with websockets.connect(uri) as websocket:
phrases = []
wf = wave.open(file_name, "rb")
await websocket.send(
'{ "config" : { "sample_rate" : %d } }' % (wf.getframerate())
)
buffer_size = int(wf.getframerate() * 0.2) # 0.2 seconds of audio
while True:
data = wf.readframes(buffer_size)
if len(data) == 0:
break
await websocket.send(data)
accept = json.loads(await websocket.recv())
accept_feature_extractor(phrases, accept)
await websocket.send('{"eof" : 1}')
accept = json.loads(await websocket.recv())
accept_feature_extractor(phrases, accept)
return ' '.join(phrases)
def string_to_array(result):
return [int(x) for x in result.split(",")]
def main():
with open('config.json', 'r') as f:
config = json.load(f)
# stt server init
stt_server = config['stt_server']
pygame.init()
pygame.display.set_mode((100, 100))
pygame.display.set_caption('Recording')
# conversation
while True:
# record audio
escape = record_audio('user.wav', 0)
# transcribe and receive response
user_text = asyncio.run(stt(stt_server, 'user.wav'))
print('=== user:', user_text)
if escape:
print('Escape pressed')
break
if __name__ == '__main__':
main()