add lock to soundlib volume utility

This commit is contained in:
petrucci4prez 2017-06-11 19:34:24 -04:00
parent 4cf3328339
commit 624574df3d
1 changed files with 34 additions and 31 deletions

View File

@ -2,7 +2,7 @@
Implements all sound functionality Implements all sound functionality
''' '''
import logging, os, hashlib, queue, time, psutil import logging, os, hashlib, queue, time, psutil
from threading import Event from threading import Event, RLock
from exceptionThreading import ExceptionThread, async from exceptionThreading import ExceptionThread, async
from pygame import mixer from pygame import mixer
from subprocess import call from subprocess import call
@ -114,6 +114,7 @@ class SoundLib:
self._ttsQueue = queue.Queue() self._ttsQueue = queue.Queue()
self._stopper = Event() self._stopper = Event()
self._lock = RLock()
def start(self): def start(self):
self._startMonitor() self._startMonitor()
@ -137,42 +138,44 @@ class SoundLib:
@async(daemon=False) @async(daemon=False)
def _fader(self, lowerVolume, totalDuration, fadeDuration=0.2, stepSize=5): def _fader(self, lowerVolume, totalDuration, fadeDuration=0.2, stepSize=5):
alarm = self.soundEffects['triggered'] with self._lock:
alarmVolume = alarm.volume alarm = self.soundEffects['triggered']
alarmVolumeDelta = alarmVolume - lowerVolume alarmVolume = alarm.volume
alarmVolumeDelta = alarmVolume - lowerVolume
masterVolume = self.volume
masterVolumeDelta = self.volume - lowerVolume masterVolume = self.volume
masterVolumeDelta = self.volume - lowerVolume
sleepFadeTime = fadeDuration / stepSize
sleepFadeTime = fadeDuration / stepSize
for i in range(0, stepSize):
if alarmVolumeDelta > 0: for i in range(0, stepSize):
alarm.set_volume(alarmVolume - alarmVolumeDelta * i / stepSize, force=True) if alarmVolumeDelta > 0:
alarm.set_volume(alarmVolume - alarmVolumeDelta * i / stepSize, force=True)
if masterVolumeDelta > 0:
self._applyVolumesToSounds(masterVolume - masterVolumeDelta * i / stepSize)
if masterVolumeDelta > 0: time.sleep(sleepFadeTime)
self._applyVolumesToSounds(masterVolume - masterVolumeDelta * i / stepSize)
time.sleep(sleepFadeTime)
time.sleep(totalDuration - 2 * fadeDuration)
for i in range(stepSize - 1, -1, -1):
if alarmVolumeDelta > 0:
alarm.set_volume(alarmVolume - alarmVolumeDelta * i / stepSize, force=True)
if masterVolumeDelta > 0: time.sleep(totalDuration - 2 * fadeDuration)
self._applyVolumesToSounds(masterVolume - masterVolumeDelta * i / stepSize)
time.sleep(sleepFadeTime) for i in range(stepSize - 1, -1, -1):
if alarmVolumeDelta > 0:
alarm.set_volume(alarmVolume - alarmVolumeDelta * i / stepSize, force=True)
if masterVolumeDelta > 0:
self._applyVolumesToSounds(masterVolume - masterVolumeDelta * i / stepSize)
time.sleep(sleepFadeTime)
# will not change sounds that have preset volume # will not change sounds that have preset volume
def _applyVolumesToSounds(self, volume): def _applyVolumesToSounds(self, volume):
self.volume = volume with self._lock:
v = volume/100 self.volume = volume
s = self.soundEffects v = volume/100
for name, sound in s.items(): s = self.soundEffects
sound.set_volume(v) for name, sound in s.items():
sound.set_volume(v)
def _ttsMonitor(self): def _ttsMonitor(self):
q = self._ttsQueue q = self._ttsQueue