The following snippet is a follow-up to Random midi player, the first version I wrote. This one is not obfuscated in any way whatsoever. It randomly select 3 non-percussion instruments and tacks on the percussion instruments. Each instrument randomly selects 50 notes to play. The percussion doesn't play a note on every beat, just to play with things. So 3-4 instruments all play at the same time, each with their own notes. This was just a test on my play_notes subroutine :P
The script takes one optional argument: the number of beats per minute to play. Default is 240. Midi plays automatically on *nix if you have timidity installed. Code follows.
#!/usr/bin/perl -w use strict; use MIDI::Simple; sub play_notes { my @measures = @_; my $tmp_note = $measures[0]; $tmp_note =~ s/;\s*[^:]+:.*$//; my $note_count; ++$note_count while $tmp_note =~ m#\s+#g; my $notes; for my $index (0 .. $#measures) { for my $play (split /;\s*/, $measures[$index]) { my ($inst, $note) = $play =~ m#^([^:]+):\s*(.*)$#; @{$notes->[$index]->{$inst}} = split /\s+/, $note; } } for my $index (0 .. $#{$notes}) { for my $index2 (0 .. $note_count-1) { my @play; for my $inst (keys %{$notes->[$index]}) { my ($channel, $patch) = split '/', $inst; push @play, sub{ my $it = shift; patch_change $channel, $patch; $it->n("c$channel", $notes->[$index]->{$inst}->[$index2]); }; } synch @play; } } } new_score; set_tempo( 60000000 / ($ARGV[0] || 240) ); my @notes = join '; ', ( ( map{ "1/$_: " . join ' ', map { int(rand 128) } 1..50 } int(rand 128), int(rand 128), int(rand 128) ), ( "9/0: " . join ' ', map { $_ % (int(rand 2) ? 3 : 5 ) ? int(rand 53)+35 : 'rest' } 1..50 ) ); play_notes(@notes); print "Playing midi music...\n"; open Q, "| timidity - > /dev/null"; write_score \*Q; close Q;
If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.
In reply to Random midi player, version 2 by Coruscate
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |