-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.raku
executable file
·121 lines (101 loc) · 3.26 KB
/
video.raku
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
#!/usr/bin/env raku
# --------------------------------------------------------------------
class SvgImg {
has $.svg is rw;
method open (
$width = 960 * 4,
$height = 540 * 4,
$bg-color = 'black',
) {
my $inst = self.bless;
$inst.svg = qq:to/END/;
<svg
xmlns="http://www.w3.org/2000/svg"
width="$width"
height="$height"
style="background-color: $bg-color"
>
END
return $inst;
}
method close { $!svg ~= "</svg>\n"; }
method open-group { $!svg ~= " <g>\n"; }
method close-group { $!svg ~= " </g>\n"; }
method rectangle ($width, $height, $x, $y, $fill) {
$!svg ~= " <rect width='$width' height='$height'";
$!svg ~= " x='$x' y='$y' style='fill: $fill'/>\n";
}
}
# --------------------------------------------------------------------
sub MAIN ($score-file, $dst-dir) {
mkdir $dst-dir;
my @frames = get_frames($score-file);
# Keep just the basename, truncating the extension.
(my $basename = $score-file.IO.basename) ~~ s/ '.' .* //;
my $rect-wyd = 90;
my $rect-hyt = 60;
for @frames.kv -> $iFrame, @frame {
my $img = SvgImg.open;
for @frame.kv -> $iRow, @row {
$img.open-group();
for @row.kv -> $iElem, $elem {
my $y = $iRow * $rect-hyt;
my $x = $iElem * $rect-wyd;
my $color = do given $elem {
when '*' { 'red' }
when '0' { 'yellow' }
when / <[a .. h]> | z / { 'pink' }
when / <[A .. H]> | Z / { 'green' }
when / <[1 .. 9]> / { 'blue' }
default { 'black' }
};
$img.rectangle(
$rect-wyd,
$rect-hyt,
$x,
$y,
$color,
);
# Last played note marker.
$img.rectangle(
$rect-wyd / 2,
$rect-hyt / 2,
$x + ($rect-wyd / 4),
$y + ($rect-hyt / 4),
'purple',
) if $elem eq 'z' | 'Z' | 9;
}
$img.close-group();
}
$img.close;
spurt sprintf(
"$dst-dir/$basename.%02d.svg",
$iFrame + 1
), $img.svg;
}
}
# --------------------------------------------------------------------
sub get_frames ($score-file) {
my @frame;
my @frames;
for $score-file.IO.lines -> $l {
my $line = $l.trim;
# Ignore empty lines, comments 「-」, and sections 「@」.
if $line.chars && $line !~~ /^ <[-@]> / {
# Create a new frame.
if $line.starts-with: '#' {
if @frame.elems {
@frames.push: @frame.clone;
@frame = Empty;
}
}
# Save a frame's row.
else {
my @row = $line.comb;
@frame.push: @row;
}
}
}
@frames.push: @frame;
return @frames;
}