0.2.0-dev Download ZIP
117 lines ·3.5 KB Raw
| 1 | using EvilEngine.Music.Decoding; |
| 2 | using Godot; |
| 3 | |
| 4 | namespace EvilEngine.Client; |
| 5 | |
| 6 | internal sealed class PcmFeeder : IDisposable |
| 7 | { |
| 8 | private const int Chunk = 2048; |
| 9 | private readonly int capacity; |
| 10 | private readonly Vector2[] frames = new Vector2[Chunk]; |
| 11 | private readonly AudioStreamGeneratorPlayback playback; |
| 12 | private readonly float[] samples; |
| 13 | |
| 14 | private readonly IPcmSource source; |
| 15 | private readonly long start; |
| 16 | private readonly Thread thread; |
| 17 | private volatile bool finished; |
| 18 | private long pushed; |
| 19 | private volatile bool stopping; |
| 20 | |
| 21 | public PcmFeeder(IPcmSource source, AudioStreamGeneratorPlayback playback) |
| 22 | { |
| 23 | this.source = source; |
| 24 | this.playback = playback; |
| 25 | capacity = playback.GetFramesAvailable(); |
| 26 | start = source.Position; |
| 27 | samples = new float[Chunk * source.Channels]; |
| 28 | Feed(capacity); |
| 29 | thread = new Thread(Run) { IsBackground = true, Name = "Music feeder" }; |
| 30 | thread.Start(); |
| 31 | } |
| 32 | |
| 33 | public double Seconds |
| 34 | { |
| 35 | get |
| 36 | { |
| 37 | var queued = capacity - playback.GetFramesAvailable(); |
| 38 | return Math.Max(start, start + Interlocked.Read(ref pushed) - queued) / (double)source.SampleRate; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public bool Drained => finished && capacity - playback.GetFramesAvailable() <= 0; |
| 43 | |
| 44 | public void Dispose() |
| 45 | { |
| 46 | stopping = true; |
| 47 | if (thread.IsAlive && Thread.CurrentThread != thread) |
| 48 | thread.Join(); |
| 49 | } |
| 50 | |
| 51 | private void Run() |
| 52 | { |
| 53 | try |
| 54 | { |
| 55 | while (!stopping && !finished) |
| 56 | { |
| 57 | var free = playback.GetFramesAvailable(); |
| 58 | if (free < Chunk) |
| 59 | { |
| 60 | Thread.Sleep(5); |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | Feed(free); |
| 65 | } |
| 66 | } |
| 67 | catch (Exception error) when (error is IOException or InvalidDataException or EndOfStreamException) |
| 68 | { |
| 69 | GD.PushWarning($"Music: decoding stopped: {error.Message}"); |
| 70 | finished = true; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private void Feed(int free) |
| 75 | { |
| 76 | var channels = source.Channels; |
| 77 | while (free >= Chunk && !stopping) |
| 78 | { |
| 79 | var read = source.Read(samples); |
| 80 | if (read == 0) |
| 81 | { |
| 82 | finished = true; |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | for (var i = 0; i < read; i++) |
| 87 | frames[i] = Stereo(samples.AsSpan(i * channels, channels)); |
| 88 | playback.PushBuffer(frames.AsSpan(0, read)); |
| 89 | Interlocked.Add(ref pushed, read); |
| 90 | free -= read; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private static Vector2 Stereo(ReadOnlySpan<float> frame) |
| 95 | { |
| 96 | switch (frame.Length) |
| 97 | { |
| 98 | case 1: |
| 99 | return new Vector2(frame[0], frame[0]); |
| 100 | case 2: |
| 101 | return new Vector2(frame[0], frame[1]); |
| 102 | case >= 6: |
| 103 | const float side = 0.7071f; |
| 104 | const float scale = 1 / (1 + 2 * side); |
| 105 | return new Vector2((frame[0] + side * frame[2] + side * frame[4]) * scale, |
| 106 | (frame[1] + side * frame[2] + side * frame[5]) * scale); |
| 107 | default: |
| 108 | float left = 0, right = 0; |
| 109 | for (var c = 0; c < frame.Length; c++) |
| 110 | if (c % 2 == 0) |
| 111 | left += frame[c]; |
| 112 | else |
| 113 | right += frame[c]; |
| 114 | return new Vector2(left / ((frame.Length + 1) / 2), right / (frame.Length / 2)); |
| 115 | } |
| 116 | } |
| 117 | } |