-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathSoundEffects.cs
219 lines (192 loc) · 6.76 KB
/
SoundEffects.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
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Telegram.Td.Api;
using Windows.Foundation;
using Windows.Media.Audio;
using Windows.Media.Render;
using Windows.Storage;
namespace Telegram.Common
{
public static class SoundEffects
{
enum EffectType
{
Generic,
Voip,
VideoChat,
Custom
}
private static readonly Dictionary<EffectType, AudioGraph> _graphs = new();
private static readonly Dictionary<EffectType, int> _counts = new();
private static readonly DisposableMutex _lock = new();
private static bool _suspended;
public static void Suspend()
{
_suspended = true;
Stop();
}
public static void Resume()
{
_suspended = false;
}
public static async void Stop()
{
using (await _lock.WaitAsync())
{
try
{
foreach (var graph in _graphs.Values)
{
graph.Stop();
graph.Dispose();
}
}
catch { }
_graphs.Clear();
}
}
private static void Stop(EffectType type)
{
try
{
if (_graphs.TryGetValue(type, out AudioGraph graph))
{
graph.Stop();
graph.Dispose();
}
}
catch { }
_graphs.Remove(type);
}
public static void Play(SoundEffect effect)
{
if (_suspended)
{
return;
}
static Task PlayImpl(string url, int? loopCount = 0, EffectType type = EffectType.Generic)
{
return Play(StorageFile.GetFileFromApplicationUriAsync(new Uri(url)), loopCount, type);
}
switch (effect)
{
case SoundEffect.Sent:
_ = PlayImpl("ms-appx:///Assets/Audio/sent.mp3");
break;
case SoundEffect.VoipIncoming:
_ = PlayImpl("ms-appx:///Assets/Audio/voip_incoming.mp3", null, EffectType.Voip);
break;
case SoundEffect.VoipRingback:
_ = PlayImpl("ms-appx:///Assets/Audio/voip_ringback.mp3", null, EffectType.Voip);
break;
case SoundEffect.VoipConnecting:
_ = PlayImpl("ms-appx:///Assets/Audio/voip_connecting.mp3", type: EffectType.Voip);
break;
case SoundEffect.VoipBusy:
_ = PlayImpl("ms-appx:///Assets/Audio/voip_busy.mp3", 4, type: EffectType.Voip);
break;
case SoundEffect.VoipEnd:
_ = PlayImpl("ms-appx:///Assets/Audio/voip_end.mp3", type: EffectType.Voip);
break;
case SoundEffect.VoipFailed:
_ = PlayImpl("ms-appx:///Assets/Audio/voip_failed.mp3", type: EffectType.Voip);
break;
case SoundEffect.VideoChatJoin:
_ = PlayImpl("ms-appx:///Assets/Audio/voicechat_join.mp3", type: EffectType.VideoChat);
break;
case SoundEffect.VideoChatLeave:
_ = PlayImpl("ms-appx:///Assets/Audio/voicechat_leave.mp3", type: EffectType.VideoChat);
break;
}
}
public static void Play(File file)
{
if (file.Local.IsDownloadingCompleted)
{
_ = Play(StorageFile.GetFileFromPathAsync(file.Local.Path), type: EffectType.Custom);
}
}
private static async Task Play(IAsyncOperation<StorageFile> fileTask, int? loopCount = 0, EffectType type = EffectType.Generic)
{
try
{
if (_suspended)
{
return;
}
var file = await fileTask;
// This seems to fail in some conditions.
var settings = new AudioGraphSettings(AudioRenderCategory.Media);
settings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.SystemDefault;
var result = await AudioGraph.CreateAsync(settings);
if (result.Status != AudioGraphCreationStatus.Success)
{
return;
}
var fileInputNodeResult = await result.Graph.CreateFileInputNodeAsync(file);
if (fileInputNodeResult.Status != AudioFileNodeCreationStatus.Success)
{
return;
}
fileInputNodeResult.FileInputNode.LoopCount = loopCount;
var deviceOutputNodeResult = await result.Graph.CreateDeviceOutputNodeAsync();
if (deviceOutputNodeResult.Status != AudioDeviceNodeCreationStatus.Success)
{
return;
}
fileInputNodeResult.FileInputNode
.AddOutgoingConnection(deviceOutputNodeResult.DeviceOutputNode);
async void handler(AudioFileInputNode node, object args)
{
using (await _lock.WaitAsync())
{
try
{
if (_counts[type] == 0)
{
node.FileCompleted -= handler;
Stop(type);
}
else
{
_counts[type]--;
}
}
catch
{
Stop(type);
}
}
}
using (await _lock.WaitAsync())
{
Stop(type);
_graphs[type] = result.Graph;
_counts[type] = loopCount ?? -1;
fileInputNodeResult.FileInputNode.FileCompleted += handler;
result.Graph.Start();
}
}
catch { }
}
}
public enum SoundEffect
{
Sent,
VoipIncoming,
VoipRingback,
VoipBusy,
VoipFailed,
VoipEnd,
VoipConnecting,
VideoChatJoin,
VideoChatLeave
}
}