-
Notifications
You must be signed in to change notification settings - Fork 14
/
vsq2mid.pl
executable file
·221 lines (211 loc) · 4.49 KB
/
vsq2mid.pl
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
#!/usr/bin/perl
#
# eCantorix - singing speech synthesis using eSpeak
# Copyright (C) 2012 Rudolf Polzer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use FindBin;
use lib $FindBin::Bin;
use strict;
use warnings;
use MIDI;
use Getopt::Long;
use eCantorix::Util;
our %VSQ_PHONEMES = (
"a" => "A:",
"b" => "b",
"b'" => "b",
"C" => "C",
"d" => "d",
"d'" => "d",
"e" => "e",
"g" => "g",
"g'" => "g",
"h" => "h",
"i" => "I",
"j" => "j",
"J" => "n^",
"k" => "k",
"k'" => "k",
"m" => "m",
"m'" => "m",
"M" => "u:", # ?
"n" => "n",
"N" => "N",
"N'" => "N",
"N\\" => "N",
"N\\'" => "N",
"o" => "o:", # ?
"p" => "p",
"p\\" => "f",
"p'" => "p",
"p\\'" => "f",
"s" => "s",
"S" => "S",
"t" => "t",
"t'" => "tC",
"w" => "w",
"z" => "z",
"Z" => "Z",
"1" => "h",
"2" => "h",
"3" => "h",
"4" => "l", # Japanese can't speak r ;)
"4'" => "l", # Japanese can't speak r ;)
"5" => "h",
"6" => "h",
"*" => "h",
"dz" => "dz",
"ts" => "ts",
"tS" => "tS",
"dZ" => "dZ"
);
sub mapphonemes($)
{
my ($phonemes) = @_;
my $ephonemes = "";
for(split /\s+/, $phonemes)
{
if(exists $VSQ_PHONEMES{$_})
{
$ephonemes .= $VSQ_PHONEMES{$_};
}
else
{
warn "Unknown phoneme: $_, trying as-is";
$ephonemes .= $_;
}
}
if($ephonemes !~ /[\@325aAeEiIoO0uUV]/)
{
# if all we have is consonants, repeat the last one a lot
# note: for n^ we need to repeat the n
$ephonemes =~ s/(.)(\^?)$/$1$1$1$1$1$1$1$1$2/;
}
return $ephonemes;
}
sub mapAnote($$$$$)
{
my ($start, $ev, $lyric, $channel, $options) = @_;
my @events = ();
my $len = $ev->{'Length'};
my $note = $ev->{'Note#'};
my $velocity = $ev->{'Dynamics'};
unless($lyric->{L0} =~ /^"([^"]*)","([^"]*)"/)
{
warn "Invalid lyric L0 info: $lyric->{L0}";
return ();
}
my $text = $1;
my $phonemes = $2;
if($options->{use_phonemes})
{
my $ephonemes = mapphonemes $phonemes;
push @events, ['lyric', $start, "[[$ephonemes]]"];
}
else
{
if($text ne "-")
{
push @events, ['lyric', $start, $text];
}
}
push @events, ['note_on', $start, $channel, $note, $velocity];
push @events, ['note_off', $start + $len, $channel, $note, $velocity];
return @events;
}
sub mapvsqhash($$$)
{
my ($ini, $channel, $options) = @_;
my @events = ();
for my $k(sort { $a <=> $b } keys %{$ini->{EventList}})
{
my $v = $ini->{EventList}->{$k};
next
if $v eq 'EOS';
my $ev = $ini->{$v};
if($ev->{Type} eq 'Anote')
{
my $lyrichandle = $ev->{'LyricHandle'};
my $lyric = $ini->{$lyrichandle};
push @events,
mapAnote $k, $ev, $lyric, $channel, $options;
}
else
{
warn "Unsupported VSQ event: $ev->{Type}";
}
}
return @events;
}
sub maptrack($$)
{
my ($track, $options) = @_;
my @vsq_ini = ();
my @events = ();
my %channels = ();
for(abstime $track->events())
{
if($_->[0] eq 'text_event' && $_->[2] =~ /^DM:(\d+):(.*)/s)
{
$vsq_ini[$1] = $2;
}
else
{
push @events, $_;
if($_->[0] eq 'control_change')
{
++$channels{$_->[2]};
}
}
}
if(@vsq_ini)
{
my $channel = 0;
my @channels = keys %channels;
warn "No unique channel on a vocaloid track: @channels\n"
if @channels != 1;
$channel = $channels[0]
if @channels == 1;
my $vsq_ini = join "", @vsq_ini;
require Config::Tiny;
my $ini = Config::Tiny->read_string($vsq_ini)
or die "VSQ import: invalid INI file";
push @events, mapvsqhash $ini, $channel, $options;
$track->events_r([reltime sorttime @events]);
}
}
sub mapopus($$)
{
my ($opus, $options) = @_;
for my $track($opus->tracks())
{
maptrack $track, $options;
}
}
my $infile = "-";
my $outfile = "-";
my $options = {
use_phonemes => 0
};
Getopt::Long::Configure("gnu_getopt", "auto_help", "auto_version");
GetOptions(
'input|i=s' => \$infile,
'output|o=s' => \$outfile,
'phonemes|p' => \$options->{use_phonemes}
);
my $opus = MIDI::Opus->new({from_file => $infile})
or die "MIDI::Opus: could not read $infile";
mapopus $opus, $options;
$opus->write_to_file($outfile);