From cb70e083275311f19ff7c0277a6c64fc131d4200 Mon Sep 17 00:00:00 2001 From: Ali Hamdi Ali Fadel Date: Thu, 27 Jun 2024 18:30:39 +0000 Subject: [PATCH] Parallelize AudioSplitter._segments_to_data method --- src/audio_splitter.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/audio_splitter.py b/src/audio_splitter.py index 1466cf5..2689bd4 100644 --- a/src/audio_splitter.py +++ b/src/audio_splitter.py @@ -2,6 +2,8 @@ import os import subprocess +from concurrent.futures import ThreadPoolExecutor + from auditok import AudioRegion from auditok.core import split from pydub import AudioSegment @@ -112,12 +114,13 @@ def _expand_segment_with_noise(self, segment: AudioRegion, noise_seconds: int, n return pre_noise + audio_segment + post_noise def _segments_to_data(self, segments: list[tuple[AudioSegment, float, float]]) -> list[tuple[bytes, float, float]]: - segments_data = [] - - for segment in segments: + def process_segment(segment: tuple[AudioSegment, float, float]) -> tuple[bytes, float, float]: output_buffer = io.BytesIO() - segment[0].export(output_buffer, format='mp3') - segments_data.append((output_buffer.getvalue(), segment[1], segment[2])) + + return (output_buffer.getvalue(), segment[1], segment[2]) + + with ThreadPoolExecutor() as executor: + segments_data = list(executor.map(process_segment, segments)) return segments_data