我怎样才能返回void函数的值

时间:2016-06-22 08:00:06

标签: ios swift

我有一个计算麦克风输入fft的功能。 目标是在我调用run函数时创建一个框架,我使用所有band来调用float数组。

现在一切正常,但我不知道如何从gotSomeAudio函数返回run函数中的Array。

非常感谢您的帮助

@objc
public class FFT:NSObject{


    var audioInput: TempiAudioInput!

    @objc
    public func run() -> Array<Float>{

        let audioInputCallback: TempiAudioInputCallback = { (numberOfFrames, timeStamp, inout samples: [Float]) -> Void in
            self.gotSomeAudio(numberOfFrames, timeStamp: timeStamp, samples: samples)
        }


        audioInput = TempiAudioInput(audioInputCallback: audioInputCallback, sampleRate: 44100, numberOfChannels: 1)
        audioInput.startRecording()

        // how can i return the Array from the gotSomeAudio function? 
        return xyz
    }




    @objc
    public func gotSomeAudio(numberOfFrames: Int, timeStamp: Double, samples: [Float]) -> Array<Float> {

        let fft = TempiFFT(withSize: numberOfFrames, sampleRate: 44100)

        // Setting a window type reduces errors
        fft.windowType = TempiFFTWindowType.hanning

        // Perform the FFT
        fft.fftForward(samples)

        // Map FFT data to logical bands. This gives 4 bands per octave across 7 octaves = 28 bands.
        //fft.calculateLogarithmicBands(minFrequency: 100, maxFrequency: 11025, bandsPerOctave: 4)
        //fft.calculateLinearBands(minFrequency: 0, maxFrequency: fft.nyquistFrequency, numberOfBands: Int(screenWidth))
        fft.calculateLogarithmicBands(minFrequency: 100, maxFrequency: 11025, bandsPerOctave: 4)

        // Process some data
        return fft.bandFrequencies

    }

}

1 个答案:

答案 0 :(得分:3)

您可以将其传递给回调函数:

public func run(complete: Array<Float> -> Void) {

    let audioInputCallback: TempiAudioInputCallback = { (numberOfFrames, timeStamp, inout samples: [Float]) -> Void in
        complete(self.gotSomeAudio(numberOfFrames, timeStamp: timeStamp, samples: samples))
    }

...
myInstance.run() { floatArray in
    // Use floatArray here.
}