Drums hinzugefügt
This commit is contained in:
parent
e8f305939f
commit
985edcceac
1 changed files with 96 additions and 0 deletions
96
drum.py
Normal file
96
drum.py
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
import pygame
|
||||||
|
import pygame.midi
|
||||||
|
from os import environ
|
||||||
|
from pygame.locals import QUIT, JOYBUTTONUP, JOYBUTTONDOWN, \
|
||||||
|
JOYAXISMOTION, JOYHATMOTION
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
# Braucht man fuer die Abfrage-Rate
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
# Bekannte Joysticks (ich verwende aber nur den ersten)
|
||||||
|
JOYSTICKS = []
|
||||||
|
|
||||||
|
# Verwandle die gedrueckten Buttons ueber Bits in eine Zahl (Button 0 und 1 gedrueckt waeren dann 2^0 + 2^1 = 3)
|
||||||
|
PRESSED = 0
|
||||||
|
|
||||||
|
DRUMSETS = [[41,38,45,42,36,49]]
|
||||||
|
DRUMSET = 0
|
||||||
|
|
||||||
|
def proc_event(event,midi_out):
|
||||||
|
global PRESSED
|
||||||
|
global VELOCITY
|
||||||
|
"Parse and act upon event"
|
||||||
|
if event.type == QUIT:
|
||||||
|
print("Received event 'Quit', exiting.")
|
||||||
|
exit(0)
|
||||||
|
elif event.type == JOYAXISMOTION:
|
||||||
|
#print("axis %d value %0.3f" % (event.axis, event.value))
|
||||||
|
try:
|
||||||
|
# Bei analogen Joysticks erst ab einem Fuenftel reagieren
|
||||||
|
if abs(event.value) > 0:
|
||||||
|
VELOCITY = int(abs(event.value) * 127)
|
||||||
|
else:
|
||||||
|
VELOCITY = 100
|
||||||
|
if PRESSED > 0:
|
||||||
|
print PRESSED
|
||||||
|
print VELOCITY
|
||||||
|
NOTE = DRUMSETS[DRUMSET][PRESSED-1]
|
||||||
|
print NOTE
|
||||||
|
midi_out.note_on(NOTE,VELOCITY,9)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
elif event.type == JOYBUTTONDOWN:
|
||||||
|
try:
|
||||||
|
if event.button < 6:
|
||||||
|
PRESSED = event.button + 1
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
elif event.type == JOYBUTTONUP:
|
||||||
|
try:
|
||||||
|
PRESSED = 0
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
# Initialisieren
|
||||||
|
pygame.init()
|
||||||
|
pygame.midi.init()
|
||||||
|
|
||||||
|
# Default-Ausgabe (normalerweise "Midi Through")
|
||||||
|
port = pygame.midi.get_default_output_id()
|
||||||
|
###print ("using output_id :%s:" % port)
|
||||||
|
|
||||||
|
# Latenz 0 (man kann es ja mal versuchen)
|
||||||
|
midi_out = pygame.midi.Output(port, 0)
|
||||||
|
|
||||||
|
# Was man nicht braucht, wird einfach abgeschaltet
|
||||||
|
environ["SDL_VIDEODRIVER"] = "dummy"
|
||||||
|
environ["SDL_AUDIODRIVER"] = "dummy"
|
||||||
|
|
||||||
|
# Alle Joysticks, die du findest
|
||||||
|
#for i in range(0, pygame.joystick.get_count()):
|
||||||
|
# JOYSTICKS.append(pygame.joystick.Joystick(i))
|
||||||
|
|
||||||
|
# Ein Joystick/Controller reicht
|
||||||
|
JOYSTICKS.append(pygame.joystick.Joystick(0))
|
||||||
|
JOYSTICKS[-1].init()
|
||||||
|
print("Detected joystick '%s'" % JOYSTICKS[-1].get_name())
|
||||||
|
|
||||||
|
# 5 Millisekunden warten, bevor das Dingens hier weiterlaeuft
|
||||||
|
clock.tick(50)
|
||||||
|
|
||||||
|
while 1:
|
||||||
|
try:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
proc_event(event,midi_out)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
del midi_out
|
||||||
|
pygame.midi.quit()
|
||||||
|
print("\n" "Interrupted")
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in a new issue