-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpeechToVoice.cs
39 lines (30 loc) · 1.35 KB
/
SpeechToVoice.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
39
using System;
using System.Collections.Generic;
using Data;
using UnityEngine;
namespace Dialogues {
public class SpeechToVoice : MonoBehaviour {
// place your sounds (A to Z sounds) on a scriptableObject to optimize your data access
// inside this scriptable Object, create a public GenericDictionary<Char, AudioClip> characterToClip
[SerializeField] private GibberishAudioDataScriptableObject gibberishAudioDataScriptableObject;
[SerializeField] private AudioSource miniChimpAudioSource;
private Queue<AudioClip> clipsQueue;
private void Start() {
clipsQueue = new Queue<AudioClip>();
Play("miaou");
}
public void Play(string text) {
Char[] charactersArray = text.ToLower().ToCharArray();
foreach (var character in charactersArray) {
clipsQueue.Enqueue(Char.IsLetter(character)
? gibberishAudioDataScriptableObject.characterToClip[character]
: gibberishAudioDataScriptableObject.characterToClip['f']);
}
}
private void Update() {
if (miniChimpAudioSource.isPlaying || clipsQueue.Count <= 0) return;
miniChimpAudioSource.clip = clipsQueue.Dequeue();
miniChimpAudioSource.Play();
}
}
}