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
NAudioReaderRegistry.cs
1using NAudio.Vorbis;
2using NAudio.Wave;
3
5
9public static class NAudioReaderRegistry
10{
11 private sealed record Registration(Func<Stream, WaveStream>? StreamFactory, Func<string, WaveStream>? FileFactory);
12
13 private static readonly System.Collections.Concurrent.ConcurrentDictionary<string, Registration> Readers = new(
14 StringComparer.OrdinalIgnoreCase);
15
16 static NAudioReaderRegistry()
17 {
18 Register(".wav", stream => new WaveFileReader(stream));
19 Register(".mp3", stream => new Mp3FileReader(stream));
20 Register(".ogg", stream => new VorbisWaveReader(stream));
21 Register(".oga", stream => new VorbisWaveReader(stream));
22 Register(".mogg", stream => new VorbisWaveReader(stream));
23 RegisterFile(".wma", path => new MediaFoundationReader(path));
24 RegisterFile(".m4a", path => new MediaFoundationReader(path));
25 }
26
34 public static void Register(string extension, Func<Stream, WaveStream> readerFactory)
35 {
36 ArgumentNullException.ThrowIfNull(readerFactory);
37 Readers[NormalizeExtension(extension)] = new Registration(readerFactory, null);
38 }
39
47 public static void RegisterFile(string extension, Func<string, WaveStream> readerFactory)
48 {
49 ArgumentNullException.ThrowIfNull(readerFactory);
50 Readers[NormalizeExtension(extension)] = new Registration(null, readerFactory);
51 }
52
58 public static bool Supports(string fileNameOrExtension) => Readers.ContainsKey(GetExtension(fileNameOrExtension));
59
64 public static IEnumerable<string> SupportedExtensions() => Readers.Keys.OrderBy(ext => ext);
65
66 internal static (WaveStream Reader, string? TemporaryFilePath) Open(byte[] data, string fileNameOrExtension)
67 {
68 var extension = GetExtension(fileNameOrExtension);
69
70 if (!Readers.TryGetValue(extension, out var registration))
71 throw new NotSupportedException($"Format '{extension}' is not supported by Gondwana.Audio.NAudio.");
72
73 if (registration.StreamFactory is not null)
74 {
75 var stream = new MemoryStream(data, writable: false);
76
77 try
78 {
79 return (registration.StreamFactory(stream), null);
80 }
81 catch
82 {
83 stream.Dispose();
84 throw;
85 }
86 }
87
88 var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + extension);
89
90 try
91 {
92 File.WriteAllBytes(tempPath, data);
93 return (registration.FileFactory!(tempPath), tempPath);
94 }
95 catch
96 {
97 TryDelete(tempPath);
98 throw;
99 }
100 }
101
102 private static string GetExtension(string fileNameOrExtension)
103 {
104 var extension = Path.GetExtension(fileNameOrExtension);
105
106 if (string.IsNullOrWhiteSpace(extension) &&
107 !fileNameOrExtension.Contains('/') && !fileNameOrExtension.Contains('\\'))
108 extension = fileNameOrExtension;
109
110 return NormalizeExtension(extension);
111 }
112
113 private static string NormalizeExtension(string? extension)
114 {
115 if (string.IsNullOrWhiteSpace(extension))
116 throw new InvalidOperationException("Audio source has no file extension.");
117
118 return extension.StartsWith('.') ? extension.ToLowerInvariant() : "." + extension.ToLowerInvariant();
119 }
120
121 internal static void TryDelete(string? path)
122 {
123 if (string.IsNullOrWhiteSpace(path))
124 return;
125
126 try
127 {
128 if (File.Exists(path))
129 File.Delete(path);
130 }
131 catch
132 {
133 // Cleanup is best effort; playback disposal must not mask the original operation.
134 }
135 }
136}