|
| 1 | +// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. |
| 2 | +// See the LICENCE file in the repository root for full licence text. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using Debug = System.Diagnostics.Debug; |
| 7 | + |
| 8 | +namespace osu.Android.Native |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Managed wrapper around the native Oboe low-latency audio bridge. |
| 12 | + /// Provides accurate audio output latency measurement for rhythm-game synchronisation. |
| 13 | + /// </summary> |
| 14 | + public sealed class OboeAudioBridge : IDisposable |
| 15 | + { |
| 16 | + private long nativePtr; |
| 17 | + private bool disposed; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Creates and opens a new low-latency Oboe audio stream. |
| 21 | + /// Returns null if native library or stream creation fails. |
| 22 | + /// </summary> |
| 23 | + public static OboeAudioBridge? Create() |
| 24 | + { |
| 25 | + try |
| 26 | + { |
| 27 | + long ptr = nOboeCreate(); |
| 28 | + |
| 29 | + if (ptr == 0) |
| 30 | + { |
| 31 | + Debug.WriteLine("[osu!] Oboe stream creation failed (native returned null)"); |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + return new OboeAudioBridge(ptr); |
| 36 | + } |
| 37 | + catch (DllNotFoundException) |
| 38 | + { |
| 39 | + Debug.WriteLine("[osu!] Native library not found, Oboe unavailable"); |
| 40 | + return null; |
| 41 | + } |
| 42 | + catch (Exception e) |
| 43 | + { |
| 44 | + Debug.WriteLine($"[osu!] Oboe creation failed: {e.Message}"); |
| 45 | + return null; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private OboeAudioBridge(long ptr) |
| 50 | + { |
| 51 | + nativePtr = ptr; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Starts the audio output stream. |
| 56 | + /// </summary> |
| 57 | + /// <returns>True if the stream started successfully.</returns> |
| 58 | + public bool Start() |
| 59 | + { |
| 60 | + if (disposed || nativePtr == 0) return false; |
| 61 | + |
| 62 | + try |
| 63 | + { |
| 64 | + return nOboeStart(nativePtr) != 0; |
| 65 | + } |
| 66 | + catch (Exception e) |
| 67 | + { |
| 68 | + Debug.WriteLine($"[osu!] Oboe start failed: {e.Message}"); |
| 69 | + return false; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Stops the audio output stream. |
| 75 | + /// </summary> |
| 76 | + public void Stop() |
| 77 | + { |
| 78 | + if (disposed || nativePtr == 0) return; |
| 79 | + |
| 80 | + try |
| 81 | + { |
| 82 | + nOboeStop(nativePtr); |
| 83 | + } |
| 84 | + catch (Exception e) |
| 85 | + { |
| 86 | + Debug.WriteLine($"[osu!] Oboe stop failed: {e.Message}"); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Returns the measured audio output latency in milliseconds, or -1 if unavailable. |
| 92 | + /// This can be used to automatically calibrate audio offset for gameplay. |
| 93 | + /// </summary> |
| 94 | + public double GetOutputLatencyMs() |
| 95 | + { |
| 96 | + if (disposed || nativePtr == 0) return -1; |
| 97 | + |
| 98 | + try |
| 99 | + { |
| 100 | + return nOboeGetLatencyMs(nativePtr); |
| 101 | + } |
| 102 | + catch |
| 103 | + { |
| 104 | + return -1; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Returns true if the Oboe stream is currently running. |
| 110 | + /// </summary> |
| 111 | + public bool IsActive |
| 112 | + { |
| 113 | + get |
| 114 | + { |
| 115 | + if (disposed || nativePtr == 0) return false; |
| 116 | + |
| 117 | + try |
| 118 | + { |
| 119 | + return nOboeIsActive(nativePtr) != 0; |
| 120 | + } |
| 121 | + catch |
| 122 | + { |
| 123 | + return false; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public void Dispose() |
| 129 | + { |
| 130 | + if (disposed) return; |
| 131 | + |
| 132 | + disposed = true; |
| 133 | + |
| 134 | + if (nativePtr != 0) |
| 135 | + { |
| 136 | + try |
| 137 | + { |
| 138 | + nOboeDestroy(nativePtr); |
| 139 | + } |
| 140 | + catch (Exception e) |
| 141 | + { |
| 142 | + Debug.WriteLine($"[osu!] Oboe dispose failed: {e.Message}"); |
| 143 | + } |
| 144 | + |
| 145 | + nativePtr = 0; |
| 146 | + } |
| 147 | + |
| 148 | + GC.SuppressFinalize(this); |
| 149 | + } |
| 150 | + |
| 151 | + ~OboeAudioBridge() |
| 152 | + { |
| 153 | + Dispose(); |
| 154 | + } |
| 155 | + |
| 156 | + [DllImport("osu_native")] |
| 157 | + private static extern long nOboeCreate(); |
| 158 | + |
| 159 | + [DllImport("osu_native")] |
| 160 | + private static extern void nOboeDestroy(long ptr); |
| 161 | + |
| 162 | + [DllImport("osu_native")] |
| 163 | + private static extern byte nOboeStart(long ptr); |
| 164 | + |
| 165 | + [DllImport("osu_native")] |
| 166 | + private static extern void nOboeStop(long ptr); |
| 167 | + |
| 168 | + [DllImport("osu_native")] |
| 169 | + private static extern double nOboeGetLatencyMs(long ptr); |
| 170 | + |
| 171 | + [DllImport("osu_native")] |
| 172 | + private static extern byte nOboeIsActive(long ptr); |
| 173 | + } |
| 174 | +} |
0 commit comments