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

is there a faster way to play audio? #787

Open deostroll opened 8 months ago

deostroll commented 8 months ago

Is there a faster way to play (without pauses)?

from pydub import AudioSegment
from argparse import ArgumentParser
from pydub.playback import play

if __name__ == '__main__':
    parser = ArgumentParser('test.py')
    parser.add_argument('file')

    args = parser.parse_args()

    sound = AudioSegment.from_file(args.file)

    length = len(sound)

    print('length:', length)

    # play(sound)

    idx = 0 
    inc = 1024
    cnt = 0
    while idx < length:
        if idx + inc > length:
            clip = sound[idx:]
        else:
            clip = sound[idx: idx + inc]
            cnt += 1
            print('Count:', cnt)
        idx = idx + inc
        play(clip)

    print('done')

Run this script with a suitably big audio file. I ran one with 20s length. You will hear the pauses between chunks. (Incidentally on my machine I only have ffmpeg installed. Therefore it defaults to ffplay. And there is a conversion to wav file prior to playing the audio. (https://github.com/jiaaro/pydub/blob/master/pydub/playback.py#L15)

Looking for a smoother alternative. Perhaps my code is to blame too!