blob: ec51b18fe4e629aa45a5a536d445e527ccde2b8a (
plain) (
blame)
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
|
// WARNING: This thing likes leaking memory.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using NAudio;
using NAudio.Wave;
using System.Threading;
namespace ShiftOS
{
public class Audio
{
public static List<AudioResourceClient> Clients = null;
internal static bool Enabled = false;
public static void DisposeAll()
{
if(Clients != null)
{
foreach(var client in Clients)
{
client.Dispose();
}
}
}
}
public class AudioResourceClient
{
public AudioResourceClient(string folder)
{
if (Audio.Enabled)
{
if (Audio.Clients == null)
{
Audio.Clients = new List<AudioResourceClient>();
}
Audio.Clients.Add(this);
string real = folder.Replace(";", OSInfo.DirectorySeparator);
real_path = "resources" + OSInfo.DirectorySeparator + real;
soundPlayer = new WaveOut();
var t = new Thread(new ThreadStart(new Action(() =>
{
while (disposed == false)
{
switch (soundPlayer.PlaybackState)
{
case PlaybackState.Stopped:
if (fireEvents)
{
if (currentStream != null)
currentStream.Dispose();
currentStream = null;
currentProvider = null;
var h = SongFinished;
if (h != null)
{
API.CurrentSession.Invoke(new Action(() =>
{
h(this, new EventArgs());
}));
}
}
break;
}
}
})));
t.Start();
}
}
public string CurrentSong
{
get { return currentSong; }
}
private string real_path = null;
private WaveOut soundPlayer = null;
private bool disposed = false;
private bool playing = false;
private bool loopActive = false;
private string currentSong = "";
protected Thread soundThread;
private Stream currentStream = null;
private IWaveProvider currentProvider = null;
private bool fireEvents = false;
public event EventHandler SongFinished;
public void Play(byte[] songbytes)
{
if (Audio.Enabled)
{
var t = new Thread(new ThreadStart(new Action(() =>
{
try
{
if (disposed == false)
{
if (currentProvider != null)
{
currentProvider = null;
}
if (currentStream != null)
{
currentStream.Dispose();
currentStream = null;
}
Stream s = new MemoryStream(songbytes);
IWaveProvider provider = new RawSourceWaveStream(s, new WaveFormat());
soundPlayer.Init(provider);
soundPlayer.Play();
fireEvents = true;
currentStream = s;
currentProvider = provider;
}
}
catch
{
}
})));
soundThread = t;
t.Start();
}
}
public void PlayMP3(byte[] songbytes)
{
if (Audio.Enabled)
{
var t = new Thread(new ThreadStart(new Action(() =>
{
if (currentProvider != null)
{
currentProvider = null;
}
if (currentStream != null)
{
currentStream.Dispose();
currentStream = null;
}
Stream s = new MemoryStream(songbytes);
IWaveProvider provider = new Mp3FileReader(s);
soundPlayer.Init(provider);
soundPlayer.Play();
currentStream = s;
currentProvider = provider;
})));
soundThread = t;
t.Start();
}
}
public void Play(string file)
{
if (Audio.Enabled)
{
if (File.Exists(real_path + OSInfo.DirectorySeparator + file + ".wav"))
{
currentSong = file;
Play(File.ReadAllBytes(real_path + OSInfo.DirectorySeparator + file + ".wav"));
}
else if (File.Exists(real_path + OSInfo.DirectorySeparator + file + ".mp3"))
{
currentSong = file;
PlayMP3(File.ReadAllBytes(real_path + OSInfo.DirectorySeparator + file + ".mp3"));
}
}
}
public void Stop()
{
if (Audio.Enabled)
{
var t = new Thread(new ThreadStart(new Action(() =>
{
fireEvents = false;
soundPlayer.Stop();
if (currentProvider != null)
{
currentProvider = null;
}
if (currentStream != null)
{
currentStream.Dispose();
currentStream = null;
}
})));
t.Start();
}
}
public void Pause()
{
if (Audio.Enabled)
{
if (soundPlayer.PlaybackState == PlaybackState.Playing)
{
soundPlayer.Pause();
}
else if (soundPlayer.PlaybackState == PlaybackState.Paused)
{
soundPlayer.Resume();
}
}
}
public void PlayRandom()
{
if (Audio.Enabled)
{
int r = new Random().Next(0, Directory.GetFiles(real_path).Length);
var files = Directory.GetFiles(real_path);
FileInfo finf = new FileInfo(files[r]);
string name = finf.Name.Replace(finf.Extension, "");
Play(name);
}
}
public void Dispose()
{
if (Audio.Enabled)
{
if (currentProvider != null)
{
currentProvider = null;
}
if (currentStream != null)
{
currentStream.Dispose();
currentStream = null;
}
soundPlayer.Dispose();
disposed = true;
}
}
}
}
|