Created
October 2, 2024 01:19
-
-
Save ology/3c0a89717bdb6f26b9e040a236486a8b to your computer and use it in GitHub Desktop.
Set patch and use a couple handy variables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# mess around with trig functions, producing MIDI of dubious merit | |
use 5.36.0; | |
use MIDI; | |
my @events; | |
my $out_file = shift // "$0.midi"; | |
# how fine or coarse to check on the output of the equation (will depend | |
# on the equation, how many notes you want, how much silence from | |
# overchecking for zero-crossings, etc) | |
my $step = 0.09; | |
# how long to carry the steps for | |
my $max_step = 101; | |
# how long of a MIDI event to generate (also used for silence | |
# accumulation). the MIDI tempo could also be played around with, | |
# or $step could be used as a real-time value to sleep for, with a | |
# live synth | |
my $tick = 8; | |
my $patch = 12; | |
my $channel = 0; | |
my $velocity = 100; | |
sub equation ($t) { | |
sin( $t * 5 ) + cos( $t * 2 ); | |
#sin( $t * 5 ) + cos( ( $t + sin( $t / 5 ) ) * 2 ); | |
} | |
sub add_note ($dtime, $x) { | |
#my @n = qw(48 55 48 48 48 55 55 48); | |
#state $i = 0; | |
#my $n = $n[$i++ % @n]; | |
my @n = qw(48 49 53 55 56); | |
my $n = $n[abs($x * @n)]; | |
push @events, | |
[ note_on => $dtime, $channel, $n, $velocity ], | |
[ note_off => $tick, $channel, $n, 0 ]; | |
} | |
my $t = 0; | |
my $z = 0; | |
my $swaps = 0; | |
my $dtime = 0; | |
push @events, [ patch_change => $dtime, $channel, $patch ]; | |
while ( $t < $max_step ) { | |
my $x = equation($t); | |
# highlander with the previous value in a perhaps dubious use of xor | |
# but it works so here we are | |
if ( ( $x >= 0 ) ^ ( $z >= 0 ) ) { | |
$swaps++; | |
#say "$t $x $prev"; | |
add_note($dtime, $x); | |
$dtime = 0; | |
} else { | |
$dtime += $tick; | |
} | |
$t += $step; | |
$z = $x; | |
} | |
say "swaps $swaps"; | |
sub make_tracks () { [ MIDI::Track->new( { events => \@events } ) ] } | |
MIDI::Opus->new( | |
{ format => 0, ticks => 96, tracks => make_tracks() } ) | |
->write_to_file($out_file); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment