Skip to content

Commit a585485

Browse files
committed
Open AI - TTS 1 Audio Streaming to Speed Up Playback, issue 4
1 parent 879c028 commit a585485

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/Audio.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
audio.cpp
44
55
Created on: Oct 28.2018 */char audioI2SVers[] ="\
6-
Version 3.3.2o ";
7-
/* Updated on: Jul 10.2025
6+
Version 3.3.2p ";
7+
/* Updated on: Jul 11.2025
88
99
Author: Wolle (schreibfaul1)
1010
Audio library for ESP32, ESP32-S3 or ESP32-P4
@@ -483,6 +483,7 @@ bool Audio::openai_speech(const String& api_key, const String& model, const Stri
483483

484484
String post_body = "{"
485485
"\"model\": \"" + model + "\"," +
486+
"\"stream\": true," + // add
486487
"\"input\": \"" + input_clean + "\"," +
487488
"\"instructions\": \"" + instructions_clean + "\"," +
488489
"\"voice\": \"" + voice + "\"," +
@@ -491,14 +492,16 @@ bool Audio::openai_speech(const String& api_key, const String& model, const Stri
491492
"}";
492493

493494
String http_request =
494-
"POST " + String(path) + " HTTP/1.0\r\n" // UNKNOWN ERROR CODE (0050) - crashing on HTTP/1.1 need to use HTTP/1.0
495+
// "POST " + String(path) + " HTTP/1.0\r\n" // UNKNOWN ERROR CODE (0050) - crashing on HTTP/1.1 need to use HTTP/1.0
496+
"POST " + String(path) + " HTTP/1.1\r\n"
495497
+ "Host: " + host.get() + "\r\n"
496498
+ "Authorization: Bearer " + api_key + "\r\n"
497499
+ "Accept-Encoding: identity;q=1,*;q=0\r\n"
498500
+ "User-Agent: nArija/1.0\r\n"
499501
+ "Content-Type: application/json; charset=utf-8\r\n"
500502
+ "Content-Length: " + post_body.length() + "\r\n"
501-
+ "Connection: close\r\n" + "\r\n"
503+
// + "Connection: close\r\n" + "\r\n"
504+
+ "\r\n"
502505
+ post_body + "\r\n"
503506
;
504507

@@ -3394,7 +3397,7 @@ void Audio::processWebStream() {
33943397
if(!m_pwst.chunkSize){
33953398
if(m_pwst.f_skipCRLF){
33963399
if(_client->available() < 2) { // avoid getting out of sync
3397-
AUDIO_INFO("webstream chunked: not enough bytes available for skipCRLF");
3400+
if(!m_f_tts) AUDIO_INFO("webstream chunked: not enough bytes available for skipCRLF");
33983401
return;
33993402
}
34003403
int a =_client->read(); if(a != 0x0D) log_w("chunk count error, expected: 0x0D, received: 0x%02X", a); // skipCR

src/psram_unique_ptr.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,7 @@ class ps_ptr {
12051205

12061206
void truncate_at(std::size_t pos) {
12071207
if (!valid()) return;
1208+
if(pos >= strlen()) return;
12081209
char* str = get();
12091210
std::size_t len = std::strlen(str);
12101211
if (pos < len) str[pos] = '\0';
@@ -1243,6 +1244,26 @@ class ps_ptr {
12431244
std::memmove(str, pos, remaining + 1); // inkl. '\0'
12441245
}
12451246
}
1247+
// —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
1248+
// 📌📌📌 R E M O V E _ B E F O R E ( B Y I N D E X ) 📌📌📌
1249+
// t.assign("HelloWorld");
1250+
// t.remove_before(5); // entfernt "Hello"
1251+
// t.remove_before(5, false); // entfernt nur "Hell", das Zeichen an idx bleibt
1252+
1253+
void remove_before(int idx, bool includeIdx = true) {
1254+
if (!valid()) return;
1255+
1256+
char* str = get();
1257+
std::size_t len = std::strlen(str);
1258+
1259+
if (idx < 0 || static_cast<std::size_t>(idx) > len) return;
1260+
1261+
char* pos = str + idx;
1262+
if (!includeIdx && idx < len) ++pos; // wenn das Zeichen nicht entfernt werden soll
1263+
1264+
std::size_t remaining = std::strlen(pos);
1265+
std::memmove(str, pos, remaining + 1); // inkl. '\0'
1266+
}
12461267
// —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
12471268
// 📌📌📌 C O N T A I N S 📌📌📌
12481269

0 commit comments

Comments
 (0)