Gondwana Game Engine Development (master) - 2.6.1
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
WaveProviderToWaveStream.cs
1using NAudio.Wave;
2
3namespace Gondwana.Audio.Midi;
4
8public class WaveProviderToWaveStream : WaveStream
9{
10 private readonly IWaveProvider source;
11 private readonly WaveFormat waveFormat;
12 private long position;
13 private readonly long _midiLengthBytes;
14
15 // Delegate for seeking into the underlying synthesizer
16 private readonly Action<TimeSpan>? _seekHandler;
17
24 public WaveProviderToWaveStream(IWaveProvider source, double durationSeconds, Action<TimeSpan>? seekHandler)
25 {
26 this.source = source;
27 this.waveFormat = source.WaveFormat;
28 _midiLengthBytes = (long)(durationSeconds * waveFormat.AverageBytesPerSecond);
29 _seekHandler = seekHandler;
30 }
31
35 public override WaveFormat WaveFormat => waveFormat;
36
40 public override long Length => _midiLengthBytes;
41
46 public override long Position
47 {
48 get => position;
49 set
50 {
51 if (_seekHandler == null)
52 throw new NotSupportedException("Seeking is not supported for this stream.");
53
54 // clamp
55 var clamped = Math.Max(0, Math.Min(value, Length));
56 // align to frame (BlockAlign is bytes per frame)
57 clamped -= clamped % waveFormat.BlockAlign;
58
59 position = clamped;
60
61 var seconds = (double)position / waveFormat.AverageBytesPerSecond;
62 _seekHandler(TimeSpan.FromSeconds(seconds));
63 }
64 }
65
73 public override int Read(byte[] buffer, int offset, int count)
74 {
75 int read = source.Read(buffer, offset, count);
76 position += read;
77 return read;
78 }
79}
WaveProviderToWaveStream(IWaveProvider source, double durationSeconds, Action< TimeSpan >? seekHandler)
Initializes a new instance of the WaveProviderToWaveStream class.
override long Position
Gets or sets the current position in the stream in bytes.
override WaveFormat WaveFormat
Gets the wave format of the stream.
override long Length
Gets the length of the stream in bytes.
override int Read(byte[] buffer, int offset, int count)
Reads audio data from the underlying wave provider.