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
MidiFileReader.cs
1using System.Reflection;
3using MeltySynth;
4using Microsoft.Extensions.Logging;
5using NAudio.Wave;
6
7namespace Gondwana.Audio.Midi;
8
23public static class MidiFileReader
24{
25 private static readonly Lazy<SoundFont> _soundFont = new(() =>
26 {
27 var assembly = Assembly.GetExecutingAssembly();
28
29 using var stream = assembly.GetManifestResourceStream("Gondwana.Audio.Midi.TimGM6mb.sf2")
30 ?? throw new InvalidOperationException("Embedded SoundFont 'TimGM6mb.sf2' not found.");
31
32 return new SoundFont(stream);
33 });
34
48 public static SoundFont SoundFont => _soundFont.Value;
49
66 internal static void RegisterDefaultReaders()
67 {
68 NAudioReaderRegistry.Register(".mid", CreateReader);
69 NAudioReaderRegistry.Register(".midi", CreateReader);
70 Engine.Logger.LogInformation("Registered Gondwana.Audio.Midi readers with the NAudio backend.");
71 }
72
108 public static WaveStream CreateReader(Stream stream)
109 {
110 using var buffer = new MemoryStream();
111 stream.CopyTo(buffer);
112 buffer.Position = 0;
113
114 var synth = new Synthesizer(SoundFont, 44100);
115 var midi = new MidiFile(buffer);
116 var sequencer = new MidiFileSequencer(synth);
117 sequencer.Play(midi, loop: false);
118
119 var provider = new SynthesizerSampleProvider(
120 sequencer,
121 synth,
122 midi,
123 loop: false);
124
125 return new WaveProviderToWaveStream(provider.ToWaveProvider(), midi.Length.TotalSeconds, provider.Seek);
126 }
127}