As part of side-project (inspired by this) to breed Irish jigs (just the 'A' part to start off with), I had the need for a large, random but still 'genetically strong' population of tunes - I was getting bored waiting for 100 generations of random notes to start getting interesting.

To that end, here's a little Markov-chain routine that analyses as many tunes as I could be bothered to type in and then prints "random diddly". The LENGTH constant can be used to refine the length of the chain 'key' - 1 will just pick single random weighted notes from a flat data structure, whereas a setting of 8 or 9 will duplicate an existing tune near-exactly (although some of the more 'symmetrical' tunes) can wrap round a few times before stopping). A setting of 3 or 4 can produce some quite interesting tunes - not structured, but then that's what the breeding will attempt to achieve.

When I get a chance, I'll write a ABC P::RD module so I don't have to type in the data (or play back the notes off the screen on my penny-whistle :) ) - unless anybody can point me in the direction of an existing one.

All suggestions are as ever welcome.
#!/usr/bin/perl ########################################### # Variable Length Markov Diddly. ########################################### use strict; use constant LENGTH => 4; # length of patte +rn key my %probs; # probabilities +of next note while (<DATA>) { # read the existin +g tunes... chomp; my @line = split /,/; my @notes = ("START") x (LENGTH); # cyclic buffer while (@line) { shift (@notes); push @notes, shift(@line).",".shift(@line); # "note,length" $probs{key(@notes)}{$notes[$#notes]}++; } shift (@notes); $probs{key(@notes)}{"END"}++; } my @notes = ("START") x LENGTH; do { shift(@notes); my %next = %{$probs{key(@notes)}}; # array of probs my @next = map {($_) x $next{$_}} keys %next; # 1 entry per 'pr +ob' push @notes, $next[int(rand(@next))]; print "$notes[$#notes] " unless ($notes[$#notes] eq "END"); } while ($notes[$#notes] ne "END"); sub key {return join("",@_[0..LENGTH-2]);} # make space-sep +arated key # a bunch of common session tunes (plus one of mine <g>) __DATA__ e2,4,e2,2,b2,2,e2,2,b2,2,e2,2,e2,2,b2,2,a2,2,fs2,2,d2,2,e2,4,e2,2,b2,4 +,cs3,2,d3,2,cs3,2,b2,2,a2,2,fs2,2,d2,2,e2,4,e2,2,b2,2,e2,2,b2,2,e2,2, +e2,2,b2,2,a2,2,fs2,2,d2,2,g2,4,g2,2,fs2,2,g2,2,a2,2,b2,2,a2,2,g2,2,fs +2,2,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2,12 g2,2,g2,2,g2,2,g2,6,g2,2,g2,2,g2,2,g2,4,d3,2,b2,4,g2,2,b2,4,d3,2,g3,4, +d3,2,b2,4,g2,2,d2,2,d2,2,d2,2,d2,6,d2,2,d2,2,d2,2,d2,4,a2,2,fs2,4,d2, +2,fs2,4,a2,2,c3,4,a2,2,fs2,4,d2,2 a2,6,a2,6,a2,2,fs2,2,d3,2,a2,2,fs2,2,d2,2,g2,6,g2,6,g2,2,e2,2,fs2,2,g3 +,2,fs2,2,e2,2,a2,6,a2,6,a2,2,fs2,2,d3,2,a2,2,fs2,2,d2,2,e2,2,fs2,2,g2 +,2,fs2,2,g2,2,e2,2,d2,12 a2,2,d2,2,d2,2,b2,2,d2,2,d2,2,c3,6,c3,2,b2,2,a2,2,g2,2,a2,2,b2,2,a2,2, +g2,2,fs2,2,g2,2,fs2,2,g2,2,e2,2,fs2,2,g2,2,a2,2,d2,2,d2,2,b2,2,d2,2,d +2,2,c3,6,c3,2,b2,2,a2,2,g2,2,a2,2,b2,2,a2,2,g2,2,fs2,2,g2,12
Cheers,
Ben

In reply to Markov Diddly by benn

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.