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
|
#!/usr/bin/env python3
import random
import pyaudio
import audioop
import paramiko
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 96000 #44100
INPUT_BLOCK_TIME = 0.5 # seconds
INPUT_FRAMES_PER_BLOCK = int(RATE * INPUT_BLOCK_TIME)
INPUT_INDEX = None #3 # JACK
SERVER = "192.168.2.61"
USER = "root"
PASSWORD = "root"
COMMAND = "irsend SEND_ONCE led %s"
ON = "AN"
MAP = [
'WEISS',
'ROT',
'ROT1',
'ROT2',
'ROT3',
'GRUEN',
'GRUEN1',
'GRUEN2',
'GRUEN3',
'BLAU',
'BLAU1',
'BLAU2',
'BLAU3'
]
SEND_THRESHOLD = 0 #len(MAP) / 6
def main():
history = []
pa = pyaudio.PyAudio()
stream = pa.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
input_device_index=INPUT_INDEX,
frames_per_buffer=INPUT_FRAMES_PER_BLOCK)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # aww
ssh.connect(SERVER, username=USER, password=PASSWORD)
stdin, stdout, stderr = ssh.exec_command(COMMAND % ON)
stdout.readlines() # block
lastIndex = -1
while True:
block = stream.read(INPUT_FRAMES_PER_BLOCK)
score = audioop.rms(block, 2)
lower = history[0 : int(len(history) / 3)]
average = history[int(len(history) / 3) : int(len(history) / 3 * 2)]
upper = history[int(len(history) / 3 * 2) : len(history)]
if len(lower) != 0 and len(average) != 0 and len(upper) != 0:
lowerAvg = sum(lower) / len(lower)
averageAvg = sum(average) / len(average)
upperAvg = sum(upper) / len(upper)
index = int((score - lowerAvg) / upperAvg * (len(MAP) - 1))
if index > len(MAP) - 1:
print("toobig")
index = len(MAP) - 1
if index < 0:
print("toosmall")
index = 0
if abs(index - lastIndex) > SEND_THRESHOLD:
stdin, stdout, stderr = ssh.exec_command(COMMAND % MAP[index])
stdout.readlines() # block
lastIndex = index
history.append(score)
if len(history) > 10 / INPUT_BLOCK_TIME:
# keep only ^ seconds
history.pop(random.randrange(len(history)))
history.sort()
if __name__ == "__main__":
main()
|