-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbase-primes
64 lines (47 loc) · 1.49 KB
/
base-primes
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env perl
# Play the prime numbers as base converted note strings
use strict;
use warnings;
use List::Util qw(uniq);
use lib $ENV{HOME} . '/sandbox/MIDI-Util/lib';
use MIDI::Util qw(setup_score);
use Math::BaseConvert;
use Math::Prime::XS qw(primes);
use Music::Scales;
my $patch = shift || 5;
my $bpm = shift || 100;
my $limit = shift || 500; # Max number of primes
my @scale = @ARGV; # C4 Ds4 F4 G4 As4 C5 <- Example notes to choose from
my $as_chords = 1; # Boolean
if (!@scale) {
@scale = get_scale_notes('A', 'pminor');
@scale = map { $_ . 4 } @scale;
for (@scale) {
s/#/s/;
s/b/f/;
}
}
my $notes = join '|', @scale; # "OR regular expression" of scale notes
Math::BaseConvert::dig(\@scale); # Set the base digits to the scale notes
my $score = setup_score(patch => $patch, bpm => $bpm);
my $note_count = 0;
my $phrase_count = 0;
# For each prime number...
for my $n (primes($limit)) {
my $x = cnv($n, 10, scalar(@scale)); # Base-convert the decimal to a note string
my @phrase = $x =~ /($notes)/g; # Separate the note string into an array
# Output the progress:
printf "%d => %s (%d)\n", $n, join(' ', @phrase), scalar(@phrase);
if ($as_chords) {
$score->n('qn', uniq(@phrase));
}
else {
$score->n('qn', $_) for @phrase;
}
$phrase_count++;
$note_count += @phrase;
}
$score->n('wn', $scale[0]);
$score->write_score("$0.mid");
print "Total notes: $note_count\n";
print "Total phrases: $phrase_count\n";