-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAudioPlayer.cs
More file actions
213 lines (165 loc) · 6.23 KB
/
AudioPlayer.cs
File metadata and controls
213 lines (165 loc) · 6.23 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using GTA;
using GTA.Math;
using GTA.UI;
using IrrKlang;
using KlangRageAudioLibrary.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace KlangRageAudioLibrary
{
public partial class AudioPlayer
{
private float _originalVolume;
private readonly List<ISound> _instancesToRemove;
private readonly ISoundSource _soundSource;
private readonly ISoundEngine _soundEngine;
internal bool Disposed;
private AudioPlayer()
{
SoundInstances = new List<ISound>();
_instancesToRemove = new List<ISound>();
}
internal AudioPlayer(AudioEngine audioEngine, string name, Stream res, AudioPreset preset) : this()
{
_soundEngine = AudioEngine.SoundEngine;
FilePath = null;
_soundSource = _soundEngine.AddSoundSourceFromIOStream(res, name);
Add(preset, audioEngine.DefaultSourceEntity);
}
internal AudioPlayer(AudioEngine audioEngine, string name, AudioPreset preset) : this()
{
_soundEngine = AudioEngine.SoundEngine;
FileInfo audioInfo = new FileInfo($".\\scripts\\{audioEngine.BaseSoundFolder}/{name}");
if (audioInfo.Exists == false)
{
Screen.ShowSubtitle($"File: {audioInfo.FullName} doesn't exist!");
return;
}
FilePath = audioInfo.FullName;
_soundEngine.AddSoundSourceFromFile(FilePath);
Add(preset, audioEngine.DefaultSourceEntity);
}
public void Play(bool stopPrevious = false, bool ignoreFadeOut = false)
{
if (SourceEntity == null || (!ignoreFadeOut && Screen.IsFadedOut))
return;
if (stopPrevious && SoundInstances.Count > 0)
{
SoundInstances.ForEach(x => x.Stop());
}
ISound iSound;
if (Flags.HasFlag(AudioFlags.No3D))
{
if (_soundSource == default)
iSound = _soundEngine.Play2D(FilePath, Flags.HasFlag(AudioFlags.Loop), true, StreamMode.AutoDetect);
else
iSound = _soundEngine.Play2D(_soundSource, Flags.HasFlag(AudioFlags.Loop), true, false);
}
else
{
if (_soundSource == default)
iSound = _soundEngine.Play3D(FilePath,
MathUtils.Vector3ToVector3D(Vector3.Zero), Flags.HasFlag(AudioFlags.Loop), true, StreamMode.AutoDetect);
else
iSound = _soundEngine.Play3D(_soundSource, 0, 0, 0, Flags.HasFlag(AudioFlags.Loop), true, false);
}
if (iSound == null)
{
throw new Exception($"KRAL Engine init failed. File Path: {FilePath}");
}
SoundInstances.Add(iSound);
Last = iSound;
if (StartFadeIn)
{
Last.Volume = 0;
IsDoingFadeIn = true;
}
Last.Paused = false;
}
internal void Tick()
{
if (SourceEntity == null)
return;
SoundInstances.ForEach(x =>
{
if (x.Finished)
_instancesToRemove.Add(x);
});
if (_instancesToRemove.Count > 0)
{
_instancesToRemove.ForEach(x => SoundInstances.Remove(x));
_instancesToRemove.Clear();
}
InstancesNumber = SoundInstances.Count();
IsAnyInstancePlaying = InstancesNumber > 0;
if (SoundInstances.All(x => x.Paused is true))
return;
SoundInstances.ForEach(x =>
{
x.MinDistance = MinimumDistance;
x.Velocity = MathUtils.Vector3ToVector3D(Velocity);
// For some reason irrklang doesn't work with speed less than 0.1
//if (!x.Paused)
// x.PlaybackSpeed = Game.TimeScale < 0.1f ? 0.11f : Game.TimeScale;
});
// Update volume in case if it was changed
_originalVolume = Volume;
// Do Fade Out/In
if (IsDoingFadeIn)
{
ProcessFadeIn();
}
else if (IsDoingFadeOut)
{
ProcessFadeOut();
}
else
{
// If fade not processing we can update volume
// and process interior / exterior sounds
SoundInstances.ForEach(x => x.Volume = Volume);
ProcessInteriorSound();
ProcessExteriorSound();
}
// Adjust sound position relatively to world
int boneIndex = SourceEntity.Bones[SourceBone].Index;
Vector3D soundPos = MathUtils.Vector3ToVector3D(boneIndex == -1 ? SourceEntity.Position : SourceEntity.Bones[boneIndex].Position);
SoundInstances.ForEach(x => x.Position = soundPos);
}
public void Stop(bool instant = false)
{
if (SoundInstances.All(x => x.Finished) ||
SoundInstances.All(x => x.Paused is true))
return;
IsDoingFadeIn = false;
if (StopFadeOut && !instant)
{
SoundInstances?.Where(x => x != Last).ToList().ForEach(x => x.Stop());
IsDoingFadeOut = true;
}
else
{
SoundInstances?.ForEach(x => x.Stop());
}
}
public void Dispose()
{
SoundInstances?.ForEach(x => x.Stop());
SoundInstances?.Clear();
Disposed = true;
}
private void Add(AudioPreset preset, Entity entity)
{
Flags = preset.Flags;
Volume = preset.Volume;
MinimumDistance = preset.MinimumDistance;
IsInteriorSound = Flags.HasFlag(AudioFlags.InteriorSound);
IsExteriorSound = Flags.HasFlag(AudioFlags.ExteriorSound);
StartFadeIn = Flags.HasFlag(AudioFlags.FadeIn);
StopFadeOut = Flags.HasFlag(AudioFlags.FadeOut);
SourceEntity = entity;
}
}
}