User:Birgit Bachler/techday 04

From XPUB & Lens-Based wiki

mic in > drum out

#!/usr/bin/python

import sys, pygame, thread, os

c=0
cx=0

def reader ():
    while True:
        line = sys.stdin.readline()
        evt = pygame.event.Event(pygame.USEREVENT, {"msg" : line.strip()})
        pygame.event.post(evt)

pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("python")
clock = pygame.time.Clock()
level, lo, hi = (0, 0, 1)    
thread.start_new_thread(reader, ())

while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT or \
        (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
            sys.exit()
        elif event.type == pygame.USEREVENT:
            # print event
            level = event.msg
            msg = event.msg.split()
            cmd = msg[0]
            level = float(msg[1])
            lo = float(msg[2])
            hi = float(msg[3])
    s = (level - lo) / (hi - lo)
    cx += (c - cx)
    c = int(s*255)
    tone = int(s*34)
    screen.fill((cx, 0, c))
    if s > 0.25:
        os.system("play drums/"+str(tone)+".wav 2> /dev/null &")
    pygame.display.update()
    clock.tick(30)


Attachments