Allows to extract sound samples from Video or Sounds files very efficiently (it relies on the Accelerate framework). SoundWaveForm expose an optimized cross platform drawing that renders the waveform into an Image.
- macOS 10.11 & +
- iOS 8 & +
- swift 5.x
- if you need to support swift 4 use v3.0.2
- if you need to support swift 3 use the v2.0.1
The framework is composed of a SamplesExtractor and a WaveFormDrawer.
// Configure the drawings
let configuration = WaveformConfiguration( size: waveFormView.bounds.size,
backgroundColor: WaveColor.lightGray,
color: WaveColor.red,
style: .striped,
position: .middle,
scale: 1)
// Extract the downsampled samples
// Proceed to extraction
SamplesExtractor.samples(audioTrack: track,
timeRange: nil,
desiredNumberOfSamples: 500,
onSuccess:{ samples, sampleMax, id in
// Let's display the waveform in a view
self.waveFormView.image = WaveFormDrawer.image(from: samples, with: configuration)
},
onFailure: { error, id in
... // Handle the error e.g: print("\(id ?? "") \(error)")
}
)
You can define AVAssetReader.timeRange.
let asset = AVURLAsset(url: url)
let audioTracks:[AVAssetTrack] = asset.tracks(withMediaType: AVMediaTypeAudio)
if let track:AVAssetTrack = audioTracks.first{
// Define the timeRange from second 1 to second 10
let startTime = CMTime(seconds: 1, preferredTimescale: 1000)
let endTime = CMTime(seconds: 10, preferredTimescale: 1000)
let timeRange = CMTimeRangeMake(startTime, endTime)
// Proceed to extraction (refer to previous code)
SamplesExtractor.samples(audioTrack: track,
timeRange:timeRange,
desiredNumberOfSamples: 500,
onSuccess:{ samples, sampleMax, id in
... // Proceeed
},
onFailure: { error, id in
... // Handle the error
}
)
}
- Via SPM: add
https://github.com/benoit-pereira-da-silva/SoundWaveForm
- Via Carthage: Add to your Cartfile
github "benoit-pereira-da-silva/SoundWaveForm"
- Copy the two source files :
Sources/SoundWaveForm/
This project has been largely inspired by FDWaveformView and DSWaveformImage. Thanks to William aka @fulldecent and Daniel @dmrschmidt.