Skip to content

Commit

Permalink
ENH: release global interpreter lock
Browse files Browse the repository at this point in the history
Release GIL when execute `Synthesis` `CheapTrick` `Dio` `Harvest` `D4C` `StoneMask`.

Update World.
NOTE: Updating the world is necessary to prevent a data race in `randn()`.

Other small optimizations.
  • Loading branch information
sabonerune committed Oct 31, 2024
1 parent fa84b8b commit ba4b424
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
52 changes: 29 additions & 23 deletions pyworld/pyworld.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cdef extern from "world/synthesis.h":
int f0_length, const double * const *spectrogram,
const double * const *aperiodicity,
int fft_size, double frame_period,
int fs, int y_length, double *y) except +
int fs, int y_length, double *y) except + nogil


cdef extern from "world/cheaptrick.h":
Expand All @@ -25,7 +25,7 @@ cdef extern from "world/cheaptrick.h":
void InitializeCheapTrickOption(int fs, CheapTrickOption *option) except +
void CheapTrick(const double *x, int x_length, int fs, const double *temporal_positions,
const double *f0, int f0_length, const CheapTrickOption *option,
double **spectrogram) except +
double **spectrogram) except + nogil


cdef extern from "world/dio.h":
Expand All @@ -40,7 +40,7 @@ cdef extern from "world/dio.h":
void InitializeDioOption(DioOption *option) except +
int GetSamplesForDIO(int fs, int x_length, double frame_period)
void Dio(const double *x, int x_length, int fs, const DioOption *option,
double *temporal_positions, double *f0) except +
double *temporal_positions, double *f0) except + nogil


cdef extern from "world/harvest.h":
Expand All @@ -52,7 +52,7 @@ cdef extern from "world/harvest.h":
void InitializeHarvestOption(HarvestOption *option)
int GetSamplesForHarvest(int fs, int x_length, double frame_period)
void Harvest(const double *x, int x_length, int fs, const HarvestOption *option,
double *temporal_positions, double *f0) except +
double *temporal_positions, double *f0) except + nogil


cdef extern from "world/d4c.h":
Expand All @@ -62,13 +62,13 @@ cdef extern from "world/d4c.h":
void InitializeD4COption(D4COption *option) except +
void D4C(const double *x, int x_length, int fs, const double *temporal_positions,
const double *f0, int f0_length, int fft_size, const D4COption *option,
double **aperiodicity) except +
double **aperiodicity) except + nogil


cdef extern from "world/stonemask.h":
void StoneMask(const double *x, int x_length, int fs,
const double *temporal_positions, const double *f0, int f0_length,
double *refined_f0) except +
double *refined_f0) except + nogil


cdef extern from "world/codec.h":
Expand All @@ -85,9 +85,9 @@ cdef extern from "world/codec.h":
double **spectrogram) except +


default_frame_period = 5.0
default_f0_floor = 71.0
default_f0_ceil = 800.0
cdef double default_frame_period = 5.0
cdef double default_f0_floor = 71.0
cdef double default_f0_ceil = 800.0


def dio(np.ndarray[double, ndim=1, mode="c"] x not None, int fs,
Expand Down Expand Up @@ -147,7 +147,8 @@ def dio(np.ndarray[double, ndim=1, mode="c"] x not None, int fs,
np.zeros(f0_length, dtype=np.dtype('float64'))
cdef np.ndarray[double, ndim=1, mode="c"] temporal_positions = \
np.zeros(f0_length, dtype=np.dtype('float64'))
Dio(&x[0], x_length, fs, &option, &temporal_positions[0], &f0[0])
with (nogil, cython.boundscheck(False)):
Dio(&x[0], x_length, fs, &option, &temporal_positions[0], &f0[0])
return f0, temporal_positions


Expand Down Expand Up @@ -190,7 +191,8 @@ def harvest(np.ndarray[double, ndim=1, mode="c"] x not None, int fs,
np.zeros(f0_length, dtype=np.dtype('float64'))
cdef np.ndarray[double, ndim=1, mode="c"] temporal_positions = \
np.zeros(f0_length, dtype=np.dtype('float64'))
Harvest(&x[0], x_length, fs, &option, &temporal_positions[0], &f0[0])
with (nogil, cython.boundscheck(False)):
Harvest(&x[0], x_length, fs, &option, &temporal_positions[0], &f0[0])
return f0, temporal_positions


Expand Down Expand Up @@ -220,12 +222,13 @@ def stonemask(np.ndarray[double, ndim=1, mode="c"] x not None,
cdef int f0_length = <int>len(f0)
cdef np.ndarray[double, ndim=1, mode="c"] refined_f0 = \
np.zeros(f0_length, dtype=np.dtype('float64'))
StoneMask(&x[0], x_length, fs, &temporal_positions[0],
&f0[0], f0_length, &refined_f0[0])
with (nogil, cython.boundscheck(False)):
StoneMask(&x[0], x_length, fs, &temporal_positions[0],
&f0[0], f0_length, &refined_f0[0])
return refined_f0


def get_cheaptrick_fft_size(fs, f0_floor=default_f0_floor):
def get_cheaptrick_fft_size(int fs, f0_floor=default_f0_floor):
"""Calculate suitable FFT size for CheapTrick given F0 floor.
Parameters
Expand All @@ -247,7 +250,7 @@ def get_cheaptrick_fft_size(fs, f0_floor=default_f0_floor):
cdef int fft_size = GetFFTSizeForCheapTrick(fs, &option)
return fft_size

def get_cheaptrick_f0_floor(fs, fft_size):
def get_cheaptrick_f0_floor(int fs, int fft_size):
"""Calculates actual lower F0 limit for CheapTrick
based on the sampling frequency and FFT size used. Whenever F0 is below
this threshold the spectrum will be analyzed as if the frame is unvoiced
Expand Down Expand Up @@ -322,8 +325,9 @@ def cheaptrick(np.ndarray[double, ndim=1, mode="c"] x not None,
for i in range(f0_length):
cpp_spectrogram[i] = &spectrogram[i, 0]

CheapTrick(&x[0], x_length, fs, &temporal_positions[0],
&f0[0], f0_length, &option, cpp_spectrogram)
with (nogil, cython.boundscheck(False)):
CheapTrick(&x[0], x_length, fs, &temporal_positions[0],
&f0[0], f0_length, &option, cpp_spectrogram)
return np.array(spectrogram, dtype=np.float64)


Expand Down Expand Up @@ -389,9 +393,10 @@ def d4c(np.ndarray[double, ndim=1, mode="c"] x not None,
for i in range(f0_length):
cpp_aperiodicity[i] = &aperiodicity[i, 0]

D4C(&x[0], x_length, fs, &temporal_positions[0],
&f0[0], f0_length, fft_size0, &option,
cpp_aperiodicity)
with (nogil, cython.boundscheck(False)):
D4C(&x[0], x_length, fs, &temporal_positions[0],
&f0[0], f0_length, fft_size0, &option,
cpp_aperiodicity)
return np.array(aperiodicity, dtype=np.float64)


Expand Down Expand Up @@ -433,7 +438,7 @@ def synthesize(np.ndarray[double, ndim=1, mode="c"] f0 not None,
.format(spectrogram.shape[1], aperiodicity.shape[1]))

cdef int f0_length = <int>len(f0)
y_length = int(f0_length * frame_period * fs / 1000)
cdef int y_length = <int>(f0_length * frame_period * fs / 1000)
cdef int fft_size = (<int>spectrogram.shape[1] - 1)*2
cdef np.ndarray[double, ndim=1, mode="c"] y = \
np.zeros(y_length, dtype=np.dtype('float64'))
Expand All @@ -449,8 +454,9 @@ def synthesize(np.ndarray[double, ndim=1, mode="c"] f0 not None,
cpp_spectrogram[i] = &spectrogram0[i, 0]
cpp_aperiodicity[i] = &aperiodicity0[i, 0]

Synthesis(&f0[0], f0_length, cpp_spectrogram,
cpp_aperiodicity, fft_size, frame_period, fs, y_length, &y[0])
with (nogil, cython.boundscheck(False)):
Synthesis(&f0[0], f0_length, cpp_spectrogram,
cpp_aperiodicity, fft_size, frame_period, fs, y_length, &y[0])
return y


Expand Down

0 comments on commit ba4b424

Please sign in to comment.