-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmbkv.py
40 lines (28 loc) · 815 Bytes
/
mbkv.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
import pyaudio
import numpy as np
import os
# Number of data points to read at a time
CHUNK = 2**11
# Time seolution of the recording device (Hz)
RATE = 44100
# Start the PyAudio class
p=pyaudio.PyAudio()
# Uses the default input device
stream=p.open(format=pyaudio.paInt16,channels=1,rate=RATE,input=True,
frames_per_buffer=CHUNK)
while True:
data = np.fromstring(stream.read(CHUNK),dtype=np.int16)
peak=np.average(np.abs(data))*2
v = int(50*peak/2**16)
# Just printing the value of v so that I can make this script
# more accurate.
print(v)
if v == 0:
os.system("kbdlight set 0")
elif v < 8 and v > 0:
os.system("kbdlight set 127")
else:
os.system("kbdlight max")
stream.stop_stream()
stream.close()
p.terminate()