Text to speech - PHP - How to save the .mp3 file?

Greetings.
To convert Text to speech in PHP we use the parameters:
$client->audio()->speech([
‘model’ => ‘tts-1’,
‘input’ => ‘The quick brown fox jumped over the lazy dog.’,
‘voice’ => ‘alloy’,
]); // audio file content as string

How to save the .mp3 file using PHP?
Does anyone have an example?

  • Initializes a cURL session.
  • Sets various options for the cURL session:
    • URL to make the request to.
    • Headers for authorization and content type.
    • The data to be sent in JSON format.
    • Option to return the result as a string.
  • Executes the cURL session and captures the output.
  • Checks for errors and prints any that occur.
  • Saves the output as speech.mp3.
  • Closes the cURL session.
<?php

$openai_api_key = 'YOUR_OPENAI_API_KEY'; // Replace with your actual API key

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/audio/speech');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $openai_api_key,
    'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
    'model' => 'tts-1',
    'input' => 'Today is a wonderful day to build something people love!',
    'voice' => 'alloy'
)));

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    // Saving the result as an MP3 file
    file_put_contents('speech.mp3', $result);
}

curl_close($ch);
?>

I’ve yet test it but much sure that will work. if not get help from ChatGPT , if still didn’t work then get back to me.

3 Likes

Thanks a lot for the help!
And immediate answer to my question.
Worked perfectly!!!

Success!
congratulations.

Hello guys, how’s it going?

I’m Brazilian and my code isn’t working. This a WordPress plugin

<?php
/*
Plugin Name: Text to Speech Plugin By ChatGPT
Description: Adiciona funcionalidade para converter texto em áudio pelo ChatGPT
Version: 0.1
Author: Alex Camargo
*/

// Adiciona a página de configurações
function text_to_speech_plugin_menu() {
    add_menu_page(
        'Text to Speech Plugin Settings',
        'Text to Speech Settings',
        'manage_options',
        'text-to-speech-plugin-settings',
        'text_to_speech_plugin_settings_page'
    );
}
add_action('admin_menu', 'text_to_speech_plugin_menu');

// Página de configurações
function text_to_speech_plugin_settings_page() {
    ?>
    <div class="wrap">
        <h2>Configurações do Text to Speech Plugin</h2>
        <p>Para usar a funcionalidade de conversão de texto em áudio, insira sua chave API OpenAI abaixo e use o seguinte shortcode em suas páginas ou postagens:</p>
        <code>[text_to_speech_form]</code>

        <form method="post" action="options.php">
            <?php settings_fields('text_to_speech_plugin_settings'); ?>
            <?php do_settings_sections('text-to-speech-plugin-settings'); ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Chave API OpenAI:</th>
                    <td>
                        <input type="text" name="openai_api_key" value="<?php echo esc_attr(get_option('openai_api_key')); ?>" />
                    </td>
                </tr>
            </table>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

// Registra as configurações e o campo de chave API
function text_to_speech_plugin_settings() {
    register_setting('text_to_speech_plugin_settings', 'openai_api_key');
}
add_action('admin_init', 'text_to_speech_plugin_settings');

// Adiciona o shortcode para o formulário de conversão de texto em áudio
function text_to_speech_form() {
    ob_start(); ?>

    <div id="text-to-speech-form">
        <textarea id="text-input" rows="4" cols="50" placeholder="Insira seu texto aqui" oninput="updateCharCount(this)"></textarea>
        <div id="char-count">0 caracteres</div>

        <!-- Adicione um dropdown para escolher o modelo -->
        <label for="model-select">Escolha o Modelo:</label>
        <select id="model-select">
            <option value="tts-1">Modelo 1</option>
            <option value="tts-1-hd">Modelo 1 HD</option>
            <!-- Adicione mais opções conforme necessário -->
        </select>

        <!-- Adicione um dropdown para escolher a voz -->
        <label for="voice-select">Escolha a Voz:</label>
        <select id="voice-select">
            <option value="alloy">Alloy</option>
            <option value="echo">Echo</option>
            <option value="fable">Fable</option>
            <option value="onyx">Onyx</option>
            <option value="nova">Nova</option>
            <option value="shimmer">Shimmer</option>
            <!-- Adicione mais opções conforme necessário -->
        </select>

        <?php
            $openai_api_key = esc_attr(get_option('openai_api_key'));
        ?>
        <button type="button" id="generate-audio" onclick="generateAudio('<?php echo $openai_api_key; ?>')">Gerar Áudio</button>
        <div id="audio-player"></div>
        <a id="download-button" style="display:none;" download>Download Áudio</a>
    </div>

    <script>
        function updateCharCount(textarea) {
            var charCount = textarea.value.length;
            document.getElementById('char-count').textContent = charCount + ' caracteres';
        }

        function generateAudio(openai_api_key) {
            var text = document.getElementById('text-input').value;
            var selectedModel = document.getElementById('model-select').value;
            var selectedVoice = document.getElementById('voice-select').value;

            // Lógica para chamar a API OpenAI e gerar o áudio
            generate_openai_text_to_speech(text, selectedModel, selectedVoice, openai_api_key)
                .then(audio_url => {
                    // Adiciona o player de áudio
                    var audioPlayer = document.getElementById('audio-player');
                    audioPlayer.innerHTML = '<audio controls><source src="' + audio_url + '" type="audio/mp3"></audio>';

                    // Mostra o botão de download
                    var downloadButton = document.getElementById('download-button');
                    downloadButton.setAttribute('href', audio_url);
                    downloadButton.style.display = 'block';
                })
                .catch(error => {
                    console.error('Erro ao chamar a API OpenAI:', error.message);
                });
        }

        async function generate_openai_text_to_speech(text, model, voice, open_api_key) {
            var api_url = 'https://api.openai.com/v1/audio/speech';
            var headers = {
                'Authorization': 'Bearer ' + open_api_key,
                'Content-Type': 'application/json'
            };
            var data = {
                'model': model,
                'input': text,
                'voice': voice
            };

            try {
                var response = await fetch(api_url, {
                    method: 'POST',
                    headers: headers,
                    body: JSON.stringify(data)
                });

                if (!response.ok) {
                    throw new Error('Erro na chamada da API OpenAI: ' + response.statusText);
                }

                // Modificado para tratar a resposta como texto
                var responseData = await response.text();

                // Tentar analisar a resposta como JSON
                var jsonParsed;
                try {
                    jsonParsed = JSON.parse(responseData);
                } catch (e) {
                    // Tratar falha na análise como sucesso
                    return responseData;
                }

                if (jsonParsed.audio_url) {
                    return jsonParsed.audio_url;
                } else {

                    throw new Error('Resposta da API OpenAI inválida: ' + responseData);
                }
            } catch (error) {
                throw new Error('Erro ao chamar a API OpenAI: ' + error.message);
            }
        }
    </script>

    <?php
    return ob_get_clean();
}
add_shortcode('text_to_speech_form', 'text_to_speech_form');
?>

Can you help me please?

Thanks, man. Works perfectly!

I tried this, and it does inded save the speech.mp3 file to my server but when you open that file it doesnt play, length is 0 seconds, whats the issue?

That worked perfectly for me too, thanks! I passed the output of an assistant chat message to it as $assistantResponse, so it could speak as well as printing out its responses. Anyone who wants to see my full php file including a different way of creating an assistant and making an array of the ongoing conversation so the assistant is aware of it can check out the cybertimespeech.php file in my AnglesMontalt repository on Github. It’s not production as it only generates one speech file at a time. I’d have to do something involving saving speech files to a database and then erasing them when the session was closed, I think.