wip: avoiding repeated copying of audio in speedup #746
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue #736 demonstrates that the
speedup
effect function becomes prohibitively slow for long audio segments. This is due to the loop in lines 90,91 of the speedup function (pydub/effects.py
), where the shortened chunks are appended usingAudioSegment.append
, which result in repeated copying (and necessary memory allocations) of the already processed part for creating of a new immutableAudioSegment
for each chunk.This PR shows a much more performant way by essentially copying the byte logic of
AudioSegment.append
intospeedup
. For the example given in issue #736 , this completes in about 11 seconds. However, the current implementation does not currently deal with a variety of corner cases around the length of chunks and crossfading which was previously elegantly (but imperformantly) solved by abstracting throughAudioSegment.append
. Also, the introduced code duplication is not ideal.Therefore, this PR draft is mostly meant to start a discussion on how to approach this best. The main problem of the old approach lies in the immutability and the corresponding copying of
AudioSegment
s. Maybe seperating some of the composition routines (append, overlay, fade, etc) to work on preallocated low-level buffers could be a way to avoid duplication while still getting good performance?