Jacob Vlijm
Jacob Vlijm

Reputation: 3159

How can I repeatedly play a sound sample, allowing the next loop to overlap the previous

Not sure if this isn't a dupe, but the posts I found so far didn't solve my issue.


A while ago, I wrote a (music) metronome for Ubuntu. The metronome is written in python3/Gtk

To repeatedly play the metronome- tick (a recorded sound sample), I used subprocess.Popen() to play the sound, using ogg123 as a cli tool:

subprocess.Popen(["ogg123", soundfile])

This works fine, I can easily run up to 240 beats per minute.

On WIndows

I decided to rewrite the project on Windows (python3/tkinter/ttk). I am having a hard time however to play the sound, repeating the beat sample in higher tempi. The next beat simply won't start while the previous one (appearantly) hasn't finished yet, playing the beat sample.

Is there a way, in python3 on Windows, I can start playing the next beat while the sample is still playing?

Currently, I am using winsound:

winsound.Playsound()

Running this in a loop has, as mentioned issues.

Upvotes: 3

Views: 4029

Answers (2)

Perry
Perry

Reputation: 312

Suppose you want the audio to be greater than fix length like for example 20000 milliseconds or 20s. Then you can simply add the audio repeatedly

sound = AudioSegment.from_file(GENERAL_FOLDER + "/" + "beep.wav", "wav")

sound_extended = sound

while sound_extended < 20000:
    sound_extended = sound_extended + sound

Upvotes: 0

Anil_M
Anil_M

Reputation: 11453

You can use pydub for audio manipulation , including playing repetedly.

Here is an example. You can develop this further using examples from pydub site.

from pydub import AudioSegment
from pydub.playback import play

n = 2

audio = AudioSegment.from_file("sound.wav") #your audio file
play(audio * n)  #Play audio 2 times

Change n above to the number that you need.

Upvotes: 1

Related Questions