#!/usr/bin/perl ########################################### # Variable Length Markov Diddly. ########################################### use strict; use constant LENGTH => 4; # length of pattern key my %probs; # probabilities of next note while () { # read the existing 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 'prob' 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-separated key # a bunch of common session tunes (plus one of mine ) __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,fs2,2,e2,2,d2,2 d2,2,g2,2,g2,2,b2,2,g2,2,g2,2,a2,2,g2,2,a2,2,b2,2,g2,2,g2,2,e3,2,g3,2,e3,2,d3,2,b2,2,g2,2,a2,2,g2,2,a2,2,b2,2,g2,2,e2,2,d2,2,g2,2,g2,2,b2,2,g2,2,g2,2,a2,2,g2,2,a2,2,b2,2,g2,2,g2,2,e3,2,g3,2,e3,2,d3,2,b2,2,g2,2,a2,2,g2,2,fs2,2,g2,6 d2,4,d2,2,a2,6,b2,2,a2,2,g2,2,a2,2,b2,2,a2,2,d2,4,d2,2,a2,6,b2,2,a2,2,g2,2,e2,2,g2,2,e2,2,d2,4,d2,2,a2,6,b2,2,a2,2,g2,2,a2,2,b2,2,c3,2,d2,2,c3,2,b2,2,c3,2,b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