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