#!/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 = [ [48,38,46,42,36,49], [43,38,47,49,36,51], [43,38,47,42,36,57], [41,38,45,42,36,49] ] DRUMSET = 0 def proc_event(event,midi_out): global PRESSED global DRUMSET 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: if abs(event.value) > 0: VELOCITY = int(abs(event.value) * 127) #VELOCITY = 100 else: VELOCITY = 100 if PRESSED > 0: print PRESSED print VELOCITY NOTE = DRUMSETS[DRUMSET][PRESSED-1] print DRUMSET print NOTE midi_out.note_on(NOTE,VELOCITY,9) except: pass elif event.type == JOYBUTTONDOWN: try: if event.button < 6: PRESSED = event.button + 1 elif event.button == 6: DRUMSET = 1 print DRUMSET elif event.button == 7: DRUMSET = 2 print DRUMSET elif event.button == 8: DRUMSET = 0 print DRUMSET elif event.button == 13: DRUMSET = (DRUMSET - 1) % len(DRUMSETS) print DRUMSET elif event.button == 14: DRUMSET = (DRUMSET + 1) % len(DRUMSETS) print DRUMSET else: print len(DRUMSETS) 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()