Audio
class Audio
A singleton class to help abstract function calls and methods for setting up different AVFoundation components.
-
The engine that is used for all audio effects.
AVAudioEngineallows for real time audio editing which let’s the programmer tap the microphone for input then forward all data through different effect chains. Finally, the output of these effect chains can be mixed together throughaudioMixerNode.Declaration
Swift
var audioEngine: AVAudioEngine = AVAudioEngine() -
This is used if the user recorded on the previous screen. It is set up such that the audio plays in a faux-loop fasion. Rather than using the .loop feature which would repeat an audio buffer endlessly, audio files are scheduled such that when the file ends, UIElemnts are modified before the next audio file gets queued.
Declaration
Swift
var audioPlayerNode: AVAudioPlayerNode! -
This is used if the user skipped recording on the previous screen. It uses the
audioEngineto tap input from the mic and send it to thisAVAudioSessionto play through the speakers after being manipulated for effects.Declaration
Swift
var audioSession: AVAudioSession! -
The equalizer for the 0th channel.
Declaration
Swift
var leftEqualizer: AVAudioUnitEQ! -
The equalizer for the 1st channel.
Declaration
Swift
var rightEqualizer: AVAudioUnitEQ! -
The mixer is used to mix together multichannel audio.
Declaration
Swift
var audioMixerNode: AVAudioMixerNode! -
Information regarding the recorded clip from the previous
GeneralUIViewController.Declaration
Swift
var recordedAudio: RecordedAudioObject! -
The audio file in which this class attempts to record.
Declaration
Swift
var audioFile: AVAudioFile! -
The `AVAudioRecorder’ that performs recording from the microphone.
Declaration
Swift
var recorder: AVAudioRecorder? -
The
AVAudioRecorderDelegate' that must be passed intorecorder.Declaration
Swift
var recorderDelegate: AVAudioRecorderDelegate? -
A notification that is used to update UIElements in
EqualizerViewController.Declaration
Swift
let notificationName = Notification.Name("NotificationIdentifier")
-
A type alias to allow creating functions that accept other functions as parameters.
Declaration
Swift
typealias HandlerFunction = () -> Void
-
A sharedInstance that can be used by all other classes. This is reaquired to make this class a singleton.
Declaration
Swift
static let sharedInstance = Audio() -
Private initializer ensuring singleton pattern enforcement for the Audio manager. Creates the shared
AVAudioSessioninstance that will be used throughout the application for managing audio hardware resources. The private access level prevents external instantiation, ensuring only one Audio instance exists viasharedInstance. - Note: CallAudio.sharedInstanceto access the singleton instance - Important: TheAVAudioSessionmust be configured viasetupAVAudioSession()before use - SeeAlso:setupAVAudioSession()for microphone permission and session configurationDeclaration
Swift
class Audio -
Creates an
AVAudioSessionas asharedInstancethen requests recording permission from the user. If the user allows recording, theAVAudioSessionhas settings changed and therecordmethod is called.Declaration
Swift
func setupAVAudioSession() -
Called in other VC’s
viewDidLoad, this function sets up the equalizer with 14 bands per channel.Declaration
Swift
func setupEqualizer(numberOfBands: Int) -
This function creates an
AVAudioEnginestored inaudioEngine. Then based onrealTime, it sets up real-time multichannel microphone to speaker output or streams from an audio file. In both of these cases the input is sent to an equalizer that the user may modify. These two channels are then mixed together withaudioEngine.mainMixerNode.Declaration
Swift
func setupAudioEngine(realTime: Bool, handler: @escaping HandlerFunction) -
A recursive function that is used to infinitely schedule the same audio clip that was recorded on the previous screen.
Declaration
Swift
func schedule() -
Sets up the AVAudioRecorder and records into
filename (@param)usingsettings (@param). A time interval is also included as an iterative callback which allows for getting metering results.Declaration
Swift
func recordWithAVAudioRecorder(filename: String, settings: [String:Int], metered: Bool, timeIntervalForResults: TimeInterval) -
This function is called if in real-time mode. It starts the recording process and streams the audio to speaker after digital signal processing.
Declaration
Swift
func startRecordingWithAudioEngine() -
This function is called if in real-time mode. It stops the recording process and stops the audioEngine after removing the recording tap.
Declaration
Swift
func stopRecordingWithAudioEngine() -
Plays the audioPlayerNode
Declaration
Swift
func playWithAudioPlayerNode() -
Pauses the audioPlayerNode
Declaration
Swift
func pauseWithAudioPlayerNode() -
If the recording succeeded store the URL of the recorded clip then call
performeSegue. Else notify the user that recording failed.Declaration
Swift
func recordingEndedSuccessfully()
View on GitHub
Audio Class Reference