-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpeechBlendClip.cs
163 lines (132 loc) · 4.51 KB
/
SpeechBlendClip.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Slate.ActionClips
{
[Attachable(typeof(ActorAudioTrack), typeof(DirectorAudioTrack))]
public class SpeechBlendClip : ActionClip, ISubClipContainable
{
[SerializeField]
[HideInInspector]
private float _length = 1f;
[SerializeField]
[HideInInspector]
private float _blendIn = 0.25f;
[SerializeField]
[HideInInspector]
private float _blendOut = 0.25f;
[Required]
public AudioClip audioClip;
[AnimatableParameter(0, 1)]
public float volume = 1;
[AnimatableParameter(-3, 3)]
public float pitch = 1;
[AnimatableParameter(-1, 1)]
public float stereoPan = 0;
public float clipOffset;
[Multiline(5)]
public string subtitlesText;
public Color subtitlesColor = Color.white;
public SpeechBlend SpeechBlend;
float ISubClipContainable.subClipOffset
{
get { return clipOffset; }
set { clipOffset = value; }
}
float ISubClipContainable.subClipLength
{
get { return audioClip != null ? audioClip.length : 0; }
}
float ISubClipContainable.subClipSpeed
{
get { return 1; }
}
public override float length
{
get { return _length; }
set { _length = value; }
}
public override float blendIn
{
get { return _blendIn; }
set { _blendIn = value; }
}
public override float blendOut
{
get { return _blendOut; }
set { _blendOut = value; }
}
public override bool isValid
{
get { return audioClip != null; }
}
public override string info
{
get { return isValid ? (string.IsNullOrEmpty(subtitlesText) ? audioClip.name : string.Format("<i>'{0}'</i>", subtitlesText)) : base.info; }
}
private AudioTrack track
{
get { return (AudioTrack)parent; }
}
private AudioSource source
{
get { return track.source; }
}
protected override void OnEnter() { Do(); }
protected override void OnReverseEnter() { Do(); }
protected override void OnExit() { Undo(); }
protected override void OnReverse() { Undo(); }
void Do()
{
if (source != null)
{
source.clip = audioClip;
if (SpeechBlend != null)
{
SpeechBlend.ResetSpeechBlend();
SpeechBlend.InitSpeechBlend();
SpeechBlend.voiceAudioSource = source;
}
}
}
protected override void OnUpdate(float time, float previousTime)
{
if (source != null)
{
var weight = Easing.Ease(EaseType.QuadraticInOut, 0, 1, GetClipWeight(time));
var settings = track.sampleSettings;
settings.volume *= weight * volume;
settings.pitch *= pitch;
settings.pan += stereoPan;
AudioSampler.Sample(source, audioClip, time - clipOffset, previousTime - clipOffset, settings);
if (SpeechBlend != null)
{
SpeechBlend.DoFixedUpdate();
SpeechBlend.DoLateUpdate();
}
if (!string.IsNullOrEmpty(subtitlesText))
{
var lerpColor = subtitlesColor;
lerpColor.a = weight;
DirectorGUI.UpdateSubtitles(string.Format("{0}{1}", parent is ActorAudioTrack ? (actor.name.Replace("(Clone)", "") + ": ") : "", subtitlesText), lerpColor);
}
}
}
void Undo()
{
if (source != null)
{
source.clip = null;
}
}
////////////////////////////////////////
///////////GUI AND EDITOR STUFF/////////
////////////////////////////////////////
#if UNITY_EDITOR
protected override void OnClipGUI(Rect rect)
{
EditorTools.DrawLoopedAudioTexture(rect, audioClip, length, clipOffset);
}
#endif
}
}