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.

# tts_test.py: 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 = client.audio.speech.create(model="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
  play(audio)


if __name__ == "__main__":
  #text = input("Enter text: ")
  client = OpenAI()
  text = "this is a test, dude!  Does it sound like a human?"
  stream_and_play(text)
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