SPB Git

Repository Public

WaveformRenderer.swift 11.4 KB 236 lines swift

1// ORIX DJ2// File: App/Sources/Features/Waveforms/WaveformRenderer.swift3// Description: Metal renderer of the scrolling deck waveform. The analysed three-band summary is uploaded once per4//              track as a texture; each frame is a single full-screen pass that draws the bands, beat grid, cue points,5//              loop and playhead from uniforms. No per-frame geometry, no raw audio.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import AppKit10import MetalKit11import ORIXDJCore12import simd1314/// Uniform block. Only float4 members, so Swift and Metal agree on the layout.15struct WaveformUniforms {16    /// width px, height px, playhead s, seconds per pixel17    var view = SIMD4<Float>(0, 0, 0, 0)18    /// columns per second, column count, texture width, duration s19    var data = SIMD4<Float>(0, 0, 0, 0)20    /// bpm, first beat s, downbeat index, cue s21    var grid = SIMD4<Float>(0, 0, 0, -1)22    /// loop start s, loop end s, loop active, gain23    var loop = SIMD4<Float>(0, 0, 0, 1)24    var hotA = SIMD4<Float>(-1, -1, -1, -1)25    var hotB = SIMD4<Float>(-1, -1, -1, -1)26    var low = SIMD4<Float>(1, 0.3, 0.2, 1)27    var mid = SIMD4<Float>(0.2, 0.9, 0.4, 1)28    var high = SIMD4<Float>(0.3, 0.55, 1, 1)29    var deck = SIMD4<Float>(0.13, 0.83, 0.93, 1)30    var background = SIMD4<Float>(0.02, 0.027, 0.055, 1)31    /// pixel scale, playing flag, unused, unused32    var misc = SIMD4<Float>(2, 0, 0, 0)33}3435@MainActor36final class WaveformRenderer: NSObject, MTKViewDelegate {37    static let device: MTLDevice? = MTLCreateSystemDefaultDevice()38    private static let textureWidth = 40963940    private static let shaderSource = """41    #include <metal_stdlib>42    using namespace metal;4344    struct Uniforms {45        float4 view; float4 data; float4 grid; float4 loop; float4 hotA; float4 hotB;46        float4 low; float4 mid; float4 high; float4 deck; float4 background; float4 misc;47    };4849    struct VSOut { float4 position [[position]]; };5051    vertex VSOut waveVertex(uint vid [[vertex_id]]) {52        float2 p = float2((vid << 1) & 2, vid & 2);53        VSOut o; o.position = float4(p * 2.0 - 1.0, 0, 1); return o;54    }5556    static float3 columnAt(texture2d<float> tex, float index, float count, float width) {57        if (index < 0 || index >= count) return float3(0);58        uint i = uint(index);59        return tex.read(uint2(i % uint(width), i / uint(width))).rgb;60    }6162    static float lineAlpha(float seconds, float t, float spp, float halfWidthPx) {63        return clamp(halfWidthPx + 0.5 - abs(t - seconds) / spp, 0.0, 1.0);64    }6566    fragment float4 waveFragment(VSOut in [[stage_in]], constant Uniforms &u [[buffer(0)]], texture2d<float> tex [[texture(0)]]) {67        float w = u.view.x, h = u.view.y, spp = u.view.w;68        float scale = u.misc.x;69        float t = u.view.z + (in.position.x - w * 0.5) * spp;70        float y = abs(in.position.y / h * 2.0 - 1.0);71        float3 color = u.background.rgb;72        float duration = u.data.w;7374        // Outside the track: slightly lighter so the start and end are visible.75        if (t < 0 || t > duration) color += 0.012;7677        // Loop region.78        bool inLoop = u.loop.z > 0.5 && t >= u.loop.x && t < u.loop.y;79        if (inLoop) color = mix(color, u.deck.rgb, 0.16);8081        // Beat grid (only when beats are far enough apart to read).82        float bpm = u.grid.x;83        if (bpm > 0 && t >= 0 && t <= duration) {84            float beatSeconds = 60.0 / bpm;85            if (beatSeconds / spp > 7.0 * scale) {86                float b = (t - u.grid.y) / beatSeconds;87                float nearest = round(b);88                float d = abs(b - nearest) * beatSeconds / spp;89                float a = clamp(0.5 * scale + 0.5 - d, 0.0, 1.0);90                float bar = fmod(nearest - u.grid.z + 4000.0, 4.0);91                bool down = bar < 0.5 || bar > 3.5;92                color = mix(color, float3(1.0), a * (down ? 0.42 : 0.14));93            }94        }9596        // Three-band waveform: the tallest of the columns under this pixel.97        if (u.data.y > 0) {98            float c = t * u.data.x;99            float span = max(1.0, spp * u.data.x);100            int taps = clamp(int(ceil(span)), 1, 8);101            float3 a = float3(0);102            for (int i = 0; i < taps; i++) a = max(a, columnAt(tex, c + float(i) * span / float(taps), u.data.y, u.data.z));103            a = clamp(a * u.loop.w, 0.0, 1.0) * 0.94;104            float px = h * 0.5;105            float lo = clamp((a.r - y) * px, 0.0, 1.0);106            float mi = clamp((a.g * 0.86 - y) * px, 0.0, 1.0);107            float hi = clamp((a.b * 0.72 - y) * px, 0.0, 1.0);108            color = mix(color, u.low.rgb, lo * u.low.a);109            color = mix(color, u.mid.rgb, mi * u.mid.a);110            color = mix(color, u.high.rgb, hi * u.high.a);111            // Past audio is dimmed so the playhead reads at a glance.112            if (t < u.view.z) color *= 0.62;113        }114115        // Centre line.116        color = mix(color, float3(1.0), clamp(1.0 - y * h * 0.5, 0.0, 1.0) * 0.10);117118        // Loop edges, cue point, hot cues.119        if (u.loop.z > 0.5) {120            color = mix(color, u.deck.rgb, lineAlpha(u.loop.x, t, spp, scale) * 0.95);121            color = mix(color, u.deck.rgb, lineAlpha(u.loop.y, t, spp, scale) * 0.95);122        }123        if (u.grid.w >= 0) color = mix(color, float3(1.0, 0.62, 0.04), lineAlpha(u.grid.w, t, spp, scale) * (y > 0.55 ? 1.0 : 0.55));124        const float3 palette[8] = { float3(1.0, 0.29, 0.23), float3(1.0, 0.62, 0.26), float3(0.98, 0.75, 0.14), float3(0.29, 0.87, 0.5),125                                     float3(0.13, 0.83, 0.93), float3(0.3, 0.55, 1.0), float3(0.65, 0.55, 0.98), float3(0.96, 0.45, 0.71) };126        for (int i = 0; i < 8; i++) {127            float s = i < 4 ? u.hotA[i] : u.hotB[i - 4];128            if (s >= 0) {129                // A line with a flag at the top, so hot cues differ from the cue point by shape as well as colour.130                float line = lineAlpha(s, t, spp, 0.5 * scale);131                float flag = (in.position.y < 9.0 * scale && t >= s && (t - s) / spp < 9.0 * scale) ? 1.0 : 0.0;132                color = mix(color, palette[i], max(line * 0.9, flag));133            }134        }135136        // Playhead.137        float ph = abs(in.position.x - w * 0.5);138        color = mix(color, float3(1.0), clamp(scale * 0.75 + 0.5 - ph, 0.0, 1.0));139        return float4(color, 1.0);140    }141    """142143    private static let pipeline: MTLRenderPipelineState? = {144        guard let device else { return nil }145        do {146            let library = try device.makeLibrary(source: shaderSource, options: nil)147            let d = MTLRenderPipelineDescriptor()148            d.vertexFunction = library.makeFunction(name: "waveVertex")149            d.fragmentFunction = library.makeFunction(name: "waveFragment")150            d.colorAttachments[0].pixelFormat = .bgra8Unorm151            return try device.makeRenderPipelineState(descriptor: d)152        } catch {153            NSLog("ORIX DJ: waveform shader failed to compile: \(error)")154            return nil155        }156    }()157158    private static let queue: MTLCommandQueue? = device?.makeCommandQueue()159160    let deck: Int161    weak var engine: DJEngine?162    var palette: WaveformPalette = .spectrum163    /// Seconds of audio across the width of the view.164    var secondsVisible = 6.0165    var deckColor = SIMD4<Float>(0.13, 0.83, 0.93, 1)166167    private var texture: MTLTexture?168    private var loadedTrack: LoadedTrack?169    private var columns = 0170    private var columnsPerSecond: Float = 150171172    init(deck: Int, engine: DJEngine) { self.deck = deck; self.engine = engine }173174    static func rgb(_ hex: UInt32) -> SIMD4<Float> {175        SIMD4(Float((hex >> 16) & 0xFF) / 255, Float((hex >> 8) & 0xFF) / 255, Float(hex & 0xFF) / 255, 1)176    }177178    private func upload(_ track: LoadedTrack?) {179        loadedTrack = track180        texture = nil; columns = 0181        guard let device = Self.device, let wave = track?.waveform, wave.count > 0 else { return }182        let width = Self.textureWidth183        let height = (wave.count + width - 1) / width184        let d = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .rgba8Unorm, width: width, height: height, mipmapped: false)185        d.usage = .shaderRead186        guard let tex = device.makeTexture(descriptor: d) else { return }187        var pixels = [UInt8](repeating: 0, count: width * height * 4)188        for i in 0 ..< wave.count {189            pixels[i * 4] = wave.low[i]; pixels[i * 4 + 1] = wave.mid[i]; pixels[i * 4 + 2] = wave.high[i]; pixels[i * 4 + 3] = wave.peak[i]190        }191        pixels.withUnsafeBytes { tex.replace(region: MTLRegionMake2D(0, 0, width, height), mipmapLevel: 0, withBytes: $0.baseAddress!, bytesPerRow: width * 4) }192        texture = tex; columns = wave.count; columnsPerSecond = Float(wave.columnsPerSecond)193    }194195    func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}196197    func draw(in view: MTKView) {198        guard let engine, let pipeline = Self.pipeline, let queue = Self.queue, let pass = view.currentRenderPassDescriptor, let drawable = view.currentDrawable else { return }199        let status = engine.deckStatus(deck)200        if status.track != loadedTrack || (status.track?.waveform?.count ?? 0) != columns { upload(status.track) }201        var u = WaveformUniforms()202        let size = view.drawableSize203        u.view = SIMD4(Float(size.width), Float(size.height), Float(status.position), Float(secondsVisible / max(1, Double(size.width))))204        u.data = SIMD4(columnsPerSecond, Float(columns), Float(Self.textureWidth), Float(status.track?.duration ?? 0))205        if let grid = status.track?.grid { u.grid = SIMD4(Float(grid.bpm), Float(grid.firstBeatSeconds), Float(grid.downbeatIndex), Float(status.cuePoint)) }206        else { u.grid = SIMD4(0, 0, 0, status.track == nil ? -1 : Float(status.cuePoint)) }207        if let loop = status.loop { u.loop = SIMD4(Float(loop.start), Float(loop.end), loop.active ? 1 : 0, 1) }208        var hot = [Float](repeating: -1, count: 8)209        for (i, h) in status.hotcues.prefix(8).enumerated() { if let h { hot[i] = Float(h) } }210        u.hotA = SIMD4(hot[0], hot[1], hot[2], hot[3]); u.hotB = SIMD4(hot[4], hot[5], hot[6], hot[7])211        let bands = palette.bands212        u.low = Self.rgb(bands.low); u.mid = Self.rgb(bands.mid); u.high = Self.rgb(bands.high)213        u.deck = deckColor214        u.misc = SIMD4(Float(view.window?.backingScaleFactor ?? 2), status.playing ? 1 : 0, 0, 0)215        guard let buffer = queue.makeCommandBuffer(), let encoder = buffer.makeRenderCommandEncoder(descriptor: pass) else { return }216        encoder.setRenderPipelineState(pipeline)217        withUnsafeBytes(of: &u) { encoder.setFragmentBytes($0.baseAddress!, length: MemoryLayout<WaveformUniforms>.stride, index: 0) }218        if let texture { encoder.setFragmentTexture(texture, index: 0) } else if let empty = Self.emptyTexture { encoder.setFragmentTexture(empty, index: 0) }219        encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)220        encoder.endEncoding()221        buffer.present(drawable)222        buffer.commit()223        // Idle decks do not need a continuous redraw.224        view.preferredFramesPerSecond = status.playing ? 60 : 15225    }226227    private static let emptyTexture: MTLTexture? = {228        guard let device else { return nil }229        let d = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .rgba8Unorm, width: 1, height: 1, mipmapped: false)230        let t = device.makeTexture(descriptor: d)231        var zero: UInt32 = 0232        t?.replace(region: MTLRegionMake2D(0, 0, 1, 1), mipmapLevel: 0, withBytes: &zero, bytesPerRow: 4)233        return t234    }()235}236
Files main
Menu

Theme