Skip to content

Commit

Permalink
Merge branch 'master' of github.com:fulldecent/FDWaveformView
Browse files Browse the repository at this point in the history
  • Loading branch information
fulldecent committed Feb 12, 2020
2 parents b839177 + 2d1af2e commit 8ddf610
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Released on 2020-02-12.
#### Changed
- Switch to new standard library clamp functions
- Added by [William Entriken](https://github.com/fulldecent)
- Fixed timescale bug for some mp4 files
- Added by [Doug Earnshaw](https://github.com/pixlwave)

---

Expand Down
20 changes: 11 additions & 9 deletions Sources/FDWaveformView/FDWaveformRenderOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,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: 16,
Expand All @@ -165,13 +174,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)
}

var sampleMax = format.type.floorValue
let samplesPerPixel = max(1, channelCount * slice.count / targetSamples)
let filter = [Float](repeating: 1.0 / Float(samplesPerPixel), count: samplesPerPixel)
Expand Down

0 comments on commit 8ddf610

Please sign in to comment.