Skip to content

Commit

Permalink
Fix PermissionError on Windows for playsound
Browse files Browse the repository at this point in the history
Convert ``FileIO`` object's attributes to strings
Windows doesn't ``PathLike`` attributes
  • Loading branch information
dormant-user committed May 9, 2023
1 parent e52b2f1 commit eab9329
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
16 changes: 14 additions & 2 deletions jarvis_ui/executables/starter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import string
import struct
from datetime import datetime
from multiprocessing import Process
from multiprocessing.managers import DictProxy # noqa
from threading import Timer
from typing import NoReturn, Union

import pvporcupine
Expand Down Expand Up @@ -54,8 +56,18 @@ def processor() -> Union[str, None]:
if response is True:
logger.info("Response received as audio.")
display.write_screen("Response received as audio.")
playsound(sound=fileio.speech_wav_file)
os.remove(fileio.speech_wav_file)
# Because Windows runs into PermissionError if audio file is open when removing it
if settings.operating_system == "Windows":
player = Process(target=playsound, kwargs={'sound': fileio.speech_wav_file})
player.start()
player.join()
if player.is_alive():
player.terminate()
player.kill()
Timer(interval=3, function=os.remove, args=(fileio.speech_wav_file,)).start()
else:
playsound(sound=fileio.speech_wav_file)
os.remove(fileio.speech_wav_file)
return
response = response.get('detail', '')
logger.info(f"Response: {response}")
Expand Down
1 change: 1 addition & 0 deletions jarvis_ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def start() -> NoReturn:
from jarvis_ui.modules.models import env # noqa: F401
status_manager = Manager().dict()
status_manager["LOCKED"] = False # Instantiate DictProxy
# todo: Process won't work for Linux as it doesn't stream on audio I/O devices in child processes
process = Process(target=initiator, args=(status_manager,))
process.name = pathlib.Path(__file__).stem
process.start()
Expand Down
7 changes: 7 additions & 0 deletions jarvis_ui/modules/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class Settings(BaseSettings):


settings = Settings()
# Intermittently changes to Windows_NT because of pydantic
if settings.operating_system.startswith('Windows'):
settings.operating_system = "Windows"


def import_module() -> NoReturn:
Expand Down Expand Up @@ -211,6 +214,10 @@ class FileIO(BaseSettings):

env = EnvConfig()
fileio = FileIO()
# because playaudio in Windows uses string concatenation assuming input sound is going to be a string
if settings.operating_system == "Windows":
for key, value in fileio.__dict__.items():
setattr(fileio, key, value.__str__())
raw_token = env.token
env.token = UNICODE_PREFIX + UNICODE_PREFIX.join(binascii.hexlify(data=env.token.encode(encoding="utf-8"),
sep="-").decode(encoding="utf-8").split(sep="-"))
Expand Down
6 changes: 6 additions & 0 deletions release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Release Notes
=============

0.6.0 (05/09/2023)
------------------
- Fix ``PermissionError`` on Windows for playsound
- Convert ``FileIO`` object's attributes to strings
- Windows doesn't ``PathLike`` attributes

0.5.9 (05/09/2023)
------------------
- Improve audio quality on predefined responses
Expand Down

0 comments on commit eab9329

Please sign in to comment.