-
Notifications
You must be signed in to change notification settings - Fork 5
/
chord-mutate
79 lines (58 loc) · 1.47 KB
/
chord-mutate
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env perl
use strict;
use warnings;
use MIDI::Util qw(setup_score set_chan_patch);
use Music::ScaleNote;
my $max = shift || 16; # loop bound
my $trebp = shift || 4; # Treble patch
my $bassp = shift || 35; # Bass patch
my @chord = qw( C4 Ds4 C5 );
my @bass = qw( C3 G3 As3 C4 );
my @duras = qw( en qn qn );
my $half = 'hn';
my $whole = 'wn';
my $score = setup_score();
my $msn = Music::ScaleNote->new(
scale_note => 'C',
scale_name => 'pminor',
note_format => 'midi',
# verbose => 1,
);
$score->synch(
\&top,
\&bottom,
);
$score->write_score("$0.mid");
sub top {
set_chan_patch( $score, 0, $trebp );
# $score->r('hn');
for my $n ( 1 .. $max + 2 ) {
$score->n( $duras[ int rand @duras ], @chord );
@chord = mutate( $msn, @chord );
}
$score->n( $half, @chord );
}
sub mutate {
my ( $msn, @chord ) = @_;
my $roll1 = $chord[ int rand @chord ];
my $roll2 = $chord[ int rand @chord ];
my @mutated;
for my $n ( @chord ) {
if ( $n eq $roll1 || $n eq $roll2 ) {
my $note = $msn->get_offset(
note_name => $n,
offset => int rand 2 ? 1 : -1,
);
$n = $note->format('midi');
}
push @mutated, $n;
}
return @mutated;
}
sub bottom {
set_chan_patch( $score, 1, $bassp );
for my $n ( 1 .. $max / 4 ) {
$score->n( $whole, $bass[ int rand @bass ] );
}
$score->n( $whole, $bass[0] );
}