Search code examples
iosavfoundationaudiokit

AVFoundation / AudioKit Playing a low frequency sound produces click/pop


I'm creating a sort of drum pad app, and it seems like AKPlayer is the best of many classes available in AudioKit for playing my samples.

Trouble is, when I start playing a low-frequency (bass note) sample, there's always a clicking sound (or "pop") when playing the sample!

This can be easily heard by running an unmodified "Mixing Nodes" demo from the AudioKit Playgrounds. Turn down the volume on everything but the Bass, and you'll notice the clicking every time the Bass sample loops.

I also tested the issue with AVFoundation and it is the case too. This only happens with low frequency sounds like bass. Lead and other sounds dont produce this. I also tested with my own bass sample and same issue was here.

Here is the code for both:

import AudioKitPlaygrounds
import AudioKit

import AVFoundation


//: This section prepares the players
let drumFile = try AKAudioFile(readFileName: "drum.wav")
let bassFile = try AKAudioFile(readFileName: "bass.wav")
let guitarFile = try AKAudioFile(readFileName: "guitar.wav")
let leadFile = try AKAudioFile(readFileName: "lead.wav")

var drums = AKPlayer(audioFile: drumFile)
var bass = AKPlayer(audioFile: bassFile)
var guitar = AKPlayer(audioFile: guitarFile)
var lead = AKPlayer(audioFile: leadFile)

bass.play() // will produce click/pop
guitar.play() // will not produce click/pop, only low frequency samples produce click


let path = Bundle.main.path(forResource: "3", ofType:"aac")!
let url = URL(fileURLWithPath: "path")

var bombSoundEffect = try AVAudioPlayer(contentsOf: bassFile.url)
bombSoundEffect.prepareToPlay()

// uncomment below for AVFoundation
// bombSoundEffect.numberOfLoops = -1
// bombSoundEffect.play()

How may I get rid of this click/pop?


Solution

  • Rework your bass sample so that it starts and ends with some silence.

    To avoid clicks and pops when looping, the values at the seam must be continuous, that is they must not differ too much. Their velocities need to match too, not sure about acceleration. You could achieve this easily by loading the sample into memory and multiplying it by a curve that has the above properties. Something that eases in from 0 to 1 at the beginning then back out at the end.

    Also load up your sample in sound editor and inspect the beginning and end. Maybe the end has been cut off too sharply.