removed unwanted bar now with waveforms, major updates

This commit is contained in:
Mark Gregory
2026-11-06 10:42:21 +00:00
parent 15c9f5914d
commit f947308421
10 changed files with 695 additions and 105 deletions
+161 -29
View File
@@ -18,9 +18,46 @@ import CoreAudio
var nudgeStep: Double = 0.05
@Published private(set) var cuePosition: Double = 0
@Published private(set) var hotCues: [Double?] = [nil,nil,nil,nil]
@Published private(set) var loopIn: Double?
@Published private(set) var loopOut: Double?
@Published var loopEnabled = false
@Published private(set) var loopIn: Double? { didSet { if loopEnabled { prepareNativeLoop() } } }
@Published private(set) var loopOut: Double? { didSet { if loopEnabled { prepareNativeLoop() } } }
@Published var loopEnabled = false { didSet {
if loopEnabled { prepareNativeLoop() }
else {
loopBufferTask?.cancel();loopBufferTask = nil;loopBuffer = nil
if nativeLoopActive { let position = displayTime;nativeLoopActive = false;schedule(at:position,play:playing) }
}
} }
private var loopBufferTask: Task<Void,Never>?
private var loopBuffer: AVAudioPCMBuffer?
private var preparedLoop: ClosedRange<Double>?
private(set) var nativeLoopActive = false
private func loopPosition(_ position: Double) -> Double {
guard nativeLoopActive,let range = preparedLoop,position >= range.upperBound else { return position }
return range.lowerBound+(position-range.upperBound).truncatingRemainder(dividingBy:range.upperBound-range.lowerBound)
}
private func prepareNativeLoop() {
loopBufferTask?.cancel()
guard loopEnabled,let start = loopIn,let end = loopOut,end > start,let url = audioURL else { return }
// Bound memory for long manual loops; common beat loops stay sample-timed.
guard let file,(end-start)*file.processingFormat.sampleRate*Double(file.processingFormat.channelCount)*4 <= 64*1024*1024 else { return }
let songID = currentSongID
loopBufferTask = Task { @MainActor [weak self] in
let worker = Task.detached(priority:.userInitiated) { () throws -> AVAudioPCMBuffer in
let source = try AVAudioFile(forReading:url)
let first = AVAudioFramePosition(start*source.processingFormat.sampleRate)
let count = AVAudioFrameCount(max(1,min(source.length-first,AVAudioFramePosition((end-start)*source.processingFormat.sampleRate))))
guard let buffer = AVAudioPCMBuffer(pcmFormat:source.processingFormat,frameCapacity:count) else { throw LibraryError.message("Cannot prepare loop audio") }
source.framePosition = first;try source.read(into:buffer,frameCount:count);try Task.checkCancellation();return buffer
}
do {
let buffer = try await withTaskCancellationHandler(operation:{try await worker.value},onCancel:{worker.cancel()})
guard !Task.isCancelled,let self,self.currentSongID == songID,self.loopEnabled,self.loopIn == start,self.loopOut == end else { return }
let position = self.displayTime,resume = self.playing
self.loopBuffer = buffer;self.preparedLoop = start...end
self.schedule(at:position < end ? position : start,play:resume,notifyPlayback:false)
} catch { }
}
}
var isShowScreen: Bool { slideImage != nil }
var canJog: Bool { currentSongID != nil && duration > 0 }
/// Read the audio clock for display frames without publishing the entire player at 60 Hz.
@@ -30,7 +67,7 @@ import CoreAudio
let now = mach_absolute_time()
let elapsed = render.isHostTimeValid && now >= render.hostTime ? min(0.1,AVAudioTime.seconds(forHostTime:now-render.hostTime)) : 0
let position = anchor+Double(current.sampleTime)/file.processingFormat.sampleRate+elapsed*tempo
return min(duration,max(0,position))
return min(duration,max(0,loopPosition(position)))
}
func stopDeck() {
guard canJog else { return }
@@ -45,7 +82,7 @@ import CoreAudio
private func pauseDeck() {
cancelPendingPlayback()
if slideImage != nil,playing { time = min(duration,slideAnchor+ProcessInfo.processInfo.systemUptime-slideStart);slideAnchor = time }
else if let file,playing,let render = node.lastRenderTime,let current = node.playerTime(forNodeTime:render) { time = min(duration,max(0,anchor+Double(current.sampleTime)/file.processingFormat.sampleRate)) }
else if let file,playing,let render = node.lastRenderTime,let current = node.playerTime(forNodeTime:render) { time = min(duration,max(0,loopPosition(anchor+Double(current.sampleTime)/file.processingFormat.sampleRate))) }
node.pause();playing = false;timer?.invalidate();timer = nil
}
func hotCue(_ index: Int,clear: Bool = false) {
@@ -163,6 +200,22 @@ import CoreAudio
@Published private(set) var leftLevel: Float = 0
@Published private(set) var rightLevel: Float = 0
private let tone = AVAudioUnitEQ(numberOfBands:4)
private let graphicEQ = AVAudioUnitEQ(numberOfBands:10)
private let vocalReduction = AVAudioUnitEffect(audioComponentDescription:DJVocalReductionUnit.component)
@Published var vocalReductionEnabled = false { didSet { (vocalReduction.auAudioUnit as? DJVocalReductionUnit)?.setAmount(vocalReductionEnabled ? 1 : 0) } }
private(set) var graphicGains = [Double](repeating:0,count:10)
private(set) var graphicEnabled = false
func applyGraphicEQ(_ gains: [Double],enabled: Bool) {
graphicGains = (0..<10).map { gains.indices.contains($0) && gains[$0].isFinite ? min(12,max(-12,gains[$0])) : 0 }
graphicEnabled = enabled;graphicEQ.bypass = !enabled
let frequencies: [Double] = [31,62,125,250,500,1000,2000,4000,8000,16000]
let rate = file?.processingFormat.sampleRate ?? 44100
for i in 0..<10 {
let band = graphicEQ.bands[i];band.filterType = .parametric
band.frequency = Float(min(rate*0.45,frequencies[i]));band.bandwidth = 1
band.gain = Float(graphicGains[i]);band.bypass = false
}
}
private let meter = AudioLevelMeter()
private var metering = false
@Published private(set) var waveformLoading = false
@@ -174,14 +227,16 @@ import CoreAudio
var sourceAudioURL: URL? { audioURL }
private var waveformTask: Task<Void, Never>?
private var waveformGeneration = UUID()
@Published var playing = false
private let workloadID = UUID()
@Published var playing = false { didSet { if playing != oldValue { WaveformWorkload.shared.setPlaying(playing,id:workloadID) } } }
deinit { WaveformWorkload.shared.setPlaying(false,id:workloadID) }
@Published var time: Double = 0
@Published var duration: Double = 0
@Published var title = "Select a song"
@Published var volume: Double = 0.8 { didSet { engine.mainMixerNode.outputVolume = Float(volume); saveMix() } }
@Published var repeating = false
@Published var pitch: Double = 0 { didSet { effect.pitch = Float(pitch * 100) } }
@Published var tempo: Double = 1 { didSet { effect.rate = Float(tempo) } }
@Published var pitch: Double = 0 { didSet { effect.pitch = Float(pitch * 100);effect.bypass = pitch == 0 && tempo == 1 } }
@Published var tempo: Double = 1 { didSet { effect.rate = Float(tempo);effect.bypass = pitch == 0 && tempo == 1 } }
@Published var graphicsOffset: Double = 0 { didSet { if graphicsOffset != oldValue { invalidateGraphics();render() } } }
private(set) var currentSongID: Int64?
var onFinished: (() -> Void)?
@@ -197,7 +252,7 @@ import CoreAudio
init(waveformCacheDirectory: URL? = nil, persistsMix: Bool = true) {
self.persistsMix = persistsMix
self.waveformCacheDirectory = waveformCacheDirectory ?? FileManager.default.urls(for:.applicationSupportDirectory,in:.userDomainMask)[0].appendingPathComponent("KJamsClone/Waveforms")
engine.attach(node);engine.attach(effect);engine.attach(tone)
engine.attach(node);engine.attach(effect);engine.attach(tone);engine.attach(graphicEQ);engine.attach(vocalReduction);graphicEQ.bypass = true
if persistsMix,let data = try? Data(contentsOf:mixURL),let mix = try? JSONDecoder().decode(GlobalMix.self,from:data) {
volume = min(1,max(0,mix.volume));lowGain = min(12,max(-12,mix.low));midGain = min(12,max(-12,mix.mid));highGain = min(12,max(-12,mix.high));filter = min(1,max(-1,mix.filter));toneEnabled = mix.enabled
}
@@ -219,12 +274,14 @@ import CoreAudio
guard loaded.length > 0 else { throw LibraryError.message("Audio file is empty") }
engine.connect(node,to:effect,format:loaded.processingFormat)
engine.connect(effect,to:tone,format:loaded.processingFormat)
engine.connect(tone,to:engine.mainMixerNode,format:loaded.processingFormat)
engine.connect(tone,to:graphicEQ,format:loaded.processingFormat)
engine.connect(graphicEQ,to:vocalReduction,format:loaded.processingFormat)
engine.connect(vocalReduction,to:engine.mainMixerNode,format:loaded.processingFormat)
file = loaded; cdg = graphics; decoder = CDGDecoder();prepareGraphicsCheckpoints()
duration = Double(loaded.length)/loaded.processingFormat.sampleRate
title = song.title; currentSongID = song.id
pitch = preference.pitch; tempo = preference.tempo
updateTone()
updateTone();applyGraphicEQ(graphicGains,enabled:graphicEnabled)
let meter = self.meter
engine.mainMixerNode.installTap(onBus:0,bufferSize:1024,format:nil) { buffer, _ in meter.consume(buffer) }
metering = true
@@ -289,6 +346,16 @@ import CoreAudio
let start = min(file.length,max(0,AVAudioFramePosition(seconds*rate)))
anchor = Double(start)/rate; time = anchor
guard start < file.length else { playing = false; render(); return }
nativeLoopActive = false
if loopEnabled,let range = preparedLoop,range.lowerBound == loopIn,range.upperBound == loopOut,let loopBuffer,seconds < range.upperBound {
let endFrame = AVAudioFramePosition(range.upperBound*rate)
let remainder = AVAudioFrameCount(max(1,endFrame-start))
node.scheduleSegment(file,startingFrame:start,frameCount:remainder,at:nil)
node.scheduleBuffer(loopBuffer,at:nil,options:.loops)
nativeLoopActive = true
if play { if notifyPlayback { startNodePlayback() } else { node.play() } }
playing = play;render();return
}
node.scheduleSegment(file,startingFrame:start,frameCount:AVAudioFrameCount(min(file.length-start,Int64(UInt32.max))),at:nil,completionCallbackType:.dataPlayedBack) { [weak self] _ in
Task { @MainActor in
guard let self, self.generation == token else { return }
@@ -330,7 +397,7 @@ import CoreAudio
generation = UUID(); timer?.invalidate(); timer = nil; node.stop(); engine.stop()
if metering { engine.mainMixerNode.removeTap(onBus:0); metering = false }
meter.reset(); leftLevel = 0; rightLevel = 0
engine.disconnectNodeOutput(node); engine.disconnectNodeOutput(effect); engine.disconnectNodeOutput(tone)
engine.disconnectNodeOutput(node); engine.disconnectNodeOutput(effect); engine.disconnectNodeOutput(tone);engine.disconnectNodeOutput(graphicEQ);engine.disconnectNodeOutput(vocalReduction)
file = nil; playing = false; time = 0; anchor = 0; duration = 0; image = nil; currentSongID = nil; title = "Select a song"
}
var audibleScrubbing = false
@@ -346,10 +413,7 @@ import CoreAudio
let target = min(duration,max(0,value)),now = ProcessInfo.processInfo.systemUptime
scrubTarget = target
// Do not starve HAL by stopping/restarting the node on every mouse event.
if now-lastScrubAudio < 0.12 {
if !playing { time = target;render() }
return
}
if now-lastScrubAudio < 0.06 { return }
lastScrubAudio = now
if playing { seek(target);return }
scrubPreviewTask?.cancel()
@@ -429,9 +493,9 @@ import CoreAudio
let left = playing ? levels.0 : 0,right = playing ? levels.1 : 0
if leftLevel != left { leftLevel = left };if rightLevel != right { rightLevel = right }
if playing, let file, let renderTime = node.lastRenderTime, let playerTime = node.playerTime(forNodeTime:renderTime) {
let next = min(duration,max(0,anchor+Double(playerTime.sampleTime)/file.processingFormat.sampleRate))
let next = min(duration,max(0,loopPosition(anchor+Double(playerTime.sampleTime)/file.processingFormat.sampleRate)))
if abs(time-next) > 0.0001 { time = next }
if loopEnabled,let loopIn,let loopOut,time >= loopOut { schedule(at:loopIn,play:true);return }
if !nativeLoopActive,loopEnabled,let loopIn,let loopOut,time >= loopOut { schedule(at:loopIn,play:true);return }
}
if !fading { setFade(autoFade && fadeSeconds > 0 && duration > 0 ? min(1,max(0,(duration-time)/max(0.5,tempo)/fadeSeconds)) : 1) }
render()
@@ -461,6 +525,8 @@ enum AudioWaveform {
}
static func analyse(url: URL, bins: Int = 4096, cancelled: () -> Bool = { false }) throws -> Analysis {
guard bins > 0 else { return .init(levels:[],bands:[]) }
try WaveformWorkload.shared.acquire(cancelled:cancelled)
defer { WaveformWorkload.shared.release() }
let file = try AVAudioFile(forReading: url, commonFormat: .pcmFormatFloat32, interleaved: false)
guard file.length > 0, let buffer = AVAudioPCMBuffer(pcmFormat: file.processingFormat, frameCapacity: 16384) else { return .init(levels:[],bands:[]) }
var energy = [Double](repeating: 0, count: bins)
@@ -473,23 +539,37 @@ enum AudioWaveform {
var lows = [Double](repeating:0,count:bins), mids = lows, highs = lows
while position < file.length {
if cancelled() { throw CancellationError() }
if WaveformWorkload.shared.playbackActive { Thread.sleep(forTimeInterval:0.001) }
try file.read(into: buffer, frameCount: AVAudioFrameCount(min(16384, file.length-position)))
let frames = Int(buffer.frameLength)
guard frames > 0, let data = buffer.floatChannelData else { break }
for frame in 0..<frames {
let bin = min(bins-1, Int((position+Int64(frame))*Int64(bins)/file.length))
var sum: Double = 0
// Split at bin boundaries once, then process contiguous channel samples.
// This removes per-sample integer division and array indexing of filter states.
var offset = 0
while offset < frames {
let absolute = position+Int64(offset)
let bin = min(bins-1,Int(absolute*Int64(bins)/file.length))
let boundary = ((Int64(bin+1)*file.length)+Int64(bins)-1)/Int64(bins)
let end = min(frames,max(offset+1,Int(boundary-position)))
var total = 0.0,lowEnergy = 0.0,midEnergy = 0.0,highEnergy = 0.0
for channel in 0..<channels {
let sample = Double(data[channel][frame])
if sample.isFinite {
sum += sample * sample
lowState[channel] = lowAlpha*lowState[channel]+(1-lowAlpha)*sample
highState[channel] = highAlpha*highState[channel]+(1-highAlpha)*sample
let low = lowState[channel], mid = highState[channel]-low, high = sample-highState[channel]
lows[bin] += low*low; mids[bin] += mid*mid; highs[bin] += high*high
let samples = data[channel]
var low = lowState[channel],high = highState[channel]
for frame in offset..<end {
let sample = Double(samples[frame])
if sample.isFinite {
total += sample*sample
low = lowAlpha*low+(1-lowAlpha)*sample
high = highAlpha*high+(1-highAlpha)*sample
let mid = high-low,treble = sample-high
lowEnergy += low*low;midEnergy += mid*mid;highEnergy += treble*treble
}
}
lowState[channel] = low;highState[channel] = high
}
energy[bin] += sum / Double(max(channels,1)); counts[bin] += 1
energy[bin] += total/Double(max(channels,1));counts[bin] += end-offset
lows[bin] += lowEnergy;mids[bin] += midEnergy;highs[bin] += highEnergy
offset = end
}
position += Int64(frames)
}
@@ -568,3 +648,55 @@ enum WaveformCache {
}
}
/// Stereo centre cancellation, with a non-blocking control read on the render thread.
private final class DJVocalControl: @unchecked Sendable {
private let lock = NSLock()
private var amount: Float = 0
func set(_ value: Float) { lock.lock();amount = value;lock.unlock() }
func read() -> Float { guard lock.try() else { return 0 };defer { lock.unlock() };return amount }
}
final class DJVocalReductionUnit: AUAudioUnit {
private var input: AUAudioUnitBus!
private var output: AUAudioUnitBus!
private var inputs: AUAudioUnitBusArray!
private var outputs: AUAudioUnitBusArray!
private let control = DJVocalControl()
static let component: AudioComponentDescription = {
let description = AudioComponentDescription(componentType:kAudioUnitType_Effect,componentSubType:0x6B6A7672,componentManufacturer:0x4B4A4D58,componentFlags:0,componentFlagsMask:0)
AUAudioUnit.registerSubclass(DJVocalReductionUnit.self,as:description,name:"KJMix: Centre Vocal Reduction",version:1)
return description
}()
override init(componentDescription: AudioComponentDescription,options: AudioComponentInstantiationOptions = []) throws {
try super.init(componentDescription:componentDescription,options:options)
let format = AVAudioFormat(standardFormatWithSampleRate:44100,channels:2)!
input = try AUAudioUnitBus(format:format);output = try AUAudioUnitBus(format:format)
inputs = AUAudioUnitBusArray(audioUnit:self,busType:.input,busses:[input])
outputs = AUAudioUnitBusArray(audioUnit:self,busType:.output,busses:[output])
maximumFramesToRender = 4096
}
override var inputBusses: AUAudioUnitBusArray { inputs }
override var outputBusses: AUAudioUnitBusArray { outputs }
func setAmount(_ value: Float) { control.set(min(1,max(0,value))) }
static func reduce(left: Float,right: Float,amount: Float) -> (Float,Float) {
let side = (left-right)*0.5
return (left*(1-amount)+side*amount,right*(1-amount)+side*amount)
}
override var internalRenderBlock: AUInternalRenderBlock {
let control = self.control
return { flags,time,frames,_,data,_,pull in
guard let pull else { return kAudioUnitErr_NoConnection }
let result = pull(flags,time,frames,0,data)
guard result == noErr else { return result }
let amount = control.read()
guard amount > 0 else { return noErr }
let buffers = UnsafeMutableAudioBufferListPointer(data)
if buffers.count == 2,let l = buffers[0].mData?.assumingMemoryBound(to:Float.self),let r = buffers[1].mData?.assumingMemoryBound(to:Float.self) {
for i in 0..<Int(frames) { let value = DJVocalReductionUnit.reduce(left:l[i],right:r[i],amount:amount);l[i] = value.0;r[i] = value.1 }
} else if buffers.count == 1,buffers[0].mNumberChannels == 2,let values = buffers[0].mData?.assumingMemoryBound(to:Float.self) {
for i in 0..<Int(frames) { let value = DJVocalReductionUnit.reduce(left:values[i*2],right:values[i*2+1],amount:amount);values[i*2] = value.0;values[i*2+1] = value.1 }
}
return noErr
}
}
}