0

The problem is when I am using sounddevice's loop option to loop a playback, I cannot get it to stop.

import soundfile as sf
import sounddevice as sd

weight = 1.4

data, fs = sf.read('sound.wav')
sd.play(data * weight, fs, blocking=True,loop=True)
sd.stop()

How to stop the loop, after it has started. Is it possible to write a function such that when the function is called the loop stops and the audio stream is closed?

1 Answers1

2

The problem is that you are setting blocking=True which does not allow for any other functions to be run before this exits. Combined with loop=True, this produces an infinite loop.

What you want is:

import soundfile as sf
import sounddevice as sd

weight = 1.4

data, fs = sf.read('sound.wav')
sd.play(data * weight, fs,loop=True)
sd.stop()

Hope this helps!

Atto Allas
  • 610
  • 5
  • 16