- make BT I/O script call more smooth

This commit is contained in:
Dmytro Bogovych 2023-08-24 18:15:11 +03:00
parent bf60346fc7
commit a069b5c471
2 changed files with 18 additions and 26 deletions

View File

@ -288,11 +288,10 @@ def perform_answerer():
while True:
# Remove old recording
record_file = f'/dev/shm/bt_record.wav'
cmd = f'/usr/bin/python3 {DIR_THIS}/bt_call_controller.py --play-file {REFERENCE_AUDIO} --record-file {record_file} --timelimit {int(reference_length)}'
retcode = os.system(cmd)
if retcode != 0:
utils.log(f'Got non-zero exit code {retcode} from BT call controller, exiting.')
try:
bt_call_controller.run(play_file=REFERENCE_AUDIO, record_file=record_file, timelimit_seconds=int(reference_length), target=None)
except Exception as e:
utils.log(f'BT I/O failed, exiting. Error: {str(e)}')
break
# Call analyzer script

View File

@ -231,26 +231,23 @@ def get_pid(name):
return int(subprocess(["pidof","-s",name]))
def main(args: dict):
def run(play_file: str, record_file: str, timelimit_seconds: int, target: str):
global CALL_PATH, CALL_LOCK, CALL_ADDED, CALL_REMOVED
# Ensure Ctrl-C handler is default
# signal.signal(signal.SIGINT, signal.SIG_DFL)
# Check if input file exists
if not os.path.exists(args['play_file']):
if not os.path.exists(play_file):
utils.log(f'Problem: file to play ({args["play_file"]}) doesn\'t exists.')
exit(os.EX_DATAERR)
# Duration in seconds
watchdog_timeout = int(args['timelimit'])
if watchdog_timeout == 0:
if timelimit_seconds == 0:
# Use duration of played file
audio_file = soundfile.SoundFile(args['play_file'])
watchdog_timeout = int(audio_file.frames / audio_file.samplerate + 0.5)
utils.log(f'Play timeout is set to {watchdog_timeout} seconds')
audio_file = soundfile.SoundFile(play_file)
timelimit_seconds = int(audio_file.frames / audio_file.samplerate + 0.5)
utils.log(f'Play timeout is set to {timelimit_seconds} seconds')
# Empty call path means 'no call started'
# CALL_LOCK.acquire()
@ -269,15 +266,11 @@ def main(args: dict):
PHONE.setup_dbus_loop()
# Start call
if 'target' in args:
target_number = args['target']
if target_number is not None and len(target_number) > 0:
if target is not None and len(target) > 0:
# Make a call
dial_number(target_number, args['play_file'])
dial_number(target, play_file)
else:
answer_call(args['play_file'])
else:
answer_call(args['play_file'])
answer_call(play_file)
# Don't make volume 100% - that's too much
audio_volume = 50
@ -286,14 +279,14 @@ def main(args: dict):
set_headset_mic_volume(audio_volume)
# Start recording
utils.log(f'Start recording with ALSA to {args["record_file"]}')
process_recording = capture_phone_alsaoutput(args['record_file'])
utils.log(f'Start recording with ALSA to {record_file}')
process_recording = capture_phone_alsaoutput(record_file)
utils.log(f'Main loop PID: {os.getpid()}, TID: {threading.get_ident()}')
# Wait until call is finished
time_start = time.time()
while not CALL_REMOVED.value and time_start + watchdog_timeout > time.time():
while not CALL_REMOVED.value and time_start + timelimit_seconds > time.time():
time.sleep(0.5)
utils.log(f'Call {CALL_PATH} finished.')
@ -319,7 +312,7 @@ if __name__ == "__main__":
retcode = 0
try:
main(args)
run(args['play_file'], args['record_file'], args['timelimit'], args['target'] if 'target' in args else None)
except KeyboardInterrupt as e:
print('Ctrl-C pressed, exiting')
cleanup()