server: add oai-compat /v1/audio/speech endpoint (#31763)

* server: add oai-compat /v1/audio/speech endpoint

* add e2e pytest

* server: share the s16 rounding, type speech form fields

The WAV writer of the audio helper truncates while pcm_f32_to_s16
rounds, so wav and pcm differ by one LSB. Both now round.

pcm_f32_to_s16 takes a pointer and a count and converts the TTS
bytes without an intermediate copy.

/tts and /v1/audio/speech parse their body with parse_tts_body, so
multipart fields such as speed get their type before the speech
conversion checks them.

---------

Co-authored-by: Pascal <admin@serveurperso.com>
This commit is contained in:
Xuan-Son NguyenandPascal authored and GitHub committed 2026-11-09 23:58:52 +01:00
1 parent 20c2e96655
commit 08f945ffb5
13 files changed
+537 -200

No files matched your search

+15
View File
@@ -9,6 +9,8 @@
#include "server-common.h"
#include <algorithm>
#include <cmath>
#include <filesystem>
#include <random>
#include <sstream>
@@ -200,6 +202,19 @@ const char * get_media_marker() {
return marker.c_str();
}
//
// audio utils
//
std::string pcm_f32_to_s16(const float * samples, size_t n) {
std::string bytes(n * sizeof(int16_t), '\0');
for (size_t i = 0; i < n; i++) {
const int16_t v = (int16_t) std::lround(std::max(-1.0f, std::min(1.0f, samples[i])) * 32767.0f);
memcpy(&bytes[2 * i], &v, sizeof(v));
}
return bytes;
}
//
// model output modalities
//