Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
BrowserAudioPlaybackHandle.cs
1using System.Runtime.Versioning;
2
4
5[SupportedOSPlatform("browser")]
6internal sealed class BrowserAudioPlaybackHandle : IAudioPlaybackHandle
7{
8 private readonly string _key;
9 private bool _disposed;
10 private bool _isLooping;
11 private float _volume;
12 private float _pan;
13 private float _playbackSpeed;
14
23 public BrowserAudioPlaybackHandle(
24 string key,
25 string uri,
26 float volume,
27 float pan,
28 float playbackSpeed)
29 {
30 // Each handle owns its JS entry, including during replacement of a manager key.
31 _key = Guid.NewGuid().ToString("N");
32 _volume = Math.Clamp(volume, 0f, 1f);
33 _pan = Math.Clamp(pan, -1f, 1f);
34
35 _playbackSpeed = Math.Clamp(
36 playbackSpeed,
37 AudioResource.MinimumPlaybackSpeed,
38 AudioResource.MaximumPlaybackSpeed);
39
40 BrowserAudioInterop.Load(
41 _key,
42 uri,
43 loop: false,
44 _volume,
45 _pan,
46 _playbackSpeed,
47 OnEnded);
48 }
49
59 public BrowserAudioPlaybackHandle(
60 string key,
61 byte[] data,
62 string mimeType,
63 float volume,
64 float pan,
65 float playbackSpeed)
66 {
67 ArgumentNullException.ThrowIfNull(data);
68
69 // Packed/stream-backed browser audio is exposed to HTMLAudioElement through a
70 // short-lived Blob URL owned by the JavaScript entry.
71 _key = Guid.NewGuid().ToString("N");
72 _volume = Math.Clamp(volume, 0f, 1f);
73 _pan = Math.Clamp(pan, -1f, 1f);
74
75 _playbackSpeed = Math.Clamp(
76 playbackSpeed,
77 AudioResource.MinimumPlaybackSpeed,
78 AudioResource.MaximumPlaybackSpeed);
79
80 BrowserAudioInterop.LoadBytes(
81 _key,
82 Convert.ToBase64String(data),
83 mimeType,
84 loop: false,
85 _volume,
86 _pan,
87 _playbackSpeed,
88 OnEnded);
89 }
90
92 public event EventHandler? PlaybackCompleted;
93
94 private void OnEnded()
95 {
96 if (!_disposed && !_isLooping)
97 PlaybackCompleted?.Invoke(this, EventArgs.Empty);
98 }
99
101 public AudioPlaybackState State => BrowserAudioInterop.GetState(_key) switch
102 {
103 1 => AudioPlaybackState.Playing,
104 2 => AudioPlaybackState.Paused,
105 _ => AudioPlaybackState.Stopped
106 };
107
109 public TimeSpan CurrentTime => TimeSpan.FromSeconds(Math.Max(0d, BrowserAudioInterop.GetCurrentTime(_key)));
110
112 public TimeSpan Duration => TimeSpan.FromSeconds(Math.Max(0d, BrowserAudioInterop.GetDuration(_key)));
113
115 public string? TemporaryFilePath => null;
116
118 public bool IsLooping
119 {
120 get => _isLooping;
121 set
122 {
123 _isLooping = value;
124 BrowserAudioInterop.SetLoop(_key, value);
125 }
126 }
127
129 public float Volume
130 {
131 get => _volume;
132 set
133 {
134 _volume = Math.Clamp(value, 0f, 1f);
135 BrowserAudioInterop.SetVolume(_key, _volume);
136 }
137 }
138
140 public float Pan
141 {
142 get => _pan;
143 set
144 {
145 _pan = Math.Clamp(value, -1f, 1f);
146 BrowserAudioInterop.SetPan(_key, _pan);
147 }
148 }
149
151 public float PlaybackSpeed
152 {
153 get => _playbackSpeed;
154 set
155 {
156 _playbackSpeed = Math.Clamp(value, AudioResource.MinimumPlaybackSpeed, AudioResource.MaximumPlaybackSpeed);
157 BrowserAudioInterop.SetPlaybackSpeed(_key, _playbackSpeed);
158 }
159 }
160
162 public void Play(bool fromStart = true)
163 {
164 ThrowIfDisposed();
165 BrowserAudioInterop.Play(_key, fromStart);
166 }
167
169 public void Pause()
170 {
171 ThrowIfDisposed();
172 BrowserAudioInterop.Pause(_key);
173 }
174
176 public void Resume()
177 {
178 ThrowIfDisposed();
179
180 if (State == AudioPlaybackState.Paused)
181 BrowserAudioInterop.Play(_key, fromStart: false);
182 }
183
185 public void Seek(TimeSpan position)
186 {
187 ThrowIfDisposed();
188 var seconds = Math.Max(0d, position.TotalSeconds);
189 BrowserAudioInterop.SetCurrentTime(_key, seconds);
190 }
191
193 public void Stop()
194 {
195 ThrowIfDisposed();
196 BrowserAudioInterop.Stop(_key);
197 }
198
199 private void ThrowIfDisposed()
200 {
201 ObjectDisposedException.ThrowIf(_disposed, this);
202 }
203
207 public void Dispose()
208 {
209 if (_disposed)
210 return;
211
212 BrowserAudioInterop.Unload(_key);
213 PlaybackCompleted = null;
214 _disposed = true;
215 }
216}
void Resume()
Resume playback after a pause.
void Pause()
Pause playback. Calling Resume should continue from the paused position.
void Stop()
Stop playback and reset the current position to the start.
AudioPlaybackState
Describes the current playback state of an audio resource without exposing backend-specific playback ...