-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinversion
43 lines (35 loc) · 1.33 KB
/
inversion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env perl
use strict;
use warnings;
# This program names all musical chord permutations given a set of notes.
# The lists generated are not musical inversions but rather mathematical
# perversions of chords.
# Load handy modules.
use Math::Combinatorics;
use Music::Chord::Namer qw( chordname );
# Get a list of notes from the command-line or use a Cmaj triad.
my @notes = @ARGV ? map { ucfirst } @ARGV : qw( C E G );
# Get a list of all possible permutations.
my @perms = permute( @notes );
# Declare chord-name <=> permutation dictionaries.
my %chords_by_perms = ();
my %perms_by_chords = ();
# Traverse the permutations.
for my $p ( @perms ) {
# Record the chord names for each permutation.
for my $name ( chordname(@$p) ) {
# Make a chord-name to permutation table entry.
push @{ $chords_by_perms{ $name } }, "@$p";
# Make a permutation to chord-name table entry.
push @{ $perms_by_chords{ "@$p" } }, $name;
}
}
# Curiosity may or may not have killed Schrödinger's cat.
use Data::Dumper;
# Show chord-name to permutations.
warn Data::Dumper->new([\%chords_by_perms])->Indent(1)->Terse(1)->Quotekeys(0)->Sortkeys(1)->Dump;
# Show permutation to chord-names.
warn Data::Dumper->new([\%perms_by_chords])->Indent(1)->Terse(1)->Quotekeys(0)->Sortkeys(1)->Dump;
__END__
In vi exec:
:!perl % c\# e gb|less