@@ -151,8 +151,17 @@ final public class FDWaveformRenderOperation: Operation {
151
151
let reader = try ? AVAssetReader ( asset: audioContext. asset)
152
152
else { return nil }
153
153
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) )
156
165
let outputSettingsDict : [ String : Any ] = [
157
166
AVFormatIDKey: Int ( kAudioFormatLinearPCM) ,
158
167
AVLinearPCMBitDepthKey: 16 ,
@@ -165,13 +174,6 @@ final public class FDWaveformRenderOperation: Operation {
165
174
readerOutput. alwaysCopiesSampleData = false
166
175
reader. add ( readerOutput)
167
176
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
-
175
177
var sampleMax = format. type. floorValue
176
178
let samplesPerPixel = max ( 1 , channelCount * slice. count / targetSamples)
177
179
let filter = [ Float] ( repeating: 1.0 / Float( samplesPerPixel) , count: samplesPerPixel)
0 commit comments