Stereo I2S Audio Amplifier - Text to Speech
This example shows how you can use TTS (Text-to-Speech) model from Google of OpenAI to generate audio for given text prompt.
void setup()
{
// Generate audio for given text
const char* prompt = "So many things to say, so little time.";
// Google TTS
audio.connecttospeech(prompt, "en");
// OpenAI TTS
//audio.openai_speech("openAI-secret-key", "tts-1", prompt, "", "shimer", "mp3", "1");
}
audio.connecttospeech()
Generate human-like audio from given text prompt using Google TTS model.
Returns type: Bool
Returns value: Returns true if connection was successful.
Function parameters:
| Type | Name | Description |
|---|---|---|
const char* | speech | Text prompt. |
const char* | lang | Language in which to generate audio. |
audio.openai_speech()
Generate human-like audio from given text prompt using OpenAI's TTS model.
Returns type: Bool
Returns value: Returns true if request was valid.
Function parameters:
| Type | Name | Description |
|---|---|---|
String | api_key | OpenAI API key. |
String | model | TTS model (tts-1 or tts-1-hd). |
String | input | Text to generate audio for (max. length is 4096 characters). |
String | instructions | (Optional) Description of desired characteristics of the generated audio. |
String | voice | Voice to use when generating the audio. Supported voices: alloy, echo, fable, onyx, nova and shimmer |
String | response_format | Optional. Format in which to play audio. Supported formats: MP3 (default), opus, aac and flac. |
String | speed | (Optional) Speed of generated audio, values ranging from 0.25 to 4 with 1.0 being default. |
Full Example
The full example is listed below:
#include "Soldered_I2S_Audio_Amplifier.h"
#include "WiFi.h"
// Declare GPIOs used
#define I2S_DOUT 25
#define I2S_BCLK 27
#define I2S_LRCLK 26
// Network credentials
const char* SSID = "";
const char* PASSWORD = "";
// Create an instance of audio player object
I2SAudio audio;
// Audio callback function, gives full information of current audio source (optional but good to have)
void my_audio_info(I2SAudio::msg_t m)
{
Serial.printf("%s: %s\n", m.s, m.msg);
}
void setup()
{
Serial.begin(115200);
// Declare callback function
I2SAudio::audio_info_callback = my_audio_info;
// Connect to existing WiFi network
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
// Set audio amplifier pins to output I2S digital audio
audio.setPinout(I2S_BCLK, I2S_LRCLK, I2S_DOUT);
// Set volume, default 0 ... 21
audio.setVolume(10);
// Generate audio for given text
const char* prompt = "So many things to say, so little time.";
// Google TTS
audio.connecttospeech(prompt, "en");
// OpenAI TTS
//audio.openai_speech("openAI-secret-key", "tts-1", prompt, "", "shimer", "mp3", "1");
}
void loop()
{
// audio.loop() must be called constantly
audio.loop();
// Prevent distortion
vTaskDelay(1);
}
Text_to_Speech.ino
Full example on our GitHub