Hello
I would need help understanding why sherpa-onnx-c-api.dll is crashing. I placed sherpa-onnx.dll and win-x64 sherpa-onnx-c-api.dll into Assets:Plugins, but I found out that if I use sherpa-onnx-c-api.dll, Unity crashes when launching.
My code
using System;
using System.Threading;
using UnityEngine;
using SherpaOnnx; // Ujistěte se, že knihovna je dostupná v Unity
public class OfflineTtsUnity : MonoBehaviour
{
public AudioSource audioSource; // Přiřaďte AudioSource ve scéně
void Start()
{
// Spuštění TTS generace na samostatném vlákně
Thread ttsThread = new Thread(RunTtsGeneration);
ttsThread.Start();
}
void RunTtsGeneration()
{
// Nastavení cesty ke složce se soubory – doporučuje se umístit soubory do StreamingAssets/kokoro-en-v0_19
string basePath = Application.streamingAssetsPath + "/kokoro-en-v0_19";
var config = new OfflineTtsConfig();
config.Model.Kokoro.Model = basePath + "/model.onnx";
config.Model.Kokoro.Voices = basePath + "/voices.bin";
config.Model.Kokoro.Tokens = basePath + "/tokens.txt";
config.Model.Kokoro.DataDir = basePath + "/espeak-ng-data";
config.Model.NumThreads = 2;
config.Model.Debug = 1;
config.Model.Provider = "cpu";
var tts = new OfflineTts(config);
float speed = 1.0f;
string text = "Today as always, men fall into two groups: slaves and free men. Whoever " +
"does not have two-thirds of his day for himself, is a slave, whatever " +
"he may be: a statesman, a businessman, an official, or a scholar. " +
"Friends fell out often because life was changing so fast. The easiest " +
"thing in the world was to lose touch with someone.";
int sid = 0;
// Generování audia – metoda Generate vrací objekt, který obsahuje PCM data
var audio = tts.Generate(text, speed, sid);
if (audio != null)
{
// Pokud váš audio objekt nemá metodu GetSamples(), nahraďte tuto část získáním PCM dat.
// Níže je předpoklad, že existuje vlastnost 'Samples' a 'SampleRate'.
float[] samples = audio.Samples; // Upravte dle skutečné implementace
int sampleRate = audio.SampleRate; // Upravte dle skutečné implementace
// Přepnutí na hlavní vlákno a vytvoření AudioClip
MainThreadDispatcher.Instance.Enqueue(() =>
{
AudioClip clip = AudioClip.Create("TTS_Clip", samples.Length, 1, sampleRate, false);
clip.SetData(samples, 0);
audioSource.clip = clip;
audioSource.Play();
});
}
else
{
Debug.LogError("Failed to generate TTS audio.");
}
}
}
using System;
using System.Collections.Generic;
using UnityEngine;
public class MainThreadDispatcher : MonoBehaviour
{
private static readonly Queue<Action> _executionQueue = new Queue<Action>();
private static MainThreadDispatcher _instance;
public static MainThreadDispatcher Instance
{
get
{
if (_instance == null)
{
// Vytvoření nové instance, pokud ještě neexistuje
var obj = new GameObject("MainThreadDispatcher");
_instance = obj.AddComponent<MainThreadDispatcher>();
DontDestroyOnLoad(obj);
}
return _instance;
}
}
public void Enqueue(Action action)
{
lock (_executionQueue)
{
_executionQueue.Enqueue(action);
}
}
void Update()
{
while (_executionQueue.Count > 0)
{
Action action = null;
lock (_executionQueue)
{
if (_executionQueue.Count > 0)
{
action = _executionQueue.Dequeue();
}
}
action?.Invoke();
}
}
}