
Little Drummer Toy
This is a MEMO to Santa, from the elves at Workshop R&D, Bench 7. It regards a future robot toy and the classification is secret.
~
Dear Santa,
Every year we elves ask the same question: what do kids really want? And every time the data comes back the same: Noise. Loud, rhythmic, parent-testing noise. So, we have been building a small, marching robot with drumsticks for hands, felt boots, a brain made of a "lump of [Perl] code" with the excellent software synthesizer, FluidSynth, and a small, but quality, speaker.
Description
The toy is a "simple 16th-note-phrase drummer" with preset patterns and grooves. You, Santa, can also give it parts functions that say which drums strike which of sixteen steps. It plays these parts in real-time, ticking along and sending MIDI clock pulses as it goes. Eventually, you will be able to upload parts via USB to the physical robot device. Since that involves the tinkering elves at Desk 3, who are still working on the physical mechanics, we will only concern ourselves with the programming bits.
Playing with the toy
We know you're a dabbler in programming, Santa. So here is a detailed description of the insides of this robot, and how to play with it.
Like other toys with electronic or programmed guts, we've tested the robot brain on our computers of all flavors. On Windows, you can use the built-in Microsoft GS Wavetable Synth. For other machines, you can install FluidSynth with a package manager. Then, install the Perl dependencies with cpanm Music::SimpleDrumMachine. That's it!
And, by the way, the robot-brain Perl code allows you to use any named, open MIDI port on the system. So Santa, if a physical device, like your Korg Volca Drum, is MIDI connected, it can be used, too. Pretty cool!
To play with the toy without a hardware synth, you, Santa, can install and use FluidSynth.
In order to see what the names of the MIDI ports are, that are open on your system, run the list_devices.pl program in the Music::SimpleDrumMachine distribution eg/ directory.
Example 1
Here is an example of robot-brain code. To run it, do this:
perl little-drummer.pl wavetable # on windows
perl little-drummer.pl fluidsynth # or with fluidsynth
perl little-drummer.pl usb # device connected to a usb interface
This runs at 90 beats per minute and on MIDI channel 9 (zero-based numbering), which are the defaults. The program plays a loop that alternates between two one-bar patterns. That is, parts A and B take turns, going back and forth.
Here is the code:
#!/usr/bin/env perl
use v5.36;
use Music::SimpleDrumMachine ();
my $port = shift // 'fluidsynth'; # unique part of the open MIDI port name
my $bpm = shift // 90; # beats-per-minute
my $chan = shift // 9; # MIDI channel 0-15
my $dm = Music::SimpleDrumMachine->new(
port_name => $port,
bpm => $bpm,
chan => $chan,
bars => 1,
parts => {
A => \&part_A,
B => \&part_B,
},
next_part => 'A', # start-off with this part
verbose => 1,
);
sub part_A {
say 'Part A';
my %patterns = (
closed => [qw(1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0)],
kick => [qw(1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0)],
snare => [qw(1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 0)],
);
return 'B', \%patterns; # required return arguments
}
sub part_B {
say 'Part B';
my %patterns = (
closed => [qw(1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0)],
kick => [qw(1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0)], # CHANGE 1: 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0
snare => [qw(1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1)],
# crash => [qw(1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)], # CHANGE 2: uncomment line
);
return 'A', \%patterns;
}
And here's what that sounds like:
Change #1 is to add another bass-drum kick to beat 3 of 16, to part B:
Change #2 is to add a crash cymbal strike on the first beat of part B:
Not terribly exciting yet!
Details
Every part is a function that returns two things: the name of whatever part should play next, and a hash-ref of patterns, keyed by the name of the drum or percussion instrument. A pattern is a list of sixteen steps, where 1 is a hit and 0 is a rest.
The drum names are short and stand for a General MIDI percussion note number: kick=35, snare=38, closed=42, open=46, cowbell=56, and many more, which you can see in the drums section of the Music::SimpleDrumMachine documentation.
Danceable grooves
Naturally, since Bench 7 has a few musically inclined elves, we have come up with some funky grooves! These employ what are known as "Euclidean rhythms." Parts B and C have their hihat cymbal patterns created by the euclid() function of the Music::CreatingRhythms module. These use a prime number of pulses that are spread over sixteen beats. This creates a number of possible patterns, with (usually) a different one on each run.
Install one more Perl dependency with cpanm Music::CreatingRhythms.
Note that these patterns play four bars before changing to the next part.
For continuity with the previous example, part A is the same.
#!/usr/bin/env perl
use v5.36;
use Music::SimpleDrumMachine ();
use Music::CreatingRhythms (); # for euclidean beats
my $port = shift // 'fluidsynth';
my $bpm = shift // 90;
my $chan = shift // 9;
my $beats = 16;
my %primes = ( # for computing patterns
all => [qw(2 3 5 7 11 13)],
to_5 => [qw(2 3 5)],
to_7 => [qw(2 3 5 7)],
);
my $next_part; # = { A => 4, B => 3, C => 2 }; # <- CHANGE 2
my $mcr = Music::CreatingRhythms->new;
my $dm = Music::SimpleDrumMachine->new(
port_name => $port,
bpm => $bpm,
chan => $chan,
filling => 0, # CHANGE 1: comment line
bars => 4, # measures per part
parts => {
A => \&part_A,
B => \&part_B,
C => \&part_C,
},
next_part => 'A', # $next_part, # <- CHANGE 2
save => 'euclidean-drummer.mid',
verbose => 1,
);
sub part_A {
say 'Part A';
my %patterns = (
closed => [qw(1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0)],
kick => [qw(1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0)],
snare => [qw(1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 0)],
);
$next_part = 'B'; # CHANGE 2: comment line
return $next_part, \%patterns;
}
sub part_B {
say 'Part B';
# choose the first (all) prime list to use for the closed hihat
my ($p) = primes_list();
my %patterns = (
closed => $mcr->euclid($p, $beats),
kick => [qw(1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1)],
snare => [qw(0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0)],
);
$next_part = 'C'; # CHANGE 2: comment line
return $next_part, \%patterns;
}
sub part_C {
say 'Part C';
# choose primes to use for the closed and open hihats
my ($p, $q) = primes_list();
my %patterns = (
closed => $mcr->euclid($p, $beats),
open => $mcr->euclid($q, $beats),
kick => [qw(1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0)],
snare => [qw(0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0)],
);
$next_part = 'A'; # CHANGE 2: comment line
return $next_part, \%patterns;
}
sub primes_list { # return a random prime for each sorted key
return map { $primes{$_}[ int rand $primes{$_}->@* ] } sort keys %primes;
}
And here's what that sounds like:
For change #1, we comment out the filling attribute. That enables the module's built-in, automatic drum fills. So on the 4th measure, a snare-drum fill is played.
Change #2 expands the next_part attribute to use a set of parts and weighted probabilities. Parts A, B, and C each have a weight that sets how likely they are to be picked next.
Here is the verbose debugging output:
> perl euclidean-drummer.pl synth 100
Beats: 0
Part B
Beats: 48
_default_fill
size: 2
_default_part
Beats: 64
Part A
Beats: 112
_default_fill
size: 2
_default_part
Beats: 128
Part C
Beats: 176
_default_fill
size: 4
Beats: 192
Part A
Beats: 240
_default_fill
size: 4
Beats: 256
Part A
Beats: 304
_default_fill
size: 4
Beats: 320
Part C
Beats: 368
_default_fill
size: 4
Beats: 384
Part B
^C
Stop
Saved score to euclidean-drummer.mid
Recording a snapshot
In R&D, we elves sometimes find it handy to save and share snapshots of robot grooves, as MIDI files. If the save attribute is set to a filename, everything the robot plays is mirrored into a MIDI::Simple score. Then, when you press Ctrl-C, the drums stop, the score is written to the named file, and the program exits.
Merry Christmas, Santa!
If you have any questions or suggestions, let us know!
Production begins in the near future, after everything has been perfected and made child-proof.
May your loops be eventful, your timers periodic, and your constructors sound.
Pa rum pa pum pum!
Signed
The friendly team at Workshop R&D, Bench 7
References
Music::SimpleDrumMachine and its eg/* example programs
Article example: little-drummer
Article example: euclidean-drummer