.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/resampling/plot_batch_with_threads.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_resampling_plot_batch_with_threads.py: Process many clips with threads =============================== File reads, ``resample`` and the analysis methods run without holding the GIL, so a thread pool processes clips in parallel on a regular Python build as well as on a free-threaded one. The results are the same as a plain loop. .. GENERATED FROM PYTHON SOURCE LINES 9-17 .. code-block:: Python from concurrent.futures import ThreadPoolExecutor import matplotlib.pyplot as plt import numpy as np import audio_samples as aus .. GENERATED FROM PYTHON SOURCE LINES 18-19 Twelve clips at different pitches, at different sample rates. .. GENERATED FROM PYTHON SOURCE LINES 19-32 .. code-block:: Python rates = [8000, 16000, 22050, 44100] clips = [ aus.generation.compound_tone([(110.0 * (i + 1), 0.7), (220.0 * (i + 1), 0.3)], 1.0, rates[i % 4]).as_f32() for i in range(12) ] def features(clip): """The same 16 kHz MFCC summary for any input rate.""" mfcc = clip.resample(16000, "high").mfcc_frames(n_mfcc=13, n_fft=512, hop_length=160) return mfcc[0].mean(axis=1) .. GENERATED FROM PYTHON SOURCE LINES 33-42 .. code-block:: Python with ThreadPoolExecutor(4) as pool: rows = list(pool.map(features, clips)) in_a_loop = [features(c) for c in clips] assert all(np.array_equal(a, b) for a, b in zip(rows, in_a_loop)) fig, ax = plt.subplots(figsize=(7, 3)) ax.imshow(np.stack(rows), aspect="auto", origin="lower") ax.set(xlabel="MFCC coefficient", ylabel="Clip", title="Mean MFCC per clip") .. image-sg:: /gallery/resampling/images/sphx_glr_plot_batch_with_threads_001.png :alt: Mean MFCC per clip :srcset: /gallery/resampling/images/sphx_glr_plot_batch_with_threads_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [Text(0.5, 3.833333333333327, 'MFCC coefficient'), Text(54.222222222222214, 0.5, 'Clip'), Text(0.5, 1.0, 'Mean MFCC per clip')] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.219 seconds) .. _sphx_glr_download_gallery_resampling_plot_batch_with_threads.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_batch_with_threads.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_batch_with_threads.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_batch_with_threads.zip `