Connecting Nodes Part 2

The intention of most of the AudioKit Playgrounds is to highlight a particular concept. To keep things clear, we have kept the amount of code to a minimum. But the flipside of that decision is that code from playgrounds will look a little different from production. In general, to see best practices, you can check out the AudioKit examples project, but here in this playground we're going to redo the code from the "Connecting Nodes" playground in a way that is more like how the code would appear in a project.

import AudioKitPlaygrounds

Here we begin the code how it would appear in a project

import AudioKit

// Create a class to handle the audio set up
class AudioEngine {

    // Declare your nodes as instance variables
    var player: AKPlayer!
    var delay: AKDelay!
    var reverb: AKReverb!
    var file: AKAudioFile!

    init() {
        // Set up a player to the loop the file's playback
        do {
            file = try AKAudioFile(readFileName: "drumloop.wav")
        } catch {
            AKLog("File Not Found")
        player = AKPlayer(audioFile: file)
        player.isLooping = true

        // Next we'll connect the audio player to a delay effect
        delay = AKDelay(player)

        // Set the parameters of the delay here
        delay.time = 0.1 // seconds = 0.8 // Normalized Value 0 - 1
        delay.dryWetMix = 0.2 // Normalized Value 0 - 1

        // Continue adding more nodes as you wish, for example, reverb:
        reverb = AKReverb(delay)

        AudioKit.output = reverb
        do {
            try AudioKit.start()
        } catch {
            AKLog("AudioKit did not start!")

// Create your engine and start the player
let engine = AudioEngine()

The next few lines are also just for playgrounds

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true