-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdice-game
450 lines (438 loc) · 9.39 KB
/
dice-game
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/usr/bin/env perl
use strict;
use warnings;
##
# Musical dice game, attributed to Mozart
# Write-up: https://ology.github.io/2022/01/17/if-mozart-were-a-programmer/
# 16 measures x 11 dice sums (2-12) = 176 2D-grid of 3/8 measures
# Examples:
# $ perl dice-game
# $ perl dice-game 8 80 0 0
# $ perl dice-game 16 70 4 35 39,45,7,8,26,44,48,9,25,8,37,19,28,39,31,29
# $ timidity dice-game.mid
##
use MIDI::Util qw(setup_score set_chan_patch);
use Music::Cadence ();
use constant DQN => 'dqn'; # Dotted-quarter-note duration
use constant TOTAL_BARS => 48; # Total available measure subroutines
my $max = shift || 16; # Number of measures to produce
my $bpm = shift || 70; # Beats per minute
my $tpatch = shift // 4; # Treble patch
my $bpatch = shift // 0; # Bass patch
my $choice = shift || ''; # Comma-separated sub numbers. Blank for random
# Declare the available measures
my @barst = map { my $sub = 'bart' . $_; \&$sub } 1 .. TOTAL_BARS; # Treble
my @barsb = map { my $sub = 'barb' . $_; \&$sub } 1 .. TOTAL_BARS; # Bass
my $score = setup_score(bpm => $bpm);
# Choose the measures to play - either user defined or at random
# * Technically, this should be the sum of two dice for random choice
my @choices = $choice
? map { $_ - 1 } split /,/, $choice
: map { int rand @barst } 1 .. $max;
# Define the procedures to actually play the chosen measures
my $tproc = sub { # Treble
set_chan_patch($score, 0, $tpatch);
# Add phrases to the score
$barst[$_]->() for @choices;
# Outro
my $mc = Music::Cadence->new(octave => 4, format => 'midinum');
my $chords = $mc->cadence(type => 'plagal');
$score->n(DQN, @$_) for @$chords;
$chords = $mc->cadence(type => 'perfect');
$score->n(DQN, @$_) for @$chords;
};
my $bproc = sub { # Bass
set_chan_patch($score, 1, $bpatch);
# Add phrases to the score
$barsb[$_]->() for @choices;
# Outro
$score->n(DQN, $_) for qw(F3 C3 G3 C2);
};
# Mash the phrases together
$score->synch($tproc, $bproc);
$score->write_score("$0.mid");
# Available measures:
sub bart1 {
print '', (caller(0))[3], "\n";
$score->n('en', $_) for qw(F5 D5 G5);
}
sub barb1 {
$score->n('en', $_) for qw(F3 D3 G3);
}
sub bart2 {
print '', (caller(0))[3], "\n";
$score->n(qw(en A4));
$score->n('sn', $_) for qw(Fs4 G4 B4 G5);
}
sub barb2 {
$score->n(qw(qn B2 G3));
$score->r(qw(en));
}
sub bart3 {
print '', (caller(0))[3], "\n";
$score->n('en', $_) for qw(G5 C5 E5);
}
sub barb3 {
$score->n(qw(qn C3 E3));
$score->r(qw(en));
}
sub bart4 {
print '', (caller(0))[3], "\n";
$score->n(qw(en G5));
$score->n(qw(qn D5));
}
sub barb4 {
$score->n('sn', $_) for qw(G2 B2);
$score->n('en', $_) for qw(G3 B2);
}
sub bart5 {
print '', (caller(0))[3], "\n";
$score->n(qw(qn G5 D5 B4 G4));
$score->r(qw(en));
}
sub barb5 {
$score->n(qw(en G2));
$score->n(qw(sn B3 G3));
$score->n(qw(sn G3 F3));
$score->n(qw(sn Fs3 E3));
$score->n(qw(sn E3 D3));
}
sub bart6 {
print '', (caller(0))[3], "\n";
$score->n('en', $_) for qw(G4 C5 E5);
}
sub barb6 {
barb3();
}
sub bart7 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(E5 C5 E5 G5 C6 G5);
}
sub barb7 {
$score->n(qw(qn C3 G3));
$score->r(qw(en));
}
sub bart8 {
print '', (caller(0))[3], "\n";
$score->n(qw(qn C5));
$score->r(qw(en));
}
sub barb8 {
$score->n('en', $_) for qw(C3 G2 C2);
}
sub bart9 {
print '', (caller(0))[3], "\n";
$score->n(qw(en E5 C5));
$score->n(qw(en B4 D5));
$score->r(qw(en));
}
sub barb9 {
$score->n(qw(qn G3));
$score->n(qw(en G2));
}
sub bart10 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(B4 A4 B4 C5 D5 B4);
}
sub barb10 {
$score->n(qw(qn G3));
$score->r(qw(en));
}
sub bart11 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(E5 C5 B4 A4 G4 Fs4);
}
sub barb11 {
$score->n('en', $_) for qw(C3 D3 D2);
}
sub bart12 {
print '', (caller(0))[3], "\n";
$score->n(qw(en C5 E4)) for 1 .. 3;
}
sub barb12 {
$score->n(qw(en C3)) for 1 .. 3;
}
sub bart13 {
print '', (caller(0))[3], "\n";
$score->n('en', $_) for qw(C5 G4 E4);
}
sub barb13 {
$score->n(qw(qn E3 G3));
$score->r(qw(en));
}
sub bart14 {
bart8();
}
sub barb14 {
barb8();
}
sub bart15 {
print '', (caller(0))[3], "\n";
$score->n(qw(en E5));
$score->n('sn', $_) for qw(G5 E5);
$score->n(qw(en C5));
}
sub barb15 {
$score->n(qw(qn C3 G3));
$score->n(qw(en C3 E3));
}
sub bart16 {
print '', (caller(0))[3], "\n";
$score->n('en', $_) for qw(A5 Fs5 D5);
}
sub barb16 {
$score->n(qw(qn Fs3 D3));
$score->n(qw(en C3 Fs3));
}
sub bart17 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(C5 G4 C5 E5 G4 C5);
}
sub barb17 {
barb13();
}
sub bart18 {
print '', (caller(0))[3], "\n";
$score->n('en', $_) for qw(G4 C5 E5);
}
sub barb18 {
$score->n(qw(qn C3 E3));
$score->n(qw(en C3 G3));
}
sub bart19 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(E5 C5);
$score->n('en', $_) for qw(E5 G5);
}
sub barb19 {
$score->n(qw(qn C3 G3));
$score->n(qw(en C3 E3));
}
sub bart20 {
print '', (caller(0))[3], "\n";
$score->n(qw(en G5));
$score->n('sn', $_) for qw(B5 D6);
$score->n(qw(en D5));
}
sub barb20 {
$score->n(qw(qn D3));
$score->r(qw(en));
}
sub bart21 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(C5 E5 G5 D5 A4 Fs5);
}
sub barb21 {
barb11();
}
sub bart22 {
print '', (caller(0))[3], "\n";
$score->n('en', $_) for qw(E5 C5 G4);
}
sub barb22 {
$score->n(qw(qn C3));
$score->r(qw(en));
}
sub bart23 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(F5 E5 D5 E5 F5 G5);
}
sub barb23 {
$score->n('sn', $_) for qw(F3 E3 D3 E3 F3 G3);
}
sub bart24 {
bart5();
}
sub barb24 {
barb5();
}
sub bart25 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(D4 Fs4 A4 D5 Fs5 A5);
}
sub barb25 {
$score->n(qw(qn D3));
$score->n(qw(en C3));
}
sub bart26 {
print '', (caller(0))[3], "\n";
for (1 .. 3) {
$score->n(qw(en C5 E5));
}
}
sub barb26 {
$score->n('sn', $_) for qw(C3 E3 G3 E3 C4 C3);
}
sub bart27 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(F5 E5 F5 D5 C5 B4);
}
sub barb27 {
$score->n(qw(qn G3 B3));
$score->r(qw(en));
}
sub bart28 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(Fs5 D5 A4 A5 Fs5 D5);
}
sub barb28 {
$score->n(qw(qn C3 A3));
$score->r(qw(en));
}
sub bart29 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(B4 D5 G5 D5);
$score->n(qw(en B4));
}
sub barb29 {
barb9();
}
sub bart30 {
bart5();
}
sub barb30 {
barb5();
}
sub bart31 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(E5 C5);
$score->n('en', $_) for qw(G4 E5);
}
sub barb31 {
$score->n(qw(qn C3 G3));
$score->n(qw(en C3 G3));
}
sub bart32 {
print '', (caller(0))[3], "\n";
$score->n('en', $_) for qw(G4 C5 F5);
}
sub barb32 {
barb6();
}
sub bart33 {
bart5();
}
sub barb33 {
barb5();
}
sub bart34 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(E5 C5 D5 B4);
$score->n(qw(en G4));
}
sub barb34 {
barb10();
}
sub bart35 {
print '', (caller(0))[3], "\n";
$score->n('en', $_) for qw(A4 D5 Fs5);
}
sub barb35 {
$score->n(qw(qn Fs3 D3));
$score->n(qw(en C3 A3));
}
sub bart36 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(A4 E5 D5 G5 Fs5 A5);
}
sub barb36 {
barb11();
}
sub bart37 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(G5 B5 G5 D5);
$score->n(qw(en B4));
}
sub barb37 {
$score->n(qw(qn B3 D3));
$score->r(qw(en));
}
sub bart38 {
print '', (caller(0))[3], "\n";
$score->n('en', $_) for qw(C5 G4 E5);
}
sub barb38 {
for (1 .. 3) {
$score->n(qw(sn C3 E3));
$score->n(qw(sn G3));
}
}
sub bart39 {
print '', (caller(0))[3], "\n";
$score->n('en', $_) for qw(G5 G4 G4);
}
sub barb39 {
$score->n('sn', $_) for qw(B2 D3 G3 D3 B2 G2);
}
sub bart40 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(C5 B4 C5 E5 G4 C5);
}
sub barb40 {
barb6();
}
sub bart41 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(C5 B4 C5 E5);
$score->n(qw(en G4));
}
sub barb41 {
barb6();
}
sub bart42 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(B4 C5 D5 B4 A4 G4);
}
sub barb42 {
$score->n(qw(qn A2));
$score->r(qw(en));
}
sub bart43 {
print '', (caller(0))[3], "\n";
$score->n(qw(en G5));
$score->n('sn', $_) for qw(F5 E5 D5 C5);
}
sub barb43 {
barb6();
}
sub bart44 {
print '', (caller(0))[3], "\n";
$score->n(qw(en A4));
$score->n('sn', $_) for qw(F5 D5 A4 B4);
}
sub barb44 {
$score->n(qw(qn F3));
$score->n(qw(en G3));
}
sub bart45 {
print '', (caller(0))[3], "\n";
$score->n('sn', $_) for qw(C5 B4 C5 G4 E4 C4);
}
sub barb45 {
barb13();
}
sub bart46 {
print '', (caller(0))[3], "\n";
$score->n(qw(en G5));
$score->n('sn', $_) for qw(B5 G5 D5 B4);
}
sub barb46 {
barb37();
}
sub bart47 {
print '', (caller(0))[3], "\n";
$score->n(qw(en G5));
$score->n('sn', $_) for qw(G5 D5);
$score->n(qw(en B5));
}
sub barb47 {
barb37();
}
sub bart48 {
print '', (caller(0))[3], "\n";
$score->n(qw(en E5));
$score->n('sn', $_) for qw(C5 E5 G5 C6);
}
sub barb48 {
barb19();
}