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
SynthesiserSampleProvider.cs
1using MeltySynth;
2using NAudio.Wave;
3
4namespace Gondwana.Audio.Midi;
5
7public class SynthesizerSampleProvider : ISampleProvider
8{
9 private readonly object _gate = new();
10 private readonly MidiFileSequencer _sequencer;
11 private readonly Synthesizer _synthesizer;
12 private readonly MeltySynth.MidiFile _midiFile;
13 private readonly bool _loop;
14 private readonly long _lengthFrames;
15 private readonly float[] _left = new float[8192];
16 private readonly float[] _right = new float[8192];
17 private long _positionFrames;
18
25 MidiFileSequencer sequencer,
26 Synthesizer synth,
27 MeltySynth.MidiFile midiFile,
28 bool loop)
29 {
30 _sequencer = sequencer;
31 _synthesizer = synth;
32 _midiFile = midiFile;
33 _loop = loop;
34 _lengthFrames = (long)(midiFile.Length.TotalSeconds * synth.SampleRate);
35 WaveFormat = WaveFormat.CreateIeeeFloatWaveFormat(synth.SampleRate, 2);
36 }
37
39 public WaveFormat WaveFormat { get; }
40
47 public int Read(float[] buffer, int offset, int count)
48 {
49 ArgumentNullException.ThrowIfNull(buffer);
50 _ = buffer.AsSpan(offset, count);
51
52 lock (_gate)
53 {
54 int framesRequested = count / 2;
55 int framesRendered = 0;
56
57 while (framesRendered < framesRequested && _lengthFrames > 0)
58 {
59 if (_positionFrames >= _lengthFrames)
60 {
61 if (!_loop)
62 break;
63
64 Restart();
65 }
66
67 int renderCount = (int)Math.Min(Math.Min(framesRequested - framesRendered, _left.Length), _lengthFrames - _positionFrames);
68 Render(renderCount);
69
70 for (int i = 0; i < renderCount; i++)
71 {
72 buffer[offset++] = _left[i];
73 buffer[offset++] = _right[i];
74 }
75
76 framesRendered += renderCount;
77 }
78
79 return framesRendered * 2;
80 }
81 }
82
90 public void Seek(TimeSpan time)
91 {
92 long target = (long)(Math.Clamp(time.TotalSeconds, 0, _midiFile.Length.TotalSeconds) * _synthesizer.SampleRate);
93
94 lock (_gate)
95 {
96 if (target < _positionFrames)
97 Restart();
98
99 while (_positionFrames < target)
100 Render((int)Math.Min(target - _positionFrames, _left.Length));
101 }
102 }
103
104 private void Render(int frames)
105 {
106 _sequencer.Render(_left.AsSpan(0, frames), _right.AsSpan(0, frames));
107 _positionFrames += frames;
108 }
109
110 private void Restart()
111 {
112 _sequencer.Play(_midiFile, loop: false);
113 _positionFrames = 0;
114 }
115}
WaveFormat WaveFormat
Gets the synthesizer's sample rate as stereo IEEE float audio.
void Seek(TimeSpan time)
Seeks within the timeline, retaining synthesizer state for forward seeks.
int Read(float[] buffer, int offset, int count)
Reads complete stereo frames; returns zero at EOF, including on subsequent reads.
SynthesizerSampleProvider(MidiFileSequencer sequencer, Synthesizer synth, MeltySynth.MidiFile midiFile, bool loop)
Wraps a sequencer that has already started the supplied MIDI file at its beginning.