forked from hataori-p/real-voice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantizePitch.lua
404 lines (333 loc) · 10.6 KB
/
quantizePitch.lua
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
SCRIPT_TITLE = "RV Quantize Pitch"
function getClientInfo()
return {
name = SV:T(SCRIPT_TITLE),
author = "Hataori@protonmail.com",
category = "Real Voice",
versionNumber = 2,
minEditorVersion = 65537
}
end
local NOTES = {'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'Bb', 'B'}
local SCALES = {
['chroma'] = 'C-C#-D-D#-E-F-F#-G-G#-A-Bb-B-',
['C/a'] = 'C-D-E-F-G-A-B-',
['C#/Db/bb'] = 'C#-D#-F-F#-G#-Bb-C-',
['D/b'] = 'D-E-F#-G-A-B-C#-',
['Eb/c'] = 'D#-F-G-G#-Bb-C-D-',
['E/c#'] = 'E-F#-G#-A-B-C#-D#-',
['F/d'] = 'F-G-A-Bb-C-D-E-',
['F#/Gb/d#/eb'] = 'F#-G#-Bb-B-C#-D#-F-',
['G/e'] = 'G-A-B-C-D-E-F#-',
['Ab/f'] = 'G#-Bb-C-C#-D#-F-G-',
['A/f#'] = 'A-B-C#-D-E-F#-G#-',
['Bb/g'] = 'Bb-C-D-D#-F-G-A-',
['B/Cb/g#'] = 'B-C#-D#-E-F#-G#-Bb-'
}
local inputForm = {
title = SV:T("Quantization parameters"),
message = SV:T("Set tempo before running this script."),
buttons = "OkCancel",
widgets = {
{
name = "qDiv", type = "ComboBox",
label = SV:T("Time resolution"),
choices = {"Quarter", "1/2 Quarter", "1/4 Quarter", "1/8 Quarter", "1/16 Quarter"},
default = 1
},
{
name = "scale", type = "ComboBox",
label = SV:T("Scale (Maj/Min)"),
choices = {"chroma", "C/a", "C#/Db/bb", "D/b", "Eb/c", "E/c#", "F/d", "F#/Gb/d#/eb", "G/e", "Ab/f", "A/f#", "Bb/g", "B/Cb/g#"},
default = 0
},
{
name = "defLyr", type = "TextBox",
label = SV:T("Lyrics for all notes"),
default = "u"
},
{
name = "loadPitchCheck", type = "CheckBox",
text = SV:T("Load pitch automation"),
default = false
}
}
}
------------ Praat pitch
local praatPitch = {} -- class
do
local PitchHeader = {
{n="File_type", v="File type = \"ooTextFile\"", t="del"},
{n="Object_class", v="Object class = \"Pitch 1\"", t="del"},
{t="del"},
{n="xmin", t="num"},
{n="xmax", t="num"},
{n="nx", t="num"},
{n="dx", t="num"},
{n="x1", t="num"},
{n="ceiling", t="num"},
{n="maxnCandidates", t="num"}
}
function praatPitch:loadPitch(fnam) -- constructor, short text format
local o = {}
setmetatable(o, self)
self.__index = self
local data, header = {}, {}
local fi = io.open(fnam)
for i = 1, #PitchHeader do
local lin = fi:read("*l")
local h = PitchHeader[i]
if h.v then
assert(lin == h.v)
elseif h.t == "num" then
lin = tonumber(lin)
end
if h.n and h.t ~= "del" then
header[h.n] = lin
end
end
header["fileType"] = "ooTextFile"
header["objectClass"] = "Pitch 1"
assert(header.nx)
for i = 1, header.nx do
local pitch = { i = i }
pitch.t = (i - 1) * header.dx + header.x1
local int = fi:read("*n", "*l") -- intensity
local cand = fi:read("*n", "*l") -- candidates
for k = 1, cand do
local f = fi:read("*n", "*l")
if k == 1 then
pitch.f = f
end
fi:read("*n", "*l")
end
table.insert(data, pitch)
end;
fi:close()
o.header = header
o.data = data
return o
end
function praatPitch:getPitch(t) -- ret: f0 [Hz]
if t < self.data[1].t then return 0 end
if t > self.data[#self.data].t then return 0 end
local ll, rr = 1, #self.data
while (rr-ll) > 1 do
local cc = math.floor((rr + ll) / 2)
if t <= self.data[cc].t then
rr = cc
else
ll = cc
end
end
local pf, pt = self.data[ll].f, self.data[rr].f
if pf == 0 or pt == 0 then return 0 end
local pf, pt = math.log(pf), math.log(pt)
local fro, til = self.data[ll].t, self.data[rr].t
return math.exp(pf + (pt - pf) / (til - fro) * (t - fro))
end
end -- end class
--------- qunatization
function HzToHalftone(hz)
if hz<=0 then return end
return 12 * math.log(hz / 440) / math.log(2)
end
-- halftone number from HzToHalftone, 0 = 440 Hz, scale string from SCALES
function isInScale(ht, scale)
local cbase = math.fmod(ht + 57, 12) -- every C = 0
local nam = NOTES[cbase + 1]
return string.find(scale, nam.."-", 1, true)
end
-- quantize to scale
function quantizeNote(hz, scale)
local ht = HzToHalftone(hz)
if not ht then return end
local qht = math.floor(ht + 0.5)
if isInScale(qht, scale) then return qht end
local lht, hht = qht - 1, qht + 1
while not isInScale(lht, scale) do
lht = lht - 1
end
while not isInScale(hht, scale) do
hht = hht + 1
end
if math.abs(ht - lht) < math.abs(ht - hht) then
return lht
else
return hht
end
end
local function getProjectPathName()
local projectFileName = SV:getProject():getFileName()
if not projectFileName then return end
local projectName, projectDir
projectFileName = projectFileName:gsub("\\", "/")
projectDir, projectName = projectFileName:match("^(.*/)([^/]+)%.svp$")
if not projectDir or not projectName then error(SV:T("project dir or name not found")) end
return projectName, projectDir
end
function process()
-- pitch file in project folder
local projectName, projectDir = getProjectPathName()
local fileName = projectDir..projectName.."_pitch.txt"
local fi = io.open(fileName)
if not fi then
SV:showMessageBox(SV:T("Error"), SV:T("Cannot open pitch file").." '"..fileName.."'")
return
else
fi:close()
end
local dlgResult = SV:showCustomDialog(inputForm)
if not dlgResult.status then return end -- cancel pressed
local quarterDivider = 2^dlgResult.answers.qDiv
local qInterval = SV.QUARTER / quarterDivider
local scaleName = inputForm.widgets[2].choices[dlgResult.answers.scale + 1]
local qScale = SCALES[scaleName]
assert(qScale)
local pitch = praatPitch:loadPitch(fileName) -- pitch instance
if not pitch then
SV:showMessageBox(SV:T("Error"), SV:T("wrong file format"))
return
end
local timeAxis = SV:getProject():getTimeAxis()
local scope = SV:getMainEditor():getCurrentGroup()
local group = scope:getTarget()
local tstart = pitch.header.xmin
local tend = pitch.header.xmax
local ints = {}
local tms = timeAxis:getAllTempoMarks()
table.insert(tms, {position = timeAxis:getBlickFromSeconds(tend), positionSeconds = tend})
for tmi = 1, #tms - 1 do
local tm = tms[tmi]
local intNum = SV:blickRoundDiv(tms[tmi + 1].position - tm.position, qInterval)
local b = tm.position
for i = 1, intNum do
local tst, ten = timeAxis:getSecondsFromBlick(b), timeAxis:getSecondsFromBlick(b + qInterval)
local med, cnt, unvoc = {}, 0, 0
local t = tst
while t <= ten do
local f0 = pitch:getPitch(t)
if f0 > 50 then
local qn = quantizeNote(f0, qScale)
table.insert(med, qn)
else
unvoc = unvoc + 1
end
t = t + 0.001
cnt = cnt + 1
end
if #med > 2 and (#med / cnt) > 0.5 then -- more than 50% voiced length
table.sort(med)
med = med[math.floor(#med / 2) + 1]
table.insert(ints, {st = b, en = b + qInterval, pitch = med + 69})
else
table.insert(ints, {st = b, en = b + qInterval})
end
b = b + qInterval
end
end
local notes = {}
local i, j = 1, 1
while i <= #ints do
local p0 = ints[i].pitch
j = i + 1
while j <= #ints and ints[j].pitch and ints[j].pitch == p0 do
j = j + 1
end
table.insert(notes, {st = ints[i].st, en = ints[j - 1].en, pitch = p0})
while j <= #ints and not ints[j].pitch do
j = j + 1
end
i = j
end
-- remove old notes
local ncnt = group:getNumNotes()
if ncnt > 0 then
for i = ncnt, 1, -1 do
group:removeNote(i)
end
end
-- create notes
for i, nt in ipairs(notes) do
local note = SV:create("Note")
note:setTimeRange(nt.st, nt.en - nt.st)
note:setPitch(nt.pitch)
note:setLyrics(dlgResult.answers.defLyr)
group:addNote(note)
end
-- load pitch automation
if dlgResult.answers.loadPitchCheck then
local am = group:getParameter("pitchDelta") -- pitch automation
am:removeAll()
scope:setVoice({
tF0Left = 0,
tF0Right = 0,
dF0Left = 0,
dF0Right = 0,
dF0Vbr = 0
})
local minblicks, maxblicks = math.huge, 0
for i = 1, group:getNumNotes() do
local note = group:getNote(i)
local npitch = note:getPitch()
local ncents = 100 * (npitch - 69) -- A4
local blOnset, blEnd = note:getOnset(), note:getEnd()
am:remove(blOnset, blEnd)
local tons = timeAxis:getSecondsFromBlick(blOnset) -- start time
local tend = timeAxis:getSecondsFromBlick(blEnd) -- end time
local df, f0
local t = tons + 0.0005
while t < tend - 0.0001 do
f0 = pitch:getPitch(t)
if f0 > 50 then -- voiced
df = 1200 * math.log(f0/440)/math.log(2) - ncents -- delta f0 in cents
am:add(timeAxis:getBlickFromSeconds(t), df)
end
t = t + 0.001 -- time step
end
local tempo = timeAxis:getTempoMarkAt(blOnset)
local compensation = tempo.bpm * 6.3417442
if i > 1 then
local pnote = group:getNote(i - 1)
local pnpitch = pnote:getPitch()
local pncents = 100 * (pnpitch - 69) -- A4
local pblOnset, pblEnd = pnote:getOnset(), pnote:getEnd()
local ptons = timeAxis:getSecondsFromBlick(pblOnset) -- start time
local ptend = timeAxis:getSecondsFromBlick(pblEnd) -- end time
if pblEnd == blOnset then
local pts = am:getPoints(blOnset, timeAxis:getBlickFromSeconds(tons + 0.010))
local pdif = ncents - pncents
for _, pt in ipairs(pts) do
local b, v = pt[1], pt[2]
local t = timeAxis:getSecondsFromBlick(b) - tons
local cor = 1 - (1 / (1 + math.exp(-compensation * t)))
am:add(b, v + pdif * cor)
end
end
end
if i < group:getNumNotes() then
local pnote = group:getNote(i + 1)
local pnpitch = pnote:getPitch()
local pncents = 100 * (pnpitch - 69) -- A4
local pblOnset, pblEnd = pnote:getOnset(), pnote:getEnd()
local ptons = timeAxis:getSecondsFromBlick(pblOnset) -- start time
local ptend = timeAxis:getSecondsFromBlick(pblEnd) -- end time
if blEnd == pblOnset then
local pts = am:getPoints(timeAxis:getBlickFromSeconds(tend - 0.010), blEnd - 1)
local pdif = pncents - ncents
for _, pt in ipairs(pts) do
local b, v = pt[1], pt[2]
local t = timeAxis:getSecondsFromBlick(b) - tend
local cor = 1 / (1 + math.exp(-compensation * t))
am:add(b, v - pdif * cor)
end
end
end
am:simplify(blOnset, blEnd, 0.0001)
end
end
end
function main()
process()
SV:finish()
end