Swiftpack.co - Package - benoit-pereira-da-silva/SoundWaveForm

Swift 5 Platform Carthage compatible

SoundWaveForm

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.

It supports

  • 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

Screen Shots

MacDown Screenshot MacDown Screenshot MacDown Screenshot

Usage sample

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)")
        }
)


How to extract sample from a specified timeRange?

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
				}
		)
  
}

Installation

  • 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/

Inspiration

This project has been largely inspired by FDWaveformView and DSWaveformImage. Thanks to William aka @fulldecent and Daniel @dmrschmidt.

Github

link
Stars: 104

Dependencies

Used By

Total: 0

Releases

Swift Package Manager support (with retro compatibility) -

Swift Package Manager support (with retro compatibility)

SPM -

v4.0.2 -

Swift 5

Version 3.0.2 -

Added an identifier to allow to discriminate the consumer when multiple consumers are invoking the sample extractor. This change is source breaking. You must add the ìdentifier`to the success and failure closures.

v3.0.0 -

This version uses call back to allow to support distant files

v2.0.3 -

The framework is named SoundWaveForm on any target

v2.0.2 Swift4 -

We have deprecated the swift 3 support. If necessary use the tag v2.0.1