swiftsignal-processingaudiokit

I cannot connect correctly the outputs in audiokit 5.5


I'm quite new to AudioKit so forgive me in advance. What I'm trying to do is an app that applies a chains of audio effects. Let's say I have a file audio (or the mic or both):

  1. I need to split the left and right channel
  2. there is an amplitude check to verify if audio signal is above or below a threshold
  3. there are two delays. delay 1 and delay 2.
  4. if the signal is above the threshold it is equalised by one equaliser, otherwise it is equalised by another. Depending of the delays, the left channel input and the right channel input could switched between equaliser 1 or 2 independently. The output for the left channel input, after the equalisation, should be the corresponding left channel output and the same should happen to right channel input. Of course the code doesn't present any errors, but when I try to test the app in the simulator the app crashes - telling that I'm trying to start the engine without setting up a proper output. I have no idea what to do.

The thread is in this line:

engine.output = stereoOutputMixer

Here the init of the class I wrote:

var audioFilePlayer: AudioPlayer?
class AudioProcessor: ObservableObject {
    var engine: AudioEngine
    var mic: AudioEngine.InputNode!
    var mixer: Mixer
    
    var equalizer1: Equalizer
    var equalizer2: Equalizer
    var amplitudeTap: AmplitudeTap?
    var threshold: Float = 40.0
    var equalizer1Mixer: Mixer
    var equalizer2Mixer: Mixer
    
    var leftPanner: Panner
    var rightPanner: Panner
    var leftPannerMixer: Mixer
    var rightPannerMixer: Mixer

    var stereoOutputMixer: Mixer
    var delay1: Delay
    var delay2: Delay
    var highPassFilter: HighPassFilter
    var lowPassFilter: LowPassFilter
    var leftVolumeMixer: Mixer
    var rightVolumeMixer: Mixer
    var micMixer: Mixer
    var fileMixer: Mixer
    var leftOutputPanner: Panner
    var rightOutputPanner: Panner

    var isAboveThreshold: Bool = false

    var preferences: UserPreferences

    init(preferences: UserPreferences = UserPreferences()) {
        // First, initialize all properties
        self.preferences = preferences
        self.engine = AudioEngine()
        self.mixer = Mixer()

        // Initialize equalizers with default frequencies
        let frequencies: [Double] = [125, 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 6000, 8000]
        self.equalizer1 = Equalizer(engine: engine, input: mixer, frequencies: frequencies)
        self.equalizer2 = Equalizer(engine: engine, input: mixer, frequencies: frequencies)
        
        self.delay1 = Delay(equalizer1.output)
        self.delay1.time = preferences.delay1Time
        self.delay2 = Delay(equalizer2.output)
        self.delay2.time = preferences.delay2Time
        self.threshold = preferences.threshold  // Initialize threshold

        self.highPassFilter = HighPassFilter(mixer, cutoffFrequency: preferences.highPassFilterFrequency, resonance: 1)
        self.lowPassFilter = LowPassFilter(mixer, cutoffFrequency: preferences.lowPassFilterFrequency, resonance: 1)

        self.leftPanner = Panner(mixer, pan: -1)
        self.rightPanner = Panner(mixer, pan: 1)

        self.leftOutputPanner = Panner(equalizer1.output, pan: -1)
        self.rightOutputPanner = Panner(equalizer2.output, pan: 1)

        self.leftVolumeMixer = Mixer()
        self.rightVolumeMixer = Mixer()

        self.stereoOutputMixer = Mixer()

        self.micMixer = Mixer()
        self.fileMixer = Mixer()

        self.isAboveThreshold = false
        self.amplitudeTap = nil

        // Now that all properties are initialized, call methods to set up the audio chain
        // Initialize mixers for each equalizer
        equalizer1Mixer = Mixer()
        equalizer2Mixer = Mixer()

        // Initialize panners and their mixers
        leftPannerMixer = Mixer()
        rightPannerMixer = Mixer()
        
        setupDelays()
        setupFilters()
        setupPanners()
        setupVolumeControlMixers()
        setupMicAndFileMixers()
        applyEqualizerGains(preferences: preferences)
        setVolumeBalance(leftVolume: preferences.balanceVolumeLeft, rightVolume: preferences.balanceVolumeRight)
        setMicPlaybackBalance(micVolume: preferences.micGain, playbackVolume: preferences.playbackVolume)

        if preferences.micEnabled {
            setupMicrophone()
        }
        startEngine()
        setupInitialAudioChain()
        
        setupAmplitudeMonitoring()
        checkAmplitudeAndApplyDelays()
        applyDelaysAndSwitchEqualizers()
        engine.output = stereoOutputMixer
    }

Solution

  • You are calling startEngine() which presumably contains engine.start() before setting the engine's output. i.e engine.output = stereoOutputMixer

    Also make sure that you have added the inputs to the stereoOutputMixer. Use engine.connectionTreeDescription to view the engine's output tree.