Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release GIL while resampling #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/samplerate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ class Resampler {
sr_ratio // src_ratio, sampling rate conversion ratio
};

error_handler(src_process(_state, &src_data));
error_handler([&]() {
py::gil_scoped_release release;
return src_process(_state, &src_data);
}());

// create a shorter view of the array
if ((size_t)src_data.output_frames_gen < new_size) {
Expand Down Expand Up @@ -294,8 +297,12 @@ class CallbackResampler {
if (_state == nullptr) _create();

// read from the callback
size_t output_frames_gen = src_callback_read(
_state, _ratio, (long)frames, static_cast<float *>(outbuf.ptr));
size_t output_frames_gen = 0;
{
py::gil_scoped_release release;
output_frames_gen = src_callback_read(_state, _ratio, (long)frames,
static_cast<float *>(outbuf.ptr));
}

// check error status
if (output_frames_gen == 0) {
Expand Down Expand Up @@ -340,11 +347,14 @@ long the_callback_func(void *cb_data, float **data) {
CallbackResampler *cb = static_cast<CallbackResampler *>(cb_data);
int cb_channels = cb->get_channels();

// get the data as a numpy array
auto input = cb->callback();
py::buffer_info inbuf;
{
py::gil_scoped_acquire acquire;

// accessors for the arrays
py::buffer_info inbuf = input.request();
// get the data as a numpy array
auto input = cb->callback();
inbuf = input.request();
}

// end of stream is signaled by a None, which is cast to a ndarray with ndim
// == 0
Expand Down Expand Up @@ -407,9 +417,10 @@ py::array_t<float, py::array::c_style> resample(
sr_ratio // src_ratio, sampling rate conversion ratio
};

int ret_code = src_simple(&src_data, converter_type_int, channels);

error_handler(ret_code);
error_handler([&]() {
py::gil_scoped_release release;
return src_simple(&src_data, converter_type_int, channels);
}());

// create a shorter view of the array
if ((size_t)src_data.output_frames_gen < new_size) {
Expand Down
Loading