soundboard-host/host.py

110 lines
2.5 KiB
Python
Raw Normal View History

2013-07-14 15:45:54 +00:00
#!/usr/bin/env python3
import sys
2013-07-14 16:24:16 +00:00
import os
2013-07-14 15:45:54 +00:00
import time
import random
2013-07-18 20:53:05 +00:00
import socket
2013-07-14 15:45:54 +00:00
import pygame
from pygame.locals import *
2013-07-14 16:24:16 +00:00
os.environ["SDL_VIDEODRIVER"] = "dummy"
2013-07-14 15:45:54 +00:00
# "we'll fix it in post!"
MAPPING = [
8,
7,
6,
5,
1,
2,
3,
4,
9,
10,
11,
0
]
2013-07-14 15:48:31 +00:00
SPATH = "sounds/"
2013-07-14 15:45:54 +00:00
2013-07-18 20:53:05 +00:00
def logmesohard(name):
s = socket.socket()
s.connect(('10.8.1.1', 2003))
s.sendall('hackerspace.soundboard.{} 1'.format(name.replace('.', '_'_)))
s.close()
2013-07-14 15:45:54 +00:00
class SimpleSound:
def __init__(self, path):
self.path = path
def create(self):
2013-07-14 15:48:31 +00:00
print("Loading {}".format(self.path))
2013-07-14 15:45:54 +00:00
self.s = pygame.mixer.Sound(SPATH + self.path)
def play(self):
2013-07-14 15:48:31 +00:00
print("Playing {}".format(self.path))
2013-07-18 20:53:05 +00:00
logmesohard(self.path)
2013-07-14 15:45:54 +00:00
self.s.play()
return self.s.get_length() * 0.5
class RandomSound:
def __init__(self, paths):
self.paths = paths
def create(self):
2013-07-14 15:48:31 +00:00
print("Loading {}".format(", ".join(self.paths)))
self.sounds = [(pygame.mixer.Sound(SPATH + p), p) for p in self.paths]
2013-07-14 15:45:54 +00:00
def play(self):
2013-07-14 15:48:31 +00:00
s, path = random.choice(self.sounds)
2013-07-18 20:53:05 +00:00
logmesohard(self.paths[0])
2013-07-14 15:48:31 +00:00
print("Playing {}".format(path))
2013-07-14 15:45:54 +00:00
print(s)
s.play()
return s.get_length() * 0.5
SOUNDS = [
SimpleSound("nope.wav"),
SimpleSound("yesyes.wav"),
SimpleSound("ba-dum-tish.wav"),
SimpleSound("crickets.wav"),
SimpleSound("wind.wav"),
SimpleSound("trombone.wav"),
RandomSound(["whip.wav", "whip2.wav", "whip4.wav"]),
SimpleSound("finishim.wav"),
SimpleSound("wtfboom.wav"),
SimpleSound("noooooooo.wav"),
SimpleSound("fanfare.wav"),
SimpleSound("dundundun.wav"),
]
play_again_at = time.time()
if __name__ == "__main__":
2013-07-14 16:29:19 +00:00
# are we a fucking rpi?
f = open("/proc/cpuinfo", "r")
d = f.read()
f.close()
if "BCM2708" in d:
print("We are on an rpi. Doing ALSA hack.")
os.system("amixer cset numid=3 1")
2013-07-14 16:24:16 +00:00
pygame.mixer.init()
pygame.display.init()
pygame.display.set_mode((1,1))
2013-07-14 15:45:54 +00:00
for s in SOUNDS:
s.create()
pygame.joystick.init()
j = pygame.joystick.Joystick(0)
j.init()
2013-07-14 15:48:31 +00:00
print("Ready!")
2013-07-14 15:45:54 +00:00
while True:
pygame.event.pump()
for event in pygame.event.get():
if event.type == JOYBUTTONDOWN:
n = MAPPING.index(event.button)
if n < len(SOUNDS) and time.time() >= play_again_at:
play_again_at = time.time() + SOUNDS[n].play()
time.sleep(0.1)