-
Notifications
You must be signed in to change notification settings - Fork 0
/
leiyou.py
82 lines (68 loc) · 1.71 KB
/
leiyou.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
# -*- coding: utf-8 -*-
import os
import thread
from pyaudio import PyAudio, paInt16
import numpy as np
import wave
import pylab as pl
NUM_SAMPLES = 4000 # 内部缓存的块的大小
SAMPLING_RATE = 8000 # 取样频率
THRESHOLD = 800 # THRESHOLD value for dy/dx
MAXQUEUE = 6
MAXDELTA = 0.08
event = []
def push(v):
event.append(v)
if (len(event) > MAXQUEUE):
del event[0]
def play():
os.system("aplay shuia.wav")
def main():
# 开启声音输入
pa = PyAudio()
stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True,
frames_per_buffer=NUM_SAMPLES);
time = np.arange(0, NUM_SAMPLES) * (1.0/SAMPLING_RATE);
delta = 0
while True:
print "delta = ", delta
delta += NUM_SAMPLES * (1.0/SAMPLING_RATE)
# 读入NUM_SAMPLES个取样
string_audio_data = stream.read(NUM_SAMPLES)
# 将读入的数据转换为数组
audio = np.fromstring(string_audio_data, dtype=np.short)
curr = []
for i in range(1, len(time)):
dy = audio[i] - audio[i-1]
if (abs(dy) > THRESHOLD):
# print abs(dy)
curr.append(delta + time[i])
blk_sum = 0
blk_num = 0
for i in range(1, len(curr)):
if (curr[i] - curr[i-1] < MAXDELTA):
blk_sum += curr[i]
blk_num += 1
else:
if (blk_num > 60):
print "blk::sum, num = ", blk_sum/blk_num, blk_num
push(blk_sum/blk_num)
blk_sum = 0
blk_num = 0
if (blk_num > 60):
print "blk::sum, num = ", blk_sum/blk_num, blk_num
push(blk_sum/blk_num)
cnt = 0
for i in range(1, len(event)):
if (event[i] - event[i-1] < 0.4):
cnt += 1
else:
cnt = 0
if (cnt >= 2):
thread.start_new_thread(play, ())
print "-- shuia"
del event[0:i+1]
break
print event
if __name__ == '__main__':
main()