2

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
        }
    } 
Robert Atkins
  • 23,528
  • 15
  • 68
  • 97
Amit Singh
  • 63
  • 8

1 Answers1

2

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

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks Ducan for your code it is really helpful to build understanding ... Here we need to scroll back & forth over waveform as well, so I did bit more research and found that UIScrollView helps for it. So now I render lines on content view's layer and change content view frame to shift it back & increate content view width, and change scrollview contentsize & contentoffset accordingly please let me know your opinion. following is link to watch demo output video. https://github.com/singham2012/audio/blob/main/README.md – Amit Singh May 27 '21 at 10:46