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

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by benoit-pereira-da-silva.
benoit-pereira-da-silva/SoundWaveForm v4.0.6
Generate WaveForms Images from Sounds and Videos on macOS and iOS (Swift 5.x)
⭐️ 159
🕓 3 years ago
.package(url: "https://github.com/benoit-pereira-da-silva/SoundWaveForm.git", from: "v4.0.6")

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: 159
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Swift Package Manager support (with retro compatibility)
3 years ago

Swift Package Manager support (with retro compatibility)

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics