@@ -199,19 +199,20 @@ class Resampler {
199199 sr_ratio // src_ratio, sampling rate conversion ratio
200200 };
201201
202- // Only release GIL for large data sizes where resampling work dominates
203- // the GIL release/acquire overhead. For small data, keep the GIL to avoid
204- // unnecessary overhead in single-threaded scenarios.
202+ // Perform resampling - only release GIL for large data sizes where
203+ // resampling work dominates the GIL release/acquire overhead
204+ auto do_resample = [&]() {
205+ return src_process (_state, &src_data);
206+ };
207+
205208 int err_code;
206- long output_frames_gen;
207209 if (inbuf.shape [0 ] >= GIL_RELEASE_THRESHOLD_FRAMES) {
208210 py::gil_scoped_release release;
209- err_code = src_process (_state, &src_data);
210- output_frames_gen = src_data.output_frames_gen ;
211+ err_code = do_resample ();
211212 } else {
212- err_code = src_process (_state, &src_data);
213- output_frames_gen = src_data.output_frames_gen ;
213+ err_code = do_resample ();
214214 }
215+ long output_frames_gen = src_data.output_frames_gen ;
215216 error_handler (err_code);
216217
217218 // create a shorter view of the array
@@ -339,26 +340,26 @@ class CallbackResampler {
339340 // clear any previous callback error
340341 clear_callback_error ();
341342
342- // read from the callback - note: GIL is managed by the_callback_func
343- // which acquires it only when calling the Python callback.
344- // Only release GIL for large frame counts where resampling work dominates
345- // the GIL release/acquire overhead.
346- size_t output_frames_gen = 0 ;
347- int err_code = 0 ;
343+ // Perform callback resampling - only release GIL for large frame counts
344+ // where resampling work dominates the GIL release/acquire overhead.
345+ // Note: the_callback_func will acquire GIL when calling Python callback.
346+ auto do_callback_read = [&]() {
347+ size_t gen = src_callback_read (_state, _ratio, (long )frames,
348+ static_cast <float *>(outbuf.ptr ));
349+ return std::make_pair (gen, gen == 0 ? src_error (_state) : 0 );
350+ };
351+
352+ size_t output_frames_gen;
353+ int err_code;
348354 if (frames >= GIL_RELEASE_THRESHOLD_FRAMES) {
349355 py::gil_scoped_release release;
350- output_frames_gen = src_callback_read (_state, _ratio, (long )frames,
351- static_cast <float *>(outbuf.ptr ));
352- // Get error code while GIL is released
353- if (output_frames_gen == 0 ) {
354- err_code = src_error (_state);
355- }
356+ auto result = do_callback_read ();
357+ output_frames_gen = result.first ;
358+ err_code = result.second ;
356359 } else {
357- output_frames_gen = src_callback_read (_state, _ratio, (long )frames,
358- static_cast <float *>(outbuf.ptr ));
359- if (output_frames_gen == 0 ) {
360- err_code = src_error (_state);
361- }
360+ auto result = do_callback_read ();
361+ output_frames_gen = result.first ;
362+ err_code = result.second ;
362363 }
363364
364365 // check if callback had an error
@@ -490,22 +491,21 @@ py::array_t<float, py::array::c_style> resample(
490491 sr_ratio // src_ratio, sampling rate conversion ratio
491492 };
492493
493- // Only release GIL for large data sizes where resampling work dominates
494- // the GIL release/acquire overhead. For small data, keep the GIL to avoid
495- // unnecessary overhead in single-threaded scenarios.
494+ // Perform resampling - only release GIL for large data sizes where
495+ // resampling work dominates the GIL release/acquire overhead
496+ auto do_resample = [&]() {
497+ return src_simple (&src_data, converter_type_int, channels);
498+ };
499+
496500 int err_code;
497- long output_frames_gen;
498- long input_frames_used;
499501 if (inbuf.shape [0 ] >= GIL_RELEASE_THRESHOLD_FRAMES) {
500502 py::gil_scoped_release release;
501- err_code = src_simple (&src_data, converter_type_int, channels);
502- output_frames_gen = src_data.output_frames_gen ;
503- input_frames_used = src_data.input_frames_used ;
503+ err_code = do_resample ();
504504 } else {
505- err_code = src_simple (&src_data, converter_type_int, channels);
506- output_frames_gen = src_data.output_frames_gen ;
507- input_frames_used = src_data.input_frames_used ;
505+ err_code = do_resample ();
508506 }
507+ long output_frames_gen = src_data.output_frames_gen ;
508+ long input_frames_used = src_data.input_frames_used ;
509509 error_handler (err_code);
510510
511511 // create a shorter view of the array
0 commit comments