-
Notifications
You must be signed in to change notification settings - Fork 1
/
loopSound.py
37 lines (32 loc) · 1.1 KB
/
loopSound.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from threading import Thread
import simpleaudio as sa
class LoopSound:
"""
A class to keep track of a looping sound.
Automatically creates a new thread when starting the loop to keep it playing
without interrupting any other functionality.
"""
def __init__(self, soundFile):
self.soundFile = soundFile
self.looping = True
self.loopThread = None
self.playObj = None
self.startLoop()
def soundLoop(self):
"""Main loop for a looping sound."""
while self.looping:
waveObj = sa.WaveObject.from_wave_file(self.soundFile)
self.playObj = waveObj.play()
self.playObj.wait_done()
def startLoop(self):
"""Starts the sound playing on loop."""
self.looping = True
self.loopThread = Thread(target=self.soundLoop)
self.loopThread.start()
def stop(self):
"""Stops the sound after it completes its current loop."""
self.looping = False
def stopImmediately(self):
"""Stops sound immediately"""
self.playObj.stop()
self.looping = False