-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPiperExample.cs
38 lines (32 loc) · 1.35 KB
/
PiperExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
namespace Abuksigun.Piper
{
public class PiperExample : MonoBehaviour
{
[SerializeField] AudioSource audioSource;
[SerializeField] string text = "One more lib and we are done";
[SerializeField] string modelPath;
[SerializeField] string espeakDataPath;
Piper piper;
PiperVoice voice;
PiperSpeaker piperSpeaker;
[ContextMenu("Run Stream")]
async void Run()
{
if (gameObject.scene.name == null)
throw new InvalidOperationException("This script must be attached to a game object in a scene, otherwise AudioSource can't play :(");
string fullModelPath = Path.Join(Application.streamingAssetsPath, modelPath);
string fullEspeakDataPath = Path.Join(Application.streamingAssetsPath, espeakDataPath);
piper ??= await Piper.LoadPiper(fullEspeakDataPath);
voice ??= await PiperVoice.LoadPiperVoice(piper, fullModelPath);
piperSpeaker ??= new PiperSpeaker(voice);
_ = piperSpeaker.ContinueSpeach(text).ContinueWith(x => Debug.Log($"Generation finished with status: {x.Status}"));
audioSource.clip = piperSpeaker.AudioClip;
audioSource.loop = true;
audioSource.Play();
}
}
}