# How To Turn Text To Speech With OpenAI

**URL:** <https://onehack.st/t/how-to-turn-text-to-speech-with-openai/263931>\
**Category:** Tutorials & Methods\
**Tags:** freebies, tips-tricks, audio, ai\
**Created:** [November 13, 2023, 1:03am UTC](https://onehack.st/t/how-to-turn-text-to-speech-with-openai/263931 "2023-11-13T01:03:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prometheus](https://onehack.st/user_avatar/onehack.st/prometheus/32/34950_2.png) [@Prometheus](https://onehack.st/u/Prometheus)\
**Post date:** [November 13, 2023, 1:03am UTC](https://onehack.st/t/how-to-turn-text-to-speech-with-openai/263931/1 "2023-11-13T01:03:44Z")

</div>

**OpenAI** has recently launched the Audio API that includes a text to speech feature called `speech` , based on their TTS (text-to-speech) technology. This feature offers six built-in voices named _Alloy_ , _Echo_ , _Fable_ , _Onyx_ , _Nova_ , and _Shimmer_ .

These voices can be incredibly useful for tasks such as narrating blog posts, creating spoken audio in various languages, adding voiceovers to video tutorials, or delivering real-time spoken feedback. In my experience, the output is impressively natural-sounding. If you are not using any text-to-speech tools, then this offering by OpenAI is something you should consider trying.

In this article, we’ll explore **how to set up OpenAI’s TTS** and **create your first text-to-speech application**. For this demonstration, we will be using the following setup:

- Operating System – macOS
- Application – Terminal
- Programming Language – cURL

This guide is also applicable to Windows users. Where necessary, I’ll mention any tools and commands that differ from those used on macOS.

#### Step 1 – Set Up cURL

Many operating systems come with [cURL](https://curl.se/) pre-installed. If not, we will first install [Homebrew](https://brew.sh/), a package manager for macOS, which we will then use to install cURL.

##### Check if cURL is Installed

To check if you already have cURL on your system, make sure you’re connected to the Internet, then type the following command in your Terminal:

**Windows users:** Use Command Prompt or Windows PowerShell

curl [https://platform.openai.com](https://platform.openai.com)

If cURL is set up correctly and you have an Internet connection, it will send an HTTP request to retrieve the contents of _[platform.openai.com](http://platform.openai.com)_, and you should see output similar to this screenshot:

 ![image](https://onehack.st/uploads/default/original/3X/9/5/95cf4db88393684c8c77c2040eb35a0b25ef5e7a.jpeg)

##### How to Install cURL

If you encounter an error indicating that cURL is not installed, you can install it by following the provided steps.

**Windows users:** [How to install cURL on Windows.](https://linuxhint.com/install-use-curl-windows/)

Open a new Terminal window, and enter the commands below to first install Homebrew:

`/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"`

After installing Homebrew, use the following command to install cURL:

brew install curl

Lastly, run the command below to set the Homebrew version of cURL as the default one in your shell:

```auto
echo 'export PATH="$(brew --prefix)/opt/curl/bin:$PATH"' >> ~/.zshrc source ~/.zshrc

```

#### Step 2 – Get API Key from OpenAI

To obtain your API key, first go to [openai.com](https://openai.com/), log in, and then click on “ **API keys** ” in the sidebar.

 ![image](https://onehack.st/uploads/default/original/3X/3/1/319884b016ccf3fb3ba455249348866627410d0e.jpeg)

On the API keys page, click “ **+ Create new secret key** “, give it a name, and then click “ **Create secret key** “.

 ![image](https://onehack.st/uploads/default/original/3X/8/7/876f4c4b359868b58b758721d50e2108750edc7b.jpeg)

Afterward, you will receive a new secret key. Make sure to copy and keep it somewhere safe because we will use it later.

Store this secret key in a secure and accessible location. **You will not be able to view it again** through your OpenAI account. If you lose this secret key, you will have to create a new one.

 ![image](https://onehack.st/uploads/default/original/3X/1/d/1da0127904ef756134981074fa68918f4bb17dd0.jpeg)

Step 3 – Create Your First Text-to-Speech  
Now it’s time to create your first text-to-speech. Refer to the code below, and replace YOUR\_API\_KEY\_HERE with your actual API key.

```auto
curl https://api.openai.com/v1/audio/speech \
  -H "Authorization: Bearer YOUR_API_KEY_HERE" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "tts-1",
    "input": "hello world",
    "voice": "alloy"
  }' \
  --output example.mp3

```

**Example:**

```auto
curl https://api.openai.com/v1/audio/speech \
  -H "Authorization: Bearer sk-IfClJS63a7Ny3v6yKncIT3XXXXXXXXXXXXXX" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "tts-1",
    "input": "hello world",
    "voice": "alloy"
  }' \
  --output example.mp3

```

Copy the entire code, paste it into your terminal (Windows users can use Command Prompt or PowerShell), and press Enter.

That’s it! This action will create an audio file called example.mp3 that says “hello world”.

#### Other Changes You Can Make

Now that you’re familiar with converting text into lifelike spoken audio using the OpenAI Audio API, let’s delve into additional adjustments you can make that will influence the quality and style of your TTS output.

Essentially, you can modify the following:

- The model used,
- the input text,
- the voice selected,
- and the output file, including its format.

##### 1. Model

The default model is `tts-1`, which provides quick response times but at a slightly lower quality. You can switch to the `tts-1-hd` model for higher definition audio output.

**Example:**

`"model": "tts-1-hd"`

##### 2. Input

Any text enclosed within double quotes will be converted into spoken audio.

**Example:**

`"input": "hello there, how are you doing today?"`

##### 3. Voice

Currently, there are six different voices available: `alloy`, `echo`, `fable`, `onyx`, `nova`, and `shimmer`.

**Example:**

`"voice": "nova"`

##### 4. Output

By default, the output will be in .mp3 format. However, you can change the filename or choose from other supported audio formats. The currently supported formats include:

- **Opus .opus** : Ideal for internet streaming and communications with low latency.
- **AAC .aac** : Used for digital audio compression, preferred by platforms like YouTube and devices like Android and iOS.
- **FLAC .flac** : Provides lossless audio compression, favored by audiophiles for archiving purposes.

**Example:**

`--output myspeech.aac`

#### FAQ

**Where do I find the created audio file?**

The output file is located in the same folder or path where you executed the cURL script. To find out the current directory of your terminal (Windows users: PowerShell or Command Prompt), use the following command:

- macOS Terminal – `pwd`
- Windows PowerShell – `pwd`
- Windows Command Prompt – `cd`

**Can I create and use a custom copy of my voice?**

This feature is not currently supported by OpenAI.

**How do other voice options sound like?**

You can generate audio using different voice parameters to hear how other voices sound, or you can visit [this page](https://platform.openai.com/docs/guides/text-to-speech/voice-options) to listen to samples.

**Does it support other languages?**

Yes, it does support multiple languages. I’ve tested it with Japanese, Chinese (Mandarin), Vietnamese, and Spanish, and they seem to sound quite reasonable.

Source: Hongkiat

**Happy learning!**

> Credit To Chief @SaM for assigning this topic to me ❤ thanks, Sir!

---

<div class="post-metadata">

**Author:** ![Juiloc](https://onehack.st/user_avatar/onehack.st/juiloc/32/134042_2.png) [@Juiloc](https://onehack.st/u/Juiloc)\
**Post date:** [November 14, 2023, 1:31am UTC](https://onehack.st/t/how-to-turn-text-to-speech-with-openai/263931/2 "2023-11-14T01:31:29Z")

</div>

I was looking just for this, thank you for the great work.

---

<div class="post-metadata">

**Author:** ![newpatrio01](https://onehack.st/user_avatar/onehack.st/newpatrio01/32/34625_2.png) [@newpatrio01](https://onehack.st/u/newpatrio01)\
**Post date:** [February 18, 2024, 10:31pm UTC](https://onehack.st/t/how-to-turn-text-to-speech-with-openai/263931/3 "2024-02-18T22:31:03Z")

</div>

Thank you very much sir ! you are the great !

---

<div class="post-metadata">

**Author:** ![Roberto\_Diaz](https://onehack.st/user_avatar/onehack.st/roberto_diaz/32/18084_2.png) [@Roberto\_Diaz](https://onehack.st/u/Roberto_Diaz)\
**Post date:** [February 19, 2024, 12:06pm UTC](https://onehack.st/t/how-to-turn-text-to-speech-with-openai/263931/4 "2024-02-19T12:06:42Z")

</div>

Hi @Prometheus  
Thank you very much for sharing.  
I have a question: this can be done with the free version of ChatGPT?
