I'm trying to write a music app where detection of pitch is the core of it all. I've seen solutions to this problem as well as apps on the AppStore. However most of them are pretty dated and I'd like to do this is Swift. I've been looking at AVAudioEngine as a way to do this, but I find the documentation lacking or maybe I haven't been looking hard enough.
What I have found is that I can tap the inputNode bus like this:
self.audioEngine = AVAudioEngine()
self.audioInputNode = self.audioEngine.inputNode!
self.audioInputNode.installTapOnBus(0, bufferSize:256, format: audioInputNode.outputFormatForBus(0), block: {(buffer, time) in
self.analyzeBuffer(buffer)
})
The bus is tapped 2-3 times per second and the buffer contains more than 16000 floats for each tap. Are these amplitude samples from the microphone?
The docs at least claims it's output from the node: "The buffer parameter is a buffer of audio captured from the output of an AVAudioNode."
Is it possible to use AVAudioEngine to detect pitch in real time or should I go about this another way?
I realize that Hellium3 is really giving me information to what pitch is and if it's a good idea to do these things with Swift.
My question was originally about if tapping the PCM bus is the way to obtain input signals from the microphone.
Since asking this question I've done exactly that. Use the data obtained by tapping the PCM bus and analyse the buffer windows.
It works really well and it was my lack of understanding of what a PCM bus, buffer and sampling frequency is that made me ask the question in the first place.
Knowing those three makes it easier to see that this is right on.
Edit: On demand I'll paste my (deprecated) implementation of the PitchDetector.
All credit to Philip McLeod whose research is used in my implementation above. http://www.cs.otago.ac.nz/research/publications/oucs-2008-03.pdf