Skip to content

Commit 8ddf610

Browse files
committed
Merge branch 'master' of github.com:fulldecent/FDWaveformView
2 parents b839177 + 2d1af2e commit 8ddf610

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Released on 2020-02-12.
1616
#### Changed
1717
- Switch to new standard library clamp functions
1818
- Added by [William Entriken](https://github.com/fulldecent)
19+
- Fixed timescale bug for some mp4 files
20+
- Added by [Doug Earnshaw](https://github.com/pixlwave)
1921

2022
---
2123

Sources/FDWaveformView/FDWaveformRenderOperation.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,17 @@ final public class FDWaveformRenderOperation: Operation {
151151
let reader = try? AVAssetReader(asset: audioContext.asset)
152152
else { return nil }
153153

154-
reader.timeRange = CMTimeRange(start: CMTime(value: Int64(slice.lowerBound), timescale: audioContext.asset.duration.timescale),
155-
duration: CMTime(value: Int64(slice.count), timescale: audioContext.asset.duration.timescale))
154+
var channelCount = 1
155+
var sampleRate: CMTimeScale = 44100
156+
let formatDescriptions = audioContext.assetTrack.formatDescriptions as! [CMAudioFormatDescription]
157+
for item in formatDescriptions {
158+
guard let fmtDesc = CMAudioFormatDescriptionGetStreamBasicDescription(item) else { return nil }
159+
channelCount = Int(fmtDesc.pointee.mChannelsPerFrame)
160+
sampleRate = Int32(fmtDesc.pointee.mSampleRate)
161+
}
162+
163+
reader.timeRange = CMTimeRange(start: CMTime(value: Int64(slice.lowerBound), timescale: sampleRate),
164+
duration: CMTime(value: Int64(slice.count), timescale: sampleRate))
156165
let outputSettingsDict: [String : Any] = [
157166
AVFormatIDKey: Int(kAudioFormatLinearPCM),
158167
AVLinearPCMBitDepthKey: 16,
@@ -165,13 +174,6 @@ final public class FDWaveformRenderOperation: Operation {
165174
readerOutput.alwaysCopiesSampleData = false
166175
reader.add(readerOutput)
167176

168-
var channelCount = 1
169-
let formatDescriptions = audioContext.assetTrack.formatDescriptions as! [CMAudioFormatDescription]
170-
for item in formatDescriptions {
171-
guard let fmtDesc = CMAudioFormatDescriptionGetStreamBasicDescription(item) else { return nil }
172-
channelCount = Int(fmtDesc.pointee.mChannelsPerFrame)
173-
}
174-
175177
var sampleMax = format.type.floorValue
176178
let samplesPerPixel = max(1, channelCount * slice.count / targetSamples)
177179
let filter = [Float](repeating: 1.0 / Float(samplesPerPixel), count: samplesPerPixel)

0 commit comments

Comments
 (0)