![]() |
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.
|
Provides real-time audio sample generation from MIDI data using software synthesis. More...
Public Member Functions | |
| SynthesizerSampleProvider (MidiFileSequencer sequencer, Synthesizer synth, MeltySynth.MidiFile midiFile, bool loop) | |
| Initializes a new instance of the SynthesizerSampleProvider class with the specified MIDI sequencer, synthesizer, MIDI file, and looping behavior. | |
| int | Read (float[] buffer, int offset, int count) |
| Reads synthesized audio samples into the provided buffer. | |
| void | Seek (TimeSpan time) |
| Seeks to a specific time position in the MIDI playback by resetting the synthesizer and fast-forwarding through the specified duration. | |
Properties | |
| WaveFormat | WaveFormat = WaveFormat.CreateIeeeFloatWaveFormat(44100, 2) [get] |
| Gets the wave format of the synthesized audio output. | |
Provides real-time audio sample generation from MIDI data using software synthesis.
This class bridges the MeltySynth MIDI synthesizer with NAudio's audio streaming infrastructure by implementing ISampleProvider. It continuously renders MIDI data to stereo IEEE float audio samples at 44.1 kHz, with support for optional looping and time-based seeking.
The provider monitors synthesizer output for silence detection to determine when playback has completed. When looping is enabled, the MIDI sequencer automatically restarts upon detecting silence. Otherwise, playback terminates and the provider returns zero samples.
Audio rendering is performed in chunks using internal stereo buffers (8192 frames) to balance performance with memory usage. Silence is detected using a small floating-point tolerance threshold to account for numerical precision.
Definition at line 28 of file SynthesiserSampleProvider.cs.
| Gondwana.Audio.Midi.SynthesizerSampleProvider.SynthesizerSampleProvider | ( | MidiFileSequencer | sequencer, |
| Synthesizer | synth, | ||
| MeltySynth.MidiFile | midiFile, | ||
| bool | loop ) |
Initializes a new instance of the SynthesizerSampleProvider class with the specified MIDI sequencer, synthesizer, MIDI file, and looping behavior.
| sequencer | The MidiFileSequencer that controls MIDI event playback timing and sequencing. |
| synth | The Synthesizer instance that generates audio samples from MIDI events using a SoundFont. |
| midiFile | The MeltySynth.MidiFile containing the MIDI data to be synthesized. |
| loop | true to automatically restart playback when the MIDI file completes and silence is detected; false to stop playback after the MIDI file completes. |
The sequencer must already be configured with the synthesizer and should have playback initiated before samples are requested through Read. The looping behavior is handled internally by detecting silence in the synthesizer output and manually restarting the sequencer when needed.
Definition at line 62 of file SynthesiserSampleProvider.cs.
| int Gondwana.Audio.Midi.SynthesizerSampleProvider.Read | ( | float[] | buffer, |
| int | offset, | ||
| int | count ) |
Reads synthesized audio samples into the provided buffer.
| buffer | The destination buffer to receive interleaved stereo audio samples (left, right, left, right, ...). |
| offset | The zero-based index in the buffer at which to begin writing samples. |
| count | The maximum number of samples to read. This value represents individual sample values, not frames. For stereo audio, each frame consists of 2 samples (one per channel). |
This method renders MIDI audio in real-time by requesting synthesis from the underlying Synthesizer and interleaving the stereo output into the destination buffer. Rendering is performed in chunks using internal buffers to optimize performance.
When silence is detected in the synthesized output:
_loop is true), the MIDI sequencer is automatically restarted to repeat playback from the beginning. Silence is detected by checking if all synthesized samples in a chunk have absolute values below the Tolerance threshold (1e-6).
Definition at line 123 of file SynthesiserSampleProvider.cs.
| void Gondwana.Audio.Midi.SynthesizerSampleProvider.Seek | ( | TimeSpan | time | ) |
Seeks to a specific time position in the MIDI playback by resetting the synthesizer and fast-forwarding through the specified duration.
| time | The target time position to seek to, measured from the beginning of the MIDI file. |
This method implements seek functionality by:
The fast-forward operation renders audio in chunks of 512 frames using dedicated seek buffers to minimize memory allocation overhead. All rendered samples during seeking are discarded.
Performance Note: Seeking is implemented by rendering from the beginning, which can be computationally expensive for large seek distances. The operation time scales linearly with the target time value.
After seeking completes, subsequent calls to Read will provide audio samples starting from the target time position.
Definition at line 205 of file SynthesiserSampleProvider.cs.
|
get |
Gets the wave format of the synthesized audio output.
A global::NAudio.Wave.WaveFormat configured for stereo (2 channels) IEEE float samples at 44.1 kHz.
This format is fixed and matches the output format of the MeltySynth synthesizer. All audio samples provided by Read conform to this format.
Definition at line 84 of file SynthesiserSampleProvider.cs.