Workaround for "permission denied" error using TTS API on Windows 11

The below “hello world” script for the TTS API gets a “permission denied” error on the temp .wav file when run on Windows 11 (without the pyaudio package installed, which is not directly referenced in the script).

Workaround: install the pyaudio package (pip install pyaudio) and rerun the script - it now correctly plays the synthesized audio.

# try out the OpenAI TTS API
import io
from openai import OpenAI
import openai
from pydub import AudioSegment
from pydub.playback import play
def stream_and_play(text):
  # create stream
  response ="tts-1", voice="alloy", input=text)

  # Convert the binary response content to a byte stream
  byte_stream = io.BytesIO(response.content)

  # Read the audio data from the byte stream
  audio = AudioSegment.from_file(byte_stream, format="mp3")

  # Play the audio

if __name__ == "__main__":
  #text = input("Enter text: ")
  client = OpenAI()
  text = "this is a test, dude!  Does it sound like a human?"
1 Like

FYI, when I submitted this bug a few days ago, it never appeared, so I am resubmitting it now, with newly discovered workaround.

1 Like