-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathshiro-mkpm.lua
More file actions
113 lines (99 loc) · 2.85 KB
/
Copy pathshiro-mkpm.lua
File metadata and controls
113 lines (99 loc) · 2.85 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
--[[
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, "sSt")
if opts.h then
print("Usage:")
print("shiro-mkpm.lua path-to-phoneset\n" ..
" -s state-per-phoneme -S num-stream -t default-topology\n" ..
" -w (weak skips by default)")
return
end
local state_per_phoneme = tonumber(opts.s or "3")
local num_stream = tonumber(opts.S or "3")
local default_topology = opts.t
local default_weak_skips = opts.w
if #opts._ < 1 then
print("Error: shiro-mkpm requires an input phoneme set.")
return
end
local input_list = opts._[1]
local phonemes = {}
local attrs = {}
local fh = io.open(input_list, "r")
if fh == nil then
print("Error: cannot open " .. input_list)
return
end
while true do
local line = fh:read()
if line == nil or line == "" then
break
end
line = string.delimit(line, " ")
line_attr = {}
for i = 2, #line, 2 do
line_attr[line[i]] = line[i + 1]
end
phonemes[#phonemes + 1] = line[1]
attrs[#phonemes] = line_attr
end
io.close(fh)
local function round3(x)
return math.round(x * 1000) / 1000
end
local stcount = 0
local output = {}
output.phone_map = {}
for i = 1, #phonemes do
local states = {}
for j = 1, state_per_phoneme do
states[j] = {dur = stcount, out = {}}
for k = 1, num_stream do
states[j].out[k] = stcount
end
stcount = stcount + 1
end
local dst = {states = states}
if default_topology ~= nil then
dst.topology = default_topology
end
if default_weak_skips ~= nil then
dst.pskip = 0.02
end
for k, v in pairs(attrs[i]) do
if k == "durfloor" then
dst.durfloor = {}
for j = 1, state_per_phoneme do
dst.durfloor[j] = round3(tonumber(v) / state_per_phoneme)
end
end
if k == "durceil" then
dst.durceil = {}
for j = 1, state_per_phoneme do
dst.durceil[j] = round3(tonumber(v) / state_per_phoneme)
end
end
end
output.phone_map[phonemes[i]] = dst
end
print(json.encode(output, {indent = true, keyorder = phonemes}))