0

I am using AVFoundation in order to get the microphone input and EZAudio to plot the microphone input. I have stripped down the code to very basic and I still get latency, which is weird. This is the code:

override func viewDidLoad() {
    super.viewDidLoad()
    //Audio stuff
    let bufferSize: AVAudioFrameCount = 2048

    //plot properties
    bufferPlot.color = UIColor(red: 0.5, green: 0.0, blue: 0.0, alpha: 0)
    bufferPlot.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0)
    bufferPlot.plotType = .Buffer

    //instantiate nodes
    engine = AVAudioEngine()
    input = engine.inputNode
    mainMixer = engine.mainMixerNode
    //put a tap on the main mixer.

    mainMixer.installTapOnBus(0, bufferSize: bufferSize, format: mainMixer.inputFormatForBus(0)) { (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in

        //change buffer size to 2048
        buffer.frameLength = bufferSize

        //plotting
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.bufferPlot.updateBuffer(buffer.floatChannelData.memory, withBufferSize: UInt32(bufferSize))
        })
    }

    //start engine
    var error: NSErrorPointer = NSErrorPointer()
    engine.startAndReturnError(error)
}

Am I doing something wrong?

nevos
  • 738
  • 1
  • 9
  • 21
  • 1
    yep, wrong API. You need to drop down a level and work with AudioQueue or - better still - AudioUnit. See Apple's SpeakHere sample and check out some of my audio related answers. Particularly have a look at http://atastypixel.com/blog/using-remoteio-audio-unit/ – Max MacLeod Apr 10 '15 at 10:31
  • Thanks @MaxMacLeod, I have had a look at the Apple's video about AVAudioEngine and it seemed like the right solution. Using AudioUnit will I be able to grab the raw data from the mic and analyse it? I'm not trying to record the sound, just use the raw data and visualise it. That's the video I saw: https://developer.apple.com/videos/wwdc/2014/#502 – nevos Apr 10 '15 at 10:40
  • I eventually use EZAudio microphone function which is probably uses what you suggested, thanks! – nevos Apr 10 '15 at 10:50
  • cool no probs! Good luck with it – Max MacLeod Apr 10 '15 at 11:07

0 Answers0