Update to tts_audio.sh

I was looking for a way to make sound files that didn’t involve making them on external software (Audacity) then converting them and finally uploading them to the node. I knew there had to be an easier way.
Well, I found in my search that there was a script already on the all-star image tts_audio.sh but it relied on a service called voicerss that you had to register and get a key and all that and after playing with it for a while I was never able to make it work. So I set out to find a way to do the same without having to rely on voicerss and hopefully make it easier.

I only know a bit about BASH or Python so a LOT of this code was polished by chat GPT so I don’t take full credit for this, I just put the time and asked the right questions to make it work. after testing for a while I came up with something that worked using gTTS (Google Text To Speach) free to use with limitations that only has 1 voice and sometimes you have to play with punctuation and the way you spell some words to get the right pronunciation. But hey it’s free.

First, it does need to install gTTS but first, it will need to install PIP a package manager to be able to install gTTS and it will also need to update FFMPEG (it might work with the version you already have of FFMPEG on your system but it didn’t break anything on my end so I think it is safe to upgrade)

Ok, let me explain a bit how it works and what you can expect.

    1. Upload the file to your desired folder I chose /usr/local/sbin you can upload it to whatever folder you like.
    1. Make the file executable:
chmod +x FILE NAME
    1. Open the BASH shell and if you installed it in /usr/local/sbin you can execute it from any folder you are in if you installed it on that folder but if you installed somewhere else you will have to move to that directory to be able to execute it by typing:
      (REMEMBER The first time you run the script it will look for gTTS, PIP, and FFMPEG and either update or install it on your system)
    1. Enter the text you want to convert to speech:
      Remember probably this first time you will not get the exact results you will like, that happened to me probably 1% of the time the other 99.98% it was fine. Periods will add a small pause to the speech when you go from one thing to another. Haven’t found anything else that works in the form of pauses and such.
    1. Enter the desired output file name (without extension)
    1. Do you want the speech in [E]nglish or [S]Spanish?
    1. Enter the desired output folder path:
    1. The script will create your file in WAV first, then it will convert it to .ul so Asterisk can use it.
    1. It will delete the WAV and save the .ul file to the folder you entered. Or the file you told it to create.
    1. Exit the BASH shell and open the CLI
    1. Remember the name of the file you just created and type to test it:
rpt localplay NODE NUMBER /folder/you-saved/the sound-file/SOUND-file-NAME  (NO EXTENSION)

Did it play? It did? Ok, you are done. Now go out and create whatever your heart desires.
Add a cron for any recurring sound files like a NET or general announcement.

10 */2 * * * asterisk -r -x "rpt localplay NODE /FOLDER_WHERE_SOUND_IS/SOUND-FILE_NO_EXTENSION
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
import subprocess
from tempfile import NamedTemporaryFile

def install_gtts():
        subprocess.check_call([sys.executable, "-m", "pip", "install", "gtts", "--trusted-host", "pypi.org", "--trusted-host", "files.pythonhosted.org"])
    except subprocess.CalledProcessError:
        print("Failed to install gtts. Please install it manually using the following steps:")
        print("1. Download the gtts package: wget https://files.pythonhosted.org/packages/8e/78/3498b0e5b98ed9e3d477d46c72a8c82b63a0846c1eeb63495b1e0a59a9f8/gTTS-2.2.3.tar.gz")
        print("2. Extract the package: tar xzf gTTS-2.2.3.tar.gz")
        print("3. Navigate to the extracted directory: cd gTTS-2.2.3")
        print("4. Install the package: sudo python3 setup.py install")

def check_and_install_gtts():
        import gtts
    except ImportError:
        print("gtts is not installed. Installing now...")
            import gtts
        except ImportError:
            print("Failed to install gtts. Please install it manually using the steps mentioned above.")

def convert_text_to_speech():
    text_to_convert = input("Enter the text you want to convert to speech: ").strip()
    output_filename_base = input("Enter the desired output file name (without extension): ").strip()
    lang_choice = input("Do you want the speech in [E]nglish or [S]panish? - ").strip().lower()
    output_folder = input("Enter the desired output folder path: ").strip()

    if not text_to_convert or not output_filename_base or lang_choice not in ['e', 's'] or not output_folder:
        print("Invalid input. Please try again.")

    lang = 'en' if lang_choice == 'e' else 'es'
    # Ensure the output folder exists
    os.makedirs(output_folder, exist_ok=True)

    # Construct the output file path
    output_filename = os.path.join(output_folder, "{}.ul".format(output_filename_base))

    # Create a temporary WAV file
    with NamedTemporaryFile(suffix=".wav", delete=False) as temp_wav:
        temp_wav_filename = temp_wav.name

        # Save the converted speech to a WAV file
        from gtts import gTTS
        tts = gTTS(text=text_to_convert, lang=lang)

        # Convert the WAV file to G.711 μ-law format using FFmpeg
        command = ['ffmpeg', '-i', temp_wav_filename, '-ar', '8000', '-ac', '1', '-f', 'mulaw', output_filename]
        subprocess.run(command, check=True)

        # If conversion is successful, delete the temporary WAV file
        print("The speech has been saved as {} and the temporary WAV file has been deleted.".format(output_filename))
    except subprocess.CalledProcessError as e:
        print("An error occurred during the conversion process: {}".format(e))
        if os.path.exists(temp_wav_filename):



1 Like