simonw / ospeak

CLI tool for running text through OpenAI Text to speech
Apache License 2.0
161 stars 9 forks source link

Permission denied on writing wav. Is Windows supported? #11

Open ChrisTorng opened 9 months ago

ChrisTorng commented 9 months ago

Thanks for your job. I tried it on Windows, this is shown:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "c:\users\christorng\.local\bin\ospeak.exe\__main__.py", line 7, in <module>
  File "C:\Users\ChrisTorng\.local\pipx\venvs\ospeak\Lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ChrisTorng\.local\pipx\venvs\ospeak\Lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "C:\Users\ChrisTorng\.local\pipx\venvs\ospeak\Lib\site-packages\click\core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ChrisTorng\.local\pipx\venvs\ospeak\Lib\site-packages\click\core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ChrisTorng\.local\pipx\venvs\ospeak\Lib\site-packages\ospeak\cli.py", line 84, in cli
    stream_and_play(text, voice, speed, speak or not output, token, output)
  File "C:\Users\ChrisTorng\.local\pipx\venvs\ospeak\Lib\site-packages\ospeak\cli.py", line 26, in stream_and_play
    play(audio)
  File "C:\Users\ChrisTorng\.local\pipx\venvs\ospeak\Lib\site-packages\pydub\playback.py", line 71, in play
    _play_with_ffplay(audio_segment)
  File "C:\Users\ChrisTorng\.local\pipx\venvs\ospeak\Lib\site-packages\pydub\playback.py", line 15, in _play_with_ffplay
    seg.export(f.name, "wav")
  File "C:\Users\ChrisTorng\.local\pipx\venvs\ospeak\Lib\site-packages\pydub\audio_segment.py", line 867, in export
    out_f, _ = _fd_or_path_or_tempfile(out_f, 'wb+')
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ChrisTorng\.local\pipx\venvs\ospeak\Lib\site-packages\pydub\utils.py", line 60, in _fd_or_path_or_tempfile
    fd = open(fd, mode=mode)
         ^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\CHRIST~1\\AppData\\Local\\Temp\\tmpwyvex3zy.wav'

I've tested on the same Command Prompt, I can add a new file in the folder C:\Users\CHRIST~1\AppData\Local\Temp, without permission problem.

jeffmcjunkin commented 8 months ago

Some troubleshooting with ChatGPT found the underlying issue:

NamedTemporaryFile: If you are using NamedTemporaryFile elsewhere in your code, be aware that this function works differently on Windows compared to Unix-based systems. On Unix, the file can be opened by another process immediately after creation, but on Windows, it cannot be opened by another name until it is closed.

The solution in my case was to inject simpleaudio into the venv: pipx inject ospeak simpleaudio

Pinging @simonw , maybe you could include that package (or pyaudio), at least for Windows targets? Otherwise we reliably run into the above issue.