-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundBase.cs
278 lines (246 loc) · 6.96 KB
/
SoundBase.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
using BasicTwitchSoundPlayer.IRC;
using BasicTwitchSoundPlayer.SoundStorage;
using BasicTwitchSoundPlayer.Structs;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using static BasicTwitchSoundPlayer.IRC.KrakenConnections;
namespace BasicTwitchSoundPlayer
{
public class SoundDB
{
private readonly Random m_RNG;
public List<SoundEntry> SoundList;
public Dictionary<string, SoundEntry> RewardsToSound = new Dictionary<string, SoundEntry>();
private List<NSoundPlayer> m_SoundPlayerStack;
private Dictionary<string, DateTime> m_UserDB;
private int m_Delay;
private readonly string m_SoundBaseFile;
#region ConstructorRelated
public SoundDB()
{
m_UserDB = new Dictionary<string, DateTime>();
m_SoundPlayerStack = new List<NSoundPlayer>();
m_RNG = new Random();
this.m_Delay = PrivateSettings.GetInstance().Delay;
this.m_SoundBaseFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "BasicTwitchSoundPlayer", "Sounds.xml");
SoundList = SoundStorageXML.LoadSoundBase(m_SoundBaseFile);
RebuildDictionary();
if (MainForm.TwitchSocket != null)
MainForm.TwitchSocket.OnChannelPointsRedeem += PlaySoundIfExists;
}
public void RebuildDictionary()
{
RewardsToSound.Clear();
foreach (var entry in SoundList)
{
if (string.IsNullOrEmpty(entry.RewardID))
continue;
if (RewardsToSound.ContainsKey(entry.RewardID))
{
MainForm.Instance.ThreadSafeAddPreviewText($"Sound entry {entry.RewardName} already present in dictionary!", LineType.SoundCommand);
continue;
}
RewardsToSound.Add(entry.RewardID, entry);
}
}
#endregion
#region changePropertyFunctions
internal void StopAllSounds()
{
foreach (NSoundPlayer player in m_SoundPlayerStack)
{
if (player != null)
player.Dispose();
}
m_SoundPlayerStack.Clear();
}
internal void ChangeVolume(float volume)
{
for (int i = 0; i < m_SoundPlayerStack.Count; i++)
{
if (!m_SoundPlayerStack[i].DonePlaying())
{
m_SoundPlayerStack[i].SetVolume(volume);
}
}
}
#endregion
public void SetDelay(int delay)
{
this.m_Delay = delay;
PrivateSettings.GetInstance().Delay = delay;
}
public void PlaySoundIfExists(ChannelPointRedeemRequest redeem)
{
if (redeem.state != RedemptionStates.UNFULFILLED)
return;
if (redeem.userId == null)
return;
//Iterate through existing sound players
for (int i = m_SoundPlayerStack.Count - 1; i >= 0; i--)
{
//Dispose of the ones which finished playing
if (m_SoundPlayerStack[i].DonePlaying())
{
m_SoundPlayerStack[i].Dispose();
m_SoundPlayerStack.RemoveAt(i);
}
}
//Check if our db has a user and if not add him
if (!m_UserDB.ContainsKey(redeem.userId))
{
m_UserDB.Add(redeem.userId, DateTime.MinValue);
}
if (RewardsToSound.TryGetValue(redeem.rewardId, out SoundEntry sound))
{
//check user cooldown
if (m_UserDB[redeem.userId] + TimeSpan.FromSeconds(m_Delay) < DateTime.Now)
{
//Sound is found, is not played allocate a new player, start playing it, write down when user started playing a sound so he's under cooldown
PrivateSettings programSettings = PrivateSettings.GetInstance();
NSoundPlayer player = new NSoundPlayer(programSettings.OutputDevice, sound.GetFile(m_RNG), programSettings.Volume * sound.Volume);
TimeSpan length = player.GetTimeLenght() + TimeSpan.FromSeconds(1);
m_SoundPlayerStack.Add(player);
m_UserDB[redeem.userId] = DateTime.Now + length;
MainForm.TwitchSocket.UpdateRedemptionStatus(redeem, KrakenConnections.RedemptionStates.FULFILLED);
}
else
{
MainForm.TwitchSocket.UpdateRedemptionStatus(redeem, KrakenConnections.RedemptionStates.CANCELED);
}
}
}
public void Close()
{
for (int i = 0; i < m_SoundPlayerStack.Count; i++)
{
m_SoundPlayerStack[i].Dispose();
}
if (MainForm.TwitchSocket != null)
MainForm.TwitchSocket.OnChannelPointsRedeem -= PlaySoundIfExists;
}
public void Save()
{
SoundStorageXML.SaveSoundBase(m_SoundBaseFile, SoundList);
RebuildDictionary();
}
}
enum PlayerFormat
{
Generic,
Vorbis
}
class NSoundPlayer : IDisposable
{
private bool disposed = false;
private readonly PlayerFormat format;
AudioFileReader GenericFileReader;
NAudio.Vorbis.VorbisWaveReader VorbisFileReader;
IWavePlayer directWaveOut;
public string fullFileName;
private readonly string fileName;
public NSoundPlayer(Guid SoundDevice, string soundFile, float volume)
{
fullFileName = soundFile;
fileName = soundFile.Split('\\').Last();
if (File.Exists(soundFile))
{
if (soundFile.EndsWith("ogg"))
{
format = PlayerFormat.Vorbis;
}
switch (format)
{
case PlayerFormat.Vorbis:
VorbisFileReader = new NAudio.Vorbis.VorbisWaveReader(soundFile);
directWaveOut = new DirectSoundOut(SoundDevice, 120);
directWaveOut.Init(VorbisFileReader);
directWaveOut.Volume = volume;
directWaveOut.Play();
break;
default:
GenericFileReader = new AudioFileReader(soundFile);
directWaveOut = new DirectSoundOut(SoundDevice, 120);
directWaveOut.Init(GenericFileReader);
GenericFileReader.Volume = volume;
directWaveOut.Play();
break;
}
}
}
public TimeSpan GetTimeLenght()
{
if (format == PlayerFormat.Generic)
{
if (GenericFileReader != null)
return GenericFileReader.TotalTime;
else
return TimeSpan.Zero;
}
else if (format == PlayerFormat.Vorbis)
{
if (VorbisFileReader != null)
return VorbisFileReader.TotalTime;
else
return TimeSpan.Zero;
}
else
return TimeSpan.Zero;
}
public bool DonePlaying()
{
switch (format)
{
case PlayerFormat.Vorbis:
if (VorbisFileReader.CurrentTime >= VorbisFileReader.TotalTime)
return true;
else
return false;
default:
if (GenericFileReader.CurrentTime >= GenericFileReader.TotalTime)
return true;
else
return false;
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (directWaveOut != null)
directWaveOut.Stop();
if (GenericFileReader != null)
GenericFileReader.Dispose();
if (VorbisFileReader != null)
VorbisFileReader.Dispose();
if (directWaveOut != null)
directWaveOut.Dispose();
Debug.WriteLine("[DISPOSE] Disposed of player for " + fileName + ".");
}
}
GenericFileReader = null;
VorbisFileReader = null;
directWaveOut = null;
//dispose unmanaged resources
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
internal void SetVolume(float volume)
{
if (format == PlayerFormat.Generic && GenericFileReader != null)
GenericFileReader.Volume = volume;
else if (format == PlayerFormat.Vorbis && VorbisFileReader != null)
directWaveOut.Volume = volume;
}
}
}