Skip to content

Generate WaveForms Images from Sounds and Videos on macOS and iOS (Swift 4.x)

License

Notifications You must be signed in to change notification settings

StreetVoice/SoundWaveForm

 
 

Repository files navigation

Swift 4 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 4.x (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 Carthage

Add to your Cartfile : github "benoit-pereira-da-silva/SoundWaveForm"

Inspiration

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

About

Generate WaveForms Images from Sounds and Videos on macOS and iOS (Swift 4.x)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 95.3%
  • Ruby 2.8%
  • Objective-C 1.9%