Provides functionalities for generating audio content from text and JSON data using OpenAI's APIs.
This module contains functions for creating speech from text input, saving audio files, extracting text from JSON for speech synthesis, and converting JSON data to audio files in a specified directory. It leverages OpenAI's text-to-speech and GPT-4 models to process and convert textual content into spoken audio, supporting various customization options like voice model and speech speed.
- Source:
Methods
(async, static) createSpeech(input, apikeyopt, voiceopt, speedopt, modelopt, verboseopt) → {Buffer|null}
Creates an audio speech from text using the OpenAI API.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
string | The text to be converted into speech. |
||
apikey |
string |
<optional> |
The OpenAI API key. If not provided, it will use the environment variable 'OPENAI_API_KEY'. |
|
voice |
string |
<optional> |
'echo' | The voice model to use. |
speed |
number |
<optional> |
1.0 | The speed of the speech (0.25 to 4.0). |
model |
string |
<optional> |
'tts-1' | The speech model to use. |
verbose |
boolean |
<optional> |
false | If set to true, enables verbose logging for detailed information. |
- Source:
Throws:
-
Logs an error to the console if fetching the speech fails and verbose is true.
- Type
- Error
Returns:
The audio data as a Buffer, or null if an error occurs or no API key is provided.
- Type
- Buffer | null
(async, static) getTextFromJson(json, apikeyopt, modelopt, verboseopt) → {string|null}
Pass json to chatGPT and ask it to extract the text for speech using gpt4.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
json |
Object | The JSON object containing the data to extract text from. |
||
apikey |
string |
<optional> |
The OpenAI API key. If not provided, it will use the environment variable 'OPENAI_API_KEY'. |
|
model |
string |
<optional> |
'gpt-4o-mini' | The text model to use. |
verbose |
boolean |
<optional> |
false | If set to true, enables verbose logging for detailed information. |
- Source:
Throws:
-
Logs an error to the console if there is an error in fetching or processing the request and verbose is true.
- Type
- Error
Returns:
The extracted text from the JSON object, or null if an error occurs or no API key is provided.
- Type
- string | null
(async, static) saveSpeech(mp3SaveFilePath, buffer, verboseopt) → {void}
Saves the given audio buffer to a file.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
mp3SaveFilePath |
string | The file path where the MP3 should be saved. |
||
buffer |
Buffer | The audio data to be saved. |
||
verbose |
boolean |
<optional> |
false | If set to true, enables verbose logging for detailed information. |
- Source:
Throws:
-
Logs an error to the console if there is a failure in saving the audio file and verbose is true.
- Type
- Error
Returns:
Does not return a value; saves the audio buffer to a file.
- Type
- void
(async, static) speechFromDir(fromFolder, toFolder, apikeyopt, verboseopt) → {void}
Converts all JSON files in a directory to speech files.
Recursively processes directories and skips non-JSON files.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
fromFolder |
string | The directory containing JSON files. |
||
toFolder |
string | The directory where the resulting MP3 files will be saved. |
||
apikey |
string |
<optional> |
The OpenAI API key. If not provided, it will use the environment variable 'OPENAI_API_KEY'. |
|
verbose |
boolean |
<optional> |
false | If set to true, enables verbose logging for detailed information. |
- Source:
Throws:
-
Logs an error to the console if there is a failure in reading the directory or processing files and verbose is true.
- Type
- Error
Returns:
Does not return a value; processes files in place.
- Type
- void