Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (47 sloc) 1.55 KB
#!/usr/bin/env perl
# Run this, to produce a freq file, then run stat-walk with it.
use strict;
use warnings;
use Data::Dumper;
use MIDI;
use Statistics::Frequency;
use Storable;
# Get the commandline arguments
my $file = shift || die "Usage: perl $0 tune.mid\n";
my $notes = shift || 1; # Number of notes in the preceeding list
my $opus = MIDI::Opus->new({ from_file => $file });
# Collect the MIDI events
my @events = ();
for my $i ( 0 .. scalar( $opus->tracks ) - 1 ) {
my $track = [ $opus->tracks ]->[$i];
push @events, ( grep { $_->[0] eq 'note_on' } $track->events );
}
# Setup the transition buckets
my $transitions = {};
my @preceeding;
# Gather the transitions
for my $event ( @events ) {
# The next note in succession
my $next = $event->[3];
# Add a transition if there are preceeding notes
if ( @preceeding == $notes ) {
push @{ $transitions->{ join ' ', @preceeding } }, $next;
#warn "Datum: @preceeding -> $next\n";
shift @preceeding;
}
# Add the next event to the preceeding event list
push @preceeding, $next;
}
#warn 'Note transitions: ', Dumper$transitions;
# Setup the frequency bucket
my $frequencies = {};
# Instantiate a new stat object
my $stat = Statistics::Frequency->new;
# Gather the proportional frequencies of each transition
for my $note ( keys %$transitions ) {
$stat->add_data( @{ $transitions->{$note} } );
$frequencies->{$note} = { $stat->proportional_frequencies };
$stat->clear_data;
}
warn 'Transition frequencies: ', Dumper$frequencies;
store $frequencies, "$0.dat";