-
Notifications
You must be signed in to change notification settings - Fork 6
/
SoundSystem.cs
383 lines (362 loc) · 9.35 KB
/
SoundSystem.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.Linq;
namespace Noxico
{
public class SoundSystem
{
private class Sound
{
private FMOD.System system;
public FMOD.Sound InnerSound { get; private set; }
public FMOD.Channel Channel { get; private set; }
public Sound(string file, FMOD.System system)
{
this.system = system;
FMOD.Sound ns = null;
var data = Mix.GetBytes(file);
var fCSex = new FMOD.CreateSoundExInfo()
{
Size = 216,
Length = (uint)data.Length
};
SoundSystem.CheckError(system.CreateSound(data, FMOD.SoundMode.Default | FMOD.SoundMode.OpenMemory, ref fCSex, ref ns));
InnerSound = ns;
}
public void Play()
{
if (InnerSound == null)
return;
var chan = Channel;
SoundSystem.CheckError(system.PlaySound(InnerSound, false, ref chan));
Channel = chan;
}
}
private FMOD.System system;
private Dictionary<string, Sound> sounds;
private string musicPlaying = string.Empty;
private string currentSet = string.Empty;
private FMOD.Sound music;
private FMOD.Channel musicChannel;
private float musicVolume, soundVolume;
private List<Token> musicLibrary, soundLibrary;
public float MusicVolume
{
get
{
return musicVolume;
}
set
{
if (value < 0)
musicVolume = 0;
else if (value > 1)
musicVolume = 1;
else
musicVolume = value;
}
}
public float SoundVolume
{
get
{
return soundVolume;
}
set
{
if (value < 0)
soundVolume = 0;
else if (value > 1)
soundVolume = 1;
else
soundVolume = value;
}
}
public string FadeTarget { get; private set; }
private string targetSet;
private int fadeProcess;
public bool Enabled
{
get
{
return system != null;
}
}
public SoundSystem()
{
var ret = FMOD.Result.OK;
system = null;
Program.WriteLine("SoundSystem: _ctor");
if (!IniFile.GetValue("audio", "enabled", true))
return;
musicVolume = IniFile.GetValue("audio", "musicvolume", 100) / 100f;
soundVolume = IniFile.GetValue("audio", "soundvolume", 100) / 100f;
Program.Write("SoundSystem: trying to create FMOD system...");
try
{
FMOD.Factory.CreateSystem(ref system);
}
catch (DllNotFoundException)
{
Program.WriteLine(" Library not found. Disabling sound.");
return;
}
Program.WriteLine(" " + ret.ToString());
if (ret != FMOD.Result.OK)
return;
Program.Write("SoundSystem: system.init...");
ret = system.Initialize(8, FMOD.InitFlags.Normal, IntPtr.Zero);
Program.WriteLine(" " + ret.ToString());
if (ret != FMOD.Result.OK)
{
system = null;
return;
}
music = null;
musicChannel = null;
sounds = new Dictionary<string, Sound>();
Program.WriteLine("SoundSystem: loading libraries...");
musicLibrary = Mix.GetTokenTree("music.tml");
soundLibrary = Mix.GetTokenTree("sound.tml");
Program.WriteLine("SoundSystem: _ctor DONE");
Program.WriteLine("Null report:");
if (system == null)
Program.WriteLine(" * system");
if (music == null)
Program.WriteLine(" * music");
if (musicChannel == null)
Program.WriteLine(" * musicChannel");
}
public void PlayMusic(string name, bool fade = true)
{
if (!Enabled || musicVolume < float.Epsilon)
return;
var set = targetSet;
if (name.StartsWith("set://"))
{
set = name.Substring(6);
var setNode = musicLibrary.FirstOrDefault(x => x.Name == set);
if (setNode == null)
name = "-";
else
{
name = "-";
var unmarked = new List<Token>();
foreach (var t in setNode.Tokens)
{
if (!t.HasToken("used"))
unmarked.Add(t);
}
if (unmarked.Count == 0)
{
foreach (var t in setNode.Tokens)
{
t.RemoveToken("used");
unmarked.Add(t);
}
}
while (unmarked.Count > 0)
{
var pick = Random.Next(unmarked.Count);
var file = unmarked[pick].Name;
if (!Mix.FileExists(file))
unmarked.RemoveAt(pick);
else
{
name = file;
unmarked[pick].AddToken("used");
break;
}
}
}
}
if (set == currentSet)
return;
if (fade && string.IsNullOrEmpty(FadeTarget) && musicChannel != null && name != musicPlaying)
{
FadeTarget = name;
targetSet = set;
fadeProcess = 300;
return;
}
FadeTarget = null;
if (name == "-")
{
if (musicChannel != null)
musicChannel.Stop();
if (music != null)
music.Release();
currentSet = string.Empty;
musicPlaying = string.Empty;
}
else if (musicPlaying != name)
{
if (Mix.FileExists(name))
{
if (musicChannel != null)
musicChannel.Stop();
if (music != null)
music.Release();
var data = Mix.GetBytes(name);
var fCSex = new FMOD.CreateSoundExInfo()
{
Size = 216,
Length = (uint)data.Length
};
CheckError(system.CreateSound(data, FMOD.SoundMode.LoopNormal | FMOD.SoundMode.OpenMemory, ref fCSex, ref music));
system.PlaySound(music, false, ref musicChannel);
musicPlaying = name;
currentSet = set;
musicChannel.SetVolume(musicVolume);
//NoxicoGame.HostForm.Text = file;
}
else
Program.WriteLine("PlayMusic: couldn't load song \"{0}\".", name);
}
}
public void PlaySound(string name)
{
if (!Enabled || musicVolume < float.Epsilon)
return;
if (name.StartsWith("set://"))
{
name = name.Substring(6);
var setNode = soundLibrary.FirstOrDefault(x => x.Name == name);
if (setNode == null)
name = "-";
else
{
name = "-";
var unmarked = new List<Token>();
foreach (var t in setNode.Tokens)
{
if (!t.HasToken("used"))
unmarked.Add(t);
}
if (unmarked.Count == 0)
{
foreach (var t in setNode.Tokens)
{
t.RemoveToken("used");
unmarked.Add(t);
}
}
while (unmarked.Count > 0)
{
var pick = Random.Next(unmarked.Count);
var file = unmarked[pick].Name;
if (!Mix.FileExists(file))
unmarked.RemoveAt(pick);
else
{
name = file;
unmarked[pick].AddToken("used");
break;
}
}
}
}
if (sounds.ContainsKey(name) && sounds[name] == null)
return;
if (!sounds.ContainsKey(name))
{
if (Mix.FileExists(name))
sounds.Add(name, new Sound(name, system));
else
{
sounds.Add(name, null);
return;
}
}
sounds[name].Play();
sounds[name].Channel.SetVolume(soundVolume);
}
private void Fade()
{
if (!Enabled)
return;
fadeProcess--;
if (fadeProcess < 0)
PlayMusic(FadeTarget, false);
else
{
fadeProcess -= 1;
musicChannel.SetVolume(musicVolume - (1 - ((float)fadeProcess / 300)));
}
}
public void Update()
{
if (!Enabled)
return;
if (!string.IsNullOrEmpty(FadeTarget))
Fade();
system.Update();
#if DEBUG
if (!IniFile.GetValue("audio", "debug", false))
return;
var musicchans = 0;
var type = FMOD.SoundType.Unknown;
var format = FMOD.SoundFormat.None;
var channels = 0;
var bits = 0;
var volume = 0f;
var debug = string.Empty;
music.GetFormat(out type, out format, out channels, out bits);
musicChannel.GetVolume(out volume);
if (type == FMOD.SoundType.Mp3 | type == FMOD.SoundType.OggVorbis)
{
var ms = 0u;
var len = 0u;
musicChannel.GetPosition(out ms, FMOD.TimeUnit.Milliseconds);
music.GetLength(out len, FMOD.TimeUnit.Milliseconds);
debug = string.Format("{0}ch {1}, {2} / {3}. Volume: {4:P0}", musicchans, type.ToString(), new TimeSpan(ms * 10000), new TimeSpan(len * 10000), volume);
}
else
{
var row = 0u;
var pattern = 0u;
var rows = 0u;
var patterns = 0u;
music.GetMusicNumChannels(out musicchans);
musicChannel.GetPosition(out row, FMOD.TimeUnit.ModRow);
musicChannel.GetPosition(out pattern, FMOD.TimeUnit.ModPattern);
music.GetLength(out rows, FMOD.TimeUnit.ModRow);
music.GetLength(out patterns, FMOD.TimeUnit.ModPattern);
debug = string.Format("{0}ch {1}, row {2}/{3}, pattern {4}/{5}. Volume: {6:P0}", musicchans, type.ToString(), row, rows, pattern, patterns, volume);
}
NoxicoGame.HostForm.Write(debug.PadRight(Program.Cols), System.Drawing.Color.Gray, System.Drawing.Color.Black, 0, 0);
#endif
}
public void ShutDown()
{
if (!Enabled)
return;
if (musicChannel != null)
musicChannel.Stop();
if (music != null)
music.Release();
if (sounds != null)
{
foreach (var s in sounds.Values)
{
s.Channel.Stop();
s.InnerSound.Release();
}
}
system.Close();
system = null;
}
public static void CheckError(FMOD.Result result)
{
if (result != FMOD.Result.OK)
throw new Exception(GetError(result));
}
public static string GetError(FMOD.Result result)
{
var message = i18n.GetString(string.Format("fmod_{0}", result.ToString().ToLowerInvariant()));
if (message.StartsWith("[fmod_"))
return i18n.Format("fmod_genericerror", result);
return i18n.Format("fmod_error", message);
}
}
}