Skip to content

Commit

Permalink
Parallelize AudioSplitter._segments_to_data method
Browse files Browse the repository at this point in the history
  • Loading branch information
AliOsm committed Jun 27, 2024
1 parent 7ea91f4 commit cb70e08
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/audio_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit cb70e08

Please sign in to comment.