💾 Archived View for thrig.me › blog › 2023 › 10 › 07 › periodic.pl captured on 2023-11-04 at 12:30:16.

View Raw

More Information

-=-=-=-=-=-=-

#!/usr/bin/env perl
# periodic - cowbell + lockstep periodic generators
use 5.26.0;
use warnings;
use MIDI;
sub CCPAN ()   { 0x0A }
sub COWBELL () { 56 }
sub DRUMS ()   { 9 }
sub MAXTIME () { 10240 }
sub NOW ()     { 0 }
sub QUIET ()   { 0 }

my $file = shift // 'periodic.midi';

my $track_count = 8;

# probably rain should be quiet background noise, this is up pretty high
# so I can hear it better over background noise
my @velo = qw( 105 90 95 90 );

sub velo {
    state $i = 0;
    my $v = $velo[$i];
    $i = ( $i + 1 ) % @velo;
    $v;
}

sub makeatrack {
    state $pan = 52;    # NOTE depends on track_count
    my @events;
    my ( $epoch, $notedur, $interval ) =
      ( 0, 12, 32 * ( 1 + int rand 32 ) );
    while ( $epoch < MAXTIME ) {
        push @events,
          [ control_change => $interval, DRUMS, CCPAN, $pan ],
          [ note_on        => NOW, DRUMS, COWBELL, velo ],
          [ note_off       => $notedur, DRUMS, COWBELL, QUIET ];
        $epoch += $interval + $notedur;
    }
    $pan += 3;          # NOTE depends on track_count
    MIDI::Track->new( { events => \@events } );
}

sub gentracks { [ map makeatrack, 1 .. $track_count ] }

MIDI::Opus->new( { tracks => gentracks } )->write_to_file($file);