-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathshiro-wavsplit.lua
More file actions
175 lines (151 loc) · 4.95 KB
/
Copy pathshiro-wavsplit.lua
File metadata and controls
175 lines (151 loc) · 4.95 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
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
--[[
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("(.-)[^\\/]+$")
if mypath == "" then
mypath = "./"
end
package.path = package.path .. ";" ..
mypath .. "?.lua;" .. mypath .. "external/?.lua"
json = require("dkjson")
getopt = require("getopt")
shiro_cli = require("cli-common")
opts = getopt(arg, "ntdfNsvli")
if opts.h then
print("Usage:")
print("shiro-wavsplit.lua path-to-wav-file")
print(" -n num-utterances")
print(" -t hop-time")
print(" -d feature-dimension")
print(" -f feature-type")
print(" -N num-iter")
print(" -s min-silence-duration")
print(" -v min-voicing-duration")
print(" -l load-existing-model")
print(" -i load-initial-model")
return
end
if opts.n == nil then
print("Error: shiro-wavsplit requires the number of utterances (-n).")
return
end
if #opts._ < 1 then
print("Error: shiro-wavsplit requires an input wav file.")
return
end
local num_utt = tonumber(opts.n)
local min_sil_dur = tonumber(opts.s or "0.3")
local min_voc_dur = tonumber(opts.v or "0.3")
local dir, fullname, ext = fileparts(opts._[1])
local stem = fullname:sub(1, #fullname - #ext - 1)
local path_wav = opts._[1]
local path_raw = dir .. stem .. ".raw"
local path_param = dir .. stem .. ".param"
local path_index = dir .. stem .. ".index"
local path_pm = dir .. stem .. ".phonemap"
local path_md = dir .. stem .. ".modeldef"
local path_seg_init = dir .. stem .. ".init.segm"
local path_seg_aligned = dir .. stem .. ".aligned.segm"
local path_hsmm_uninit = dir .. stem .. ".uninit.hsmm"
local path_hsmm_flat = dir .. stem .. ".flat.hsmm"
local path_hsmm_trained = dir .. stem .. ".trained.hsmm"
if opts.l ~= nil then
path_hsmm_trained = opts.l
end
if opts.i ~= nil then
path_hsmm_flat = opts.i
end
local ndim = tonumber(opts.d or "13")
local thop = tonumber(opts.t or "0.1")
local ftype = opts.f or "mfcc"
local stprune = math.floor(0.2 * num_utt)
local extradur = math.floor(10 / thop)
local niter = tonumber(opts.N or "15")
-- Make the index file.
local fp = io.open(path_index, "wb")
local phonemes = {"sil"}
for i = 1, num_utt do
phonemes[#phonemes + 1] = "utt"
phonemes[#phonemes + 1] = "sil"
end
fp:write(stem .. "," .. table.concat(phonemes, " "))
fp:close()
-- Make the phone map.
fp = io.open(path_pm, "wb")
local phonemap = {
phone_map = {
sil = {
durfloor = {min_sil_dur},
states = {
{out = {0}, dur = 0}
}
},
utt = {
durfloor = {min_voc_dur},
states = {
{out = {1}, dur = 1}
}
}
}
}
fp:write(json.encode(phonemap, {indent = true, keyorder = {"sil", "utt"}}))
fp:close()
-- Make the model definition.
os.execute("lua " .. mypath .. "shiro-pm2md.lua " ..
path_pm .. " -d " .. ndim .. " -t " .. thop .. " > " .. path_md)
-- Feature extraction.
os.execute(mypath .. "shiro-wav2raw -r 16000 -d 0.01 " .. path_wav)
os.execute(mypath .. "shiro-xxcc -f " .. ftype .. " -m " .. (ndim - 1) ..
" -e -l 1024 -p " .. (thop * 16000) .. " -s 16 " .. path_raw .. " > " ..
path_param)
-- Make the initial segmentation.
os.execute("lua " .. mypath .. "shiro-mkseg.lua " .. path_index ..
" -m " .. path_pm .. " -d " .. dir .. " -e .param -n " .. (ndim * 1) ..
" > " .. path_seg_init)
if opts.l == nil and opts.i == nil then
-- Make the initial model.
os.execute(mypath .. "shiro-mkhsmm -c " .. path_md ..
" > " .. path_hsmm_uninit)
os.execute(mypath .. "shiro-init -m " .. path_hsmm_uninit ..
" -s " .. path_seg_init .. " -FT -v 1 > " .. path_hsmm_flat)
end
if opts.l == nil then
-- Training
os.execute(mypath .. "shiro-rest -m " .. path_hsmm_flat ..
" -s " .. path_seg_init .. " -n " .. niter ..
" -d " .. extradur .. " -p " .. stprune ..
" -D -t 0 > " .. path_hsmm_trained)
end
-- Align
os.execute(mypath .. "shiro-align -m " .. path_hsmm_trained ..
" -s " .. path_seg_init .. " -d " .. extradur ..
" -p " .. stprune .. " > " .. path_seg_aligned)
-- Edit and export
fp = io.open(path_seg_aligned, "rb")
local seg = json.decode(fp:read("*a"))
fp:close()
local count = 0
for i, st in ipairs(seg.file_list[1].states) do
if st.ext[1] == "utt" then
st.ext[1] = tostring(count)
count = count + 1
end
end
fp = io.open(path_seg_aligned, "wb")
fp:write(json.encode(seg, {indent = true}))
fp:close()
os.execute("lua " .. mypath .. "shiro-seg2lab.lua " .. path_seg_aligned ..
" -t " .. thop)