Learn more about reversing audio files from our in-depth blog post.
The associated Xcode project implements a SwiftUI app for macOS and iOS that presents a list of audio files included in the bundle resources subdirectory 'Audio Files'.
Add your own audio files or use the sample set provided.
Each file in the list has an adjacent button to either play or reverse the audio.
The project is comprised of:
- The App (
ReverseAudioApp
) that displays a list of audio files in the project. - And an ObservableObject (
ReverseAudioObservable
) that manages the user interaction to reverse and play audio files in the list. - The AVFoundation code (
ReverseAudio
) that reads, reverses and writes audio files.
Reversing audio is performed in 3 steps using AVFoundation:
- Read the audio samples of a file into an
Array
of[Int16]
and reverse it - Create an array of sample buffers [CMSampleBuffer] for the array of reversed audio samples
- Write the reversed sample buffers in [CMSampleBuffer] to a file
The top level method that implements all of this, and is employed by the ReverseAudioObservable
is:
func reverseAudio(asset:AVAsset, destinationURL:URL, progress: @escaping (Float) -> (), completion: @escaping (Bool, String?) -> ())