Search code examples
iosaudiokit

AudioKit: Way to inject silence/fadeout with a loop with AKPlayer?


In my app, I give the user the option to play a small frame of audio (from a larger audio file)in order to listen over and over to do a manual transcription. AKPlayer makes this trivial. Now, because the frame of audio is pretty small, it's pretty intense to hear this loop over and over (a little maddening in the classical sense of the word). I'd like to either fade it out/fade it back in with the loop OR just inject like 500 ms of silence before the loop starts again. I have no idea where to start, here is the current working code as is:

public func playLoop(start: Double, end: Double) {
        self.chordLoopPlayer.isLooping = true
        self.chordLoopPlayer.buffering = .always
        self.chordLoopPlayer.preroll()
        let millisecondsPerSample : Double = 1000 / 44100
        let startingDuration : Double = (((start * millisecondsPerSample) / 1000) / 2)
        let endingDuration : Double = (((end * millisecondsPerSample) / 1000) / 2)
        print("StartinDuration:\(startingDuration) | EndingDuration:\(endingDuration)")
        self.chordLoopPlayer.loop.start = startingDuration
        self.chordLoopPlayer.loop.end = endingDuration

        self.chordLoopPlayer.play(from: startingDuration, to: endingDuration)

Thanks so much <3


Solution

  • You just need to set .fade values for your fade-in/fade-out prior to calling the play() function. AudioKit will execute them each time going in and out of the loop. So assuming you'd like a 2-second fade-out, and a 2-second fade-in (adjust to your taste), your code would look like:

    public func playLoop(start: Double, end: Double) {
        self.chordLoopPlayer.isLooping = true
        self.chordLoopPlayer.buffering = .always
        self.chordLoopPlayer.preroll()
        let millisecondsPerSample : Double = 1000 / 44100
        let startingDuration : Double = (((start * millisecondsPerSample) / 1000) / 2)
        let endingDuration : Double = (((end * millisecondsPerSample) / 1000) / 2)
        print("StartinDuration:\(startingDuration) | EndingDuration:\(endingDuration)")
        self.chordLoopPlayer.loop.start = startingDuration
        self.chordLoopPlayer.loop.end = endingDuration
    
        // add fade in/out values to fade in or fade out during playback; reset to 0 to disable.
        self.chordLoopPlayer.fade.inTime = 2 // in seconds
        self.chordLoopPlayer.fade.outTime = 2 // in seconds
    
        self.chordLoopPlayer.play(from: startingDuration, to: endingDuration)
    }
    

    I find the AudioKit documentation a bit frustrating in this respect, as it's not super-easy to find these properties if you don't already know what you're looking for, or to understand how to use them if you haven't already come across sample code, so I hope this is a useful example for others who happen to search on this topic on SO. In any case, the list of sub-properties associated with AudioKit's .fade property is here: https://audiokit.io/docs/Classes/AKPlayer/Fade.html