forked from ology/Music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygenic
157 lines (130 loc) · 4.28 KB
/
polygenic
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
#!/usr/bin/env perl
use strict;
use warnings;
use v5.24.0;
use Data::Dumper::Compact 'ddc';
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);
use MIDI::Util qw(set_chan_patch);
use Music::Duration::Partition;
use Music::Scales qw(get_scale_MIDI);
use Music::VoiceGen;
my $bars = shift || 16;
my $bpm = shift || 105;
my $note = shift || 'D';
my $bscale = shift || 'pminor';
my $tscale = shift || 'dorian';
my $bpatch = shift || 35;
my $tpatch = shift || 4;
# Start off in the bass register
my $octave = 1;
# Someone find the drummer!
my $d = MIDI::Drummer::Tiny->new(
file => "$0.mid",
bpm => $bpm,
bars => $bars,
signature => '5/4',
);
my $count = 0;
# Play the parts simultaneously
$d->score->synch(
\&drums,
\&bottom,
\&top,
);
# Write the score to a MIDI file
$d->write;
sub bottom {
set_chan_patch($d->score, 0, $bpatch);
my $beats = 5;
# Create a rhythmic phrase generator
my $mdp = Music::Duration::Partition->new(
size => $beats,
pool => [qw(hn dqn qn en)],
);
# Create 3 phrases
my @phrases = map { { motif => $mdp->motif } } 1 .. 3;
# Create 2 bass octaves
my @pitches = (
get_scale_MIDI($note, $octave, $bscale),
get_scale_MIDI($note, $octave + 1, $bscale),
);
# Create a pitch generator
my $voice = Music::VoiceGen->new(
pitches => \@pitches,
intervals => [qw(-4 -3 -2 -1 1 2 3 4)],
);
# Start the pitch generation in the middle of the register
$voice->context($pitches[12]);
# Create two voice mappings for each phrase
for my $phrase (@phrases) {
push $phrase->{voices}->@*, [ map { $voice->rand } $phrase->{motif}->@* ];
push $phrase->{voices}->@*, [ map { $voice->rand } $phrase->{motif}->@* ];
}
print 'Bottom: ', ddc(\@phrases);
# Add the bass notes to the score
for my $i (1 .. sprintf('%.0f', $count / $beats)) {
# Select a random phrase
my $phrase = $phrases[int rand @phrases];
# Select a random voice mapping
my $voices = ($phrase->{voices}->@*)[int rand $phrase->{voices}->@*];
# Add the voiced motif to the score
for my $n (0 .. $phrase->{motif}->$#*) {
$d->note($phrase->{motif}[$n], $voices->[$n]);
}
}
# Add a final resolving whole note to the score
$d->note($d->whole, $pitches[0]);
}
sub top {
set_chan_patch($d->score, 1, $tpatch);
my $beats = 4;
my $mdp = Music::Duration::Partition->new(
size => $beats,
# pool => [qw(hn qn en)],
# weights => [1, 2, 3],
pool => [qw(hn qn en ten)], # Solo mode
weights => [2, 3, 4, 1],
groups => [0, 0, 0, 3],
);
my @phrases = map { { motif => $mdp->motif } } 1 .. 4;
my @pitches = (
get_scale_MIDI($note, $octave + 2, $tscale),
get_scale_MIDI($note, $octave + 3, $tscale),
);
my $voice = Music::VoiceGen->new(
pitches => \@pitches,
intervals => [qw(-4 -3 -2 -1 1 2 3 4)],
);
for my $phrase (@phrases) {
push $phrase->{voices}->@*, [ map { $voice->rand } $phrase->{motif}->@* ];
push $phrase->{voices}->@*, [ map { $voice->rand } $phrase->{motif}->@* ];
}
print 'Top: ', ddc(\@phrases);
for my $i (1 .. int($count / $beats)) {
my $phrase = $phrases[int rand @phrases];
my $voices = ($phrase->{voices}->@*)[int rand $phrase->{voices}->@*];
if ($i % 2 == 0) {
for my $n (0 .. $phrase->{motif}->$#*) {
# Get a fluctuating velocity between f and fff
my $vol = 'v' . (96 + int(rand 32));
my @halved = diminution($phrase->{motif}[$n]);
$d->note($halved[0], $voices->[$n], $vol);
$d->rest($halved[0]);
}
}
else {
for my $n (0 .. $phrase->{motif}->$#*) {
my $vol = 'v' . (96 + int(rand 32)); # f-fff
$d->note($phrase->{motif}[$n], $voices->[$n], $vol);
}
}
}
$d->note($d->whole, $pitches[0]);
}
sub drums {
$count = $d->bars * $d->beats;
$d->metronome54;
$d->note($d->whole, $d->crash1, $d->kick);
}