Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Rock Band Drum Controller Player

by hardburn (Abbot)
on Apr 10, 2008 at 22:31 UTC ( #679581=CUFP: print w/ replies, xml ) Need Help??

The Rock Band Drum controller is a straight USB device, so it's quite easy to interface to it. The script below will play a set of wav files associated with each pad on the controller. Change the path to the wav's in the %SOUNDS hash as necessary.

I've only tested this with the Playstation 3 controller. Experiances/patches from XBox360 owners welcome.

You'll also need the SDL and Linux::Input modules.

Start it up with: rock_band_drums.pl /dev/input/event[x], where "x" is some number that corresponds to right USB device.

You can get some wav drum samples at http://www.users.bigpond.com/prodigalson/drum.htm

#!/usr/bin/perl # Rock Band Drum Controller Player # Copyright (C) 2008 Timm Murray # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/> +. # use strict; use warnings; use Linux::Input; use IO::Select; use SDL; use SDL::Sound; use SDL::Mixer; use constant DEBUG => 0; my $DEV = shift; die "Need a device\n" unless $DEV; my %SOUNDS = ( snare => 'rock_band_drums/snare.wav', drum => 'rock_band_drums/drum.wav', hi_hat => 'rock_band_drums/hi_hat.wav', smash => 'rock_band_drums/crash.wav', bass => 'rock_band_drums/bass.wav', ); my %EVENTS = ( 308 => $SOUNDS{bass}, # Foot 306 => $SOUNDS{snare}, # Red 307 => $SOUNDS{hi_hat}, # Yellow 304 => $SOUNDS{drum}, # Blue 305 => $SOUNDS{smash}, # Green ); { my $ret = SDL::MixOpenAudio( 44100, AUDIO_S16, 2, 4096 ); die "Can't open audio: " . SDL_GetError() . "\n" if 0 > $ret; } sub debug { my (@in) = @_; return unless DEBUG; print @in, "\n"; } sub do_event { my ($event) = @_; my $code = $$event{code}; my $value = $$event{value}; debug( "Code [$code], value [$value]" ); return 0 if ! exists $EVENTS{$code}; return 0 if $value == 0; my $file = $EVENTS{$code}; my $sound = SDL::MixLoadWAV( $file ); SDL::MixPlayChannel( -1, $sound, 0 ); return 1; } { my $js1 = Linux::Input->new( $DEV ); my $selector = IO::Select->new; $selector->add( $js1->fh ); while( my $fh = $selector->can_read ) { my @events = $js1->poll; foreach (@events) { next unless $$_{type} == 1; do_event( $_ ); } } }

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Comment on Rock Band Drum Controller Player
Select or Download Code
Re: Rock Band Drum Controller Player
by dynamo (Chaplain) on Apr 16, 2008 at 21:52 UTC
    Great job, man.
    Let's see more random perl device drivers to video game controllers out there!
Re: Rock Band Drum Controller Player
by beppu (Friar) on Jun 26, 2008 at 16:49 UTC
    Believe it or not, this is the first time I've seen anyone other than me use Linux::Input for something. I'm happy to see it being used for something cool. :)
Re: Rock Band Drum Controller Player
by Anonymous Monk on Oct 16, 2008 at 22:50 UTC
    Followed instructions. Getting this error. Bareword "AUDIO_S16" not allowed while "strict subs" in use at rock_band_drums.pl line 48. Execution of rock_band_drums.pl aborted due to compilation errors.

      Perhaps you have an old version of SDL? That symbol is exported by SDL_perl v2.1.3.


      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

        didn't work for me. maybe you are using the event fh? I didn't want to give permissions on it, so I refactored to use the Joystick class. Forgive me, I never hack perl :P Here's some code:
        #!/usr/bin/perl # Rock Band Drum Controller Player # Copyright (C) 2008 Timm Murray # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/> # use strict; use warnings; use Linux::Input::Joystick; use IO::Select; use SDL; use SDL::Sound; use SDL::Mixer; use YAML; use constant DEBUG => 0; my $DEV = shift; die "Need a device\n" unless $DEV; my %SOUNDS = ( snare => 'rock_band_drums/snare.wav', drum => 'rock_band_drums/drum.wav', hi_hat => 'rock_band_drums/hi_hat.wav', smash => 'rock_band_drums/crash.wav', bass => 'rock_band_drums/bass.wav', ); # 4 = foot # left to right # 2, 3, 0, 1 my %EVENTS = ( 4 => $SOUNDS{bass}, # Foot 2 => $SOUNDS{snare}, # Red 3 => $SOUNDS{hi_hat}, # Yellow 0 => $SOUNDS{drum}, # Blue 1 => $SOUNDS{smash}, # Green ); { my $ret = SDL::MixOpenAudio( 44100, AUDIO_S16, 2, 4096 ); die "Can't open audio: " . SDL_GetError() . "\n" if 0 > $ret; } sub debug { my (@in) = @_; return unless DEBUG; print @in, "\n"; } sub do_event { my ($event) = @_; my $type = $$event{type} || 0; my $value = $$event{value} || 0; my $number = $$event{number} || 0; debug( "Type [$type], value [$value], number [$number]" ); return 0 if ! exists $EVENTS{$number}; return 0 if $value == 0; my $file = $EVENTS{$number}; my $sound = SDL::MixLoadWAV( $file ); SDL::MixPlayChannel( -1, $sound, 0 ); return 1; } { my $js1 = Linux::Input::Joystick->new( $DEV ); my $selector = IO::Select->new; $selector->add( $js1->fh ); while (1) { while( my @events = $js1->poll ) { # print Dump($_) foreach (@events); foreach (@events) { next unless $$_{type} == 1; do_event( $_ ); } } } }
        I had the same problem try replacing AUDIO_S16 with its value 32784 of course I don't get any sound, but I don't get any errors either.
Re: Rock Band Drum Controller Player
by Anonymous Monk on Jun 10, 2009 at 00:16 UTC
    Confirmed working with the Nintendo Wii drum controller. Awesome!
Re: Rock Band Drum Controller Player
by Anonymous Monk on Oct 04, 2009 at 00:25 UTC

    fails to open mixer, saying:

    $ perl rock.band.kit.pl /dev/input/event1 Use of inherited AUTOLOAD for non-method SDL::MixOpenAudio() is deprec +ated at rock.band.kit.pl line 47. Can't locate auto/SDL/MixOpenAudi.al in @INC (@INC contains: /etc/perl + /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/per +l5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/loca +l/lib/site_perl .) at rock.band.kit.pl line 47

    there are results on google for this error message, but it looks like nobody really answered why this error exists or how to fix it. can YOU? (that's a challenge.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://679581]
Approved by Arunbear
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-10-21 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls