-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
107 lines (87 loc) · 2.43 KB
/
test.py
File metadata and controls
107 lines (87 loc) · 2.43 KB
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
import alsaaudio, time, audioop
import aubio
# from struct import *
# from scipy import *
# import pyaudio
samplerate = 8000
win_s = 2048
hop_s = win_s // 2
framesize = hop_s
card = 'sysdefault:CARD=Microphone'
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK, 'sysdefault:CARD=1')
inp.setchannels(0)
inp.setrate(samplerate)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(framesize)
max_value = 0
pitcher = aubio.pitch("default", win_s, hop_s, samplerate)
pitcher.set_unit("Hz")
pitcher.set_silence(-40)
while True:
try:
_, data = recorder.read()
samples = np.fromstring(data, dtype=aubio.float_type)
freq = pitcher(samples)[0]
energy = np.sum(samples**2)/len(samples)
print("{:10.4f}{:10.4f}".format(freq, energy))
except KeyboardInterrupt:
print("exit")
break
"""
while True:
# Read data from device
l,data = inp.read()
if l:
# Return the maximum of the absolute value of all samples in a fragment.
#print(unpack('hhl',data))
a = audioop.max(data,2)
if a > max_value:
print(a)
max_value = a
#print(audioop.max(data, 2))
time.sleep(.001)
a = 0
def design_filter(lowcut, highcut, fs, order=3):
nyq = 0.5*fs
low = lowcut/nyq
high = highcut/nyq
global a
b,a = butter(order, [low,high], btype='band')
return b,a
def normalize(block):
global a
count = len(block)/2/a
format = "%dh"%(count)
shorts = struct.unpack( format, block )
doubles = [x * SHORT_NORMALIZE for x in shorts]
return doubles
def get_rms(samples):
sum_squares = 0.0
for sample in doubles:
sum_squares += n*n
return math.sqrt( sum_squares / count )
pa = pyaudio.PyAudio()
stream = pa.open(format = pyaudio.paInt16,
channels = 1,
rate = 8000,
input = True,
frames_per_buffer = 160)
errorcount = 0
# design the filter
b,a = design_filter(19400, 19600, 48000, 3)
# compute the initial conditions.
zi = lfilter_zi(b, a)
for i in range(1000):
try:
block = stream.read(INPUT_FRAMES_PER_BLOCK)
except:
errorcount += 1
print( "(%d) Error recording: %s"%(errorcount,e) )
noisycount = 1
samples = normalize(block)
bandpass_samples,zi = lfilter(b, a, samples, zi)
amplitude = get_rms(samples)
bandpass_ampl = get_rms(bandpass_samples)
print(amplitude)
print(bandpass_ampl)
"""