Last active
March 8, 2018 23:43
-
-
Save miguelvb/197a142e6186821aa1b26b30892f9d30 to your computer and use it in GitHub Desktop.
keys to sounds in rapsberry pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import termios, fcntl, sys, os | |
import pygame.mixer | |
from time import sleep | |
from sys import exit | |
fd = sys.stdin.fileno() | |
oldterm = termios.tcgetattr(fd) | |
newattr = termios.tcgetattr(fd) | |
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO | |
termios.tcsetattr(fd, termios.TCSANOW, newattr) | |
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) | |
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) | |
pygame.mixer.init(48000, -16, 1, 1024) | |
sndA = pygame.mixer.Sound("shake.wav") | |
sndB = pygame.mixer.Sound("who.wav") | |
sndC = pygame.mixer.Sound("shake.wav") | |
soundChannelA = pygame.mixer.Channel(1) | |
soundChannelB = pygame.mixer.Channel(2) | |
soundChannelC = pygame.mixer.Channel(3) | |
print "Sampler Ready." | |
try: | |
while 1: | |
try: | |
c = sys.stdin.read(1) | |
print "Got character", repr(c) | |
if ( c == "s"): | |
soundChannelA.play(sndA) | |
if ( c == "d"): | |
soundChannelB.play(sndB) | |
if ( c == "f"): | |
soundChannelC.play(sndC) | |
sleep(.01) | |
except IOError: pass | |
finally: | |
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) | |
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment