This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
forked from lingochamp/hack-fdu-2016
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.py
115 lines (99 loc) · 3.1 KB
/
demo.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
# -*- coding: utf-8 -*-
"""
Version: 2.7
Date: 2017-5-6
author: Vast
语音识别服务 python实现
"""
import base64
import websocket
import struct
import wave
import audioop
def downsampleWav(src, dst, inrate=44100, outrate=16000, inchannels=2, outchannels=1):
"""
改变音频文件
:param src:源文件
:param dst: 输出文件
:param inrate: 原采样率
:param outrate: 目标采样率
:param inchannels: 原声道
:param outchannels: 目标声道
:return: Bool
"""
import os
if not os.path.exists(src):
print 'Source not found!'
return False
# if not os.path.exists(os.path.dirname(dst)):
# os.makedirs(os.path.dirname(dst))
try:
s_read = wave.open(src, 'r')
s_write = wave.open(dst, 'w')
except:
print 'Failed to open files!'
return False
s_read = wave.open(src, 'r')
s_write = wave.open(dst, 'w')
n_frames = s_read.getnframes()
data = s_read.readframes(n_frames)
# try:
# converted = audioop.ratecv(data, 2, inchannels, inrate, outrate, None)
# if outchannels == 1:
# converted = audioop.tomono(converted[0], 2, 1, 0)
# except:
# print 'Failed to downsample wav'
# return False
# converted = audioop.ratecv(data, 2, inchannels, inrate, outrate, None)
# if outchannels == 1:
# converted = audioop.tomono(converted[0], 2, 1, 0)
converted = audioop.ratecv(data, 2, inchannels, inrate, outrate, None)
if outchannels == 1:
converted = audioop.tomono(converted[0], 2, 1, 0)
try:
s_write.setparams((outchannels, 2, outrate, 0, 'NONE', 'Uncompressed'))
s_write.writeframes(converted)
except:
print 'Failed to write wav'
return False
try:
s_read.close()
s_write.close()
except:
print 'Failed to close wav files'
return False
return True
def Voice2Text(file):
"""
调用流利说API,把音频文件转换成字符串
:param file: 音频文件,要求wav格式
:return: 字符串
"""
downsampleWav(file,'out.wav')
先更改格式
file='out.wav'
META = """
{
"quality":-1,
"type":"asr"
}
"""
META_BASE64 = base64.standard_b64encode(META)
META_LEN = len(META_BASE64)
EOS = 'EOS'
url = 'wss://rating.llsstaging.com/llcup/stream/upload'
ws = websocket.create_connection(url, subprotocols=["binary"])
ws.send(struct.pack('>L', META_LEN)) # 发送网络序
ws.send(META_BASE64) # 发送META_BASE64
with open(file, 'rb') as f:
ws.send(f.read())
ws.send(base64.standard_b64encode(EOS))
data = ws.recv()
data=data[4:]
print data
#{"status":0,"msg":"","reqId":"","key":"3415fadb2ddc6a8c435e589bafb8583e","result":"d2VsY29tZSB0byBiZWlqaW5n","flag":0}
# print data,'\n',data[data.find('result')+9:data.find('flag":')-3]
# print base64.b64decode(data[data.find('result')+9:data.find('flag":')-3])
return base64.b64decode(data[data.find('result')+9:data.find('flag":')-3])
# if __name__ == '__main__':
# Voice2Text("")