Text-to-speech on Linux/Mac using Bash made by ChatGPT 4

Vladimir Ignatev
3 min readNov 10, 2023

--

Every time I really need say command to work on my Linux box, something goes wrong. Again, I faced a bug reported on GNU.org that has been unresolved for almost 8 years. So, I asked ChatGPT 4 to create say replacement for me. Here is what was next.

./say.sh makes text-to-speech of ultra high quality using OpenAI TTS API under the hood. GIF generated by Asciinema.org

OpenAI Text-To-Speech models

OpenAI exposes 2 models over API for converting text-to-speech: tts-1 and tts-1-hd. In the toolbox we have 6 voices: 2 female voices, 2 teenager or gender-neutral and 2 male voices. For all of the details check out the OpenAI documentation on Text to speech.

Let’s feed ChatGPT 4 with our requirements. I want she to make me a Bash script that accepts parameters like model, voice, input file or string and output.

ChatGPT 4 answers with Text-to-speech Bash script outline

In the next step, I ask her to provide meaningful defaults for input flags.

ChatGPT 4 modifies the script to handle default values for script parameters

The next step is to mix it down with a code snippet from OpenAI docs.

ChatGPT 4 integrates text-to-speech snippet into our Bash tool

Well done! Check out the full dialog with ChatGPT 4 here.

As usually, the full script available as a GitHub Gist.

Usage example

./say.sh "Hello Medium! This speech has been synthesized using TTS from OpenAI"

This command will produce an output to speech.mp3file in the same directory. Play this sound file using ffplay speech.mp3

Use another voice, model and use a contents of story.txt file as an input and play the output instantly:

echo "Hello from file!" > story.txt && ./say.sh -m tts-1-hd -v alloy -i story.txt && ffplay speech.mp3 

Conclusion

ChatGPT 4 assist in many routine tasks related to copy and practically any kind of texts. Bash scripts is not an exclusion. When we need some handy code snippet, this Gen AI makes this engineering step much easier. Due to nature of Bash language, writing such scripts by hand is error-prone with Bash language cumbersome syntax.

Bash scripts shine when we need a command line tool, especially in a scenario where we need simple setup or avoid Python environments.

Finally, text-to-speech using OpenAI’s TTS produces incredible results.

I’m in need of your support. If you loved this article, please Buy me a coffee! Thank you.

References

  1. OpenAI Text To Speech Documentation: https://platform.openai.com/docs/guides/text-to-speech
  2. Thing to screen-record your Terminal sessions: https://asciinema.org/
  3. Full ChatGPT session of creating the Bash script: https://chat.openai.com/share/b888b439-51ba-4240-8684-69f14a6aefa7
  4. GitHub Gist containing the say.sh made with a help of ChatGPT: https://gist.github.com/vladignatyev/537a26554fd043742defca77c8eeb819

--

--

Vladimir Ignatev

Software engineer with over 15+ years of experience in web and mobile. Passionate about AI, no-code and startups.