forked from sotelo/world.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.pyx
222 lines (175 loc) · 8.46 KB
/
world.pyx
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
import cython
from libc.stdlib cimport free
from cpython cimport PyObject, Py_INCREF
import numpy as np
cimport numpy as np
cimport world
np.import_array()
@cython.boundscheck(False)
@cython.wraparound(False)
cdef class ArrayWrapper:
cdef void* data_ptr
cdef int size
cdef set_data(self, int size, void* data_ptr):
self.data_ptr = data_ptr
self.size = size
def __array__(self):
cdef np.npy_intp shape[1]
shape[0] = <np.npy_intp> self.size
# Create a 1D array, of length 'size'
ndarray = np.PyArray_SimpleNewFromData(1, shape,
np.NPY_DOUBLE, self.data_ptr)
return ndarray
def __dealloc__(self):
free(<void*>self.data_ptr)
def readwav(filename):
cdef np.ndarray[int, ndim=1, mode="c"] fs
cdef np.ndarray[int, ndim=1, mode="c"] nbit
cdef np.ndarray[int, ndim=1, mode="c"] x_length
fs = np.zeros(1, dtype = np.dtype('int32'))
nbit = np.zeros(1, dtype = np.dtype('int32'))
x_length = np.zeros(1, dtype = np.dtype('int32'))
cdef double *array
cdef np.ndarray ndarray
array = wavread(filename, &fs[0], &nbit[0], &x_length[0])
array_wrapper = ArrayWrapper()
array_wrapper.set_data(x_length[0], <void*> array)
ndarray = np.array(array_wrapper, copy=False)
ndarray.base = <PyObject*> array_wrapper
Py_INCREF(array_wrapper)
return fs[0], nbit[0], x_length[0], ndarray
def writewav(np.ndarray[double, ndim=1, mode="c"] x not None, int fs, int nbit, filename):
cdef x_length = len(x)
wavwrite(&x[0], x_length, fs, nbit, filename)
def dio(np.ndarray[double, ndim=1, mode="c"] x not None, int fs, double period, option):
cdef np.ndarray[double, ndim=1, mode="c"] f0
cdef np.ndarray[double, ndim=1, mode="c"] time_axis
x_length = len(x)
f0_length = GetSamplesForDIO(fs, x_length, period)
f0 = np.zeros(f0_length, dtype = np.dtype('float64'))
time_axis = np.zeros(f0_length, dtype = np.dtype('float64'))
Dio(&x[0], x_length, fs, option.option, &time_axis[0], &f0[0])
return f0, time_axis
def stonemask(np.ndarray[double, ndim=1, mode="c"] x not None, int fs, double period,
np.ndarray[double, ndim=1, mode="c"] time_axis not None,
np.ndarray[double, ndim=1, mode="c"] f0 not None):
cdef np.ndarray[double, ndim=1, mode="c"] refined_f0
refined_f0 = np.copy(f0)
f0_length = len(f0)
x_length = len(x)
StoneMask(&x[0], x_length, fs, &time_axis[0], &f0[0], f0_length, &refined_f0[0])
return refined_f0
def star(np.ndarray[double, ndim=1, mode="c"] x not None, int fs, double period,
np.ndarray[double, ndim=1, mode="c"] time_axis not None,
np.ndarray[double, ndim=1, mode="c"] f0 not None):
x_length = len(x)
cdef int fft_size = GetFFTSizeForCheapTrick(fs)
cdef int f0_length = len(f0)
cdef double[:,::1] spectrogram = np.zeros((f0_length,fft_size/2+1))
cdef np.intp_t[:] tmp = np.zeros(f0_length, dtype=np.intp)
cdef double **cpp_spectrogram = <double**> (<void*> &tmp[0])
cdef np.intp_t i
for i in range(f0_length):
cpp_spectrogram[i] = &spectrogram[i,0]
Star(&x[0], x_length, fs, &time_axis[0], &f0[0], f0_length, cpp_spectrogram)
return np.array(spectrogram, dtype=np.float64)
def cheaptrick(np.ndarray[double, ndim=1, mode="c"] x not None, int fs, double period,
np.ndarray[double, ndim=1, mode="c"] time_axis not None,
np.ndarray[double, ndim=1, mode="c"] f0 not None):
cdef int x_length = len(x)
cdef int f0_length = len(f0)
cdef int fft_size = GetFFTSizeForCheapTrick(fs)
cdef double[:,::1] spectrogram = np.zeros((f0_length,fft_size/2+1))
cdef np.intp_t[:] tmp = np.zeros(f0_length, dtype=np.intp)
cdef double **cpp_spectrogram = <double**> (<void*> &tmp[0])
cdef np.intp_t i
for i in range(f0_length):
cpp_spectrogram[i] = &spectrogram[i,0]
CheapTrick(&x[0], x_length, fs, &time_axis[0], &f0[0], f0_length, cpp_spectrogram)
return np.array(spectrogram, dtype=np.float64)
def platinum(np.ndarray[double, ndim=1, mode="c"] x not None, int fs, double period,
np.ndarray[double, ndim=1, mode="c"] time_axis not None,
np.ndarray[double, ndim=1, mode="c"] f0 not None,
np.ndarray[double, ndim=2, mode="c"] np_spectrogram not None):
cdef int x_length = len(x)
cdef int f0_length = len(f0)
cdef int fft_size = GetFFTSizeForCheapTrick(fs)
cdef double[:,::1] spectrogram = np_spectrogram
cdef double[:,::1] residual = np.zeros((f0_length,fft_size+1))
cdef np.intp_t[:] tmp = np.zeros(f0_length, dtype=np.intp)
cdef np.intp_t[:] tmp2 = np.zeros(f0_length, dtype=np.intp)
cdef double **cpp_spectrogram = <double**> (<void*> &tmp[0])
cdef double **cpp_residual = <double**> (<void*> &tmp2[0])
cdef np.intp_t i
for i in range(f0_length):
cpp_spectrogram[i] = &spectrogram[i,0]
cpp_residual[i] = &residual[i,0]
Platinum(&x[0], x_length, fs, &time_axis[0], &f0[0], f0_length,
cpp_spectrogram, fft_size, cpp_residual)
return np.array(residual, dtype=np.float64)
def synthesis(int fs, double period,
np.ndarray[double, ndim=1, mode="c"] f0 not None,
np.ndarray[double, ndim=2, mode="c"] np_spectrogram not None,
np.ndarray[double, ndim=2, mode="c"] np_residual not None,
int y_length):
cdef int f0_length = len(f0)
cdef int fft_size = GetFFTSizeForCheapTrick(fs)
cdef np.ndarray[double, ndim=1, mode="c"] y
y = np.zeros(y_length, dtype = np.dtype('float64'))
cdef double[:,::1] spectrogram = np_spectrogram
cdef double[:,::1] residual = np_residual
cdef np.intp_t[:] tmp = np.zeros(f0_length, dtype=np.intp)
cdef np.intp_t[:] tmp2 = np.zeros(f0_length, dtype=np.intp)
cdef double **cpp_spectrogram = <double**> (<void*> &tmp[0])
cdef double **cpp_residual = <double**> (<void*> &tmp2[0])
cdef np.intp_t i
for i in range(f0_length):
cpp_spectrogram[i] = &spectrogram[i,0]
cpp_residual[i] = &residual[i,0]
Synthesis( &f0[0], f0_length, cpp_spectrogram,cpp_residual, fft_size, period, fs, y_length, &y[0])
return y
def aperiodicityratio(np.ndarray[double, ndim=1, mode="c"] x not None, int fs, double period,
np.ndarray[double, ndim=1, mode="c"] time_axis not None,
np.ndarray[double, ndim=1, mode="c"] f0 not None):
cdef int x_length = len(x)
cdef int f0_length = len(f0)
cdef int fft_size = GetFFTSizeForCheapTrick(fs)
cdef double[:,::1] aperiodicity = np.zeros((f0_length,fft_size/2+1))
cdef np.intp_t[:] tmp = np.zeros(f0_length, dtype=np.intp)
cdef double **cpp_aperiodicity = <double**> (<void*> &tmp[0])
cdef np.intp_t i
for i in range(f0_length):
cpp_aperiodicity[i] = &aperiodicity[i,0]
AperiodicityRatio(&x[0], x_length, fs, &f0[0], f0_length, &time_axis[0], fft_size, cpp_aperiodicity)
return np.array(aperiodicity, dtype=np.float64)
def synthesis_from_aperiodicity(int fs, double period,
np.ndarray[double, ndim=1, mode="c"] f0 not None,
np.ndarray[double, ndim=2, mode="c"] np_spectrogram not None,
np.ndarray[double, ndim=2, mode="c"] np_aperiodicity not None,
int y_length):
cdef int f0_length = len(f0)
cdef int fft_size = GetFFTSizeForCheapTrick(fs)
cdef np.ndarray[double, ndim=1, mode="c"] y
y = np.zeros(y_length, dtype = np.dtype('float64'))
cdef double[:,::1] spectrogram = np_spectrogram
cdef double[:,::1] aperiodicity = np_aperiodicity
cdef np.intp_t[:] tmp = np.zeros(f0_length, dtype=np.intp)
cdef np.intp_t[:] tmp2 = np.zeros(f0_length, dtype=np.intp)
cdef double **cpp_spectrogram = <double**> (<void*> &tmp[0])
cdef double **cpp_aperiodicity = <double**> (<void*> &tmp2[0])
cdef np.intp_t i
for i in range(f0_length):
cpp_spectrogram[i] = &spectrogram[i,0]
cpp_aperiodicity[i] = &aperiodicity[i,0]
SynthesisFromAperiodicity(&f0[0], f0_length, cpp_spectrogram, cpp_aperiodicity, fft_size, period, fs, y_length, &y[0])
return y
class pyDioOption:
def __init__(self, f0_floor, f0_ceil, channels_in_octave, frame_period, speed):
cdef DioOption option
InitializeDioOption(&option)
option.f0_floor = f0_floor
option.f0_ceil = f0_ceil
option.channels_in_octave = channels_in_octave
option.frame_period = frame_period
option.speed = speed
self.option = option