@@ -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