Skip to content
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
37 changes: 33 additions & 4 deletions src/nodetool/media/audio/audio_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,39 @@ def concatenate_audios(audios: list[AudioSegment]) -> AudioSegment:
Returns:
AudioSegment: The concatenated audio segment.
"""
concatenated_audio = AudioSegment.empty()
for audio in audios:
concatenated_audio += audio
return concatenated_audio
if not audios:
return AudioSegment.empty()

# ⚑ Bolt Optimization: Calculate the target parameters that pydub's += operator
# would implicitly upgrade to by finding the max across all input segments,
# convert all segments to those targets, and then do a fast O(N) byte join
# to avoid the O(N^2) penalty of repeated += copying.
first = audios[0]
target_sample_width = first.sample_width
target_frame_rate = first.frame_rate
target_channels = first.channels

for a in audios[1:]:
target_sample_width = max(target_sample_width, a.sample_width)
target_frame_rate = max(target_frame_rate, a.frame_rate)
target_channels = max(target_channels, a.channels)

raw_data_list = []
for a in audios:
if a.sample_width != target_sample_width:
a = a.set_sample_width(target_sample_width)
if a.frame_rate != target_frame_rate:
a = a.set_frame_rate(target_frame_rate)
if a.channels != target_channels:
a = a.set_channels(target_channels)
raw_data_list.append(a.raw_data)

res = first._spawn(b"".join(raw_data_list), overrides={
"sample_width": target_sample_width,
"frame_rate": target_frame_rate,
"channels": target_channels
})
return res


def remove_silence(
Expand Down
Loading