removed unwanted bar now with waveforms, major updates and enhancements
This commit is contained in:
+37
-10
@@ -68,19 +68,43 @@ import CoreAudio
|
||||
generation = UUID();let token = generation
|
||||
nativeLoopActive = false;pendingLoopSwitch = nil;scheduledLoop = nil;scheduledRawLoop = nil
|
||||
// Interrupt only the scheduled loop, leaving the running node and its clock intact.
|
||||
node.scheduleBuffer(bridge,at:nil,options:.interrupts,completionHandler:nil)
|
||||
let next = min(file.length,AVAudioFramePosition(position*rate)+AVAudioFramePosition(count))
|
||||
if next < file.length {
|
||||
node.scheduleSegment(file,startingFrame:next,frameCount:AVAudioFrameCount(min(file.length-next,Int64(UInt32.max))),at:nil,completionCallbackType:.dataPlayedBack) { [weak self] _ in
|
||||
Task { @MainActor in
|
||||
guard let self,self.generation == token else { return }
|
||||
if self.repeating { self.schedule(at:0,play:true) }
|
||||
else { self.playing = false;self.time = self.duration;self.timer?.invalidate();self.timer = nil;self.render();self.onFinished?() }
|
||||
}
|
||||
let finished: AVAudioPlayerNodeCompletionHandler = { [weak self] _ in
|
||||
Task { @MainActor in
|
||||
guard let self,self.generation == token else { return }
|
||||
if self.repeating { self.schedule(at:0,play:true) }
|
||||
else { self.playing = false;self.time = self.duration;self.timer?.invalidate();self.timer = nil;self.render();self.onFinished?() }
|
||||
}
|
||||
}
|
||||
if next < file.length {
|
||||
node.scheduleBuffer(bridge,at:nil,options:.interrupts,completionHandler:nil)
|
||||
node.scheduleSegment(file,startingFrame:next,frameCount:AVAudioFrameCount(min(file.length-next,Int64(UInt32.max))),at:nil,completionCallbackType:.dataPlayedBack,completionHandler:finished)
|
||||
} else {
|
||||
node.scheduleBuffer(bridge,at:nil,options:.interrupts,completionCallbackType:.dataPlayedBack,completionHandler:finished)
|
||||
}
|
||||
startTimer();self.render();return true
|
||||
}
|
||||
private func activateNativeLoopWithoutRestart() -> Bool {
|
||||
guard playing,let file,let range = preparedLoop,let raw = preparedRawLoop,let buffer = loopBuffer,
|
||||
let renderTime = node.lastRenderTime,let clock = node.playerTime(forNodeTime:renderTime),
|
||||
let source = buffer.floatChannelData,!raw.format.isInterleaved else { return false }
|
||||
let current = displayTime
|
||||
guard current >= range.lowerBound else { return false } // Keep playing toward a future loop.
|
||||
let position = current < range.upperBound ? current : range.lowerBound,rate = file.processingFormat.sampleRate
|
||||
let offset = max(0,min(Int(raw.frameLength)-1,Int((position-range.lowerBound)*rate)))
|
||||
let count = Int(raw.frameLength)-offset
|
||||
guard count > 0,let bridge = AVAudioPCMBuffer(pcmFormat:raw.format,frameCapacity:AVAudioFrameCount(count)),let output = bridge.floatChannelData else { return false }
|
||||
bridge.frameLength = AVAudioFrameCount(count)
|
||||
for channel in 0..<Int(raw.format.channelCount) { memcpy(output[channel],source[channel].advanced(by:offset),count*MemoryLayout<Float>.size) }
|
||||
let now = mach_absolute_time(),elapsed = renderTime.isHostTimeValid && now >= renderTime.hostTime ? min(0.1,AVAudioTime.seconds(forHostTime:now-renderTime.hostTime))*tempo : 0
|
||||
anchor = position-Double(clock.sampleTime)/rate-elapsed;time = position
|
||||
generation = UUID();pendingLoopSwitch = nil;scheduledLoop = range;scheduledRawLoop = raw;nativeLoopActive = true
|
||||
// Replace the remaining file command, not the running player. The bridge
|
||||
// continues the current pass; the prepared buffer then repeats in audio time.
|
||||
node.scheduleBuffer(bridge,at:nil,options:.interrupts,completionHandler:nil)
|
||||
node.scheduleBuffer(buffer,at:nil,options:.loops,completionHandler:nil)
|
||||
render();return true
|
||||
}
|
||||
private func prepareNativeLoop() {
|
||||
loopBufferTask?.cancel()
|
||||
guard loopEnabled,let start = loopIn,let end = loopOut,end > start,let url = audioURL else { return }
|
||||
@@ -127,9 +151,11 @@ import CoreAudio
|
||||
let remaining = max(0,activeRange.upperBound-self.loopPosition(raw))
|
||||
self.node.scheduleBuffer(buffer,at:nil,options:[.loops,.interruptsAtLoop],completionHandler:nil)
|
||||
self.pendingLoopSwitch = (raw+remaining,start...end,self.preparedRawLoop!)
|
||||
} else if resume {
|
||||
_ = self.activateNativeLoopWithoutRestart()
|
||||
} else {
|
||||
self.loopAudioRestartCount += 1
|
||||
self.schedule(at:position < end ? position : start,play:resume,notifyPlayback:false)
|
||||
self.schedule(at:position < end ? position : start,play:false,notifyPlayback:false)
|
||||
}
|
||||
} catch { }
|
||||
}
|
||||
@@ -425,7 +451,7 @@ 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;pendingLoopSwitch = nil;scheduledLoop = nil
|
||||
nativeLoopActive = false;pendingLoopSwitch = nil;scheduledLoop = nil;scheduledRawLoop = nil
|
||||
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))
|
||||
@@ -574,6 +600,7 @@ import CoreAudio
|
||||
if playing, let file, let renderTime = node.lastRenderTime, let playerTime = node.playerTime(forNodeTime:renderTime) {
|
||||
let next = min(duration,max(0,loopPosition(anchor+Double(playerTime.sampleTime)/file.processingFormat.sampleRate)))
|
||||
if abs(time-next) > 0.0001 { time = next }
|
||||
if !nativeLoopActive,loopEnabled,loopBuffer != nil { _ = activateNativeLoopWithoutRestart() }
|
||||
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) }
|
||||
|
||||
Reference in New Issue
Block a user