2

I am working on a fft analysis in swift with ezaudio.

My problem is how can i get the all fft data from ezaudio.

I would make a algorithm to look is a frequency present when yes how much strong.

Example:

I looking in the FFT Data is the Frequency 2000Hz present, is this Frequency present how much energy it have.

Here my code:

import UIKit
import Accelerate

class ViewController: UIViewController, EZMicrophoneDelegate,     EZAudioFFTDelegate{

private let ViewControllerFFTWindowSize: vDSP_Length = 4096

var microphone: EZMicrophone!
var fft: EZAudioFFTRolling!

override func loadView() {
    super.loadView()

    //setup audio session
    let session = AVAudioSession.sharedInstance()
    do{
        try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try session.setActive(true)
    }catch{
        print("Audio Session setup Fails")
    }

    microphone = EZMicrophone(delegate: self, startsImmediately: true)
    fft = EZAudioFFTRolling(windowSize: ViewControllerFFTWindowSize, sampleRate: Float(microphone.audioStreamBasicDescription().mSampleRate), delegate: self)

    microphone.startFetchingAudio()
}

func microphone(microphone: EZMicrophone!, hasAudioReceived buffer: UnsafeMutablePointer<UnsafeMutablePointer<Float>>, withBufferSize bufferSize: UInt32, withNumberOfChannels numberOfChannels: UInt32) {

    fft.computeFFTWithBuffer(buffer[0], withBufferSize: bufferSize)

}

func fft(fft: EZAudioFFT!, updatedWithFFTData fftData: UnsafeMutablePointer<Float>, bufferSize: vDSP_Length) {
    var maxF = fft.fftData

    print(maxF)

    var data = fft.fftData
    print(data)

    //here coming my algorithm


}


}

With this code its giving a strange output on console:

var data = fft.fftData
print(data)

Output: 0x00000001119be000

Many Many thanks for help

Imran
  • 2,985
  • 19
  • 33
norbu
  • 301
  • 1
  • 3
  • 7
  • Aren't you looking for `fft.maxFrequency` instead of `fft.fftData`? – Eric Aya Oct 12 '15 at 09:36
  • no i want not only looking for the max frequency. i want looking for multiple frequency with a high energy. can you help me? – norbu Oct 12 '15 at 09:39
  • My question is only, how can i ged the fft data not how can i analyses this. – norbu Oct 12 '15 at 09:48
  • The docs say yes (http://cocoadocs.org/docsets/EZAudio/1.1.2/Classes/EZAudioFFT.html#//api/name/fftData), but when i print this to the console is the output 0x00000001119be000. I don't understand how can i get the float array data. Do you have an idea? – norbu Oct 12 '15 at 11:40

1 Answers1

4

fftData is an UnsafeMutablePointer, which is the Swift equivalent of a C pointer. A pointer is a memory address, not an actual value (hence the name: it points to memory, but isn't the memory itself).

So, when you print it, you get an address (0x00000001119be000). If you want to read the actual value of the memory at that address, you have to look it up. For example, to see the value (in this case a Float) at index 0, you can...

print(fftData[0])

To see all the values, just loop through and print them individually...

for (var i = 0; i < bufferSize; i++) {
   print(fftData[i])
}

UnsafeMutablePointer also has a property called memory...

print(fftData.memory)

but it will only return the value at that exact memory address, as opposed to the range of values you are interested in, making it equal to fftData[0]

Charlie Martin
  • 8,208
  • 3
  • 35
  • 41