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
VariableSpeedSampleProvider.cs
1using NAudio.Wave;
2
4
10internal sealed class VariableSpeedSampleProvider : ISampleProvider
11{
12 private readonly ISampleProvider _source;
13 private readonly int _channels;
14 private readonly float[] _currentFrame;
15 private readonly float[] _nextFrame;
16 private readonly object _sync = new();
17
18 private bool _initialized;
19 private bool _hasCurrent;
20 private bool _hasNext;
21 private double _phase;
22 private float _playbackSpeed = 1.0f;
23
30 public VariableSpeedSampleProvider(ISampleProvider source)
31 {
32 _source = source ?? throw new ArgumentNullException(nameof(source));
33 _channels = source.WaveFormat.Channels;
34
35 if (_channels <= 0)
36 throw new ArgumentException("Source must expose at least one audio channel.", nameof(source));
37
38 _currentFrame = new float[_channels];
39 _nextFrame = new float[_channels];
40 }
41
45 public WaveFormat WaveFormat => _source.WaveFormat;
46
54 public float PlaybackSpeed
55 {
56 get => Volatile.Read(ref _playbackSpeed);
57 set => Volatile.Write(
58 ref _playbackSpeed,
59 Math.Clamp(value, AudioResource.MinimumPlaybackSpeed, AudioResource.MaximumPlaybackSpeed));
60 }
61
70 public int Read(float[] buffer, int offset, int count)
71 {
72 if (count <= 0)
73 return 0;
74
75 lock (_sync)
76 {
77 EnsureInitialized();
78
79 if (!_hasCurrent)
80 return 0;
81
82 var frameCapacity = count / _channels;
83 var framesWritten = 0;
84
85 while (framesWritten < frameCapacity && _hasCurrent)
86 {
87 var destination = offset + (framesWritten * _channels);
88 var phase = (float)_phase;
89
90 for (var channel = 0; channel < _channels; channel++)
91 {
92 var next = _hasNext ? _nextFrame[channel] : _currentFrame[channel];
93 buffer[destination + channel] = _currentFrame[channel] + ((next - _currentFrame[channel]) * phase);
94 }
95
96 framesWritten++;
97 _phase += PlaybackSpeed;
98
99 while (_phase >= 1.0 && _hasCurrent)
100 {
101 _phase -= 1.0;
102 AdvanceFrame();
103 }
104 }
105
106 return framesWritten * _channels;
107 }
108 }
109
114 public void Reset(Action? reposition = null)
115 {
116 lock (_sync)
117 {
118 reposition?.Invoke();
119 _initialized = false;
120 _hasCurrent = false;
121 _hasNext = false;
122 _phase = 0;
123 }
124 }
125
126 private void EnsureInitialized()
127 {
128 if (_initialized)
129 return;
130
131 _hasCurrent = ReadFrame(_currentFrame);
132 _hasNext = _hasCurrent && ReadFrame(_nextFrame);
133 _phase = 0;
134 _initialized = true;
135 }
136
137 private void AdvanceFrame()
138 {
139 if (!_hasNext)
140 {
141 _hasCurrent = false;
142 return;
143 }
144
145 Array.Copy(_nextFrame, _currentFrame, _channels);
146 _hasCurrent = true;
147 _hasNext = ReadFrame(_nextFrame);
148 }
149
150 private bool ReadFrame(float[] frame)
151 {
152 var read = 0;
153
154 while (read < _channels)
155 {
156 var count = _source.Read(frame, read, _channels - read);
157
158 if (count == 0)
159 return false;
160
161 read += count;
162 }
163
164 return true;
165 }
166}