jiaaro / pydub

Manipulate audio with a simple and easy high level interface
http://pydub.com
MIT License
8.98k stars 1.05k forks source link

Improve memory footprint of make_chunks #783

Open cipherCOM opened 8 months ago

cipherCOM commented 8 months ago

I have a small proposal for the make_chunks function to make a bit more mem aware. What about changing it to a generator expression instead of a list compression. This saved me quite some mem when handling hours of audio data, if I just want to iterate over them to e.g. calculate the RMS.

https://github.com/jiaaro/pydub/blob/996cec42e9621701edb83354232b2c0ca0121560/pydub/utils.py#L144

def make_chunks(audio_segment, chunk_length):
    """
    Breaks an AudioSegment into chunks that are <chunk_length> milliseconds
    long.
    if chunk_length is 50 then you'll get a list of 50 millisecond long audio
    segments back (except the last one, which can be shorter)
    """
    number_of_chunks = ceil(len(audio_segment) / float(chunk_length))
    return (audio_segment[i * chunk_length:(i + 1) * chunk_length]
            for i in range(int(number_of_chunks)))