-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasr_circle.py
67 lines (57 loc) · 1.59 KB
/
asr_circle.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 9 23:24:31 2019
@author: apple
"""
import pyaudio
import viVoicecloud as vv
#from sjtu.audio import findDevice
def findDevice(name, type):
p = pyaudio.PyAudio()
num = p.get_device_count()
for i in range(0,num):
device = p.get_device_info_by_index(i)
if(device['maxInputChannels'] > 0 and type == 'input'
and device['name'] == name):
return device['index']
elif(device['maxOutputChannels'] > 0 and type == 'output'
and device['name'] == name):
return device['index']
device_in = findDevice("ac108", "input")
Sample_channels = 1
Sample_rate = 16000
Sample_width = 2
time_seconds = 0.5
p = pyaudio.PyAudio()
stream = p.open(
rate = Sample_rate,
format = p.get_format_from_width(Sample_width),
channels = Sample_channels,
input = True,
input_device_index = device_in,
start = False)
vv.Login()
ASR = vv.asr()
while(1):
try:
ASR.SessionBegin(language = 'Chinese')
stream.start_stream()
print('***Listening')
status = 0
while status!= 3:
frames = stream.read(int(Sample_rate*time_seconds), exception_on_overflow = False)
ret, status, recStatus = ASR.AudioWrite(frames)
# print('continuing...')
stream.stop_stream()
print('--GetResult...')
words = ASR.GetResult()
ASR.SessionEnd()
print(words)
except Exception as e:
print(e)
print('stopped')
vv.Logout()
stream.close()
p.terminate()
break