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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.