0

i have setup an EZAudio in swift to calculate the fft of the realtime mic input, and then i run a special algorithm over the fft data.

My problem is i can access the fft data when i put this in the view controller, with dispatch_async.(See code the last func)

class MasterKey:NSObject,EZMicrophoneDelegate, EZAudioFFTDelegate{

var microphone: EZMicrophone!
var fft: EZAudioFFTRolling!

var tone:String = ""
var sampleRate:Float = 0.0
var fftWindowSize:vDSP_Length = 8192

var keys:MKHRangeToKey!


 init(tone:String){
    super.init()

    self.tone = tone

    /*
    * setup all dependencys for the fft analysis
    */

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

    //create a mic instance
    microphone = EZMicrophone(delegate: self, startsImmediately: true)

    self.sampleRate = Float(microphone.audioStreamBasicDescription().mSampleRate)

    //create a fft instace
    fft = EZAudioFFTRolling(windowSize: fftWindowSize, sampleRate: sampleRate, delegate: self)

    //start the mic
    microphone.startFetchingAudio()

    self.keys = MKHRangeToKey(tone: tone, sampleRate: sampleRate, fftWindowSize: Int(fftWindowSize))

}

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

    //calc the fft
    if fft != nil{
        fft.computeFFTWithBuffer(buffer[0], withBufferSize: bufferSize)
    }

}


//get the fft data from last calculstion
func fft(fft: EZAudioFFT!, updatedWithFFTData fftData: UnsafeMutablePointer<Float>, bufferSize: vDSP_Length){

    dispatch_async(dispatch_get_main_queue(), {

        print(fftData)


    })
}
}

But how can i put this in a separate class to call it when i needed?

Pleas pleas Help

Aron
  • 1,179
  • 15
  • 29

1 Answers1

0

You indicated you're using Swift. Why not just create a separate AudioFunctions.swift file and move the function (and anything related) there? You can call it from anywhere in your app without worrying about an include.

Important Note: A function doesn't have to belong to a class.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • Thanks for reply, my problem is the func fft() has no return to do that. this is a method from EZAudio. I don't know how can i do this? EZAudio is in Obj-c written, i'm not familiar with it. – Aron Jan 12 '16 at 17:10
  • I don't know what you mean. Did you try my suggestion? Move the function(s) you want to separate from your cobtroller into a separate Swift file (with whatever includes it needs to compile). You'll be able to call them from your controller. Whether or not a function returns anything has nothing to do with this. Try my suggestion and we'll go from there if you run into problems. – Joshua Nozzi Jan 12 '16 at 17:23
  • ok now i have separate the code and i can access it uh hu, thanks - just had a blockade at the moment. Now is my problem how can i ged the data from dispatch async outside of this file? is my first real time audio app sorry for noob questions – Aron Jan 12 '16 at 17:54
  • Wait, is this fft function a callback from that audio library you're using? That's a different problem than I thought you were describing, given the title of your question. – Joshua Nozzi Jan 12 '16 at 18:30