#!/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()