From 5be4a6eee74f70aa587455905101918ee642744c Mon Sep 17 00:00:00 2001 From: sunjiayu <767978963@qq.com> Date: Mon, 3 Jun 2024 10:43:39 +0800 Subject: [PATCH 1/3] append audio README for text-to-speech method --- documentation/audio/README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/documentation/audio/README.md b/documentation/audio/README.md index 0588974..107119f 100644 --- a/documentation/audio/README.md +++ b/documentation/audio/README.md @@ -62,6 +62,32 @@ liboai::FutureResponse translate_async( ) const& noexcept(false); ``` +

Text to speech

+

Turn text into lifelike spoken audio. Returns a liboai::Response containing response data. The audio data is in the content field of the liboai::Response

+ +```cpp +liboai::Response speech( + const std::string& model, + const std::string& voice, + const std::string& input, + std::optional response_format = std::nullopt, + std::optional speed = std::nullopt +) const& noexcept(false); +``` + +

Text to speech (async)

+

Asynchronously turn text into lifelike spoken audio. Returns a liboai::FutureResponse containing response data. The audio data is in the content field of the liboai::Response

+ +```cpp +liboai::FutureResponse speech_async( + const std::string& model, + const std::string& voice, + const std::string& input, + std::optional response_format = std::nullopt, + std::optional speed = std::nullopt +) const& noexcept(false); +``` +

All function parameters marked optional are not required and are resolved on OpenAI's end if not supplied.


From 4fb2c026e4c8282da5c6f53180a5415f6998a415 Mon Sep 17 00:00:00 2001 From: sunjiayu <767978963@qq.com> Date: Mon, 3 Jun 2024 10:44:33 +0800 Subject: [PATCH 2/3] fix speech examples to avoid unclosed filestream when exception happens --- documentation/audio/examples/create_speech.cpp | 3 +-- documentation/audio/examples/create_speech_async.cpp | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/documentation/audio/examples/create_speech.cpp b/documentation/audio/examples/create_speech.cpp index 959ec84..306bf9f 100644 --- a/documentation/audio/examples/create_speech.cpp +++ b/documentation/audio/examples/create_speech.cpp @@ -5,8 +5,6 @@ using namespace liboai; int main() { OpenAI oai; - std::ofstream ocout("demo.mp3", std::ios::binary); - if (oai.auth.SetKeyEnv("OPENAI_API_KEY")) { try { Response res = oai.Audio->speech( @@ -14,6 +12,7 @@ int main() { "alloy", "Today is a wonderful day to build something people love!" ); + std::ofstream ocout("demo.mp3", std::ios::binary); ocout << res.content; ocout.close(); std::cout << res.content.size() << std::endl; diff --git a/documentation/audio/examples/create_speech_async.cpp b/documentation/audio/examples/create_speech_async.cpp index cc60ad1..cd404cb 100644 --- a/documentation/audio/examples/create_speech_async.cpp +++ b/documentation/audio/examples/create_speech_async.cpp @@ -7,7 +7,6 @@ int main() { if (oai.auth.SetKeyEnv("OPENAI_API_KEY")) { try { - std::ofstream ocout("demo.mp3", std::ios::binary); auto fut = oai.Audio->speech_async( "tts-1", "alloy", @@ -20,7 +19,7 @@ int main() { // get the contained response auto res = fut.get(); - + std::ofstream ocout("demo.mp3", std::ios::binary); ocout << res.content; ocout.close(); std::cout << res.content.size() << std::endl; From 267512424661ee4bdbdfbad814c20cd6fdb23934 Mon Sep 17 00:00:00 2001 From: sunjiayu <767978963@qq.com> Date: Mon, 3 Jun 2024 11:33:23 +0800 Subject: [PATCH 3/3] add functionality to endpoint in audio README --- documentation/audio/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/documentation/audio/README.md b/documentation/audio/README.md index 107119f..793c6e7 100644 --- a/documentation/audio/README.md +++ b/documentation/audio/README.md @@ -3,6 +3,7 @@ This class and its associated liboai::OpenAI interface allow access to the Audio endpoint of the OpenAI API; this endpoint's functionality can be found below.

- Turn audio to text. +- Turn text to audio.

Methods