-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSound.cs
182 lines (167 loc) · 3.4 KB
/
Sound.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kawa.Mix;
namespace KiSSLab
{
public class SoundSystem
{
public class Sound
{
private FMOD.System system;
public FMOD.Sound InnerSound { get; private set; }
public FMOD.Channel Channel { get; private set; }
public Sound(string file, Mix mix, 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;
}
public void Stop()
{
Channel.Stop();
}
~Sound()
{
Channel.Stop();
InnerSound.Release();
}
}
private FMOD.System system;
private Dictionary<string, Sound> sounds;
private FMOD.Sound music;
private FMOD.Channel musicChannel;
public bool Enabled
{
get
{
return system != null;
}
}
public SoundSystem()
{
var ret = FMOD.Result.OK;
system = null;
try
{
FMOD.Factory.CreateSystem(ref system);
}
catch (DllNotFoundException)
{
return;
}
if (ret != FMOD.Result.OK)
return;
ret = system.Initialize(8, FMOD.InitFlags.Normal, IntPtr.Zero);
if (ret != FMOD.Result.OK)
{
system = null;
return;
}
sounds = new Dictionary<string, Sound>();
music = null;
musicChannel = null;
}
public void PlayMusic(Mix mix, string name)
{
if (!Enabled)
return;
if (mix.FileExists(name))
{
StopMusic();
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);
musicChannel.SetVolume(0.5f);
}
}
public Sound PlaySound(Mix mix, string name)
{
if (!Enabled)
return null;
if (sounds.ContainsKey(name) && sounds[name] == null)
return null;
if (!sounds.ContainsKey(name))
{
if (mix.FileExists(name))
{
sounds.Add(name, new Sound(name, mix, system));
}
else
{
sounds.Add(name, null);
return null;
}
}
sounds[name].Play();
return sounds[name];
}
public void Update()
{
if (!Enabled)
return;
system.Update();
}
public void StopMusic()
{
if (!Enabled)
return;
if (musicChannel != null)
musicChannel.Stop();
if (music != null)
music.Release();
}
public void StopEverything()
{
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();
}
sounds.Clear();
}
}
public void ShutDown()
{
if (!Enabled)
return;
StopEverything();
system.Close();
system = null;
}
public static void CheckError(FMOD.Result result)
{
if (result != FMOD.Result.OK)
throw new Exception(string.Format("FMOD error: {0}", result));
}
}
}