1

I'm developing a music instrument in iOS with two audio samples (high and low pitches) that are played with view touches. The first sample is very short (a half second) and the other is a little bigger (two seconds). When I play repeatedly and fast the low pitch sound, there is an audio click/pop. There is no problem playing the high pitch sound. Both audio samples have fade in and fade out in their init/end and there is no clip problem with them.

I'm using this code to load the audio files (simplified here):

engine = AVAudioEngine()
mixer = engine.mainMixerNode

let player = AVAudioPlayerNode()
do {
    let audioFile = try AVAudioFile(forReading: instrumentURL)
    let audioFormat = audioFile.processingFormat
    let audioFrameCount = UInt32(audioFile.length)
    let audioFileBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: audioFrameCount)
    try audioFile.readIntoBuffer(audioFileBuffer)
    engine.attachNode(player)
    engine.connect(player, to: mixer, format: audioFileBuffer.format)
} catch {
    print("Init Error!")
}

and this code to play the samples:

player.play()
player.scheduleBuffer(audioFileBuffer, atTime: nil, options: option, completionHandler: nil)

I'm using a similar functionality in Android with the same audio samples without any click/pop problem.

Is this click/pop problem an implementation error? How can I fix this problem?

Update 1

I just tried another approach, with AVAudioPlayer and I got the same pop/click problem.

Update 2

I think the problem is to start the audio file again before its end. The sound stops abruptly.

msampaio
  • 3,394
  • 6
  • 33
  • 53

0 Answers0