DSWaveformImage offers a few interfaces for the purpose of drawing the envelope waveform of audio data in iOS. To do so, you can use
WaveformImageDrawer
to generate a waveformUIImage
from an audio fileWaveformImageView
to render a static waveform from an audio file orWaveformLiveView
to realtime render a waveform of live audio data (e.g. fromAVAudioRecorder
)
Additionally, you can get a waveform's (normalized) [Float]
samples directly as well by
creating an instance of WaveformAnalyzer
.
You may also find the following iOS controls written in Swift interesting:
- SwiftColorWheel - a delightful color picker
- QRCode - a customizable QR code generator
- use SPM: add
https://github.com/dmrschmidt/DSWaveformImage
and set "Up to Next Major" with "9.0.0"
Deprecated or discouraged but still possible alternative ways for older apps:
- since it has no other dependencies you may simply copy the
DSWaveformImage
folder directly into your project - use carthage:
github "dmrschmidt/DSWaveformImage" ~> 7.0
- or, sunset since 6.1.1:
use cocoapods:pod 'DSWaveformImage', '~> 6.1'
Note: Calculations are always performed and returned on a background thread, so make sure to return to the main thread before doing any UI work.
Check Waveform.Configuration
in WaveformImageTypes for various configuration options.
let waveformImageDrawer = WaveformImageDrawer()
let audioURL = Bundle.main.url(forResource: "example_sound", withExtension: "m4a")!
waveformImageDrawer.waveformImage(fromAudioAt: audioURL, with: .init(
size: topWaveformView.bounds.size,
style: .filled(UIColor.black),
position: .top) { image in
// need to jump back to main queue
DispatchQueue.main.async {
self.topWaveformView.image = image
}
}
let audioURL = Bundle.main.url(forResource: "example_sound", withExtension: "m4a")!
waveformImageView = WaveformImageView(frame: CGRect(x: 0, y: 0, width: 500, height: 300)
waveformImageView.waveformAudioURL = audioURL
Find a full example in the sample project's RecordingViewController.
let waveformView = WaveformLiveView()
// configure and start AVAudioRecorder
let recorder = AVAudioRecorder()
recorder.isMeteringEnabled = true // required to get current power levels
// after all the other recording (omitted for focus) setup, periodically (every 20ms or so):
recorder.updateMeters() // gets the current value
let currentAmplitude = 1 - pow(10, recorder.averagePower(forChannel: 0) / 20)
waveformView.add(sample: currentAmplitude)
let audioURL = Bundle.main.url(forResource: "example_sound", withExtension: "m4a")!
waveformAnalyzer = WaveformAnalyzer(audioAssetURL: audioURL)
waveformAnalyzer.samples(count: 200) { samples in
print("so many samples: \(samples)")
}
If you're playing back audio files and would like to indicate the playback progress to your users, you can find inspiration in this ticket.
There's various other ways of course, depending on your use case and design. Using WaveformLiveView
may be another one in conjunction with AVAudioPlayer
(note that AVPlayer
does not offer
the same simple access to its audio metering data, so is not as suitable out of the box).
For one example way to display waveforms for audio files on remote URLs see dmrschmidt#22.
Waveforms can be rendered in 3 different styles: .filled
, .gradient
and
.striped
.
Similarly, there are 3 positions relative to the canvas, .top
, .middle
and .bottom
.
The effect of each of those can be seen here:
live-recording.mov
In 9.0.0 a few public API's have been slightly changed to be more concise. All types have also been grouped under the Waveform
enum-namespace. Meaning WaveformConfiguration
for instance has become Waveform.Configuration
and so on.
In 7.0.0 colors have moved into associated values on the respective style
enum.
Waveform
and the UIImage
category have been removed in 6.0.0 to simplify the API.
See Usage
for current usage.
SoundCard lets you send postcards with audio messages.
DSWaveformImage is used to draw the waveforms of the audio messages on postcards sent by SoundCard.
Check it out on the App Store.