Search code examples
macosaudioswiftuiaudiokit

Audio Visualizer Using AudioKit and SwiftUI


Matt Pfeiffer posted a tutorial "Build an Audio Visualizer Using AudioKit and SwiftUI", and posted his source code here. Unfortunately, it doesn't compile. Another user posted a question describing the problem on the tutorial site, but it remains unanswered. That user points out a couple of simple changes that allow it to compile.

However, the line mic?.setOutput(to: micMixer) in Conductor.swift produces the fatal run-time error:

Cannot use optional chaining on non-optional value of type ‘AKMicrophone’

Any help in fixing this error would be appreciated.


Solution

  • I commented on the tutorial, but will leave this here as well:

    There have been changes made to the AudioKit framework since the tutorial was released that require adjusting how you setup the microphone.

    I have rewritten the code to create this visualizer using AudioKit version 5 – the tutorial uses version 4, which depending on your version (4.x) could require one of a couple variants on how the microphone should be setup.

    Please, see the following github repository, which uses AudioKit V5: https://github.com/Matt54/AudioVisualizerAK5

    I won’t list every change here between the projects as you can just download the project and run it / copy the boilerplate. However, some notable changes are that I have consolidated the two SwiftUI Views into a single View (FFTView) and I now use a callback from AudioKit’s FFTTAP in order to update the amplitude data for the view.

    It’s also worth mentioning that with AudioKit V5, you can use the swift package manager to install AudioKit into your projects. See the “Installation via Swift Package Manager” section here: https://github.com/AudioKit/AudioKit

    Reach out if you have any more specific questions about this project.

    Matt