Skip to content

Commit

Permalink
Add implementation of multiple exit jingles
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-stepanov committed Apr 27, 2024
1 parent 3829ba8 commit 98ce4ba
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions advent/advent.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
REC_CONFIDENCE = 10 # (%) - lowest still OK without false positives
TV_DEAD_TIME = 30 # (s) - action dead time after previous action taken on TV
MUTE_TIMEOUT = 600 # (s) - if TV is muted, unmute automatically after this time. Must be >= TV_DEAD_TIME
NUM_EXIT_JINGLES = 1 # - number of exit jingles to consider in order to exit action
LOG_FILE = 'advent.log'

# Globals
Expand Down Expand Up @@ -73,6 +74,7 @@ def __init__(self, tvc = TVControl(), action = 'mute', in_action = False, volume
self.action_lock = threading.Lock()
self.last_detection_time = datetime.now()
self.last_action_time = datetime.now() - timedelta(seconds=TV_DEAD_TIME)
self.exit_jingles = 0

def getAction(self):
return self.action
Expand All @@ -88,6 +90,9 @@ def isInAction(self):
return self.in_action

def startAction(self):
global NUM_EXIT_JINGLES

self.exit_jingles = NUM_EXIT_JINGLES
if self.action == 'lower_volume':
if self.volume_delta:
self.in_action = self.tvc.changeVolume(self.volume_delta)
Expand All @@ -98,8 +103,12 @@ def startAction(self):
return self.in_action

def stopAction(self):
self.in_action = not(self.tvc.restoreVolume() if self.action == 'lower_volume' else self.tvc.toggleMute())
return not(self.in_action)
self.exit_jingles -= 1
if self.exit_jingles == 0:
self.in_action = not(self.tvc.restoreVolume() if self.action == 'lower_volume' else self.tvc.toggleMute())
return 2 if self.in_action else 0
else:
return 1

def getTimeSinceLastAction(self):
return datetime.now() - self.last_action_time
Expand Down Expand Up @@ -282,8 +291,11 @@ def run(self):

if self.tv.isInAction():
if ad_end:
if self.tv.stopAction():
ret = self.tv.stopAction()
if ret == 0:
LOGGER.info('TV volume restored' if self.tv.getAction() == 'lower_volume' else 'TV unmuted')
elif ret == 1:
LOGGER.info('Muti-jingle ignored; remaining in action')
else:
LOGGER.warning('Warning: TV action failed')
else:
Expand Down Expand Up @@ -333,6 +345,7 @@ def main():
global MUTE_TIMEOUT
global MUTE_TIMEOUT_TD
global TV_CODES
global NUM_EXIT_JINGLES

## Command-line parser
parser = argparse.ArgumentParser(description='Combat TV commercials by detecting ad jingles in the input audio stream',
Expand All @@ -344,7 +357,7 @@ def main():
parser.add_argument('-V', '--volume', help=f'delta for volume lowering (defaults: PulseAudio: -50 (%%), HarmonyHub and BroadLink: -5)', type=int)
parser.add_argument('-d', '--tv_codes', help='path to a folder with TV control codes (used with BroadLink; default: $HOME/tv-codes)', default=TV_CODES)
parser.add_argument('-m', '--mute_timeout', help=f'undo hit action automatically after timeout (s) (default: {MUTE_TIMEOUT}; use 0 to disable)', type=int)
parser.add_argument('-j', '--exit_jingle', help='exit hit action after N exit jingles (default: 1)', type=int)
parser.add_argument('-j', '--exit_jingles', help='exit hit action after N exit jingles (default: 1)', type=int)
parser.add_argument('-n', '--num_threads', help=f'run N recognition threads (default: {NUM_THREADS})', type=int)
parser.add_argument('-i', '--rec_interval', help=f'audio recognition interval (s) (default: {REC_INTERVAL})', type=float)
parser.add_argument('-c', '--rec_confidence', help=f'audio recognition confidence (%%) (default: {REC_CONFIDENCE})', type=int)
Expand Down Expand Up @@ -415,6 +428,9 @@ def main():
REC_CONFIDENCE = args.rec_confidence
LOGGER.info(f'Recognition interval is {REC_INTERVAL} s with confidence of {REC_CONFIDENCE}%')

if args.exit_jingles != None:
NUM_EXIT_JINGLES = args.exit_jingles

# Thread control
if args.num_threads != None:
if args.num_threads < 1:
Expand Down

0 comments on commit 98ce4ba

Please sign in to comment.