-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.py
77 lines (71 loc) · 2.51 KB
/
test2.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
import alsaaudio, time, audioop
import aubio
import numpy as np
import matplotlib.pyplot as plt
samplerate = 16000 #8000
win_s = 2048
hop_s = win_s // 2
framesize = hop_s
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, device='hw:1')
#'sysdefault:CARD=1')
# ,alsaaudio.PCM_NONBLOCK,device='sysdefault:CARD=1') #cardindex=1)
inp.setchannels(1)
inp.setrate(samplerate)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE) #used to be S16 not float
inp.setperiodsize(framesize)
#inp.setperiodsize(1600)
#recorder = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, cardindex=1)
#recorder.setperiodsize(framesize)
#recorder.setrate(samplerate)
#recorder.setformat(alsaaudio.PCM_FORMAT_FLOAT_LE)
#recorer.setchannels(1)
pitcher = aubio.pitch("default", win_s, hop_s, samplerate)
pitcher.set_unit("Hz")
#pitcher.set_silence(-40)
plt.ion()
while True:
try:
_, data = inp.read()
#print(data)
samples = np.fromstring(data, dtype=np.int16) #dtype=aubio.float_type)
if len(samples) == 0:
continue
w = np.fft.fft(samples)
freqs = np.fft.fftfreq(len(samples))
frate = samplerate
freq_to_strength = np.column_stack((np.transpose(list(map(lambda x: abs(x*frate), freqs))), np.transpose(np.abs(w))))
fts = freq_to_strength[freq_to_strength[:,0].argsort()]
plt.clf()
plt.plot(fts[200:7000,:1], fts[200:7000,1:2])
#print(samples[10:20])
#print("len samples != 0")
#freq = pitcher(samples)[0]
#energy = np.sum(samples**2)/len(samples)
#print(freq)
#print(energy)
#print("{:10.4f}{:10.4f}".format(freq, energy))
#print("got to max")
#print(data)
length = len(data)
#print(audioop.max(data[0:length//3], 2), audioop.max(data[length//3:2*length//3+1],2), audioop.max(data[2*length//3:length-1],2))
#print(samples[20:30])
count = 0
new_samp = list(filter(lambda a: a, samples))
print(len(new_samp))
print(new_samp[100:200])
#freq = pitcher(new_samp)[0]
#energy = np.sum(new_samp**2)/len(new_samp)
#print(freq, energy)
#print(np.fft.rfft(samples))
#print("got np")
print('plotting')
# plt.plot(new_samp)
#plt.draw()
#print(abs(audioop.max(data[0:length//3],2) - audioop.max(data[length//3:2*length//3+1],2)))
except KeyboardInterrupt:
plt.show(block = True)
break
except ValueError:
pass
plt.pause(0.01)
time.sleep(0.01)