Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 55 additions & 43 deletions Audio/Engine/Facade/WaveformScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public final class WaveformScanner {
return accumulator.finalWaveform()
}

/// After one converted block: next block, no frames left, or the segment's end reached.
private enum BlockStep {
case next, stop, done
}

/// What the decoder said of the track.
private struct Track {
let rate: Double
Expand Down Expand Up @@ -179,56 +184,63 @@ public final class WaveformScanner {
chunk.lossless = track.lossless
converter.add(chunk)
while !converter.isEmpty(), !cancelled.isSet {
let floats = converter.removeSamples(asFloat32: 4096)
let frames = floats.frameCount()
guard frames > 0 else { break }
let format = floats.format
let channels = Int(format.mChannelsPerFrame)
let data = floats.removeSamples(frames)

if framesPerBucket == 0 {
let rate = format.mSampleRate
let scale = track.rate > 0 ? rate / track.rate : 1
position = Double(start) * scale
if track.totalFrames > 0 {
let total = Double(track.totalFrames) * scale
framesPerBucket = accumulator.isProgressive ? total / Double(Waveform.bucketCount) : Double(Self.unknownLengthBlock)
stop = end.map { Double($0) * scale } ?? total * 1.01 + rate
} else {
framesPerBucket = Double(Self.unknownLengthBlock)
stop = Self.unknownLengthLimit * rate
// The block's temporaries (the float chunk, its data) go per block: drained only per readAudio, a
// decoder that hands over small chunks piled up the whole track here (hundreds of MB)
let step = autoreleasepool { () -> BlockStep in
let floats = converter.removeSamples(asFloat32: 4096)
let frames = floats.frameCount()
guard frames > 0 else { return .stop }
let format = floats.format
let channels = Int(format.mChannelsPerFrame)
let data = floats.removeSamples(frames)

if framesPerBucket == 0 {
let rate = format.mSampleRate
let scale = track.rate > 0 ? rate / track.rate : 1
position = Double(start) * scale
if track.totalFrames > 0 {
let total = Double(track.totalFrames) * scale
framesPerBucket = accumulator.isProgressive ? total / Double(Waveform.bucketCount) : Double(Self.unknownLengthBlock)
stop = end.map { Double($0) * scale } ?? total * 1.01 + rate
} else {
framesPerBucket = Double(Self.unknownLengthBlock)
stop = Self.unknownLengthLimit * rate
}
}
}

if mono.count < frames {
mono = [Float](repeating: 0, count: frames)
}
if channels > 1 {
if downmix == nil || downmixFormat.map({ !Self.sameFormat($0, format) }) ?? true {
let config = floats.channelConfig != 0 ? floats.channelConfig : AudioChunk.guessChannelConfig(UInt32(channels))
downmix = DownmixProcessor(inputFormat: Self.floatFormat(rate: format.mSampleRate, channels: channels), inputConfig: config,
andOutputFormat: Self.floatFormat(rate: format.mSampleRate, channels: 1), outputConfig: UInt32(AudioChannelFrontCenter))
downmixFormat = format
if mono.count < frames {
mono = [Float](repeating: 0, count: frames)
}
data.withUnsafeBytes { input in
mono.withUnsafeMutableBufferPointer { output in
downmix?.process(input.baseAddress!, frameCount: frames, output: output.baseAddress!)
if channels > 1 {
if downmix == nil || downmixFormat.map({ !Self.sameFormat($0, format) }) ?? true {
let config = floats.channelConfig != 0 ? floats.channelConfig : AudioChunk.guessChannelConfig(UInt32(channels))
downmix = DownmixProcessor(inputFormat: Self.floatFormat(rate: format.mSampleRate, channels: channels), inputConfig: config,
andOutputFormat: Self.floatFormat(rate: format.mSampleRate, channels: 1), outputConfig: UInt32(AudioChannelFrontCenter))
downmixFormat = format
}
}
} else {
data.withUnsafeBytes { input in
mono.withUnsafeMutableBytes { output in
output.copyMemory(from: UnsafeRawBufferPointer(rebasing: input.prefix(frames * MemoryLayout<Float>.size)))
data.withUnsafeBytes { input in
mono.withUnsafeMutableBufferPointer { output in
downmix?.process(input.baseAddress!, frameCount: frames, output: output.baseAddress!)
}
}
} else {
data.withUnsafeBytes { input in
mono.withUnsafeMutableBytes { output in
output.copyMemory(from: UnsafeRawBufferPointer(rebasing: input.prefix(frames * MemoryLayout<Float>.size)))
}
}
}
}

// Not past the segment's end, which the next segment reads.
let counted = min(frames, max(0, Int((stop - position).rounded(.up))))
mono.withUnsafeBufferPointer { accumulator.add(UnsafeBufferPointer(rebasing: $0.prefix(counted)), at: Int(position), framesPerBucket: framesPerBucket) }
position += Double(frames)
reporter.tick()
if position >= stop { return true }
// Not past the segment's end, which the next segment reads.
let counted = min(frames, max(0, Int((stop - position).rounded(.up))))
mono.withUnsafeBufferPointer { accumulator.add(UnsafeBufferPointer(rebasing: $0.prefix(counted)), at: Int(position), framesPerBucket: framesPerBucket) }
position += Double(frames)
reporter.tick()
if position >= stop { return .done }
return .next
}
if step == .stop { break }
if step == .done { return true }
}
}
return false
Expand Down
4 changes: 3 additions & 1 deletion Audio/Engine/Feeder/Feeder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ public final class Feeder {
}
let decodeMs = EngineLog.milliseconds(since: start)
if let chunk, chunk.frameCount() > 0 {
feed(chunk)
// The conversion's temporaries (float chunks, their data) go per chunk: this thread has no other
// pool, so outside one they lived as long as the thread (about 1 MB/s with a small-chunk decoder)
autoreleasepool { feed(chunk) }
} else {
advance()
}
Expand Down
4 changes: 3 additions & 1 deletion Audio/Engine/Pump/Pump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ public final class Pump {
while isRunning {
let start = EngineLog.now()
let gap = EngineLog.milliseconds(since: last)
let worked = pass()
// One pool per pass: this thread has no other, so whatever a pass (or a DSP stage) autoreleases would
// otherwise live as long as playback
let worked = autoreleasepool { pass() }
let took = EngineLog.milliseconds(since: start)
if gap > EngineLog.slowPass * 1000 || took > EngineLog.slowPass * 1000 {
let shallowMs = Double(cog_ring_readable(ring)) / outputFormat.sampleRate * 1000
Expand Down