Skip to content
0.2.0-dev Download ZIP
124 lines ·3.7 KB Raw
1using EvilEngine.Music.Aac;
2using EvilEngine.Music.Alac;
3using EvilEngine.Music.Files;
4using EvilEngine.Music.Flac;
5using EvilEngine.Music.Opus;
6
7namespace EvilEngine.Music.Decoding;
8
9public interface IPcmSource : IDisposable
10{
11 int SampleRate { get; }
12 int Channels { get; }
13 long TotalFrames { get; }
14 long Position { get; }
15 int Read(Span<float> interleaved);
16 void Seek(long frame);
17}
18
19public enum PlayPath
20{
21 None,
22 Native,
23 Decoded
24}
25
26public static class Decoders
27{
28 public static PlayPath How(TrackInfo info)
29 {
30 return (info.Codec, info.Container) switch
31 {
32 (AudioCodec.Mp3, AudioContainer.Mpeg) => PlayPath.Native,
33 (AudioCodec.Vorbis, AudioContainer.Ogg) => PlayPath.Native,
34 (AudioCodec.Flac, AudioContainer.Flac) => PlayPath.Decoded,
35 (AudioCodec.Pcm or AudioCodec.Float, AudioContainer.Wave or AudioContainer.Aiff) => PlayPath.Decoded,
36 (AudioCodec.Alac, AudioContainer.Mp4) => PlayPath.Decoded,
37 (AudioCodec.Aac, AudioContainer.Mp4 or AudioContainer.Adts) => PlayPath.Decoded,
38 (AudioCodec.Opus, AudioContainer.Ogg or AudioContainer.Mp4) => PlayPath.Decoded,
39 _ => PlayPath.None
40 };
41 }
42
43 public static IPcmSource Open(string path)
44 {
45 return Open(AudioFile.Open(path), Path.GetFileName(path));
46 }
47
48 public static IPcmSource Open(Stream stream, string name)
49 {
50 try
51 {
52 Span<byte> head = stackalloc byte[4];
53 stream.ReadExactly(head);
54 stream.Position = 0;
55 if (head.SequenceEqual("OggS"u8))
56 {
57 var ogg = OggOpusStream.Open(stream);
58 return Source(ogg, () => new OpusDecoder(ogg.Head));
59 }
60
61 if (head.SequenceEqual("fLaC"u8) || (head[..3].SequenceEqual("ID3"u8) && FlacAfterTags(stream)))
62 return new FlacDecoder(stream);
63 if (AudioFile.Pcm(stream) is { } layout)
64 return new PcmDecoder(stream, layout);
65 if (Mp4Atoms.IsMp4(Head(stream)))
66 return Mp4(Mp4Track.Open(stream));
67 if (AdtsStream.Info(stream) is not null)
68 {
69 var adts = AdtsStream.Open(stream);
70 return Source(adts, () => new AacDecoder(adts.Config));
71 }
72
73 throw new InvalidDataException($"{name} isn't a stream the engine decodes.");
74 }
75 catch
76 {
77 stream.Dispose();
78 throw;
79 }
80 }
81
82 private static bool FlacAfterTags(Stream stream)
83 {
84 stream.Position = 0;
85 Id3v2.Skip(stream);
86 Span<byte> magic = stackalloc byte[4];
87 var flac = Bytes.ReadAtMost(stream, magic) == 4 && magic.SequenceEqual("fLaC"u8);
88 stream.Position = 0;
89 return flac;
90 }
91
92 private static byte[] Head(Stream stream)
93 {
94 var head = new byte[12];
95 stream.Position = 0;
96 var read = Bytes.ReadAtMost(stream, head);
97 stream.Position = 0;
98 return head[..read];
99 }
100
101 private static PacketSource Mp4(Mp4Track track)
102 {
103 return Source(track, () => track.Format switch
104 {
105 "alac" => new AlacDecoder(track.Config),
106 "Opus" => new OpusDecoder(track.Config),
107 "mp4a" when track.ObjectType is 0x40 or 0x66 or 0x67 => new AacDecoder(track.Config),
108 _ => throw new InvalidDataException($"MP4 audio in {track.Format} isn't a codec the engine decodes.")
109 });
110 }
111
112 private static PacketSource Source(IPacketStream packets, Func<IPacketDecoder> decoder)
113 {
114 try
115 {
116 return new PacketSource(packets, decoder());
117 }
118 catch
119 {
120 packets.Dispose();
121 throw;
122 }
123 }
124}