-
Notifications
You must be signed in to change notification settings - Fork 0
/
asr_once.py
61 lines (52 loc) · 1.4 KB
/
asr_once.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
#!/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")
#print(device_in)
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()
ASR.SessionBegin(language = 'Chinese')
stream.start_stream()
print('***Listening')
status = 0
while status!= 3:
frames = stream.read(int(Sample_rate*time_seconds))
ret, status, recStatus = ASR.AudioWrite(frames)
stream.stop_stream()
print('--GetResult...')
print(recStatus)
words = ASR.GetResult()
ASR.SessionEnd()
print(words)
vv.Logout()
stream.close()
p.terminate()