Set a maximum frequency for playing sounds

This commit is contained in:
Bob Mottram 2013-07-31 22:25:34 +01:00
parent 97df9e04f1
commit dda530ca07
1 changed files with 23 additions and 5 deletions

View File

@ -34,7 +34,7 @@ import platform
import debug
from debug import logger
import subprocess
import datetime
try:
from PyQt4 import QtCore, QtGui
@ -65,6 +65,12 @@ class MyForm(QtGui.QMainWindow):
SOUND_DISCONNECTED = 4
SOUND_CONNECTION_GREEN = 5
# the last time that a message arrival sound was played
lastSoundTime = datetime.datetime.now() - datetime.timedelta(days=1)
# the maximum frequency of message sounds in seconds
maxSoundFrequencySec = 60
str_broadcast_subscribers = '[Broadcast subscribers]'
str_chan = '[chan]'
@ -429,7 +435,6 @@ class MyForm(QtGui.QMainWindow):
self.rerenderComboBoxSendFrom()
# Show or hide the application window after clicking an item within the
# tray icon or, on Windows, the try icon itself.
def appIndicatorShowOrHideWindow(self):
@ -985,13 +990,26 @@ class MyForm(QtGui.QMainWindow):
# play a sound
def playSound(self, category, label):
soundFilename = None
play = True
if label is not None:
# does a sound file exist for this particular contact?
# Does a sound file exist for this particular contact?
if (os.path.isfile(shared.appdata + 'sounds/' + label + '.wav') or
os.path.isfile(shared.appdata + 'sounds/' + label + '.mp3')):
soundFilename = shared.appdata + 'sounds/' + label
# Avoid making sounds more frequently than the threshold.
# This suppresses playing sounds repeatedly when there
# are many new messages
if (soundFilename is None and
category is not self.SOUND_CONNECTED and
category is not self.SOUND_DISCONNECTED and
category is not self.SOUND_CONNECTION_GREEN):
dt = datetime.datetime.now() - self.lastSoundTime
self.lastSoundTime = datetime.datetime.now()
if dt.total_seconds() < self.maxSoundFrequencySec:
play = False
if soundFilename is None:
if category is self.SOUND_KNOWN:
soundFilename = shared.appdata + 'sounds/known'
@ -1002,9 +1020,9 @@ class MyForm(QtGui.QMainWindow):
elif category is self.SOUND_DISCONNECTED:
soundFilename = shared.appdata + 'sounds/disconnected'
elif category is self.SOUND_CONNECTION_GREEN:
soundFilename = shared.appdata + 'sounds/green'
soundFilename = shared.appdata + 'sounds/green'
if soundFilename is not None:
if soundFilename is not None and play is True:
# if not wav then try mp3 format
if not os.path.isfile(soundFilename + '.wav'):
soundFilename = soundFilename + '.mp3'