-
Notifications
You must be signed in to change notification settings - Fork 5
/
quasi-random-durations
149 lines (124 loc) · 3.73 KB
/
quasi-random-durations
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
#!/usr/bin/env perl
use strict;
use warnings;
use v5.20.0;
use Data::Dumper::Compact 'ddc';
use MIDI::Drummer::Tiny;
use MIDI::Util qw(set_chan_patch);
use Music::Duration::Partition;
use Music::Scales qw(get_scale_MIDI);
use Music::VoiceGen;
my $beats = shift || 4;
my $bars = shift || 16;
my $bpm = shift || 105;
my $note = shift || 'A';
my $bscale = shift || 'pminor';
my $tscale = shift || 'minor';
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,
signature => $beats . '/4',
bars => $bars,
);
# Play the parts simultaneously
$d->score->synch(
\&drums,
\&bottom,
\&top,
);
# Write the scrore to a MIDI file
$d->write;
sub bottom {
set_chan_patch($d->score, 0, $bpatch);
# Create a rhythmic phrase generator
my $mdp = Music::Duration::Partition->new(
size => $beats,
pool => [qw(hn dqn qn)],
# weights => [1, 1, 2],
);
# Create 2 phrases
my @phrases = map { { motif => $mdp->motif } } 1 .. 2;
# 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 at the bottom of the register
$voice->context($pitches[0]);
# 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}->@* ];
}
# Add the bass notes to the score
for my $i (1 .. $d->bars) {
# 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
$mdp->add_to_score($d->score, $phrase->{motif}, $voices);
}
# 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 $mdp = Music::Duration::Partition->new(
size => $beats,
pool => [qw(hn qn en)],
weights => [1, 2, 3],
);
my @phrases = map { { motif => $mdp->motif } } 1 .. 4;
my @pitches = (
get_scale_MIDI($note, $octave + 2, $bscale),
get_scale_MIDI($note, $octave + 3, $bscale),
);
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}->@* ];
}
for my $i (1 .. $d->bars) {
my $phrase = $phrases[int rand @phrases];
my $voices = ($phrase->{voices}->@*)[int rand $phrase->{voices}->@*];
for my $n (0 .. $phrase->{motif}->$#*) {
# Get a fluctuating velocity between f and fff
my $vol = 'v' . (96 + int(rand 32));
$d->note($phrase->{motif}[$n], $voices->[$n], $vol);
}
}
$d->note($d->whole, $pitches[0]);
}
sub drums {
if ($beats == 3) {
$d->metronome34;
}
elsif ($beats == 4) {
$d->metronome44;
}
elsif ($beats == 5) {
$d->metronome54;
}
elsif ($beats == 7) {
$d->metronome74;
}
else {
warn "Unknown beat number\n";
$d->count_in;
}
$d->note($d->whole, $d->crash1, $d->kick);
}