-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathshiro-pm2md.lua
More file actions
120 lines (104 loc) · 3.04 KB
/
Copy pathshiro-pm2md.lua
File metadata and controls
120 lines (104 loc) · 3.04 KB
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
--[[
SHIRO
===
Copyright (c) 2017-2018 Kanru Hua. All rights reserved.
This file is part of SHIRO.
SHIRO 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.
SHIRO 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 SHIRO. If not, see <http://www.gnu.org/licenses/>.
]]
local mypath = arg[0]:match("(.-)[^\\/]+$")
package.path = package.path .. ";" ..
mypath .. "?.lua;" .. mypath .. "external/?.lua"
json = require("dkjson")
getopt = require("getopt")
shiro_cli = require("cli-common")
opts = getopt(arg, "dt")
if opts.h then
print("Usage:")
print("shiro-pm2md.lua path-to-phonemap-file -d dimension -t hop-time")
return
end
local input_pm = opts._[1]
local ndim = tonumber(opts.d or "12")
local thop = tonumber(opts.t or "0.01")
if input_pm == nil then
print("Error: shiro-pm2md requires an input phonemap file.")
return
end
local fh = io.open(input_pm, "r")
if fh == nil then
print("Error: cannot open " .. input_pm)
return
end
local pm = json.decode(fh:read("*a"))
io.close(fh)
if pm == nil then
print("Error: " .. input_pm .. " is not a valid JSON file.")
return
end
if shiro_cli.checkpm(pm) == false then return end
local maxdurstate = 0
local maxoutstate = {}
local dur_attr = {}
local nstream = 0
for p, pv in pairs(pm.phone_map) do
for i, pst in ipairs(pv.states) do
maxdurstate = math.max(maxdurstate, pst.dur + 1)
if nstream == 0 then
nstream = #pst.out
maxoutstate = pst.out
else
if nstream ~= #pst.out then
print("Error: inconsistent number of streams.")
return
end
end
for j = 1, nstream do
maxoutstate[j] = math.max(maxoutstate[j], pst.out[j] + 1)
end
end
if pv.durceil ~= nil then
for i, iceil in ipairs(pv.durceil) do
local nceil = math.ceil(iceil / thop)
dur_attr[pv.states[i].dur] = {ceil = nceil}
end
end
if pv.durfloor ~= nil then
for i, ifloor in ipairs(pv.durfloor) do
local nfloor = math.ceil(ifloor / thop)
local dst = dur_attr[pv.states[i].dur]
if dst == nil then
dst = {}
dur_attr[pv.states[i].dur] = dst
end
dst.floor = nfloor
end
end
end
output_md = {
ndurstate = maxdurstate,
streamdef = {},
dur_attr = {}
}
for i = 1, nstream do
output_md.streamdef[i] =
{nstate = maxoutstate[i], ndim = ndim, nmix = 1, weight = 1}
end
for i, attr in pairs(dur_attr) do
output_md.dur_attr[#output_md.dur_attr + 1] = {
index = i,
ceil = attr.ceil,
floor = attr.floor
}
end
print(json.encode(output_md, {indent = true,
keyorder = {"ndurstate", "streamdef", "nstate", "ndim", "nmix", "weight",
"dur_attr", "index", "ceil"}}))