Search code examples
swiftavaudioengine

how to get buffer from AVAudioEngine's installTap at high frequency


I am looking to render audio waveform on screen like iOS voice memo app, so I am using AVAudioEngine and installed Tap on input node. But it gives fastest call back on 0.1sec frequency , I need to get buffer data on faster frequency , so that can draw more and more waveform on screen for smooth and realtime visualisation.

Could you please sugger better approach for it ?

    let inputNode = self.audioEngine!.inputNode
    let format = inputNode.inputFormat(forBus: 0)
    
    inputNode.installTap(onBus: 0, bufferSize: 4096, format: format) { [self] (buffer, time) in
        processAudioData(buffer: buffer)
        DispatchQueue.main.async {
            audioPlotView.setNeedsDisplay(). // Render waveform on screen
        }
    } 

Solution

  • I think you misunderstand how to handle this. Instead of getting very small amounts of new data at a high frequency, you should factor your code to get a larger chunk of data on a longer time interval.

    You can then set up your display code to start an animation to animate in a graph of the new data over the time interval you are given. 1/10th of a second is actually a pretty short time interval for an animation.

    I would suggest averaging the amplitude of the data for that 1/10th of a second time interval and animating that new average data into place.

    BTW, see the update I posted to the answer to your question about creating an animated graph of the data.

    The animation I came up with looks like this:

    enter image description here