-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundControl.py
91 lines (76 loc) · 2.43 KB
/
soundControl.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
#安装模块
# python -m pip install pypiwin32
# pip install baidu-aip
import os
import wave
from aip import AipSpeech
import pyaudio
import winsound
import win32com.client
import time
app_id = '16787563'
api_key='BWdUiU6W9w45RnvOOGlYIAKQ'
secret_key ='Yg0UZapvh2QYhna1bB2R2mQuaXgFOBWK'
client = AipSpeech(app_id,api_key,secret_key)
framerate = 16000 # 采样率
num_samples = 2000 # 采样点
channels = 1 # 声道
sampwidth = 2 # 采样宽度2bytes
FILEPATH = 'myvoices.wav'
def save_wave_file(filepath, data):
wf = wave.open(filepath, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(sampwidth)
wf.setframerate(framerate)
wf.writeframes(b''.join(data))
wf.close()
# 录音
def my_record():
pa = pyaudio.PyAudio()
# 打开一个新的音频stream
stream = pa.open(format=pyaudio.paInt16, channels=channels,
rate=framerate, input=True, frames_per_buffer=num_samples)
my_buf = [] # 存放录音数据
t = time.time()
print('正在录音...')
while time.time() < t + 5: # 设置录音时间(秒)
# 循环read,每次read 2000frames
string_audio_data = stream.read(num_samples)
my_buf.append(string_audio_data)
print('录音结束.')
save_wave_file(FILEPATH, my_buf)
stream.close()
def listen():
# 读取录音文件
with open(FILEPATH, 'rb') as fp:
voices = fp.read()
try:
# 参数dev_pid:1536普通话(支持简单的英文识别)、1537普通话(纯中文识别)、1737英语、1637粤语、1837四川话、1936普通话远场
result = client.asr(voices, 'wav', 16000, {'dev_pid': 1537, })
# result = CLIENT.asr(get_file_content(path), 'wav', 16000, {'lan': 'zh', })
# print(result)
# print(result['result'][0])
# print(result)
result_text = result["result"][0]
print("you said: " + result_text)
return result_text
except KeyError:
print("KeyError")
def speak(str):
speak_out = win32com.client.Dispatch('SAPI.SPVOICE')
speak_out.Speak(str)
winsound.PlaySound(str, winsound.SND_ASYNC)
speak('你好,我是智能小管家!这个娃娃赶快起床了')
'''
while True:
my_record()
request_str = listen()
if request_str == '计算器。':
os.system("calc")
elif request_str == '关机。':
os.system("shutdown -s -t 60")
elif request_str == '取消关机。':
os.system("shutdown -a")
elif request_str == '退出程序。':
break
'''