-
Notifications
You must be signed in to change notification settings - Fork 5
/
ngram-play
84 lines (72 loc) · 1.95 KB
/
ngram-play
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
#!/usr/bin/env perl
# Play the top repeated note phrases of a MIDI file.
use strict;
use warnings;
use MIDI::Ngram;
use Getopt::Long;
my %opts = (
file => undef, # MIDI file to process
size => 2, # ngram size
max => 20, # -1 for all records
bpm => 100, # Beats per minute
dura => 'qn tqn', # Note durations
out => "$0.mid", # Output MIDI file
pause => '', # Insert a rest after each phrase
loop => 4, # Times to choose a weighted phrase
weight => 0, # Use weighted counts to play
ranp => 0, # Random patch instead of all piano
shuf => 0, # Shuffle phrases
single => 0, # Allow phrases seen only once
dump => 0, # Dump out MIDI info and exit
);
GetOptions( \%opts,
'help|?',
'man',
'file=s',
'size=i',
'max=i',
'bpm=i',
'dura=s',
'out=s',
'pause=s',
'loop=i',
'weight',
'ranp',
'shuf',
'single',
'dump',
) or die 'Failed GetOptions';
die "Invalid file: $!"
unless $opts{file} && -e $opts{file};
$opts{dura} = [ split /(?:\s+|\s*,\s*)/, $opts{dura} ];
# General MIDI patches that are audible and aren't horrible
my @patches = qw(
0 1 2 4 5 7 8 9
13 16 21 24 25 26
32 34 35 40 42 60
68 69 70 71 72 73
74 79
);
my $mng = MIDI::Ngram->new(
file => $opts{file},
size => $opts{size},
max => $opts{max},
bpm => $opts{bpm},
durations => $opts{dura},
out_file => $opts{out},
pause => $opts{pause},
randpatch => $opts{ranp},
loop => $opts{loop},
weight => $opts{weight},
shuffle_phrases => $opts{shuf},
single => $opts{single},
verbose => 1,
patches => \@patches,
);
$mng->process;
if ( $opts{dump} ) {
$mng->opus->dump( { dump_tracks => 1 } );
exit;
}
$mng->populate;
$mng->write;