forked from ology/Music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phrase-context
159 lines (134 loc) · 3.9 KB
/
phrase-context
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env perl
use strict;
use warnings;
use lib map { "$ENV{HOME}/sandbox/$_/lib" } qw(MIDI-Drummer-Tiny MIDI-Praxis-Variation MIDI-Util Music-Duration-Partition);
use MIDI::Drummer::Tiny;
use MIDI::Praxis::Variation qw/diminution transposition/;
use MIDI::Util qw(set_chan_patch);
use Music::Scales;
use Music::VoiceGen;
use Music::Duration::Partition;
my $beats = shift || 4;
my $bars = shift || 4;
my $bpm = shift || 105;
my $note = shift || 'A';
my $bscale = shift || 'pminor';
my $tscale = shift || 'minor';
my $bpatch = shift || 35;
my $tpatch = shift || 0;
my $octave = 1;
my @context;
my $d = MIDI::Drummer::Tiny->new(
file => "$0.mid",
bpm => $bpm,
signature => $beats . '/4',
bars => $bars,
kick => 'n36', # Override default patch
snare => 'n40', # "
);
$d->score->synch(
\&drums,
\&bass,
\&top,
);
$d->write;
sub bass {
set_chan_patch($d->score, 1, $bpatch);
my $mdp = Music::Duration::Partition->new(
size => $beats,
pool => [qw/ hn qn /],
weights => [2, 1],
);
my $motif1 = $mdp->motif;
my $motif2 = $mdp->motif;
my @pitches = (
get_scale_MIDI($note, $octave, $bscale),
get_scale_MIDI($note, $octave + 1, $bscale),
);
my @intervals = qw(-4 -3 -2 -1 1 2 3 4);
my $voice = Music::VoiceGen->new(
pitches => \@pitches,
intervals => \@intervals,
);
my @notes1 = map { $voice->rand } @$motif1;
my @notes2 = map { $voice->rand } @$motif2;
push @context, { motif => $motif1, notes => \@notes1 };
push @context, { motif => $motif2, notes => \@notes2 };
for my $n (1 .. $d->bars * 2) {
for my $i (0 .. $#$motif1) {
$d->note($motif1->[$i], $n % 2 ? $notes1[$i] : $notes2[$i]);
}
for my $i (0 .. $#$motif2) {
$d->note($motif2->[$i], $n % 2 ? $notes1[$i] : $notes2[$i]);
}
}
$d->note($d->whole, $pitches[0]);
}
sub top {
set_chan_patch($d->score, 0, $tpatch);
my $mdp = Music::Duration::Partition->new(
size => $beats,
pool => [qw/ dqn qn en /],
);
my $motif1 = $mdp->motif;
my $motif2 = $mdp->motif;
my @pitches = (
get_scale_MIDI($note, $octave + 1, $tscale),
get_scale_MIDI($note, $octave + 2, $tscale)
);
my @intervals = qw(-4 -3 -2 -1 1 2 3 4);
my $voice = Music::VoiceGen->new(
pitches => \@pitches,
intervals => \@intervals,
);
my @notes1 = map { $voice->rand } @$motif1;
my @notes2 = map { $voice->rand } @$motif1;
my @notes3 = map { $voice->rand } @$motif2;
my @notes4 = map { $voice->rand } @$motif2;
my @notes5 = transposition(24, @{ $context[0]->{notes} });
my $cm = $context[0]->{motif};
for my $n (1 .. $d->bars * 2) {
if ($n % 4 == 0 || $n % 6 == 0) {
for my $i (0 .. $#$cm) {
$d->note($cm->[$i], $notes5[$i]);
}
}
else {
for my $i (0 .. $#$motif1) {
$d->note($motif1->[$i], $n % 2 == 0 ? $notes2[$i] : $notes1[$i]);
}
}
if ($n % 3 == 0 || $n % 7 == 0 || $n % 9 == 0) {
for my $i (0 .. $#$cm) {
$d->note($cm->[$i], $notes5[$i]);
}
}
else {
for my $i (0 .. $#$motif2) {
my @halved = diminution($motif2->[$i]);
$d->note($halved[0], $n % 4 == 0 ? $notes4[$i] : $notes3[$i]);
$d->rest($halved[0]);
}
}
}
$d->note($d->whole, $pitches[0]);
}
sub drums {
my $x = $d->bars * 4;
if ($beats == 3) {
$d->metronome34($x);
}
elsif ($beats == 4) {
$d->metronome44($x);
}
elsif ($beats == 5) {
$d->metronome54($x);
}
elsif ($beats == 7) {
$d->metronome74($x);
}
else {
die 'Invalid beats';
}
$d->note($d->whole, $d->crash1, $d->kick);
}