Skip to content

Commit c1e9e5c

Browse files
authored
Fix TTS for Unreal Engine (#2349)
Unreal Engine has its own memory management, so we cannot return a struct containing a std::vector object.
1 parent 5ebb719 commit c1e9e5c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

sherpa-onnx/c-api/cxx-api.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,19 @@ GeneratedAudio OfflineTts::Generate(const std::string &text,
419419
return ans;
420420
}
421421

422+
std::shared_ptr<GeneratedAudio> OfflineTts::Generate2(
423+
const std::string &text, int32_t sid /*= 0*/, float speed /*= 1.0*/,
424+
OfflineTtsCallback callback /*= nullptr*/, void *arg /*= nullptr*/) const {
425+
auto audio = Generate(text, sid, speed, callback, arg);
426+
427+
GeneratedAudio *ans = new GeneratedAudio;
428+
ans->samples = std::move(audio.samples);
429+
ans->sample_rate = audio.sample_rate;
430+
431+
return std::shared_ptr<GeneratedAudio>(ans,
432+
[](GeneratedAudio *p) { delete p; });
433+
}
434+
422435
KeywordSpotter KeywordSpotter::Create(const KeywordSpotterConfig &config) {
423436
struct SherpaOnnxKeywordSpotterConfig c;
424437
memset(&c, 0, sizeof(c));

sherpa-onnx/c-api/cxx-api.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef SHERPA_ONNX_C_API_CXX_API_H_
77
#define SHERPA_ONNX_C_API_CXX_API_H_
88

9+
#include <memory>
910
#include <string>
1011
#include <vector>
1112

@@ -433,6 +434,13 @@ class SHERPA_ONNX_API OfflineTts
433434
OfflineTtsCallback callback = nullptr,
434435
void *arg = nullptr) const;
435436

437+
// Like Generate, but return a smart pointer.
438+
//
439+
// See also https://github.com/k2-fsa/sherpa-onnx/issues/2347
440+
std::shared_ptr<GeneratedAudio> Generate2(
441+
const std::string &text, int32_t sid = 0, float speed = 1.0,
442+
OfflineTtsCallback callback = nullptr, void *arg = nullptr) const;
443+
436444
private:
437445
explicit OfflineTts(const SherpaOnnxOfflineTts *p);
438446
};

0 commit comments

Comments
 (0)