-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterpolate.py
127 lines (107 loc) · 5.01 KB
/
interpolate.py
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
"""
MIT License
Copyright (c) 2016 Santi Dsp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Here are the main options to interpolate Ahocoder features
(either can be lf0 or voided-frequency).
"""
from __future__ import print_function
import argparse
import os
import numpy as np
def linear_interpolation(tbounds, fbounds):
"""Linear interpolation between the specified bounds"""
interp = []
for t in range(tbounds[0], tbounds[1]):
interp.append(fbounds[0] + (t - tbounds[0]) * ((fbounds[1] - fbounds[0]) /
(tbounds[1] - tbounds[0])))
return interp
def interpolation(signal, unvoiced_symbol):
tbound = [None, None]
fbound = [None, None]
signal_t_1 = signal[0]
isignal = np.copy(signal)
uv = np.ones(signal.shape, dtype=np.int8)
for t in range(1, signal.shape[0]):
if (signal[t] > unvoiced_symbol) and (signal_t_1 <= unvoiced_symbol) and (tbound == [None, None]):
# First part of signal is unvoiced, set to constant first voiced
isignal[:t] = signal[t]
uv[:t] = 0
elif (signal[t] <= unvoiced_symbol) and (signal_t_1 > unvoiced_symbol):
tbound[0] = t - 1
fbound[0] = signal_t_1
elif (signal[t] > unvoiced_symbol) and (signal_t_1 <= unvoiced_symbol):
tbound[1] = t
fbound[1] = signal[t]
isignal[tbound[0]:tbound[1]] = linear_interpolation(tbound, fbound)
uv[tbound[0]:tbound[1]] = 0
# reset values
tbound = [None, None]
fbound = [None, None]
signal_t_1 = signal[t]
# now end of signal if necessary
if tbound[0] is not None:
isignal[tbound[0]:] = fbound[0]
uv[tbound[0]:] = 0
return isignal, uv
def process_file(filename, unvoiced_symbol, gen_uv):
dire, fullname = os.path.split(filename.rstrip())
basename, ext = os.path.splitext(fullname)
raw = np.loadtxt(filename)
interp, uv = interpolation(raw, unvoiced_symbol)
out_interp_file = os.path.join(dire, basename + '.i' + ext)
print('Writing interpolation to {}'.format(out_interp_file))
np.savetxt(out_interp_file, interp)
if gen_uv:
out_uv_file = os.path.join(dire, basename + '.uv')
print('Writing u/v mask to {}'.format(out_uv_file))
np.savetxt(out_interp_file, uv)
def process_guia(guia_file, unvoiced_symbol, gen_uv):
# Interpolate files values
with open(guia_file) as fh:
for i, filename in enumerate(fh):
process_file(filename.rstrip(), unvoiced_symbol, gen_uv)
def main(opts):
if opts.f0_file:
process_file(opts.f0_file, -10000000000, opts.gen_uv)
if opts.f0_guia:
process_guia(opts.f0_guia, -10000000000, opts.gen_uv)
if opts.vf_file:
process_file(opts.vf_file, 1e3, opts.gen_uv)
if opts.vf_guia:
process_guia(opts.vf_guia, 1e3, opts.gen_uv)
if __name__ == '__main__':
parser = argparse.ArgumentParser('Here are the main options to interpolate'
' Ahocoder features')
parser.add_argument('--f0_guia', type=str,
default=None, help='Guia file containing pointers to '
'the different lf0 files to '
'interpolate.')
parser.add_argument('--f0_file', type=str,
default=None, help='Filename of a single F0 file')
parser.add_argument('--vf_guia', type=str,
default=None, help='Guia file containing pointers to '
'the different vf files to '
'interpolate.')
parser.add_argument('--vf_file', type=str,
default=None, help='Filename of a single VF file')
parser.add_argument('--no-uv', dest='gen_uv',
action='store_false', help='U/V masks are NOT '
'generated.')
parser.set_defaults(gen_uv=True)
options = parser.parse_args()
main(options)