Skip to content

Commit

Permalink
Fix waveform rendering for some assets derived from video files.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave committed Feb 11, 2020
1 parent 281d6f0 commit b45c381
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions FDWaveformView/FDWaveformRenderOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,17 @@ final public class FDWaveformRenderOperation: Operation {
let reader = try? AVAssetReader(asset: audioContext.asset)
else { return nil }

reader.timeRange = CMTimeRange(start: CMTime(value: Int64(slice.lowerBound), timescale: audioContext.asset.duration.timescale),
duration: CMTime(value: Int64(slice.count), timescale: audioContext.asset.duration.timescale))
var channelCount = 1
var sampleRate: CMTimeScale = 44100
let formatDescriptions = audioContext.assetTrack.formatDescriptions as! [CMAudioFormatDescription]
for item in formatDescriptions {
guard let fmtDesc = CMAudioFormatDescriptionGetStreamBasicDescription(item) else { return nil }
channelCount = Int(fmtDesc.pointee.mChannelsPerFrame)
sampleRate = Int32(fmtDesc.pointee.mSampleRate)
}

reader.timeRange = CMTimeRange(start: CMTime(value: Int64(slice.lowerBound), timescale: sampleRate),
duration: CMTime(value: Int64(slice.count), timescale: sampleRate))
let outputSettingsDict: [String : Any] = [
AVFormatIDKey: Int(kAudioFormatLinearPCM),
AVLinearPCMBitDepthKey: 32,
Expand All @@ -164,13 +173,6 @@ final public class FDWaveformRenderOperation: Operation {
readerOutput.alwaysCopiesSampleData = false
reader.add(readerOutput)

var channelCount = 1
let formatDescriptions = audioContext.assetTrack.formatDescriptions as! [CMAudioFormatDescription]
for item in formatDescriptions {
guard let fmtDesc = CMAudioFormatDescriptionGetStreamBasicDescription(item) else { return nil }
channelCount = Int(fmtDesc.pointee.mChannelsPerFrame)
}

let samplesPerPixel = max(1, channelCount * slice.count / targetSamples)
var outputSamples = [WaveformSample]()

Expand Down

0 comments on commit b45c381

Please sign in to comment.