NAME
Music::SimpleDrumMachine - Simple 16th-note-phrase Drummer
VERSION
version 0.0403
SYNOPSIS
use Music::SimpleDrumMachine ();
my $dm = Music::SimpleDrumMachine->new( # use defaults
port_name => 'midi device', # required
);
# OR:
$dm = Music::SimpleDrumMachine->new(
port_name => 'midi device',
bpm => 100,
parts => {
part_A => \&part_A,
part_B => \&part_B,
},
next_part => 'part_A',
fills => { fill_A => \&fill_A },
next_fill => 'fill_A',
verbose => 1,
);
sub part_A {
print "part A\n";
my %patterns = (
hihat => [qw(1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0)],
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)],
);
my $next = 'part_B';
return $next, \%patterns;
}
sub part_B {
print "part B\n";
my %patterns = (
hihat => [qw(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)],
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)],
);
my $next = 'part_A';
return $next, \%patterns;
}
sub fill_A {
print "fill_A\n";
my %patterns = (
snare => [qw(1 0 1 0 1 1 1 1 0 1 0 1 1 0 1 0)],
);
my $next = 'fill_A';
return $next, \%patterns;
}
DESCRIPTION
Music::SimpleDrumMachine is a simple 16th-note-phrase drummer. By invoking this module, your MIDI device will begin playing in real-time.
* Triplets are not supported. Boo!
* Use the extra-handy eg/list_devices.pl program to see the names of the open MIDI ports on the system.
ATTRIBUTES
add_drums
add_drums => \%drums,
Add an array-ref of hash-refs of the form [{ drum => 'name', num => midi_num, chan => channel }] to the known drums in the constructor. The chan key is optional and is only necessary if you want to assign a drum to a specific channel.
beats
$beats = $dm->beats;
The number of beats in a phrase.
Default: 16
bpm
$bpm = $dm->bpm;
The beats per minute.
Default: 120
chan
$chan = $dm->chan;
The MIDI channel.
If the channel is set to -1, multi-timbral mode is turned on and channels 0, 1, ... and up are used, instead of a single channel for all the percussion.
Default: 9
divisions
$divisions = $dm->divisions;
The number of divisions of a quarter-note into the number of beat-notes. For the default, this is the number of divisions of a quarter-note to get 16ths.
Default: 4
drums
$drums = $dm->drums;
$dm->drums($drums);
The known drums.
Default:
kick => { num => 36, chan => ..., pat => [] },
snare => { num => 38, chan => ..., pat => [] },
hihat => { num => 42, chan => ..., pat => [] },
fillcrash => { num => 49, chan => ..., pat => [] },
fill_crash
$fill_crash = $dm->fill_crash;
$dm->fill_crash($boolean);
Should we crash after a fill?
Default: 1
fills
$fills = $dm->fills;
$dm->fills($fills);
List of code-refs of the fills to play.
Default: [_default_fill()]
filling
$filling = $dm->filling;
$dm->filling($boolean);
Should we fill between parts?
Default: 1
next_fill
$next_fill = $dm->next_fill;
$dm->next_fill($next_fill);
Name of the fill to play first and set subsequently in a fill.
Default: '_default_fill'
next_part
$next_part = $dm->next_part;
$dm->next_part($next_part);
Name of the part to play first and set subsequently in a part.
Default: '_default_part'
notes
$notes = $dm->notes;
$dm->notes($notes);
The notes to set for each drum - why not?
Default: [60, 64, 67]
parts
$parts = $dm->parts;
$dm->parts($parts);
List of code-refs of the parts to play.
Default: [_default_part()]
port_name
$port = $dm->port_name;
The name of the MIDI output port.
Default: usb
ppqn
$ppqn = $dm->ppqn;
The "pulses per quarter-note" or "clocks per beat."
Default: 24
prefill_part
$prefill_part = $dm->prefill_part;
Code-ref of the part to play for 1/2-bar fills.
Default: \&_default_part
verbose
$verbose = $dm->verbose;
Show progress.
Default: 0
velo_max
$velo_max = $dm->velo_max;
$dm->velo_max($max);
The maximum allowed relative velocity from the velo_off offset.
Default: 10
velo_min
$velo_min = $dm->velo_min;
$dm->velo_min($min);
The minimum allowed relative velocity from the velo_off offset.
Default: -10
velo_off
$velo_off = $dm->velo_off;
$dm->velo_off($offset);
The velocity offset.
Default: 110
METHODS
new
$dm = Music::SimpleDrumMachine->new(%arguments);
Create a new Music::SimpleDrumMachine object.
velocity
$dm->velocity;
Return a random velocity between the velo_min (minimum) and velo_max (maximum), starting at the velo_off offset.
So, for -10, 10, 110 (the default), a number between 100 and 120 will be returned. The triple 0, 0, 127 will return 127 every time.
SEE ALSO
The eg/*.pl programs in this distribution.
AUTHOR
Gene Boggs <gene.boggs@gmail.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2026 by Gene Boggs.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.