#! /usr/bin/env python # # Rock Band Drums to Midi Script for "Rock Band Drum Circle" # Created 2011 for Art|Everywhere in Norfolk Virginia # # This is a derivitive work from pyRock.py by billb # http://pleasantfiction.ipower.com/bodega/viewtopic.php?f=26&t=155&start=0 # # -------------------------------------------------------------------- # # This script will read the drums from RockBand USB controllers # and pass it thru to a midi interface connected to the computer. # It will detect up to 16 drum sets! Wii, Xbox 360 and PS3 have been tested. # # Each drum set will be sent on a different midi channel, so you # can assign different drum kits # # The select buttons on the drum kits will switch betwene sets # of sounds, so the people playing the drums can choose their # sound sets. These notes are defined below. # # ------------------------------------------------------------------- # # Errata: # # There is no provision for the pedals in this script # # The function is supposed to send a note off command after a set amount of # time, but it doesn't seem to work. Need to troubleshoot. # # Python sometimes doesn't have the midi library as part of pygame. # Upgrading ubuntu to latest seemed to bring in the midi part of pygame. # # # ------------------------------------------------------------------- # # License: # Rock. # If you use it for something cool, let us know! Email ethan.rbd@757.org # # Released from the 757 Labs Hackerspace, www.757labs.org # # # # Import pygame module. import pygame,random,time import pygame.midi from pygame.locals import * from sys import exit # Define the drumpads and pedal "joystick buttons" BLUEPAD = 2 GREENPAD = 0 REDPAD = 1 YELLOWPAD = 3 PEDAL = 4 SELECT = 8 START = 9 PSBUTTON = 12 DPADXAXIS = 4 # -1.0 for left, 1.0 for right DPADYAXIS = 5 # -1.0 for up, 1.0 for down # # Here are the notes, and the sets! # The four numbers are the midi notes. # The four sets of four numbers are the four selectable sets of sounds. # midinotes = [[46,42,40,36],[37,39,56,54],[43,45,47,48],[60,62,64,63]] playernoteset = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] # Init pygame pygame.init() pygame.joystick.init() time.sleep(0.2) pygame.midi.init() output_id = pygame.midi.get_default_output_id() print 'output', output_id MidiOut = pygame.midi.Output(2,1) def main(): # Initialize the joystick and report the name hasnt_failed = True joystick_num = 0 while(hasnt_failed): try: j = pygame.joystick.Joystick(joystick_num) # create a joystick instance j.init() # init instance joystick_num = joystick_num + 1 print 'Enabled joystick: ' + j.get_name() except pygame.error: hasnt_failed = False print 'joystick not found' # Holding down the foot pedal causes pads to play alternate samples # Let off foot pedal to switch back to default set # May change this later to allow different behaviors for pedal pedalSwitch = 0 # While holding down the pedal, the drumpad labels will be offset labelOffset = 0 #Create clock object clock = pygame.time.Clock() # Block mouse events from entering the queue pygame.event.set_blocked(MOUSEMOTION) keepRocking = True while keepRocking: #Handle Input Events for event in pygame.event.get(): print event if event.type == pygame.JOYBUTTONDOWN: if event.button == PEDAL: pedalSwitch = 1 labelOffset = 10 if event.button == REDPAD: MidiOut.write([[[144 + event.joy,midinotes[playernoteset[event.joy]][0],127],0],[[128 + event.joy,midinotes[playernoteset[event.joy]][0],127],2000]]) if event.button == YELLOWPAD: MidiOut.write([[[144 + event.joy,midinotes[playernoteset[event.joy]][1],127],0],[[128 + event.joy,midinotes[playernoteset[event.joy]][1],127],2000]]) if event.button == BLUEPAD: MidiOut.write([[[144 + event.joy,midinotes[playernoteset[event.joy]][2],127],0],[[128 + event.joy,midinotes[playernoteset[event.joy]][2],127],2000]]) if event.button == GREENPAD: MidiOut.write([[[144 + event.joy,midinotes[playernoteset[event.joy]][3],127],0],[[128 + event.joy,midinotes[playernoteset[event.joy]][3],127],2000]]) # xbox drums - button if event.button == 6: playernoteset[event.joy] = playernoteset[event.joy] + 1 # # The 3 value here and below is part of the # of sets of notes that are avail. # If you add more notes above, you will need to increase these to 1 less than # the number of note sets. # if playernoteset[event.joy] > 3: playernoteset[event.joy] = 0 # Wii drums - button if event.button == 8: playernoteset[event.joy] = playernoteset[event.joy] + 1 if playernoteset[event.joy] > 3: playernoteset[event.joy] = 0 # xbox drums - button if event.button == 14: playernoteset[event.joy] = playernoteset[event.joy] - 1 if playernoteset[event.joy] < 0: playernoteset[event.joy] = 3 # wii drums - button if event.button == 9: playernoteset[event.joy] = playernoteset[event.joy] - 1 if playernoteset[event.joy] < 0: playernoteset[event.joy] = 3 elif event.type == pygame.JOYBUTTONUP and event.button == PEDAL: pedalSwitch = 0 labelOffset = 0 elif event.type == pygame.QUIT: keepRocking = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: keepRocking = False # limit to ~ 90 frames per second time_passed = clock.tick(90) #this calls the 'main' function when this script is executed if __name__ == '__main__': main()